[hdf-compass] 29/295: updates to asc plugin to work with File Open

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun May 8 10:35:22 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 ebdffa4524cbb651312bec39743919d3e2860738
Author: John Readey <jreadey at hdfgroup.org>
Date:   Thu Jul 24 15:54:17 2014 -0700

    updates to asc plugin to work with File Open
---
 asc_model/__init__.py   | 112 +++++++++++++++++++++++++++---------------------
 compass_viewer/frame.py |  36 +++++++++++-----
 2 files changed, 87 insertions(+), 61 deletions(-)

diff --git a/asc_model/__init__.py b/asc_model/__init__.py
index a9e3a25..d8a49d4 100644
--- a/asc_model/__init__.py
+++ b/asc_model/__init__.py
@@ -3,6 +3,8 @@ 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
 """
 
 import os
@@ -12,16 +14,19 @@ import linecache
 
 import compass_model
 
-class Filesystem(compass_model.Store):
-    
-    """
-        A "data store" represented by the file system.
 
-        Keys are absolute paths on the local file system.
+class AsciiGrid(compass_model.Store):
+
     """
+        A "data store" represented by an ascii grid file.
+    """
+
+    file_extensions = {'ASC File': ['*.asc']}
 
     def __contains__(self, key):
-        return op.exists(key)
+        if key == '/':
+            return True
+        return False
 
     @property
     def url(self):
@@ -29,7 +34,7 @@ class Filesystem(compass_model.Store):
 
     @property
     def displayname(self):
-        return "Local File System"
+        return op.basename(self._url)
 
     @property
     def root(self):
@@ -41,9 +46,11 @@ class Filesystem(compass_model.Store):
 
     @staticmethod
     def canhandle(url):
-        if url == "asc://localhost":
-            return True
-        return False
+        if not url.startswith('file://'):
+            return False
+        if not url.endswith('.asc'):
+            return False
+        return True
 
     def __init__(self, url):
         if not self.canhandle(url):
@@ -55,34 +62,34 @@ class Filesystem(compass_model.Store):
         self._valid = False
 
     def getparent(self, key):
-        if key == '/':
-            return None
-        return self[op.dirname(key)]
+        return None
 
+    def getFilePath(self):
+        prefixLen = len('file://')
+        return self._url[prefixLen:]
 
-class Directory(compass_model.Container):
+
+class Root(compass_model.Container):
 
     """
-        Represents a directory in the file system.
+        Represents the root (and only) group for ASCII Grid
     """
 
-    classkind = "Directory"
+    classkind = "Root"
 
     @staticmethod
     def canhandle(store, key):
-        return op.isdir(key)
+        print "container can handle, ", key
+        if key == '/':
+            return True
+        return False
 
     def __init__(self, store, key):
-        self._store = store
-        self._key = key
-        try:
-            self._names = [s for s in os.listdir(key) if s.endswith('.asc') or op.isdir(op.join(key, s))]
-        except OSError: 
-            self._names = []
+        pass
 
     @property
     def key(self):
-        return self._key
+        return "/"
 
     @property
     def store(self):
@@ -90,26 +97,25 @@ class Directory(compass_model.Container):
 
     @property
     def displayname(self):
-        bn = op.basename(self.key)
-        if len(bn) == 0:
-            return "/"
-        return bn
+        return self_store.displayname
+
+    @property
+    def displaytitle(self):
+        return "%s %s" % (self.store.displayname, self.key)
 
     @property
     def description(self):
         return 'folder "%s" (%d members)' % (self.displayname, len(self))
 
     def __len__(self):
-        return len(self._names)
+        return 0
 
     def __iter__(self):
-        for name in self._names:
-            key = op.join(self.key, name)
-            yield self._store[key]
+        names = []
+        return iter(names)
 
     def __getitem__(self, idx):
-        key = op.join(self.key, self._names[idx])
-        return self._store[key]
+        return None
 
 
 class ASCFile(compass_model.Array):
@@ -122,13 +128,16 @@ class ASCFile(compass_model.Array):
 
     @staticmethod
     def canhandle(store, key):
-        return op.isfile(key) and key.endswith('.asc')
+        if key == '/':
+            return True
+        return False
 
     def __init__(self, store, key):
         self._store = store
         self._key = key
