Skip to content

Commit

Permalink
return None if pretty (#354)
Browse files Browse the repository at this point in the history
Co-authored-by: mouffok <[email protected]>
  • Loading branch information
ssssarah and ssssarah authored Oct 30, 2023
1 parent 14ffc1a commit 9ffefac
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions kgforge/core/archetypes/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ def __repr__(self) -> str:
# Vocabulary.

def prefixes(self, pretty: bool) -> Optional[Dict[str, str]]:
prefixes = sorted(self._prefixes().items())
prefixes = dict(sorted(self._prefixes().items(), key=lambda item: item[0]))
if pretty:
print("Used prefixes:")
df = DataFrame(prefixes)
formatters = {x: f"{{:<{df[x].str.len().max()}s}}".format for x in df.columns}
print(df.to_string(header=False, index=False, formatters=formatters))
else:
return dict(prefixes)
return None

return dict(prefixes)

def _prefixes(self) -> Dict[str, str]:
not_supported()
Expand All @@ -68,8 +69,9 @@ def types(self, pretty: bool) -> Optional[List[str]]:
types = sorted(self._types())
if pretty:
print(*["Managed entity types:", *types], sep="\n - ")
else:
return types
return None

return types

@abstractmethod
def _types(self) -> List[str]:
Expand All @@ -94,12 +96,14 @@ def template(self, type: str, only_required: bool, output: str) -> Optional[Dict
schema = self._template(type, only_required)
if output == "hjson":
print(hjson.dumps(schema, indent=4, item_sort_key=sort_attrs))
elif output == "json":
return None
if output == "json":
print(hjson.dumpsJSON(schema, indent=4, item_sort_key=sort_attrs))
elif output == "dict":
return None
if output == "dict":
return json.loads(hjson.dumpsJSON(schema, item_sort_key=sort_attrs))
else:
raise ValueError("unrecognized output")

raise ValueError("unrecognized output")

@abstractmethod
def _template(self, type: str, only_required: bool) -> Dict:
Expand All @@ -114,8 +118,9 @@ def sources(self, pretty: bool) -> Optional[List[str]]:
sources = sorted(self._sources())
if pretty:
print(*["Data sources with managed mappings:", *sources], sep="\n - ")
else:
return sources
return None

return sources

def _sources(self) -> List[str]:
# The discovery strategy cannot be abstracted as it depends on the Model data organization.
Expand All @@ -128,8 +133,9 @@ def mappings(self, source: str, pretty: bool) -> Optional[Dict[str, List[str]]]:
print("Managed mappings for the data source per entity type and mapping type:")
for k, v in mappings.items():
print(*[f" - {k}:", *v], sep="\n * ")
else:
return mappings
return None

return mappings

def _mappings(self, source: str) -> Dict[str, List[str]]:
# POLICY Should raise ValueError if 'source' is not managed by the Model.
Expand Down

0 comments on commit 9ffefac

Please sign in to comment.