hestia-earth-utils 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ include README.md
2
+ include hestia_earth/utils/lookups/*.csv
@@ -0,0 +1,41 @@
1
+ Metadata-Version: 2.1
2
+ Name: hestia_earth.utils
3
+ Version: 0.0.1
4
+ Summary: Hestia's utils library
5
+ Home-page: https://gitlab.com/hestia-earth/hestia-utils
6
+ Author: Hestia Team
7
+ Author-email: guillaumeroyer.mail@gmail.com
8
+ License: GPL-3.0-or-later
9
+ Description: # Hestia Utils
10
+
11
+ ## Install
12
+
13
+ 1. Install the module:
14
+ ```bash
15
+ pip install hestia_earth.utils
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ 1. To download a file from the Hestia API:
21
+ ```python
22
+ from hestia_earth.schema import SchemaType
23
+ from hestia_earth.utils import download_hestia
24
+
25
+ cycle = download_hestia('cycleId', SchemaType.CYCLE)
26
+ sandContent = download_hestia('sandContent', SchemaType.TERM)
27
+ ```
28
+
29
+ 2. To search for a specific Node on Hestia:
30
+ ```python
31
+ from hestia_earth.schema import SchemaType
32
+ from hestia_earth.utils import find_node_exact
33
+
34
+ source = find_node_exact(SchemaType.SOURCE, {'bibliography.title.keyword': 'My Bibliography'})
35
+ ```
36
+
37
+ Platform: UNKNOWN
38
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
39
+ Classifier: Programming Language :: Python :: 3.6
40
+ Requires-Python: >=3
41
+ Description-Content-Type: text/markdown
@@ -0,0 +1,27 @@
1
+ # Hestia Utils
2
+
3
+ ## Install
4
+
5
+ 1. Install the module:
6
+ ```bash
7
+ pip install hestia_earth.utils
8
+ ```
9
+
10
+ ## Usage
11
+
12
+ 1. To download a file from the Hestia API:
13
+ ```python
14
+ from hestia_earth.schema import SchemaType
15
+ from hestia_earth.utils import download_hestia
16
+
17
+ cycle = download_hestia('cycleId', SchemaType.CYCLE)
18
+ sandContent = download_hestia('sandContent', SchemaType.TERM)
19
+ ```
20
+
21
+ 2. To search for a specific Node on Hestia:
22
+ ```python
23
+ from hestia_earth.schema import SchemaType
24
+ from hestia_earth.utils import find_node_exact
25
+
26
+ source = find_node_exact(SchemaType.SOURCE, {'bibliography.title.keyword': 'My Bibliography'})
27
+ ```
@@ -0,0 +1 @@
1
+ __import__('pkg_resources').declare_namespace(__name__)
@@ -0,0 +1,64 @@
1
+ from pkgutil import extend_path
2
+ import requests
3
+ import os
4
+ import json
5
+ from hestia_earth.schema import SchemaType
6
+
7
+ __path__ = extend_path(__path__, __name__)
8
+
9
+
10
+ def non_empty_value(value): return value != '' and value is not None and value != []
11
+
12
+
13
+ def join_args(values): return '&'.join(list(filter(non_empty_value, values))).strip()
14
+
15
+
16
+ def api_url(): return os.env.get('API_URL', 'https://api.hestia.earth')
17
+
18
+
19
+ def search_url(): return os.getenv('SEARCH_URL', 'https://search.hestia.earth')
20
+
21
+
22
+ def request_url(base_url: str, **kwargs):
23
+ args = list(map(lambda key: '='.join([key, str(kwargs.get(key))]), kwargs.keys()))
24
+ return f"{base_url}?{join_args(args)}"
25
+
26
+
27
+ def download_hestia(node_id: str, node_type=SchemaType.TERM, mode=''):
28
+ # try to download file from s3 first, if fail use api
29
+ try:
30
+ import boto3
31
+ file_key = f"{node_type.value}/{node_id}.jsonld"
32
+ return json.loads(boto3.client('s3').get_object(Bucket=os.getenv('AWS_BUCKET'), Key=file_key)['Body'].read())
33
+ except Exception:
34
+ url = request_url(f"{api_url()}/download", type=node_type.value, id=node_id, mode=mode)
35
+ return requests.get(url).json()
36
+ except requests.exceptions.RequestException:
37
+ return None
38
+
39
+
40
+ def find_node(node_type: SchemaType, args: dict, limit=10):
41
+ headers = {'Content-Type': 'application/json'}
42
+ query_args = list(map(lambda key: {'match': {key: args.get(key)}}, args.keys()))
43
+ must = [{'match': {'@type': node_type.value}}]
44
+ must.extend(query_args)
45
+ hits = requests.post(search_url(), json.dumps({
46
+ 'query': {'bool': {'must': must}},
47
+ 'limit': limit,
48
+ '_source': {'includes': ['name', '@id']}
49
+ }), headers=headers).json()['hits']['hits']
50
+ return list(map(lambda res: res.get('_source'), hits))
51
+
52
+
53
+ def find_node_exact(node_type: SchemaType, args: dict):
54
+ headers = {'Content-Type': 'application/json'}
55
+ query_args = list(map(lambda key: {'match': {key: args.get(key)}}, args.keys()))
56
+ must = [{'match': {'@type': node_type.value}}]
57
+ must.extend(query_args)
58
+ hits = requests.post(search_url(), json.dumps({
59
+ 'query': {'bool': {'must': must}},
60
+ 'limit': 2,
61
+ '_source': {'includes': ['name', '@id']}
62
+ }), headers=headers).json()['hits']['hits']
63
+ # do not return a duplicate
64
+ return hits[0].get('_source') if len(hits) == 1 else None
@@ -0,0 +1,7 @@
1
+ def find_term_match(values: list, term_id: str, default_val={}):
2
+ return next((v for v in values if v['term']['@id'] == term_id), default_val)
3
+
4
+
5
+ def find_primary_product(cycle: dict):
6
+ products = cycle.get('products', [])
7
+ return next((p for p in products if p.get('primary', False)), products[0]) if len(products) > 0 else None
@@ -0,0 +1 @@
1
+ VERSION = '0.0.1'
@@ -0,0 +1,41 @@
1
+ Metadata-Version: 2.1
2
+ Name: hestia-earth.utils
3
+ Version: 0.0.1
4
+ Summary: Hestia's utils library
5
+ Home-page: https://gitlab.com/hestia-earth/hestia-utils
6
+ Author: Hestia Team
7
+ Author-email: guillaumeroyer.mail@gmail.com
8
+ License: GPL-3.0-or-later
9
+ Description: # Hestia Utils
10
+
11
+ ## Install
12
+
13
+ 1. Install the module:
14
+ ```bash
15
+ pip install hestia_earth.utils
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ 1. To download a file from the Hestia API:
21
+ ```python
22
+ from hestia_earth.schema import SchemaType
23
+ from hestia_earth.utils import download_hestia
24
+
25
+ cycle = download_hestia('cycleId', SchemaType.CYCLE)
26
+ sandContent = download_hestia('sandContent', SchemaType.TERM)
27
+ ```
28
+
29
+ 2. To search for a specific Node on Hestia:
30
+ ```python
31
+ from hestia_earth.schema import SchemaType
32
+ from hestia_earth.utils import find_node_exact
33
+
34
+ source = find_node_exact(SchemaType.SOURCE, {'bibliography.title.keyword': 'My Bibliography'})
35
+ ```
36
+
37
+ Platform: UNKNOWN
38
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
39
+ Classifier: Programming Language :: Python :: 3.6
40
+ Requires-Python: >=3
41
+ Description-Content-Type: text/markdown
@@ -0,0 +1,12 @@
1
+ MANIFEST.in
2
+ README.md
3
+ setup.py
4
+ hestia_earth/__init__.py
5
+ hestia_earth.utils.egg-info/PKG-INFO
6
+ hestia_earth.utils.egg-info/SOURCES.txt
7
+ hestia_earth.utils.egg-info/dependency_links.txt
8
+ hestia_earth.utils.egg-info/requires.txt
9
+ hestia_earth.utils.egg-info/top_level.txt
10
+ hestia_earth/utils/__init__.py
11
+ hestia_earth/utils/model.py
12
+ hestia_earth/utils/version.py
@@ -0,0 +1,2 @@
1
+ hestia_earth.schema>=0.4.4
2
+ requests>=2.24.0
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,33 @@
1
+ import pathlib
2
+ from setuptools import find_packages, setup
3
+
4
+ from hestia_earth.utils.version import VERSION
5
+
6
+ # The directory containing this file
7
+ HERE = pathlib.Path(__file__).parent
8
+
9
+ # The text of the README file
10
+ README = (HERE / 'README.md').read_text()
11
+
12
+ REQUIRES = (HERE / 'requirements.txt').read_text().splitlines()
13
+
14
+ # This call to setup() does all the work
15
+ setup(
16
+ name='hestia_earth.utils',
17
+ version=VERSION,
18
+ description="Hestia's utils library",
19
+ long_description=README,
20
+ long_description_content_type='text/markdown',
21
+ url='https://gitlab.com/hestia-earth/hestia-utils',
22
+ author='Hestia Team',
23
+ author_email='guillaumeroyer.mail@gmail.com',
24
+ license='GPL-3.0-or-later',
25
+ classifiers=[
26
+ 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
27
+ 'Programming Language :: Python :: 3.6',
28
+ ],
29
+ packages=find_packages(exclude=('tests', 'scripts')),
30
+ include_package_data=True,
31
+ install_requires=REQUIRES,
32
+ python_requires='>=3'
33
+ )