diff --git a/lccs/cli.py b/lccs/cli.py index 9276a8a..be30c2a 100644 --- a/lccs/cli.py +++ b/lccs/cli.py @@ -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): @@ -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') @@ -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') diff --git a/lccs/lccs.py b/lccs/lccs.py index ea00db9..34ea9bf 100644 --- a/lccs/lccs.py +++ b/lccs/lccs.py @@ -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.""" @@ -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) @@ -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) diff --git a/lccs/utils.py b/lccs/utils.py index 257eaf2..c87f62e 100644 --- a/lccs/utils.py +++ b/lccs/utils.py @@ -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']