Commit eeb8bbd9 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Metainfo code generation improvements.

(cherry picked from commit 4edf939e)
parent e8de07b6
......@@ -280,7 +280,12 @@ class LegacyMetainfoEnvironment:
return env
def generate_metainfo_code(self, package: Package, directory: str):
def generate_metainfo_code(
self, package: Package, directory: str = None, package_name: str = None):
if directory is None:
directory = '.'
def format_description(description, indent=0, width=90):
paragraphs = [paragraph.strip() for paragraph in description.split('\n')]
......@@ -315,8 +320,14 @@ class LegacyMetainfoEnvironment:
format_type=format_type,
format_unit=format_unit)
with open(os.path.join(directory, '%s.py' % package.name), 'wt') as f:
f.write(env.get_template('package.j2').render(pkg=package))
with open(os.path.join(
directory, '%s.py' % package_name
if package_name is not None else package.name), 'wt') as f:
code = env.get_template('package.j2').render(pkg=package)
code = '\n'.join([
line.rstrip() if line.strip() != '' else ''
for line in code.split('\n')])
f.write(code)
if __name__ == '__main__':
......@@ -326,3 +337,4 @@ if __name__ == '__main__':
package_names=['%s.nomadmetainfo.json' % pkg for pkg in ['common', 'public', 'vasp']])
legacy_env = env.legacy_info_env()
env.generate_metainfo_code(env.env.all_packages['public.nomadmetainfo.json'], package_name='public')
......@@ -1938,6 +1938,7 @@ class Environment(MSection):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.all_definitions_by_name: Dict[str, List[Definition]] = dict()
self.all_packages: Dict[str, Package] = dict()
def resolve_definitions( # type: ignore
self, name: str, cls: Type[MSectionBound] = Definition) -> List[MSectionBound]:
......@@ -1961,6 +1962,7 @@ class Environment(MSection):
def on_add_sub_section(self, sub_section_def: SubSection, sub_section: MSection):
if sub_section_def == Environment.packages:
package = sub_section.m_as(Package)
self.all_packages[package.name] = package
for definition in package.m_all_contents():
if isinstance(definition, Definition):
definitions = self.all_definitions_by_name.setdefault(definition.name, [])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment