[hdf-compass] 159/295: Fix __init__.py for filesystem_model and opendap_model (+tests)

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun May 8 10:35:39 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 d7a7ccfc7ac2df79f703ef06df757c97ecaf8443
Author: giumas <giumas at yahoo.it>
Date:   Tue Oct 20 15:54:20 2015 +0200

    Fix __init__.py for filesystem_model and opendap_model (+tests)
---
 hdf_compass/filesystem_model/__init__.py           | 169 +-----------
 .../filesystem_model/{__init__.py => model.py}     |   1 -
 hdf_compass/filesystem_model/test.py               |  11 +
 hdf_compass/opendap_model/__init__.py              | 287 +--------------------
 .../opendap_model/{__init__.py => model.py}        |   0
 hdf_compass/opendap_model/test.py                  |   8 +
 6 files changed, 22 insertions(+), 454 deletions(-)

diff --git a/hdf_compass/filesystem_model/__init__.py b/hdf_compass/filesystem_model/__init__.py
index 83a7d11..c4b5ef2 100644
--- a/hdf_compass/filesystem_model/__init__.py
+++ b/hdf_compass/filesystem_model/__init__.py
@@ -9,177 +9,10 @@
 # distribution tree.  If you do not have access to this file, you may        #
 # request a copy from help at hdfgroup.org.                                     #
 ##############################################################################
-
-"""
-Example data model which represents the file system.
-
-Subclasses just two node types: Container and Array, representing
-directories and files respectively.
-"""
 from __future__ import absolute_import, division, print_function, unicode_literals
 
-import os
-import os.path as op
-
-import numpy as np
+from .model import Filesystem, Directory, File
 
 import logging
 log = logging.getLogger(__name__)
 log.addHandler(logging.NullHandler())
