[hdf-compass] 155/295: Add tests and fix __init__.py for asc_model

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun May 8 10:35:38 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository hdf-compass.

commit 71302a45710336220c6c343459a226f49d2c4807
Author: giumas <giumas at yahoo.it>
Date:   Tue Oct 20 15:04:12 2015 +0200

    Add tests and fix __init__.py for asc_model
---
 hdf_compass/array_model/model.py                   |   1 -
 hdf_compass/array_model/test.py                    |  12 +-
 hdf_compass/asc_model/__init__.py                  | 176 +--------------------
 hdf_compass/asc_model/{__init__.py => model.py}    |  38 ++---
 .../{utils/__init__.py => asc_model/test.py}       |  14 +-
 hdf_compass/hdf5_model/test.py                     |  11 ++
 hdf_compass/utils/__init__.py                      |   2 +-
 hdf_compass/utils/utils.py                         |  16 +-
 8 files changed, 62 insertions(+), 208 deletions(-)

diff --git a/hdf_compass/array_model/model.py b/hdf_compass/array_model/model.py
index fd57c61..8e5fd7e 100644
--- a/hdf_compass/array_model/model.py
+++ b/hdf_compass/array_model/model.py
@@ -19,7 +19,6 @@ import os.path as op
 
 import logging
 log = logging.getLogger(__name__)
-log.addHandler(logging.NullHandler())
 
 from hdf_compass import compass_model
 
diff --git a/hdf_compass/array_model/test.py b/hdf_compass/array_model/test.py
index 3c9906a..484c475 100644
--- a/hdf_compass/array_model/test.py
+++ b/hdf_compass/array_model/test.py
@@ -1,8 +1,18 @@
+##############################################################################
+# Copyright by The HDF Group.                                                #
+# All rights reserved.                                                       #
+#                                                                            #
+# This file is part of the HDF Compass Viewer. The full HDF Compass          #
+# copyright notice, including terms governing use, modification, and         #
+# terms governing use, modification, and redistribution, is contained in     #
+# the file COPYING, which can be found at the root of the source code        #
+# distribution tree.  If you do not have access to this file, you may        #
+# request a copy from help at hdfgroup.org.                                     #
+##############################################################################
 from __future__ import absolute_import, division, print_function
 
 from hdf_compass.compass_model.test import container, store
 from hdf_compass.array_model import ArrayStore, ArrayContainer
-from hdf_compass.utils import is_win
 
 url = "array://localhost"
 
diff --git a/hdf_compass/asc_model/__init__.py b/hdf_compass/asc_model/__init__.py
index 2f0319d..61b7806 100644
--- a/hdf_compass/asc_model/__init__.py
+++ b/hdf_compass/asc_model/__init__.py
@@ -9,182 +9,10 @@
 # distribution tree.  If you do not have access to this file, you may        #
 # request a copy from help at hdfgroup.org.                                     #
 ##############################################################################
-"""
-HDF Compass "pilot" plugin for viewing ASCII Grid Data.
-
-Subclasses consist of a Container and an Array, representing
-directories and the ASCII grid data respectively.
-See: http://en.wikipedia.org/wiki/Esri_grid for a description of
-the file format
-"""
 from __future__ import absolute_import, division, print_function, unicode_literals
 
-import sys
-import os.path as op
-import linecache
-
-import numpy as np
+from .model import AsciiGrid, ASCFile, Attributes
 
 import logging
 log = logging.getLogger(__name__)
