Skip to content

Commit

Permalink
chore: fixed example for data model generator
Browse files Browse the repository at this point in the history
For #153
  • Loading branch information
tstorek committed Aug 19, 2022
1 parent f613cd9 commit 837eee7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 6 additions & 6 deletions examples/ngsi_v2/e06_ngsi_v2_autogenerate_context_data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@
path=path.joinpath('entity.py'))
# ## 2. Parsing from external resources
#
create_data_model_file(path=path.joinpath('commons.py'),
url='https://smart-data-models.github.io/data-models/'
'common-schema.json')
create_data_model_file(
path=path.joinpath('commons.py'),
url='https://smart-data-models.github.io/data-models/common-schema.json')

# ## 3. Use generated data models
from examples.data_models.commons import \
from examples.ngsi_v2.data_models.commons import \
OpeningHoursSpecificationItem, \
time, \
DayOfWeek, \
DayOfWeekEnum, \
datetime

opening_hours = OpeningHoursSpecificationItem(
opens=time(hour=10),
closes=time(hour=19),
dayOfWeek=DayOfWeek.Saturday,
dayOfWeek=DayOfWeekEnum.Saturday,
validFrom=datetime(year=2022, month=1, day=1))

print(opening_hours.json(indent=2))
4 changes: 4 additions & 0 deletions filip/utils/model_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,16 @@ def create_context_entity_model(name: str = None,
if isinstance(path, str):
path=Path(path)

# check if directory exists and create if not
path.parent.mkdir(exist_ok=True)

with TemporaryDirectory() as temp:
temp = Path(temp)
output = Path(temp).joinpath(f'{uuid4()}.json')
output.touch(exist_ok=True)
with output.open('w') as f:
json.dump(model.schema(), f, indent=2)

if path.suffix == '.json':
# move temporary file to output directory
shutil.move(str(output), str(path))
Expand Down

0 comments on commit 837eee7

Please sign in to comment.