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):
@author Benjamin Regler
"""
def __init__(self, query, response):
def __init__(self, query, response, version=1.0):
"""Constructor.
Arguments:
query {dict} -- Query information, i.e., query filter, context,
group_by, endpoint, and URL
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._query = query or {}
self._timestamp = int(time.time())
self._response = response.get('result', {})
self._version = version
# Construct download path
path = response.get('path', '')
......@@ -67,7 +74,6 @@ class NomadQueryResult(object):
regex = re.compile(r'(?<=/[a-zA-Z0-9\-_]{3}/)[^\.]+')
paths = response['data'].get('result', [])
uri = []
for path in paths:
match = regex.search(path)
if match:
......@@ -80,6 +86,14 @@ class NomadQueryResult(object):
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):
"""Get the timestamp of the query.
......@@ -127,6 +141,9 @@ class NomadQuery(object):
@author Benjamin Regler
"""
# Version of the Nomad Query API
__version__ = 1.0
# Nomad API endpoint
endpoint = 'https://analytics-toolkit.nomad-coe.eu/api/'
......@@ -168,7 +185,7 @@ class NomadQuery(object):
"""
if 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')
return self._username
......@@ -359,7 +376,7 @@ class NomadQuery(object):
'group_by': group_by.strip().lower(),
'url': url
}
return NomadQueryResult(query, response)
return NomadQueryResult(query, response, self.__version__)
def fetch(self, name_or_index='', resolve=False, **params):
"""Fetch stored query.
......@@ -466,6 +483,7 @@ class NomadQuery(object):
data = {
'filename': filename,
'version': self.__version__,
'query': query.query(),
'timestamp': timestamp,
'uri': query.uri()
......@@ -559,7 +577,7 @@ class NomadQuery(object):
"""
try:
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):
return False
return True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment