From f2b2af6d2ef7b0c34f3f14d780ef88afb11ffaf9 Mon Sep 17 00:00:00 2001
From: Benjamin Regler <regler@fhi-berlin.mpg.de>
Date: Tue, 9 Jan 2018 09:53:35 +0100
Subject: [PATCH] Added missing docstrings, file version, and fixed bug in
 `nomad_query.username()`

---
 common/python/nomadcore/nomad_query.py | 30 ++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/common/python/nomadcore/nomad_query.py b/common/python/nomadcore/nomad_query.py
index b6d160f..40fb7f7 100644
--- a/common/python/nomadcore/nomad_query.py
+++ b/common/python/nomadcore/nomad_query.py
@@ -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
-- 
GitLab