-
-from hdf_compass 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.
-    """
-
-    def __contains__(self, key):
-        return op.exists(key)
-
-    @property
-    def url(self):
-        return self._url
-
-    @property
-    def display_name(self):
-        return "Local file system"
-
-    @property
-    def root(self):
-        return self['/']
-
-    @property
-    def valid(self):
-        return self._valid
-
-    @staticmethod
-    def can_handle(url):
-        if url == "file://localhost":
-            log.debug("able to handle %s? yes" % url)
-            return True
-        log.debug("able to handle %s? no" % url)
-        return False
-
-    def __init__(self, url):
-        if not self.can_handle(url):
-            raise ValueError(url)
-        self._url = url
-        self._valid = True
-
-    def close(self):
-        self._valid = False
-
-    def get_parent(self, key):
-        if key == "/":
-            return None
-        return self[op.dirname(key)]
-
-
-class Directory(compass_model.Container):
-    """
-        Represents a directory in the filesystem.
-    """
-
-    class_kind = "Directory"
-
-    @staticmethod
-    def can_handle(store, key):
-        return op.isdir(key)
-
-    def __init__(self, store, key):
-        self._store = store
-        self._key = key
-        try:
-            self._names = os.listdir(key)
-        except OSError:  # Permissions, etc.
-            self._names = []
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        bn = op.basename(self.key)
-        if len(bn) == 0:
-            return "/"
-        return bn
-
-    @property
-    def description(self):
-        return 'Folder "%s" (%d members)' % (self.display_name, len(self))
-
-    def __len__(self):
-        return len(self._names)
-
-    def __iter__(self):
-        for name in self._names:
-            key = op.join(self.key, name)
-            yield self._store[key]
-
-    def __getitem__(self, idx):
-        key = op.join(self.key, self._names[idx])
-        return self._store[key]
-
-
-class File(compass_model.Array):
-    """
-        Represents a file (all loaded as an array of bytes)
-    """
-
-    class_kind = "File"
-
-    @staticmethod
-    def can_handle(store, key):
-        return op.isfile(key)
-
-    def __init__(self, store, key):
-        self._store = store
-        self._key = key
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        return op.basename(self.key)
-
-    @property
-    def description(self):
-        return 'File "%s", size %d bytes' % (self.display_name, op.getsize(self.key))
-
-    @property
-    def shape(self):
-        return (op.getsize(self.key),)
-
-    @property
-    def dtype(self):
-        return np.dtype('u1')
-
-    def __getitem__(self, args):
-        try:
-            with open(self.key, 'rb') as f:
-                data = np.fromstring(f.read(), dtype='u1')
-        except (OSError, IOError):
-            data = np.zeros((len(self),), dtype='u1')
-
-        return data[args]
-
-
-Filesystem.push(File)
-Filesystem.push(Directory)
-
-compass_model.push(Filesystem)
diff --git a/hdf_compass/filesystem_model/__init__.py b/hdf_compass/filesystem_model/model.py
similarity index 99%
copy from hdf_compass/filesystem_model/__init__.py
copy to hdf_compass/filesystem_model/model.py
index 83a7d11..12cac08 100644
--- a/hdf_compass/filesystem_model/__init__.py
+++ b/hdf_compass/filesystem_model/model.py
@@ -25,7 +25,6 @@ import numpy as np
 
 import logging
 log = logging.getLogger(__name__)
-log.addHandler(logging.NullHandler())
 
 from hdf_compass import compass_model
 
diff --git a/hdf_compass/filesystem_model/test.py b/hdf_compass/filesystem_model/test.py
index f288de9..edb5608 100644
--- a/hdf_compass/filesystem_model/test.py
+++ b/hdf_compass/filesystem_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/opendap_model/__init__.py b/hdf_compass/opendap_model/__init__.py
index 25ce931..1684313 100644
--- a/hdf_compass/opendap_model/__init__.py
+++ b/hdf_compass/opendap_model/__init__.py
@@ -11,291 +11,8 @@
 ##############################################################################
 from __future__ import absolute_import, division, print_function, unicode_literals
 
-import posixpath as pp
-
-import numpy as np
-import pydap as dap
-from pydap.client import open_url
-from pydap.proxy import ArrayProxy
+from .model import Server, Dataset, Structure, Attributes, Base
 
 import logging
 log = logging.getLogger(__name__)
-log.addHandler(logging.NullHandler())
-
-from hdf_compass import compass_model
-
-
-def check_key(key, dataset):
-    if '/' not in key:
-        return key, dataset
-    new_dataset = dataset[key.split('/')[0]]
-    return key.split('/')[1], new_dataset
-
-
-class Server(compass_model.Store):
-    """
-        Represents the remote OpENDAP server to be accessed
-    """
-
-    def __contains__(self, key):
-        if '/' not in key:
-            return key in self.dataset
-
-        new_dset = self.dataset[key.split('/')[0]]
-        new_key = key.rsplit('/')[1]
-
-        return new_key in new_dset
-
-    @staticmethod
-    def can_handle(url):
-        try:
-            return isinstance(open_url(url), dap.model.DatasetType)
-        except Exception:
-            return False
-
-    def __init__(self, url):
-        if not self.can_handle(url):
-            raise ValueError(url)
-        self._url = url
-        self._valid = True
-        self._dataset = open_url(self.url)
-        self._datalength = len(self._dataset.data)
-        self._dataset.setdefault('')
-
-    def close(self):
-        self._valid = False
-
-    def get_parent(self, key):
-        return None
-
-    @property
-    def url(self):
-        return self._url
-
-    @property
-    def display_name(self):
-        return self.dataset.name
-
-    @property
-    def root(self):
-        return self['']
-
-    @property
-    def valid(self):
-        return self._valid
-
-    @property
-    def dataset(self):
-        return self._dataset
-
-    @property
-    def datalength(self):
-        return self._datalength
-
-
-class Dataset(compass_model.Container):
-    """
-        Represents Dataset/DatasetType Object in OpENDAP/Pydap.
-    """
-
-    class_kind = "Dataset"
-
-    def __len__(self):
-        return self._store.datalength
-
-    def __getitem__(self, index):
-        name = self._dset.keys()[index]
-        return self.store[pp.join(self.key, name)]
-
-    def __iter__(self):
-        pass
-
-    @staticmethod
-    def can_handle(store, key):
-        return key == ''
-
-    def __init__(self, store, key):
-        if not key == '':
-            raise ValueError("A Dataset object may only represent the root group")
-        self._store = store
-        self._key = key
-        self._url = store.url
-        self._dset = store.dataset
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        return self._dset.name
-
-    @property
-    def description(self):
-        return "A Pydap DatasetType Object."
-
-    @property
-    def dset(self):
-        return self._dset
-
-
-class Structure(compass_model.Container):
-    """
-        Represents Structure/StructureType Object in OpENDAP/Pydap.
-    """
-
-    class_kind = "Structure/Grid/Sequence"
-
-    def __len__(self):
-        return len(self._dset.data)
-
-    def __getitem__(self, index):
-        name = self._dset.keys()[index]
-        return self.store[pp.join(self.key, name)]
-
-    def __iter__(self):
-        pass
-
-    @staticmethod
-    def can_handle(store, key):
-        new_key, new_dset = check_key(key, store.dataset)
-        try:
-            return new_key in new_dset and isinstance(new_dset[new_key], dap.model.StructureType)
-        except isinstance(new_dset[new_key], dap.model.DatasetType):
-            return False
-
-    def __init__(self, store, key):
-        new_key, new_dset = check_key(key, store.dataset)
-
-        self._store = store
-        self._key = new_key
-        self._url = store.url
-        self._dset = new_dset[new_key]
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        return self._dset.name
-
-    @property
-    def description(self):
-        return "A Pydap StructureType Object."
-
-
-class Base(compass_model.Array):
-    """
-        Represents Array/BaseType Object in OpENDAP/Pydap.
-    """
-
-    class_kind = "Array"
-
-    @property
-    def shape(self):
-        return self._shape
-
-    @property
-    def dtype(self):
-        return np.dtype(self._dtype.typecode)
-
-    def __getitem__(self, index):
-        if self._data is None:
-            self._data = ArrayProxy(self._id, self._url, self._shape)[:]
-        return self._data[index]
-
-    @staticmethod
-    def can_handle(store, key):
-        new_key, new_dset = check_key(key, store.dataset)
-        return new_key in new_dset and isinstance(new_dset[new_key], dap.model.BaseType)
-
-    def __init__(self, store, key):
-        new_key, new_dset = check_key(key, store.dataset)
-
-        self._store = store
-        self._key = new_key
-        self._url = store.url
-        self._id = new_dset[new_key].id
-
-        self._shape = new_dset[new_key].shape
-        self._dtype = new_dset[new_key].type
-        self._name = new_dset[new_key].name
-
-        self._data = None
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        return self._name
-
-    @property
-    def description(self):
-        return "A Pydap BaseType Object."
-
-
-class Attributes(compass_model.KeyValue):
-    """
-        Represents the Attributes member of Pydap Objects.
-    """
-
-    class_kind = "Attributes"
-
-    @property
-    def keys(self):
-        return self._keys.keys()
-
-    def __getitem__(self, name):
-        return self._keys[name]
-
-    @staticmethod
-    def can_handle(store, key):
-        new_key = check_key(key, store.dataset)
-        return new_key != ''
-
-    def __init__(self, store, key):
-        new_key, new_dset = check_key(key, store.dataset)
-
-        self._store = store
-        self._key = new_key
-        self._keys = new_dset[self._key].attributes
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        return "%s Attributes" % self._key
-
-    @property
-    def description(self):
-        return "Attributes of %s" % self._key
-
-
-# Register Handlers
-Server.push(Attributes)
-Server.push(Dataset)
-Server.push(Structure)
-Server.push(Base)
-
-compass_model.push(Server)
+log.addHandler(logging.NullHandler())
\ No newline at end of file
diff --git a/hdf_compass/opendap_model/__init__.py b/hdf_compass/opendap_model/model.py
similarity index 100%
copy from hdf_compass/opendap_model/__init__.py
copy to hdf_compass/opendap_model/model.py
diff --git a/hdf_compass/opendap_model/test.py b/hdf_compass/opendap_model/test.py
new file mode 100644
index 0000000..53d95b9
--- /dev/null
+++ b/hdf_compass/opendap_model/test.py
@@ -0,0 +1,8 @@
+from hdf_compass.compass_model.test import container, store
+from hdf_compass.opendap_model import Server, Dataset
+
+url = "http://test.opendap.org/opendap/hyrax/data/hdf5/grid_1_2d.h5"
+s_1 = store(Server, url)
+
+url = "http://test.opendap.org/opendap/hyrax/data/nc/bears.nc"
+s_2 = store(Server, url)
\ No newline at end of file

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