本文整理汇总了Python中yaml.MarkedYAMLError方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.MarkedYAMLError方法的具体用法?Python yaml.MarkedYAMLError怎么用?Python yaml.MarkedYAMLError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.MarkedYAMLError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_folder_semver
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import MarkedYAMLError [as 别名]
def get_folder_semver(folder_path: str):
for item in os.listdir(folder_path):
item_path = os.path.join(folder_path, item)
if not os.path.isfile(item_path) or not is_yaml_file(item_path):
continue
with open(item_path, 'r') as f:
file_content = f.read()
if identify.get_operator_artifact_type(file_content) == 'ClusterServiceVersion':
try:
csv_version = safe_load(file_content)['spec']['version']
except MarkedYAMLError:
msg = f'{item} is not a valid YAML file.'
logger.error(msg)
raise OpCourierBadYaml(msg)
except KeyError:
msg = f'{item} is not a valid CSV file as "spec.version" ' \
f'field is required'
logger.error(msg)
raise OpCourierBadBundle(msg, {})
return csv_version
return None
示例2: get_operator_artifact_type
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import MarkedYAMLError [as 别名]
def get_operator_artifact_type(operatorArtifactString):
"""get_operator_artifact_type takes a yaml string and determines if it is
one of the expected bundle types.
:param operatorArtifactString: Yaml string to type check
"""
# Default to unknown file unless identified
artifact_type = UNKNOWN_FILE
try:
operatorArtifact = safe_load(operatorArtifactString)
except MarkedYAMLError:
msg = "Courier requires valid input YAML files"
logger.error(msg)
raise OpCourierBadYaml(msg)
else:
if isinstance(operatorArtifact, dict):
if "packageName" in operatorArtifact:
artifact_type = PKG_STR
elif operatorArtifact.get("kind") in {CRD_STR, CSV_STR}:
artifact_type = operatorArtifact["kind"]
return artifact_type
示例3: _parse_yaml_file
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import MarkedYAMLError [as 别名]
def _parse_yaml_file(file_path: str) -> Tuple[Dict[str, List[str]], List[FileSyntaxError]]:
"""
Parse a file in the YAML format.
:param file_path: The location of the file that will be processed.
:type file_path: str
:return: Tuple with mapping of key and list of values and list of syntax errors
"""
with open(file_path) as f:
content = f.read()
if not content:
return {}, [FileSyntaxError(line_no=1, message="The file is empty.")]
try:
secrets = yaml.safe_load(content)
except yaml.MarkedYAMLError as e:
return {}, [FileSyntaxError(line_no=e.problem_mark.line, message=str(e))]
if not isinstance(secrets, dict):
return {}, [FileSyntaxError(line_no=1, message="The file should contain the object.")]
return secrets, []
示例4: make_rosparam
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import MarkedYAMLError [as 别名]
def make_rosparam(self, name, ns, value, condition):
# ---- lazy rosparam import as per the oringinal roslaunch code
global rosparam
if rosparam is None:
import rosparam
try:
value = yaml.safe_load(value)
except yaml.MarkedYAMLError as e:
raise ConfigurationError(str(e))
# ----- try to use given name, namespace or both
ns = self._ns_join(ns or self.private_ns, self.private_ns)
if name:
name = self._ns_join(name, ns)
else:
if not isinstance(value, dict):
raise ConfigurationError("'param' attribute must be set"
" for non-dictionary values")
# ----- this will unfold, so we can use namespace in place of a name
name = ns
conditions = list(self.conditions)
if not condition is True:
conditions.append(condition)
self._yaml_param(name, value, conditions, private = False)
示例5: load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import MarkedYAMLError [as 别名]
def load(self):
# Prepare + load directory.
super().load()
# Load the files and parse Yaml.
parsed_settings = dict()
try:
for file_name in self.files:
file_path = os.path.join(self.directory, file_name)
with open(file_path, 'r') as file_handle:
parsed_settings.update(yaml.safe_load(file_handle))
except (yaml.YAMLError, yaml.MarkedYAMLError) as e:
raise ImproperlyConfigured(
'Your settings file(s) contain invalid YAML syntax! Please fix and restart!, {}'.format(str(e))
)
# Loop and set in local settings (+ uppercase keys).
for key, value in parsed_settings.items():
self.settings[key.upper()] = value
示例6: check_api_params
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import MarkedYAMLError [as 别名]
def check_api_params(checker: t.Trafaret, loads: Callable[[str], Any] = None,
query_param_checker: t.Trafaret = None) -> Any:
# FIXME: replace ... with [web.Request, Any...] in the future mypy
def wrap(handler: Callable[..., Awaitable[web.Response]]):
@functools.wraps(handler)
async def wrapped(request: web.Request, *args, **kwargs) -> web.Response:
orig_params: Any
body: str = ''
try:
body_exists = request.can_read_body
if body_exists:
body = await request.text()
if request.content_type == 'text/yaml':
orig_params = yaml.load(body, Loader=yaml.BaseLoader)
else:
orig_params = (loads or json.loads)(body)
else:
orig_params = dict(request.query)
stripped_params = orig_params.copy()
stripped_params.pop('owner_access_key', None)
log.debug('stripped raw params: {}', mask_sensitive_keys(stripped_params))
checked_params = checker.check(stripped_params)
if body_exists and query_param_checker:
query_params = query_param_checker.check(request.query)
kwargs['query'] = query_params
except (json.decoder.JSONDecodeError, yaml.YAMLError, yaml.MarkedYAMLError):
raise InvalidAPIParameters('Malformed body')
except t.DataError as e:
raise InvalidAPIParameters('Input validation error',
extra_data=e.as_dict())
return await handler(request, checked_params, *args, **kwargs)
return wrapped
return wrap
示例7: put
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import MarkedYAMLError [as 别名]
def put(request: web.Request, params: Any) -> web.Response:
dbpool = request.app['dbpool']
template_id = request.match_info['template_id']
requester_access_key, owner_access_key = await get_access_key_scopes(request, params)
log.info('PUT (ak:{0}/{1})',
requester_access_key, owner_access_key if owner_access_key != requester_access_key else '*')
async with dbpool.acquire() as conn, conn.begin():
query = (sa.select([session_templates.c.id])
.select_from(session_templates)
.where((session_templates.c.id == template_id) &
(session_templates.c.is_active)
))
result = await conn.scalar(query)
if not result:
raise TaskTemplateNotFound
try:
body = json.loads(params['payload'])
except json.JSONDecodeError:
body = yaml.load(params['payload'], Loader=yaml.BaseLoader)
except (yaml.YAMLError, yaml.MarkedYAMLError):
raise InvalidAPIParameters('Malformed payload')
body = task_template_v1.check(body)
query = (sa.update(session_templates)
.values(template=body, name=body['metadata']['name'])
.where((session_templates.c.id == template_id)))
result = await conn.execute(query)
assert result.rowcount == 1
return web.json_response({'success': True})
示例8: __init__
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import MarkedYAMLError [as 别名]
def __init__(self, yaml_file: str):
ConfigParser.check_for_tabs_in_file(yaml_file)
try:
self._configuration = yaml.safe_load(open(yaml_file, "r"))
except yaml.MarkedYAMLError as excep:
extra_info = (
f"There is something wrong in the configuration file {yaml_file}. "
)
if hasattr(excep, "problem_mark"):
extra_info += (
"The typo is probably somewhere around "
f"line {excep.problem_mark.line + 1}."
)
raise type(excep)(
f"{excep}. {terminal_colors.RED}{terminal_colors.BOLD}"
f"{extra_info}{terminal_colors.END}"
).with_traceback(sys.exc_info()[2])
self._config_folder = pathlib.Path(yaml_file).parent
self._page_ids: typing.List[str] = []
self._assets: set = set()
self.clean_configuration()