You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@climate.apache.org by jo...@apache.org on 2014/02/02 00:57:34 UTC

svn commit: r1563530 - in /incubator/climate/trunk/ocw-ui/backend: directory_helpers.py tests/test_directory_helpers.py

Author: joyce
Date: Sat Feb  1 23:57:34 2014
New Revision: 1563530

URL: http://svn.apache.org/r1563530
Log:
CLIMATE-326 - Cleanup specific results retrieval endpoint

- Refactor directory_helpers.get_results.
- Add endpoint tests for get_results.

Modified:
    incubator/climate/trunk/ocw-ui/backend/directory_helpers.py
    incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py

Modified: incubator/climate/trunk/ocw-ui/backend/directory_helpers.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/ocw-ui/backend/directory_helpers.py?rev=1563530&r1=1563529&r2=1563530&view=diff
==============================================================================
--- incubator/climate/trunk/ocw-ui/backend/directory_helpers.py (original)
+++ incubator/climate/trunk/ocw-ui/backend/directory_helpers.py Sat Feb  1 23:57:34 2014
@@ -132,28 +132,58 @@ def get_result_dir_info():
         return "%s(%s)" % (request.query.callback, {'listing': dir_info})
     return {'listing': dir_info}
 
-@dir_app.route('/getResults/<dirPath:path>')
-def getResults(dirPath):
-    dirPath = WORK_DIR + dirPath
-    dirPath = dirPath.replace('/../', '/')
-    dirPath = dirPath.replace('/./', '/')
-
-    if os.path.isdir(dirPath):
-        listing = os.listdir(dirPath)
-        listingNoHidden = [f for f in listing if f[0] != '.']
-        joinedPaths = [os.path.join(dirPath, f) for f in listingNoHidden]
-        onlyFilesNoDirs = [f for f in joinedPaths if os.path.isfile(f)]
-        finalPaths = [p.replace(WORK_DIR, '') for p in onlyFilesNoDirs]
-        sorted(finalPaths, key=lambda s: s.lower())
-        returnJSON = finalPaths
+@dir_app.route('/results/<dir_path:path>')
+def get_results(dir_path):
+    ''' Retrieve specific result files.
+
+    :param dir_path: The relative results path to list.
+    :type dir_path: String
+
+    :returns: Dictionary of the requested result's directory listing.
+
+    * Successful JSON Response *
+
+    ..sourcecode: javascript
+
+        {
+            'listing': [
+                'file1',
+                'file2'
+            ]
+        }
+
+    * Failure JSON Response *
+
+    ..sourcecode: javascript
+
+        {
+            'listing': []
+        }
+    '''
+    dir_info = []
+
+    try:
+        clean_path = _get_clean_directory_path(WORK_DIR, dir_path)
+        dir_listing = os.listdir(clean_path)
+    except:
+        # ValueError - dir_path couldn't be 'cleaned'
+        # OSError - clean_path is not a directory
+        # Either way, we don't have any results to return!
+        pass
     else:
-        returnJSON = []
+        for obj in dir_listing:
+            # Ignore hidden files
+            if obj[0] == '.': continue
+
+            # Create a path to the listed object and strip out the path leader.
+            obj = os.path.join(clean_path, obj)
+            dir_info.append(obj.replace(WORK_DIR, ''))
+
+        sorted(dir_info, key=lambda s: s.lower())
 
-    returnJSON = json.dumps(returnJSON)
     if request.query.callback:
-        return "%s(%s)" % (request.query.callback, returnJSON)
-    else:
-        return returnJSON
+        return "%s(%s)" % (request.query.callback, {'listing': dir_info})
+    return {'listing': dir_info}
 
 @dir_app.route('/path_leader/')
 def get_path_leader():

Modified: incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py?rev=1563530&r1=1563529&r2=1563530&view=diff
==============================================================================
--- incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py (original)
+++ incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py Sat Feb  1 23:57:34 2014
@@ -54,6 +54,25 @@ class TestResultDirectoryList(unittest.T
         response = test_app.get('http://localhost:8082/dir/results/')
         self.assertDictEqual(response.json, expected_return)
 
+class TestResultResultRetrieval(unittest.TestCase):
+    WORK_DIR = '/tmp/rcmes'
+
+    def test_no_test_directory_retreival(self):
+        if os.path.exists(self.WORK_DIR + '/foo'): os.rmdir(self.WORK_DIR + '/foo')
+
+        expected_return = {'listing': []}
+        response = test_app.get('http://localhost:8082/dir/results//foo')
+        self.assertDictEqual(response.json, expected_return)
+
+    def test_results_retreival(self):
+        if not os.path.exists(self.WORK_DIR): os.mkdir(self.WORK_DIR)
+        if not os.path.exists(self.WORK_DIR + '/foo'): os.mkdir(self.WORK_DIR + '/foo')
+
+        if not os.path.exists(self.WORK_DIR + '/foo/baz.txt'):
+            open(self.WORK_DIR + '/baz.txt', 'a').close()
+        if not os.path.exists(self.WORK_DIR + '/foo/test.txt'):
+            open(self.WORK_DIR + '/test.txt', 'a').close()
+
 class TestDirectoryPathCleaner(unittest.TestCase):
     PATH_LEADER = '/tmp/foo'
     VALID_CLEAN_DIR = '/tmp/foo/bar'