-        self._nrows = int(linecache.getline(self._key, 1).lstrip("ncols"))
-        self._ncols = int(linecache.getline(self._key, 2).lstrip("nrows"))
+        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
@@ -141,7 +150,7 @@ class ASCFile(compass_model.Array):
 
     @property
     def displayname(self):
-        return op.basename(self._key)
+        return self._store.displayname
 
     @property
     def description(self):
@@ -157,25 +166,28 @@ class ASCFile(compass_model.Array):
 
     def __getitem__(self, args):
         if self._data is None:
-            self._data = np.loadtxt(self._key, skiprows=6, unpack=True)
+            self._data = np.loadtxt(self._store.getFilePath(), skiprows=6, unpack=True)
         return self._data[args]
 
 
 class Attributes(compass_model.KeyValue):
 
-    classkind = "Attributes of ASC Grid File" 
+    classkind = "Attributes of ASC Grid File"
 
     @staticmethod
     def canhandle(store, key):
-        return op.isfile(key) and key.endswith('.asc')
+        if key == '/':
+            return True
+        return False
 
     def __init__(self, store, key):
         self._store = store
-        self._key = key 
-        self.data = {'NODATA Value': float(linecache.getline(self._key, 6).lstrip("NODATA_value")),
-          'cellsize': float(linecache.getline(self._key, 5).lstrip("cellsize")),
-          'yllcorner': float(linecache.getline(self._key, 4).lstrip("yllcorner")),
-          'xllcorner': float(linecache.getline(self._key, 3).lstrip("xllcorner"))}
+        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):
@@ -201,8 +213,8 @@ class Attributes(compass_model.KeyValue):
         return self.data[args]
 
 
-Filesystem.push(Attributes)
-Filesystem.push(Directory)
-Filesystem.push(ASCFile)
+AsciiGrid.push(Attributes)   # attribute data
+AsciiGrid.push(Root)         # container
+AsciiGrid.push(ASCFile)      # array
 
-compass_model.push(Filesystem)
\ No newline at end of file
+compass_model.push(AsciiGrid)
diff --git a/compass_viewer/frame.py b/compass_viewer/frame.py
index 2dae3a8..50e9555 100644
--- a/compass_viewer/frame.py
+++ b/compass_viewer/frame.py
@@ -96,21 +96,35 @@ class BaseFrame(wx.Frame):
         """ Request to open a file via the Open entry in the File menu """
         import compass_model
         
-        def make_filter_string(dct):
+        def make_filter_string():
             """ Make a wxPython dialog filter string segment from dict """
             filter_string = []
-            for key, value in dct.iteritems():
-                s = "{name} ({pattern_c})|{pattern_sc}".format(
-                    name=key, 
-                    pattern_c=",".join(value),
-                    pattern_sc=";".join(value) )
-                filter_string.append(s)
-            return "|".join(filter_string)
+            hdf_filter_string = []  # put HDF filters in the front
+            for store in compass_model.getstores():
+                if len(store.file_extensions) == 0:
+                    continue
+                for key in store.file_extensions:
+                    s = "{name} ({pattern_c})|{pattern_sc}".format(
+                        name=key, 
+                        pattern_c=",".join(store.file_extensions[key]),
+                        pattern_sc=";".join(store.file_extensions[key]) )
+                    if s.startswith("HDF"):
+                        hdf_filter_string.append(s)
+                    else:
+                	    filter_string.append(s)
+            filter_string = hdf_filter_string + filter_string
+            filter_string.append('All Files (*.*)|*.*');
+            pipe = "|"
+            return pipe.join(filter_string)
             
         # The wxPython wildcard string is a bunch of filter strings pasted together
-        wc_string = [s.file_extensions for s in compass_model.getstores() if len(s.file_extensions) != 0]
-        wc_string.append({"All Files": ["*"]})
-        wc_string = "|".join([make_filter_string(x) for x in wc_string])
+        # wc_string = [s.file_extensions for s in compass_model.getstores() if len(s.file_extensions) != 0]
+        # print "jlr -- wc_string: " , wc_string
+        # wc_string.append({"All Files": ["*"]})
+        # wc_string = "|".join([make_filter_string(x) for x in wc_string])
+        #wc_string.append("|")
+        #wc_string.append(make_filter_string(wc_string))
+        wc_string = make_filter_string()
         
         from . import open_store
         dlg = wx.FileDialog(self, "Open Local File", wildcard=wc_string, style=wx.FD_OPEN|wx.FD_FILE_MUST_EXIST)

-- 
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