Skip to content
Snippets Groups Projects
Verified Commit f2b2af6d authored by Benjamin Regler's avatar Benjamin Regler
Browse files

Added missing docstrings, file version, and fixed bug in `nomad_query.username()`

parent b5c109dd
No related branches found
No related tags found
No related merge requests found
...@@ -45,16 +45,23 @@ class NomadQueryResult(object): ...@@ -45,16 +45,23 @@ class NomadQueryResult(object):
@author Benjamin Regler @author Benjamin Regler
""" """
def __init__(self, query, response): def __init__(self, query, response, version=1.0):
"""Constructor. """Constructor.
Arguments: Arguments:
query {dict} -- Query information, i.e., query filter, context,
group_by, endpoint, and URL
response {dict} -- Response of the Nomad Query API response {dict} -- Response of the Nomad Query API
Keyword Arguments:
version {number} -- Version of the Nomad Query data file
(default: {1.0})
""" """
self._uri = [] self._uri = []
self._query = query or {} self._query = query or {}
self._timestamp = int(time.time()) self._timestamp = int(time.time())
self._response = response.get('result', {}) self._response = response.get('result', {})
self._version = version
# Construct download path # Construct download path
path = response.get('path', '') path = response.get('path', '')
...@@ -67,7 +74,6 @@ class NomadQueryResult(object): ...@@ -67,7 +74,6 @@ class NomadQueryResult(object):
regex = re.compile(r'(?<=/[a-zA-Z0-9\-_]{3}/)[^\.]+') regex = re.compile(r'(?<=/[a-zA-Z0-9\-_]{3}/)[^\.]+')
paths = response['data'].get('result', []) paths = response['data'].get('result', [])
uri = []
for path in paths: for path in paths:
match = regex.search(path) match = regex.search(path)
if match: if match:
...@@ -80,6 +86,14 @@ class NomadQueryResult(object): ...@@ -80,6 +86,14 @@ class NomadQueryResult(object):
self._uri.append('nmd://' + '/'.join(groups)) self._uri.append('nmd://' + '/'.join(groups))
def version(self):
"""Get the version of the Nomad Query data file.
Returns:
float -- Version of the Nomad Query data file
"""
return self._version
def timestamp(self): def timestamp(self):
"""Get the timestamp of the query. """Get the timestamp of the query.
...@@ -127,6 +141,9 @@ class NomadQuery(object): ...@@ -127,6 +141,9 @@ class NomadQuery(object):
@author Benjamin Regler @author Benjamin Regler
""" """
# Version of the Nomad Query API
__version__ = 1.0
# Nomad API endpoint # Nomad API endpoint
endpoint = 'https://analytics-toolkit.nomad-coe.eu/api/' endpoint = 'https://analytics-toolkit.nomad-coe.eu/api/'
...@@ -168,7 +185,7 @@ class NomadQuery(object): ...@@ -168,7 +185,7 @@ class NomadQuery(object):
""" """
if username: if username:
self._username = str(username) self._username = str(username)
self._base_path = os.path.join(self.user_path, self.username, self._base_path = os.path.join(self.user_path, self._username,
'nomad-query') 'nomad-query')
return self._username return self._username
...@@ -359,7 +376,7 @@ class NomadQuery(object): ...@@ -359,7 +376,7 @@ class NomadQuery(object):
'group_by': group_by.strip().lower(), 'group_by': group_by.strip().lower(),
'url': url 'url': url
} }
return NomadQueryResult(query, response) return NomadQueryResult(query, response, self.__version__)
def fetch(self, name_or_index='', resolve=False, **params): def fetch(self, name_or_index='', resolve=False, **params):
"""Fetch stored query. """Fetch stored query.
...@@ -466,6 +483,7 @@ class NomadQuery(object): ...@@ -466,6 +483,7 @@ class NomadQuery(object):
data = { data = {
'filename': filename, 'filename': filename,
'version': self.__version__,
'query': query.query(), 'query': query.query(),
'timestamp': timestamp, 'timestamp': timestamp,
'uri': query.uri() 'uri': query.uri()
...@@ -559,7 +577,7 @@ class NomadQuery(object): ...@@ -559,7 +577,7 @@ class NomadQuery(object):
""" """
try: try:
os.makedirs(path) os.makedirs(path)
except OSError as exc: # Python >2.5 except OSError as exc: # Python > 2.5
if exc.errno != errno.EEXIST or not os.path.isdir(path): if exc.errno != errno.EEXIST or not os.path.isdir(path):
return False return False
return True return True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment