async def get(self, schemaspace): schemaspace = url_unescape(schemaspace) parent = self.settings.get("elyra") try: metadata_manager = MetadataManager(schemaspace=schemaspace, parent=parent) metadata = metadata_manager.get_all() except (ValidationError, ValueError) as err: raise web.HTTPError(400, str(err)) from err except MetadataNotFoundError as err: raise web.HTTPError(404, str(err)) from err except Exception as err: raise web.HTTPError(500, repr(err)) from err metadata_model = {schemaspace: [r.to_dict(trim=True) for r in metadata]} self.set_header("Content-Type", "application/json") self.finish(metadata_model)
class SchemaspaceExport(SchemaspaceBase): """Handles the 'export' subcommand functionality for a specific schemaspace.""" schema_name_option = CliOption( "--schema_name", name="schema_name", description="The schema name of the metadata instances to export", required=False, ) include_invalid_flag = Flag( "--include-invalid", name="include-invalid", description="Export valid and invalid instances. " "By default only valid instances are exported.", default_value=False, ) clean_flag = Flag("--clean", name="clean", description="Clear out contents of the export directory", default_value=False) directory_option = CliOption( "--directory", name="directory", description= "The local file system path where the exported metadata will be stored", required=True, ) # 'Export' flags options: List[Option] = [ schema_name_option, include_invalid_flag, clean_flag, directory_option ] def __init__(self, **kwargs): super().__init__(**kwargs) self.metadata_manager = MetadataManager(schemaspace=self.schemaspace) def start(self): super().start() # process options schema_name = self.schema_name_option.value if schema_name: schema_list = sorted(list(self.schemas.keys())) if schema_name not in schema_list: print( f"Schema name '{schema_name}' is invalid. For the '{self.schemaspace}' schemaspace, " f"the schema name must be one of {schema_list}") self.exit(1) include_invalid = self.include_invalid_flag.value directory = self.directory_option.value clean = self.clean_flag.value try: if self.schema_name_option is not None: metadata_instances = self.metadata_manager.get_all( include_invalid=include_invalid, of_schema=schema_name) else: metadata_instances = self.metadata_manager.get_all( include_invalid=include_invalid) except MetadataNotFoundError: metadata_instances = None if not metadata_instances: print( f"No metadata instances found for schemaspace '{self.schemaspace}'" + (f" and schema '{schema_name}'" if schema_name else "")) print(f"Nothing exported to '{directory}'") return dest_directory = os.path.join(directory, self.schemaspace) if not os.path.exists(dest_directory): try: print(f"Creating directory structure for '{dest_directory}'") os.makedirs(dest_directory) except OSError as e: print( f"Error creating directory structure for '{dest_directory}': {e.strerror}: '{e.filename}'" ) self.exit(1) else: if clean: files = [ os.path.join(dest_directory, f) for f in os.listdir(dest_directory) ] if len(files) > 0: print(f"Cleaning out all files in '{dest_directory}'") [os.remove(f) for f in files if os.path.isfile(f)] print( f"Exporting metadata instances for schemaspace '{self.schemaspace}'" + (f" and schema '{schema_name}'" if schema_name else "") + (" (includes invalid)" if include_invalid else " (valid only)") + f" to '{dest_directory}'") num_valid_exported = 0 num_invalid_exported = 0 for instance in metadata_instances: dict_metadata = instance.to_dict() output_file = os.path.join(dest_directory, f'{dict_metadata["name"]}.json') if "reason" in dict_metadata and len(dict_metadata["reason"]) > 0: num_invalid_exported += 1 else: num_valid_exported += 1 with open(output_file, mode="w") as output_file: json.dump(dict_metadata, output_file, indent=4) total_exported = num_valid_exported + num_invalid_exported print(f"Exported {total_exported} " + ("instances" if total_exported > 1 else "instance") + f" ({num_invalid_exported} of which " + ("is" if num_invalid_exported == 1 else "are") + " invalid)")
class SchemaspaceList(SchemaspaceBase): """Handles the 'list' subcommand functionality for a specific schemaspace.""" json_flag = Flag("--json", name="json", description="List complete instances as JSON", default_value=False) valid_only_flag = Flag( "--valid-only", name="valid-only", description= "Only list valid instances (default includes invalid instances)", default_value=False, ) # 'List' flags options = [json_flag, valid_only_flag] def __init__(self, **kwargs): super().__init__(**kwargs) self.metadata_manager = MetadataManager(schemaspace=self.schemaspace) def start(self): super().start() # process options include_invalid = not self.valid_only_flag.value try: metadata_instances = self.metadata_manager.get_all( include_invalid=include_invalid) except MetadataNotFoundError: metadata_instances = None if self.json_flag.value: if metadata_instances is None: metadata_instances = [] print(metadata_instances) else: if not metadata_instances: print(f"No metadata instances found for {self.schemaspace}") return validity_clause = "includes invalid" if include_invalid else "valid only" print( f"Available metadata instances for {self.schemaspace} ({validity_clause}):" ) sorted_instances = sorted(metadata_instances, key=lambda inst: (inst.schema_name, inst.name)) # pad to width of longest instance max_schema_name_len = len("Schema") max_name_len = len("Instance") max_resource_len = len("Resource") for instance in sorted_instances: max_schema_name_len = max(len(instance.schema_name), max_schema_name_len) max_name_len = max(len(instance.name), max_name_len) max_resource_len = max(len(instance.resource), max_resource_len) print() print("%s %s %s " % ( "Schema".ljust(max_schema_name_len), "Instance".ljust(max_name_len), "Resource".ljust(max_resource_len), )) print("%s %s %s " % ( "------".ljust(max_schema_name_len), "--------".ljust(max_name_len), "--------".ljust(max_resource_len), )) for instance in sorted_instances: invalid = "" if instance.reason and len(instance.reason) > 0: invalid = f"**INVALID** ({instance.reason})" print("%s %s %s %s" % ( instance.schema_name.ljust(max_schema_name_len), instance.name.ljust(max_name_len), instance.resource.ljust(max_resource_len), invalid, ))