-log.addHandler(logging.NullHandler())
-
-from hdf_compass import compass_model
-
-
-class AsciiGrid(compass_model.Store):
-    """
-        A "data store" represented by an ascii grid file.
-    """
-
-    file_extensions = {'ASC File': ['*.asc']}
-
-    def __contains__(self, key):
-        if key == '/':
-            return True
-        return False
-
-    @property
-    def url(self):
-        return self._url
-
-    @property
-    def display_name(self):
-        return op.basename(self._url)
-
-    @property
-    def root(self):
-        return self['/']
-
-    @property
-    def valid(self):
-        return self._valid
-
-    @staticmethod
-    def can_handle(url):
-        if not url.startswith('file://'):
-            return False
-        if not url.endswith('.asc'):
-            return False
-        return True
-
-    def __init__(self, url):
-        if not self.can_handle(url):
-            raise ValueError(url)
-        self._url = url
-        self._value = True
-
-    def close(self):
-        self._valid = False
-
-    def get_parent(self, key):
-        return None
-
-    def getFilePath(self):
-        if sys.platform == 'win32':
-            prefixLen = len('file:///')
-        else:
-            prefixLen = len('file://')
-        return self._url[prefixLen:]
-
-
-class ASCFile(compass_model.Array):
-    """
-        Represents a .asc grid file.
-    """
-
-    class_kind = "ASCII Grid File"
-
-    @staticmethod
-    def can_handle(store, key):
-        if key == '/':
-            return True
-        return False
-
-    def __init__(self, store, key):
-        self._store = store
-        self._key = key
-        filePath = self._store.getFilePath()
-        self._nrows = int(linecache.getline(filePath, 1).lstrip("ncols"))
-        self._ncols = int(linecache.getline(filePath, 2).lstrip("nrows"))
-        self._data = None
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        return self._store.display_name
-
-    @property
-    def description(self):
-        return 'File "%s", size %d bytes' % (self.display_name, op.getsize(self.key))
-
-    @property
-    def shape(self):
-        return self._nrows, self._ncols
-
-    @property
-    def dtype(self):
-        return np.dtype('float')
-
-    def __getitem__(self, args):
-        if self._data is None:
-            self._data = np.loadtxt(self._store.getFilePath(), skiprows=6, unpack=True)
-        return self._data[args]
-
-
-class Attributes(compass_model.KeyValue):
-    class_kind = "Attributes of ASC Grid File"
-
-    @staticmethod
-    def can_handle(store, key):
-        if key == '/':
-            return True
-        return False
-
-    def __init__(self, store, key):
-        self._store = store
-        self._key = key
-        filePath = self._store.getFilePath()
-        self.data = {'NODATA Value': float(linecache.getline(filePath, 6).lstrip("NODATA_value")),
-                     'cellsize': float(linecache.getline(filePath, 5).lstrip("cellsize")),
-                     'yllcorner': float(linecache.getline(filePath, 4).lstrip("yllcorner")),
-                     'xllcorner': float(linecache.getline(filePath, 3).lstrip("xllcorner"))}
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        return self.key
-
-    @property
-    def description(self):
-        return self.display_name
-
-    def close(self):
-        self._valid = False
-
-    @property
-    def keys(self):
-        return self.data.keys()
-
-    def __getitem__(self, args):
-        return self.data[args]
-
-
-AsciiGrid.push(Attributes)  # attribute data
-AsciiGrid.push(ASCFile)  # array
-
-compass_model.push(AsciiGrid)
+log.addHandler(logging.NullHandler())
\ No newline at end of file
diff --git a/hdf_compass/asc_model/__init__.py b/hdf_compass/asc_model/model.py
similarity index 81%
copy from hdf_compass/asc_model/__init__.py
copy to hdf_compass/asc_model/model.py
index 2f0319d..cfcf5cb 100644
--- a/hdf_compass/asc_model/__init__.py
+++ b/hdf_compass/asc_model/model.py
@@ -19,7 +19,6 @@ the file format
 """
 from __future__ import absolute_import, division, print_function, unicode_literals
 
-import sys
 import os.path as op
 import linecache
 
@@ -27,15 +26,13 @@ import numpy as np
 
 import logging
 log = logging.getLogger(__name__)
-log.addHandler(logging.NullHandler())
 
 from hdf_compass import compass_model
+from hdf_compass.utils import url2path
 
 
 class AsciiGrid(compass_model.Store):
-    """
-        A "data store" represented by an ascii grid file.
-    """
+    """ A "data store" represented by an ascii grid file. """
 
     file_extensions = {'ASC File': ['*.asc']}
 
@@ -63,16 +60,19 @@ class AsciiGrid(compass_model.Store):
     @staticmethod
     def can_handle(url):
         if not url.startswith('file://'):
+            log.debug("able to handle %s? no, not starting with file://" % url)
             return False
         if not url.endswith('.asc'):
+            log.debug("able to handle %s? no, missing .asc extension" % url)
             return False
+        log.debug("able to handle %s? yes" % url)
         return True
 
     def __init__(self, url):
         if not self.can_handle(url):
             raise ValueError(url)
         self._url = url
-        self._value = True
+        self._valid = True
 
     def close(self):
         self._valid = False
@@ -81,17 +81,11 @@ class AsciiGrid(compass_model.Store):
         return None
 
     def getFilePath(self):
-        if sys.platform == 'win32':
-            prefixLen = len('file:///')
-        else:
-            prefixLen = len('file://')
-        return self._url[prefixLen:]
+        return url2path(self._url)
 
 
 class ASCFile(compass_model.Array):
-    """
-        Represents a .asc grid file.
-    """
+    """ Represents a .asc grid file. """
 
     class_kind = "ASCII Grid File"
 
@@ -104,9 +98,9 @@ class ASCFile(compass_model.Array):
     def __init__(self, store, key):
         self._store = store
         self._key = key
-        filePath = self._store.getFilePath()
-        self._nrows = int(linecache.getline(filePath, 1).lstrip("ncols"))
-        self._ncols = int(linecache.getline(filePath, 2).lstrip("nrows"))
+        file_path = self._store.getFilePath()
+        self._nrows = int(linecache.getline(file_path, 1).lstrip("ncols"))
+        self._ncols = int(linecache.getline(file_path, 2).lstrip("nrows"))
         self._data = None
 
     @property
@@ -151,11 +145,11 @@ class Attributes(compass_model.KeyValue):
     def __init__(self, store, key):
         self._store = store
         self._key = key
-        filePath = self._store.getFilePath()
-        self.data = {'NODATA Value': float(linecache.getline(filePath, 6).lstrip("NODATA_value")),
-                     'cellsize': float(linecache.getline(filePath, 5).lstrip("cellsize")),
-                     'yllcorner': float(linecache.getline(filePath, 4).lstrip("yllcorner")),
-                     'xllcorner': float(linecache.getline(filePath, 3).lstrip("xllcorner"))}
+        file_path = self._store.getFilePath()
+        self.data = {'NODATA Value': float(linecache.getline(file_path, 6).lstrip("NODATA_value")),
+                     'cellsize': float(linecache.getline(file_path, 5).lstrip("cellsize")),
+                     'yllcorner': float(linecache.getline(file_path, 4).lstrip("yllcorner")),
+                     'xllcorner': float(linecache.getline(file_path, 3).lstrip("xllcorner"))}
 
     @property
     def key(self):
diff --git a/hdf_compass/utils/__init__.py b/hdf_compass/asc_model/test.py
similarity index 81%
copy from hdf_compass/utils/__init__.py
copy to hdf_compass/asc_model/test.py
index cc3aa7e..aba570a 100644
--- a/hdf_compass/utils/__init__.py
+++ b/hdf_compass/asc_model/test.py
@@ -9,14 +9,14 @@
 # distribution tree.  If you do not have access to this file, you may        #
 # request a copy from help at hdfgroup.org.                                     #
 ##############################################################################
-from __future__ import absolute_import, division, print_function, unicode_literals
+from __future__ import absolute_import, division, print_function
 
-import logging
+import os
 
-log = logging.getLogger(__name__)
-log.addHandler(logging.NullHandler())
+from hdf_compass.compass_model.test import store
+from hdf_compass.asc_model import AsciiGrid
+from hdf_compass.utils import data_url
 
-from .utils import is_darwin, is_win, is_linux, url2path, path2url
+url = os.path.join(data_url(), "asc", "sample.asc")
 
-
-__version__ = "1.0.5+"
+s = store(AsciiGrid, url)
diff --git a/hdf_compass/hdf5_model/test.py b/hdf_compass/hdf5_model/test.py
index 4b7f491..82a8c7e 100644
--- a/hdf_compass/hdf5_model/test.py
+++ b/hdf_compass/hdf5_model/test.py
@@ -1,3 +1,14 @@
+##############################################################################
+# Copyright by The HDF Group.                                                #
+# All rights reserved.                                                       #
+#                                                                            #
+# This file is part of the HDF Compass Viewer. The full HDF Compass          #
+# copyright notice, including terms governing use, modification, and         #
+# terms governing use, modification, and redistribution, is contained in     #
+# the file COPYING, which can be found at the root of the source code        #
+# distribution tree.  If you do not have access to this file, you may        #
+# request a copy from help at hdfgroup.org.                                     #
+##############################################################################
 from __future__ import absolute_import, division, print_function
 
 from hdf_compass.compass_model.test import container, store
diff --git a/hdf_compass/utils/__init__.py b/hdf_compass/utils/__init__.py
index cc3aa7e..35fc14d 100644
--- a/hdf_compass/utils/__init__.py
+++ b/hdf_compass/utils/__init__.py
@@ -16,7 +16,7 @@ import logging
 log = logging.getLogger(__name__)
 log.addHandler(logging.NullHandler())
 
-from .utils import is_darwin, is_win, is_linux, url2path, path2url
+from .utils import is_darwin, is_win, is_linux, url2path, path2url, data_url
 
 
 __version__ = "1.0.5+"
diff --git a/hdf_compass/utils/utils.py b/hdf_compass/utils/utils.py
index 8cae96b..57689dc 100644
--- a/hdf_compass/utils/utils.py
+++ b/hdf_compass/utils/utils.py
@@ -15,6 +15,7 @@ Implementation of utils and helper functions
 from __future__ import absolute_import, division, print_function, unicode_literals
 
 import sys
+import os
 
 import logging
 log = logging.getLogger(__name__)
@@ -25,7 +26,7 @@ is_linux = sys.platform == 'linux2'
 
 
 def url2path(url):
-    """Helper function that returns the file path from an url, dealing with Windows peculiarities"""
+    """ Helper function that returns the file path from an url, dealing with Windows peculiarities """
     if is_win:
         return url.replace('file:///', '')
     else:
@@ -33,8 +34,19 @@ def url2path(url):
 
 
 def path2url(path):
-    """Helper function that returns the url from a file path, dealing with Windows peculiarities"""
+    """ Helper function that returns the url from a file path, dealing with Windows peculiarities """
     if is_win:
         return 'file:///' + path
     else:
         return 'file://' + path
+
+
+def data_url():
+    """ Helper function used to return the url to the project data folder """
+    prj_root_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
+    data_folder = os.path.join(prj_root_folder, "data")
+
+    if not os.path.exists(data_folder):
+        raise RuntimeError("data path %s does not exist" % data_folder)
+
+    return path2url(data_folder)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/hdf-compass.git



More information about the debian-science-commits mailing list