Skip to content

Commit

Permalink
Add same-dir-as-input flag in create-archive
Browse files Browse the repository at this point in the history
  • Loading branch information
ka-sarthak committed Aug 15, 2024
1 parent 7a18adc commit 1aab73e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/nomad_polymerization_reactions/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ def cli():
required=True,
type=click.Path(exists=True),
)
def _create_archive(json_file_path):
@click.option(
'--same-dir-as-input',
is_flag=True,
default=False,
help='Create the archive in the same directory as the input JSON file.',
)
def _create_archive(json_file_path, same_dir_as_input):
try:
for file_path in json_file_path:
generate_archive_from_json(file_path)
generate_archive_from_json(file_path, same_dir_as_input)
click.echo('Archive created successfully.')
except Exception as e:
click.echo(f'Archive creation failed. Error: {e}')
13 changes: 10 additions & 3 deletions src/nomad_polymerization_reactions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from structlog.stdlib import BoundLogger


def generate_archive_from_json(filepath: str, logger: 'BoundLogger' = None): # noqa: PLR0912
def generate_archive_from_json(
filepath: str, same_dir_as_input: bool = False, logger: 'BoundLogger' = None
): # noqa: PLR0912
"""
Generate an archive.yaml file from a JSON file coming from the LLM output.
Function expects a JSON of the following format:
Expand Down Expand Up @@ -39,6 +41,8 @@ def generate_archive_from_json(filepath: str, logger: 'BoundLogger' = None): #
Args:
filepath (str): Path to the JSON file.
same_dir_as_input (bool): If True, the output is created inside the same
directory as the input JSON file.
logger (BoundLogger): A structlog logger.
Returns:
Expand Down Expand Up @@ -108,8 +112,11 @@ def represent_dict(self, data):

data_dict = dict(data_dict_ordered)
entry = dict(data=data_dict)
archive_name = filepath.split('/')[-1].split('.')[0]
with open(f'{archive_name}.archive.yaml', 'w') as f:
if same_dir_as_input:
archive_path = filepath.replace('.json', '.archive.yaml')
else:
archive_path = filepath.split('/')[-1].replace('.json', '.archive.yaml')
with open(archive_path, 'w') as f:
yaml.dump(entry, f, Dumper=OrderedDumper, default_flow_style=False)

return entry

0 comments on commit 1aab73e

Please sign in to comment.