Skip to content

Commit

Permalink
hxlm (#11), urnresolver (#13): drafted concept of 'urnref' (something…
Browse files Browse the repository at this point in the history
… like when have several sources or URNs, allow urnresolver filter sources (at first just use file names)
  • Loading branch information
fititnt committed Mar 7, 2021
1 parent 041420e commit 33eb104
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 26 deletions.
57 changes: 43 additions & 14 deletions hxlm/core/bin/urnresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
# ├── attribute.csv
# └── hashtag.csv

# The data:
# ~/.local/var/hxlm/data
# The default place for all individual URNs (excluding the index one)
# ~/.config/hxlm/urn

import sys
import os
import logging
Expand All @@ -81,7 +86,7 @@
import hxl.io

import hxlm.core.htype.urn as HUrn
import hxlm.core.schema.urn.util as HUrnUtil
# import hxlm.core.schema.urn.util as HUrnUtil

# @see https://github.com/hugapi/hug
# pip3 install hug --upgrade
Expand Down Expand Up @@ -143,25 +148,49 @@ def execute_cli(self, args,
stdin=STDIN, stdout=sys.stdout, stderr=sys.stderr):
"""
The execute_cli is the main entrypoint of URNResolver. When
called will convert the HXL source to example format.
called will try to convert the URN to an valid IRI.
"""

# Test commands:
# urnresolver --debug urn:data:xz:hxl:standard:core:hashtag
# urnresolver urn:data:xz:hxl:standard:core:hashtag
# --urn-file tests/urnresolver/all-in-same-dir/

if 'debug' in args and args.debug:
print('DEBUG: CLI args [[', args, ']]')

# print('args', args)
urn_item = HUrn.cast_urn(urn=args.infile)

urn_string = args.infile

urn_item = HUrn.cast_urn(urn=urn_string)
urn_item.prepare()

if 'urn_file' in args and len(args.urn_file) > 0:
print('TODO: try load default configurations')

if 'debug' in args and args.debug:
# valt = HUrnUtil.get_urn_vault_local_info('un', 'locode')
HUrnUtil.debug_local_data('un', 'locode')
HUrnUtil.get_urn_vault_local_info(urn_item)

# print('valt', valt)
print('args', args)
print('args.infile', args.infile)
print('urn_item', urn_item)
print('about', urn_item.about())
print('about base_paths', urn_item.about('base_paths'))
print('about object_names', urn_item.about('object_names'))
print('DEBUG: urn_item [[', urn_item, ']]')
print('DEBUG: urn_item.about() [[', urn_item.about(), ']]')
print('DEBUG: urn_item.about(base_paths) [[',
urn_item.about('base_paths'), ']]')
print('DEBUG: urn_item.about(object_names) [[',
urn_item.about('object_names'), ']]')

# if 'debug' in args and args.debug:
# # valt = HUrnUtil.get_urn_vault_local_info('un', 'locode')
# # HUrnUtil.debug_local_data('un', 'locode')
# # HUrnUtil.get_urn_vault_local_info(urn_item)

# # print('valt', valt)
# # print('args', args)
# print('args.infile', args.infile)
# print('urn_item', urn_item)
# print('about', urn_item.about())
# print('about base_paths', urn_item.about('base_paths'))
# print('about object_names', urn_item.about('object_names'))

stderr.write('ERROR: urn [' + urn_string + '] strict match not found')

print(urn_item.get_resources())

Expand Down
26 changes: 24 additions & 2 deletions hxlm/core/schema/urn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ def get_urn_resolver_from_csv(urn_file: str,
# (aka lazy), but wrong (Emerson Rocha, 2021-03-07 17:25)

result = []

# print('ttt', Path(urn_file).name)
urnref = Path(urn_file).name
# raise Exception(Path(urn_file).name)

with open(urn_file, 'r') as open_urn_file:
csvreader = csv.reader(open_urn_file, delimiter=delimiter)
for row in csvreader:
Expand All @@ -151,7 +156,8 @@ def get_urn_resolver_from_csv(urn_file: str,
item = {
'urn': row[0],
# 'source_remote': row[1]
'source': [row[1]]
'source': [row[1]],
'urnref': urnref
}
result.append(item)

Expand All @@ -173,8 +179,18 @@ def get_urn_resolver_from_json(urn_file: str) -> List[dict]:
Returns:
List[dict]: the resolver list of dictionaries to parse
"""

# To help filter with several sources of URNs, we add an urnref to allow
# filter on steps
urnref = Path(urn_file).name

with open(urn_file, "r") as read_file:
data = json.load(read_file)

for item in data:
if 'urnref' not in item:
item['urnref'] = urnref

return data
# print('data', type(data), data)

Expand All @@ -194,11 +210,17 @@ def get_urn_resolver_from_yml(urn_file: str) -> List[dict]:
List[dict]: the resolver list of dictionaries to parse
"""

# print('get_urn_resolver_from_yml')
# To help filter with several sources of URNs, we add an urnref to allow
# filter on steps
urnref = Path(urn_file).name

with open(urn_file, "r") as read_file:
data = yaml.safe_load(read_file)
# print('get_urn_resolver_from_yml data', data)

for item in data:
if 'urnref' not in item:
item['urnref'] = urnref
return data


Expand Down
25 changes: 15 additions & 10 deletions tests/test_core_bin_urnresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,39 @@
# To test directly
# pytest -vv ./tests/test_core_bin_urnresolver.py

import os
import pathlib
TESTDIR = str(pathlib.Path(__file__).parent.absolute()) + '/urnresolver'
TEST_SIG_A = 'urn:data:xz:hxl:standard:core:hashtag'

from hxlm.core.schema.urn.util import (
get_urn_resolver_from_csv,
get_urn_resolver_from_json,
get_urn_resolver_from_yml,
# get_urn_resolver_local,
)
import os
import pathlib
TESTDIR = str(pathlib.Path(__file__).parent.absolute()) + '/urnresolver'
TEST_SIG_A = 'urn:data:xz:hxl:standard:core:hashtag'


def test_core_bin_urnresolver_json():
result = get_urn_resolver_from_json(TESTDIR + '/json/urn.json')
# print('result', result)
print('result', result)
print('result urnnnn', result[0]['urn'])
assert result[0]['urn'] ==TEST_SIG_A
assert result[0]['urn'] == TEST_SIG_A
assert result[0]['urnref'] == "urn.json"


def test_core_bin_urnresolver_csv():
result = get_urn_resolver_from_csv(TESTDIR + '/csv/urn.csv')
# print('result', result)
assert result[0]['urn'] ==TEST_SIG_A
assert result[0]['urn'] == TEST_SIG_A
assert result[0]['urnref'] == "urn.csv"


def test_core_bin_urnresolver_yml():
result = get_urn_resolver_from_yml(TESTDIR + '/yml/urn.yml')
# print('result', result)
assert result[0]['urn'] ==TEST_SIG_A
assert result[0]['urn'] == TEST_SIG_A
assert result[0]['urnref'] == "urn.yml"


test_core_bin_urnresolver_json()
test_core_bin_urnresolver_yml()
test_core_bin_urnresolver_yml()

0 comments on commit 33eb104

Please sign in to comment.