Skip to content

Commit

Permalink
adding support for add classes ref brazil-data-cube#14
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianazioti committed Mar 16, 2021
1 parent bd71109 commit 019b19c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
25 changes: 22 additions & 3 deletions lccs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def mappings(config: Config, system_name_source, system_name_target, verbose):
@click.option('--system_name_source', type=click.STRING, required=True, help='The classification system source.')
@click.option('--system_name_target', type=click.STRING, required=True, default=None,
help='The classification system target.')
@click.option('--mappings_path', type=click.Path(exists=True), required=True, help='File path with the mapping')
@click.option('--mappings_path', type=click.Path(exists=True), required=True, help='Json file with the mapping')
@click.option('-v', '--verbose', is_flag=True, default=False)
@pass_config
def add_mapping(config: Config, system_name_source, system_name_target, mappings_path, verbose):
Expand All @@ -187,8 +187,8 @@ def add_mapping(config: Config, system_name_source, system_name_target, mappings
click.secho('\tAdding new mapping ... ', bold=False, fg='black')

config.service.add_mapping(system_name_source=system_name_source,
system_name_target=system_name_target,
mappings=mappings_path)
system_name_target=system_name_target,
mappings=mappings_path)

click.secho(f'Added Mapping between {system_name_source} and '
f'{system_name_target}', bold=True, fg='green')
Expand Down Expand Up @@ -243,3 +243,22 @@ def add_classification_system(config: Config, name, authority_name, description,

if verbose:
click.secho('\tFinished!', bold=False, fg='black')


@cli.command()
@click.option('--system_name', type=click.STRING, required=True, help='The classification system.')
@click.option('--classes_path', type=click.Path(exists=True), required=True, help='Json file with classes')
@click.option('-v', '--verbose', is_flag=True, default=False)
@pass_config
def add_classes(config: Config, system_name, classes_path, verbose):
"""Add a mapping between classification systems."""
if verbose:
click.secho(f'Server: {config.url}', bold=True, fg='black')
click.secho('\tAdding new mapping ... ', bold=False, fg='black')

config.service.add_classes(system_name=system_name, classes=classes_path)

click.secho(f'Added classes for {system_name}', bold=True, fg='green')

if verbose:
click.secho('\tFinished!', bold=False, fg='black')
32 changes: 26 additions & 6 deletions lccs/lccs.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,30 @@ def add_classification_system(self, name: str, authority_name: str, description:
raise ValueError(f'Could not insert classification system {name}!')

return retval

def add_classes(self, system_name, classes: str):
"""Add new classes to an classification system."""
_system_id = self._id(system_name)

url = f'{self._url}/classification_systems/{_system_id.id}/classes{self._access_token}'

if type(classes) == str:
with open(classes) as file:
classes = json.load(file)

for i in classes:
if 'class_parent_id' not in i:
break
if type(i['class_parent_id']) != str:
break
i['class_parent_id'] = Utils.get_id_by_name(name=i['class_parent_id'], classes=_system_id.classes)

try:
retval = Utils._post(url, json=classes)
except RuntimeError:
raise ValueError('Could not insert classes!')

return retval

def add_style(self, system_name: str, format_name: str, style_path: str):
"""Add a new style format system."""
Expand All @@ -240,10 +264,6 @@ def add_style(self, system_name: str, format_name: str, style_path: str):

def add_mapping(self, system_name_source: str, system_name_target: str, mappings):
"""Add new classification system mapping."""
def get_id_by_name(name, classes):
"""Get id of class."""
return list(filter(lambda x: x.name == name, classes))[0]['id']

_system_source_id = self._id(system_name_source)
_system_target_id = self._id(system_name_target)

Expand All @@ -256,8 +276,8 @@ def get_id_by_name(name, classes):
for i in mappings:
if type(i['source_class_id']) != str:
break
i['source_class_id'] = get_id_by_name(i['source_class_id'], _system_source_id.classes)
i['target_class_id'] = get_id_by_name(i['target_class_id'], _system_target_id.classes)
i['source_class_id'] = Utils.get_id_by_name(i['source_class_id'], _system_source_id.classes)
i['target_class_id'] = Utils.get_id_by_name(i['target_class_id'], _system_target_id.classes)

try:
retval = Utils._post(url, json=mappings)
Expand Down
5 changes: 5 additions & 0 deletions lccs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ def render_html(template_name, **kwargs):
"""Render Jinja2 HTML template."""
template = templateEnv.get_template(template_name)
return template.render(**kwargs)

@staticmethod
def get_id_by_name(name, classes):
"""Get id of class."""
return list(filter(lambda x: x.name == name, classes))[0]['id']

0 comments on commit 019b19c

Please sign in to comment.