Skip to content

Commit

Permalink
hxlm (#11): started HMeta class, hxlm.core.model.meta
Browse files Browse the repository at this point in the history
  • Loading branch information
fititnt committed Mar 1, 2021
1 parent 27092a9 commit cfe0824
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 15 deletions.
4 changes: 2 additions & 2 deletions hxlm/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""hxlm is (TODO: document)
"""

__version__="0.7.1"
__version__="0.7.2"

import hxlm.core.constant
import hxlm.core.exception
import hxlm.core.base
import hxlm.core.model

# Users need to explicitly call this
import hxlm.core.compliance
4 changes: 4 additions & 0 deletions hxlm/core/model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""TODO: document me
"""
from hxlm.core.model.base import *
from hxlm.core.model.meta import *
1 change: 0 additions & 1 deletion hxlm/core/base.py → hxlm/core/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from hxlm.core.htype.license import LicenseHtype, CopyrightHtype
from hxlm.core.compliance import verbose_event


# TODO: implement concept of recipe https://github.com/OCHA-DAP/hxl-recipes.
# Maybe HConteiner could also have one or mroe recipes?
# (E.Rocha, 2021-02-26 08:05 UTC)
Expand Down
34 changes: 34 additions & 0 deletions hxlm/core/model/meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""hxlm.core.model.meta contans HMeta
Copyleft 🄯 2021, Emerson Rocha (Etica.AI) <[email protected]>
License: Public Domain / BSD Zero Clause License
SPDX-License-Identifier: Unlicense OR 0BSD
"""


class HMeta:
"""HMeta is the main entry point to glue collections of HConteiner and etc
In practice, is mostly used to, with help with external utils, abstract
hmeta.yml from disk
"""

def __init__(self, schemas_raw=None):
self.kind: str = 'HMeta'
self._schemas_raw = schemas_raw

def export_schemas(self):
return self._schemas_raw

def load_schemas(self, schemas_raw):
"""load_schemas load object and convert to HMeta
How the object is saved on disk (or received from external sources)
is out of scope of this class.
Args:
schemas (Object): Load generic object to HMeta
"""
self._schemas_raw = schemas_raw
# print(schemas)

34 changes: 34 additions & 0 deletions hxlm/core/schema/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import yaml
import json
from hxlm.core.model import (
HMeta
)


def export_schema_yaml(schema):
Expand All @@ -18,6 +21,14 @@ def export_schema_json(schema):


def get_schema(file):
"""Get an HMeta schema from file (raw object, without Model)
Args:
file (String): Object containing the schema on local disk
Returns:
Object: Raw object from YAML file
"""
# Funciona Ok, exceto com a Noruega https://noyaml.com/

with open(file) as f:
Expand All @@ -28,6 +39,29 @@ def get_schema(file):
return data


def get_schema_as_hmeta(file):
"""Get an HMeta schema from file (raw object, without Model)
Args:
file (String): Object containing the schema on local disk
Returns:
Object: Raw object from YAML file
"""
# Funciona Ok, exceto com a Noruega https://noyaml.com/

with open(file) as f:

data = yaml.safe_load(f)
hmeta = HMeta()
hmeta.load_schemas(schemas=data)
# hmeta.export_schemas(schemas=data)
# data = yaml.safe_load_all(f)
# print(data)
# return data
return hmeta.export_schemas()


class Dumper(yaml.Dumper):
"""Force identation on pylint, https://github.com/yaml/pyyaml/issues/234
TODO: check on future if this still need (Emerson Rocha, 2021-02-28 10:56 UTC)
Expand Down
24 changes: 12 additions & 12 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@
# print(hxlm.core.debug())
# print(hxlm.schema)

example = schema.get_schema('/workspace/git/EticaAI/HXL-Data-Science-file-formats/hxlm/data/baseline/hmeta.yml')

# print('get_schema (source file)')
# print(example)
# get_schema (source file)
# {'$schema': 'https://raw.githubusercontent.com/EticaAI/HXL-Data-Science-file-formats/main/hxlm/core/schema/hxlm.schema.json', 'title': 'hxml.core.data.baseline', 'hdatasets': [{'key': 'place', 'comments': 'HXL-CPLP-FOD_countries-territories.csv', 'source': 'https://docs.google.com/spreadsheets/d/12k4BWqq5c3mV9ihQscPIwtuDa_QRB-iFohO7dXSSptI/edit#gid=0', 'tags': ['ISO 3166', 'ISO 3166-2', 'ISO 3166-3']}, {'key': 'lang', 'comments': 'HXL-CPLP-FOD_languages', 'source': 'https://docs.google.com/spreadsheets/d/12k4BWqq5c3mV9ihQscPIwtuDa_QRB-iFohO7dXSSptI/edit#gid=0', 'tags': ['ISO 639-3', 'ISO 3692-2', 'ISO 3692-3']}], 'hfiles': [{'key': 'TODO.txt'}]}

print('export_schema_yaml (string to export)')
print(schema.export_schema_yaml(example))
# export_schema_yaml (string to export)
# $schema: https://raw.githubusercontent.com/EticaAI/HXL-Data-Science-file-formats/main/hxlm/core/schema/hxlm.schema.json
# hdatasets:
Expand All @@ -49,8 +40,17 @@
# export_schema_json (string to export)
# {"$schema": "https://raw.githubusercontent.com/EticaAI/HXL-Data-Science-file-formats/main/hxlm/core/schema/hxlm.schema.json", "title": "hxml.core.data.baseline", "hdatasets": [{"key": "place", "comments": "HXL-CPLP-FOD_countries-territories.csv", "source": "https://docs.google.com/spreadsheets/d/12k4BWqq5c3mV9ihQscPIwtuDa_QRB-iFohO7dXSSptI/edit#gid=0", "tags": ["ISO 3166", "ISO 3166-2", "ISO 3166-3"]}, {"key": "lang", "comments": "HXL-CPLP-FOD_languages", "source": "https://docs.google.com/spreadsheets/d/12k4BWqq5c3mV9ihQscPIwtuDa_QRB-iFohO7dXSSptI/edit#gid=0", "tags": ["ISO 639-3", "ISO 3692-2", "ISO 3692-3"]}], "hfiles": [{"key": "TODO.txt"}]}



schema_baseline = schema.get_schema('/workspace/git/EticaAI/HXL-Data-Science-file-formats/hxlm/core/schema/baseline.yml')
print('export_schema_yaml schema_baseline')
print(schema.export_schema_yaml(schema_baseline))
print(schema.export_schema_yaml(schema_baseline))

# example = schema.get_schema('/workspace/git/EticaAI/HXL-Data-Science-file-formats/hxlm/data/baseline/hmeta.yml')
example = schema.get_schema_as_hmeta('/workspace/git/EticaAI/HXL-Data-Science-file-formats/hxlm/data/baseline/hmeta.yml')

# print('get_schema (source file)')
# print(example)
# get_schema (source file)
# {'$schema': 'https://raw.githubusercontent.com/EticaAI/HXL-Data-Science-file-formats/main/hxlm/core/schema/hxlm.schema.json', 'title': 'hxml.core.data.baseline', 'hdatasets': [{'key': 'place', 'comments': 'HXL-CPLP-FOD_countries-territories.csv', 'source': 'https://docs.google.com/spreadsheets/d/12k4BWqq5c3mV9ihQscPIwtuDa_QRB-iFohO7dXSSptI/edit#gid=0', 'tags': ['ISO 3166', 'ISO 3166-2', 'ISO 3166-3']}, {'key': 'lang', 'comments': 'HXL-CPLP-FOD_languages', 'source': 'https://docs.google.com/spreadsheets/d/12k4BWqq5c3mV9ihQscPIwtuDa_QRB-iFohO7dXSSptI/edit#gid=0', 'tags': ['ISO 639-3', 'ISO 3692-2', 'ISO 3692-3']}], 'hfiles': [{'key': 'TODO.txt'}]}

print('export_schema_yaml (string to export)')
print(schema.export_schema_yaml(example))

0 comments on commit cfe0824

Please sign in to comment.