MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / get_meta_yaml_data

Function get_meta_yaml_data

src/packagedcode/conda.py:608–646  ·  view source on GitHub ↗

Return a mapping of conda metadata loaded from a meta.yaml files. The format support Jinja-based templating and we try a crude resolution of variables before loading the data as YAML.

(location)

Source from the content-addressed store, hash-verified

606
607
608def get_meta_yaml_data(location):
609 """
610 Return a mapping of conda metadata loaded from a meta.yaml files. The format
611 support Jinja-based templating and we try a crude resolution of variables
612 before loading the data as YAML.
613 """
614 # FIXME: use Jinja to process these
615 variables = get_variables(location)
616 yaml_lines = []
617 with io.open(location, encoding='utf-8') as metayaml:
618 for line in metayaml:
619 if not line:
620 continue
621 pure_line = line.strip()
622 if (
623 pure_line.startswith('{%')
624 and pure_line.endswith('%}')
625 and '=' in pure_line
626 ):
627 continue
628
629 # Replace the variable with the value
630 if '{{' in line and '}}' in line:
631 for variable, value in variables.items():
632 if "|lower" in line:
633 line = line.replace('{{ ' + variable + '|lower' + ' }}', value.lower())
634 else:
635 line = line.replace('{{ ' + variable + ' }}', value)
636 yaml_lines.append(line)
637
638 # Cleanup any remaining complex jinja template lines
639 # as the yaml load fails otherwise for unresolved jinja
640 cleaned_yaml_lines = [
641 line
642 for line in yaml_lines
643 if not "{{" in line
644 ]
645
646 return saneyaml.load(''.join(cleaned_yaml_lines))
647
648
649def get_variables(location):

Callers 1

parseMethod · 0.85

Calls 6

get_variablesFunction · 0.85
openMethod · 0.45
replaceMethod · 0.45
appendMethod · 0.45
loadMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected