[hdf-compass] 153/295: Fix test_icons + array_model tests and __init__.py
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 89ec68a980fb293228258df2a8ed592e89c4f193
Author: giumas <giumas at yahoo.it>
Date: Tue Oct 20 14:01:47 2015 +0200
Fix test_icons + array_model tests and __init__.py
---
hdf_compass/array_model/__init__.py | 201 +---------------------
hdf_compass/array_model/{__init__.py => model.py} | 74 ++++----
hdf_compass/array_model/test.py | 10 ++
hdf_compass/bag_model/model.py | 7 +-
hdf_compass/compass_model/test.py | 92 +++++-----
hdf_compass/compass_viewer/__init__.py | 14 +-
hdf_compass/compass_viewer/container/__init__.py | 4 +-
hdf_compass/filesystem_model/__init__.py | 2 +
hdf_compass/utils/utils.py | 1 +
9 files changed, 106 insertions(+), 299 deletions(-)
diff --git a/hdf_compass/array_model/__init__.py b/hdf_compass/array_model/__init__.py
index c352181..8ff91dd 100644
--- a/hdf_compass/array_model/__init__.py
+++ b/hdf_compass/array_model/__init__.py
@@ -9,209 +9,10 @@
# distribution tree. If you do not have access to this file, you may #
# request a copy from help at hdfgroup.org. #
##############################################################################
-
-"""
-Testing model for array types.
-"""
from __future__ import absolute_import, division, print_function, unicode_literals
-import numpy as np
+from .model import ArrayStore, ArrayContainer, ArrayKV, Array
import logging
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
-
-from hdf_compass import compass_model
-
-DT_CMP = np.dtype([(b'a', b'i'), (b'b', b'f')])
-
-DATA = {'a_0d': np.array(1),
- 'a_1d': np.arange(10),
- 'a_2d': np.arange(10 * 10).reshape((10, 10)),
- 'a_3d': np.arange(10 * 10 * 10).reshape((10, 10, 10)),
- 'a_4d': np.arange(10 * 10 * 10 * 10).reshape((10, 10, 10, 10)),
- 'cmp_0d': np.array((1, 2), dtype=DT_CMP),
- 'cmp_1d': np.ones((10,), dtype=DT_CMP),
- 'cmp_2d': np.ones((10, 10), dtype=DT_CMP),
- 'cmp_3d': np.ones((10, 10, 10), dtype=DT_CMP),
- 'cmp_4d': np.ones((10, 10, 10, 10), dtype=DT_CMP),
- 's_0d': np.array("Hello"),
- 's_1d': np.array(("Hello",)),
- 'v_0d': np.array('\x01', dtype='|V1'),
- 'nonsquare': np.arange(5 * 10).reshape((5, 10)),
- }
-
-
-class ArrayStore(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 key in DATA
-
- @property
- def url(self):
- return self._url
-
- @property
- def display_name(self):
- return "Testing arrays"
-
- @property
- def root(self):
- return ArrayContainer(self, None)
-
- @property
- def valid(self):
- return True
-
- @staticmethod
- def can_handle(url):
- if url == "array://localhost":
- return True
- return False
-
- def __init__(self, url):
- if not self.can_handle(url):
- raise ValueError(url)
- self._url = url
-
- def close(self):
- pass
-
- def get_parent(self, key):
- return self.root
-
-
-class ArrayContainer(compass_model.Container):
- """
- Represents a directory in the filesystem.
- """
-
- class_kind = "Array Container"
-
- @staticmethod
- def can_handle(store, key):
- return key is None
-
- def __init__(self, store, key):
- self._store = store
- self._key = key
- self._names = sorted(DATA.keys())
-
- @property
- def key(self):
- return None
-
- @property
- def store(self):
- return self._store
-
- @property
- def display_name(self):
- return "Array Container"
-
- @property
- def description(self):
- return self.display_name
-
- def __len__(self):
- return len(DATA)
-
- def __iter__(self):
- for name in self._names:
- yield self._store[name]
-
- def __getitem__(self, idx):
- key = self._names[idx]
- return self._store[key]
-
-
-class Array(compass_model.Array):
- """
- An N-D array
- """
-
- class_kind = "TestArray"
-
- @staticmethod
- def can_handle(store, key):
- return key in DATA
-
- def __init__(self, store, key):
- self._store = store
- self._key = key
- self.data = DATA[key]
-
- @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
-
- @property
- def shape(self):
- return self.data.shape
-
- @property
- def dtype(self):
- return self.data.dtype
-
- def __getitem__(self, args):
- return self.data[args]
-
-
-class ArrayKV(compass_model.KeyValue):
- class_kind = "Array Key/Value Attrs"
-
- @staticmethod
- def can_handle(store, key):
- return key in DATA
-
- def __init__(self, store, key):
- self._store = store
- self._key = key
- self.data = {'a': np.array((1, 2)), 'b': np.array("Hello"), 'c': np.array('\x01', dtype='|V1')}
-
- @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
-
- @property
- def keys(self):
- return self.data.keys()
-
- def __getitem__(self, args):
- return self.data[args]
-
-
-ArrayStore.push(ArrayKV)
-ArrayStore.push(ArrayContainer)
-ArrayStore.push(Array)
-
-compass_model.push(ArrayStore)
diff --git a/hdf_compass/array_model/__init__.py b/hdf_compass/array_model/model.py
similarity index 68%
copy from hdf_compass/array_model/__init__.py
copy to hdf_compass/array_model/model.py
index c352181..fd57c61 100644
--- a/hdf_compass/array_model/__init__.py
+++ b/hdf_compass/array_model/model.py
@@ -10,12 +10,12 @@
# request a copy from help at hdfgroup.org. #
##############################################################################
-"""
-Testing model for array types.
-"""
+""" Testing model for array types. """
+
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
+import os.path as op
import logging
log = logging.getLogger(__name__)
@@ -25,31 +25,34 @@ from hdf_compass import compass_model
DT_CMP = np.dtype([(b'a', b'i'), (b'b', b'f')])
-DATA = {'a_0d': np.array(1),
- 'a_1d': np.arange(10),
- 'a_2d': np.arange(10 * 10).reshape((10, 10)),
- 'a_3d': np.arange(10 * 10 * 10).reshape((10, 10, 10)),
- 'a_4d': np.arange(10 * 10 * 10 * 10).reshape((10, 10, 10, 10)),
- 'cmp_0d': np.array((1, 2), dtype=DT_CMP),
- 'cmp_1d': np.ones((10,), dtype=DT_CMP),
- 'cmp_2d': np.ones((10, 10), dtype=DT_CMP),
- 'cmp_3d': np.ones((10, 10, 10), dtype=DT_CMP),
- 'cmp_4d': np.ones((10, 10, 10, 10), dtype=DT_CMP),
- 's_0d': np.array("Hello"),
- 's_1d': np.array(("Hello",)),
- 'v_0d': np.array('\x01', dtype='|V1'),
- 'nonsquare': np.arange(5 * 10).reshape((5, 10)),
+DATA = {'array://localhost/a_0d': np.array(1),
+ 'array://localhost/a_1d': np.arange(10),
+ 'array://localhost/a_2d': np.arange(10 * 10).reshape((10, 10)),
+ 'array://localhost/a_3d': np.arange(10 * 10 * 10).reshape((10, 10, 10)),
+ 'array://localhost/a_4d': np.arange(10 * 10 * 10 * 10).reshape((10, 10, 10, 10)),
+ 'array://localhost/cmp_0d': np.array((1, 2), dtype=DT_CMP),
+ 'array://localhost/cmp_1d': np.ones((10,), dtype=DT_CMP),
+ 'array://localhost/cmp_2d': np.ones((10, 10), dtype=DT_CMP),
+ 'array://localhost/cmp_3d': np.ones((10, 10, 10), dtype=DT_CMP),
+ 'array://localhost/cmp_4d': np.ones((10, 10, 10, 10), dtype=DT_CMP),
+ 'array://localhost/s_0d': np.array("Hello"),
+ 'array://localhost/s_1d': np.array(("Hello",)),
+ 'array://localhost/v_0d': np.array('\x01', dtype='|V1'),
+ 'array://localhost/non_square': np.arange(5 * 10).reshape((5, 10)),
}
class ArrayStore(compass_model.Store):
"""
- A "data store" represented by the file system.
+ A "data store" represented by a set of arrays in memory.
- Keys are absolute paths on the local file system.
+ Keys are array names as reported in DATA.
"""
def __contains__(self, key):
+ if (key == '/') or (key is None):
+ log.debug("is root: %s" % key)
+ return True
return key in DATA
@property
@@ -62,40 +65,45 @@ class ArrayStore(compass_model.Store):
@property
def root(self):
- return ArrayContainer(self, None)
+ return self['/']
@property
def valid(self):
- return True
+ return self._valid
@staticmethod
def can_handle(url):
if url == "array://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):
- pass
+ self._valid = False
def get_parent(self, key):
- return self.root
+ if key is None:
+ return None
+ if key == "/":
+ return None
+ return self[op.dirname(key)]
class ArrayContainer(compass_model.Container):
- """
- Represents a directory in the filesystem.
- """
+ """ Represents a container of in-memory array. """
class_kind = "Array Container"
@staticmethod
def can_handle(store, key):
- return key is None
+ return (key == '/') or (key is None)
def __init__(self, store, key):
self._store = store
@@ -112,7 +120,7 @@ class ArrayContainer(compass_model.Container):
@property
def display_name(self):
- return "Array Container"
+ return "/"
@property
def description(self):
@@ -131,9 +139,7 @@ class ArrayContainer(compass_model.Container):
class Array(compass_model.Array):
- """
- An N-D array
- """
+ """ An N-D array """
class_kind = "TestArray"
@@ -156,7 +162,7 @@ class Array(compass_model.Array):
@property
def display_name(self):
- return self.key
+ return self.key.rsplit('/', 1)[-1]
@property
def description(self):
@@ -196,7 +202,7 @@ class ArrayKV(compass_model.KeyValue):
@property
def display_name(self):
- return self.key
+ return self.key.rsplit('/', 1)[-1]
@property
def description(self):
@@ -214,4 +220,4 @@ ArrayStore.push(ArrayKV)
ArrayStore.push(ArrayContainer)
ArrayStore.push(Array)
-compass_model.push(ArrayStore)
+compass_model.push(ArrayStore)
\ No newline at end of file
diff --git a/hdf_compass/array_model/test.py b/hdf_compass/array_model/test.py
new file mode 100644
index 0000000..3c9906a
--- /dev/null
+++ b/hdf_compass/array_model/test.py
@@ -0,0 +1,10 @@
+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"
+
+s = store(ArrayStore, url)
+c = container(ArrayStore, url, ArrayContainer, None)
\ No newline at end of file
diff --git a/hdf_compass/bag_model/model.py b/hdf_compass/bag_model/model.py
index 078f6f3..ba558e1 100644
--- a/hdf_compass/bag_model/model.py
+++ b/hdf_compass/bag_model/model.py
@@ -68,17 +68,16 @@ class BAGStore(compass_model.Store):
@staticmethod
def can_handle(url):
- log.debug("able to handle %s?" % url)
if not url.startswith('file://'):
- log.debug("Invalid url: %s" % url)
+ log.debug("able to handle %s? no, invalid url" % url)
return False
path = url2path(url)
if not is_bag(path):
- log.debug("Not a BAG")
+ log.debug("able to handle %s? no, not a BAG" % url)
return False
- log.debug("Yes")
+ log.debug("able to handle %s? yes" % url)
return True
def __init__(self, url):
diff --git a/hdf_compass/compass_model/test.py b/hdf_compass/compass_model/test.py
index 7b0f5a3..4141439 100644
--- a/hdf_compass/compass_model/test.py
+++ b/hdf_compass/compass_model/test.py
@@ -17,18 +17,18 @@ does basic sanity/consistency checking. More specific tests must be written
by plugin authors.
The public interface consists of the following functions, each of which
-provides a TestCase sublass which may be used with unittest:
+provides a TestCase subclass which may be used with unittest:
- store
- container
-- array
-- keyvalue
-- image
+- array [not implemented]
+- key-value [not implemented]
+- image [not implemented]
Example, in my_model.test:
- from compass_model.test import store, container
- from my_model import MyStore, MyContainer
+ from hdf_compass.compass_model.test import store, container
+ from hdf_compass.my_model import MyStore, MyContainer
URL = "file:///path/to/myfile.ext"
@@ -37,7 +37,7 @@ Example, in my_model.test:
To run unittest, which discovers classes "store_tests" and "container_tests":
- $ python -m unittest my_model.test
+ $ python -m unittest hdf_compass.my_model.test
"""
@@ -50,33 +50,33 @@ from . import Node, Store
# --- Public API --------------------------------------------------------------
-def store(storecls_, url_):
+def store(store_cls_, url_):
""" Construct a TestCase appropriate for a Store subclass.
- storecls: Your compass_model.Store implementation.
- url: A URL representing a valid datastore to test against.
+ store_cls_: Your compass_model.Store implementation.
+ url_: A URL representing a valid data-store to test against.
"""
class TestStore(_TestStore):
- storecls = storecls_
+ store_cls = store_cls_
url = url_
return TestStore
-def container(storecls_, url_, nodecls_, key_):
+def container(store_cls_, url_, node_cls_, key_):
""" Construct a TestCase class appropriate for a Container subclass.
- storecls: Your compass_model.Store implementation.
- url: A URL representing a valid datastore to test against.
- nodecls: Your compass_model.Container implementation.
- key: A valid key which points to a container.
+ store_cls_: Your compass_model.Store implementation.
+ url_: A URL representing a valid data-store to test against.
+ node_cls_: Your compass_model.Container implementation.
+ key_: A valid key which points to a container.
"""
class TestContainer(_TestContainer):
- storecls = storecls_
+ store_cls = store_cls_
url = url_
- nodecls = nodecls_
+ node_cls = node_cls_
key = key_
return TestContainer
@@ -86,22 +86,20 @@ def container(storecls_, url_, nodecls_, key_):
class _TestStore(ut.TestCase):
- """
- Base class for testing Stores.
- """
+ """ Base class for testing Stores. """
- storecls = None
+ store_cls = None
url = None
def setUp(self):
- self.store = self.storecls(self.url)
+ self.store = self.store_cls(self.url)
def tearDown(self):
if self.store.valid:
self.store.close()
def test_class(self):
- """ Verify the thing we get from storecls is actually a Store """
+ """ Verify the thing we get from store_cls is actually a Store """
self.assertIsInstance(self.store, Store)
def test_url(self):
@@ -125,8 +123,8 @@ class _TestStore(ut.TestCase):
def test_can_handle(self):
""" Verify can_handle() works properly """
- self.assertTrue(self.storecls.can_handle(self.url))
- self.assertFalse(self.storecls.can_handle("file:///no/such/path"))
+ self.assertTrue(self.store_cls.can_handle(self.url))
+ self.assertFalse(self.store_cls.can_handle("file:///no/such/path"))
def test_handlers(self):
""" The implementation has at least one Node handler registered """
@@ -136,18 +134,16 @@ class _TestStore(ut.TestCase):
class _TestNode(ut.TestCase):
- """
- Base class for testing Node objects.
- """
+ """ Base class for testing Node objects. """
- storecls = None
- nodecls = None
+ store_cls = None
+ node_cls = None
url = None
key = None
def setUp(self):
- self.store = self.storecls(self.url)
- self.node = self.nodecls(self.store, self.key)
+ self.store = self.store_cls(self.url)
+ self.node = self.node_cls(self.store, self.key)
def tearDown(self):
if self.store.valid:
@@ -156,34 +152,34 @@ class _TestNode(ut.TestCase):
def test_contains(self):
""" Consistency check for store __contains___ """
self.assertTrue(self.key in self.store)
- self.assertFalse("keynotinstore" in self.store)
+ self.assertFalse("key_not_in_store" in self.store)
def test_icons(self):
- """ Icon dict is present and contains valid items:
+ """ Icon dict is present and contains valid icon paths:
keys: int
- values: callables returning PNG data
+ values: icon paths
Required sizes: 16x16 and 64x64
"""
- for key, val in self.nodecls.icons.iteritems():
+ import os
+ for key, val in self.node_cls.icons.iteritems():
self.assertIsInstance(key, int)
- data = val()
- self.assertIsInstance(data, bytes)
- self.assertEqual(data[0:8], b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
+ self.assertIsInstance(val, unicode)
+ self.assertTrue(os.path.exists(val))
# required resolutions
- self.assertIn(16, self.nodecls.icons)
- self.assertIn(64, self.nodecls.icons)
+ self.assertIn(16, self.node_cls.icons)
+ self.assertIn(64, self.node_cls.icons)
def test_class_kind(self):
""" class_kind is present, and a string """
- self.assertIsInstance(self.nodecls.class_kind, basestring)
+ self.assertIsInstance(self.node_cls.class_kind, basestring)
def test_can_handle(self):
""" can_handle() consistency check """
- self.assertTrue(self.nodecls.can_handle(self.store, self.key))
- self.assertFalse(self.nodecls.can_handle(self.store, "/some/random/key"))
+ self.assertTrue(self.node_cls.can_handle(self.store, self.key))
+ self.assertFalse(self.node_cls.can_handle(self.store, "/some/random/key"))
def test_key(self):
""" Node.key returns a hashable object.
@@ -196,7 +192,7 @@ class _TestNode(ut.TestCase):
def test_store(self):
""" Node.store returns a data store of the same class and URL. """
- self.assertIsInstance(self.node.store, self.storecls)
+ self.assertIsInstance(self.node.store, self.store_cls)
self.assertEqual(self.node.store.url, self.store.url)
def test_display_name(self):
@@ -205,9 +201,7 @@ class _TestNode(ut.TestCase):
class _TestContainer(_TestNode):
- """
- Class for testing compass_model.Container implementations.
- """
+ """ Class for testing compass_model.Container implementations. """
def test_len(self):
""" Object length is consistent with iter() result """
diff --git a/hdf_compass/compass_viewer/__init__.py b/hdf_compass/compass_viewer/__init__.py
index b8196b1..436ec64 100644
--- a/hdf_compass/compass_viewer/__init__.py
+++ b/hdf_compass/compass_viewer/__init__.py
@@ -90,16 +90,14 @@ class CompassApp(wx.App):
self.SetAppName("HDFCompass")
-
def on_compass_open(self, evt):
""" A request has been made to open a node from somewhere in the GUI
"""
open_node(evt.node, evt.kwds.get('pos'))
-
def MacOpenFile(self, filename):
""" A file has been dropped onto the app icon """
- url = 'file://'+filename
+ url = 'file://' + filename
open_store(url)
@@ -160,6 +158,7 @@ def open_store(url):
return False
+
def can_open_store(url):
""" checks url for first matching registered Store class.
@@ -173,6 +172,7 @@ def can_open_store(url):
return False
+
def load_plugins():
""" Helper function that attempts to load all the plugins """
@@ -210,8 +210,7 @@ def load_plugins():
def run():
- """ Run HDFCompass. Handles all command-line arguments, etc.
- """
+ """ Run HDFCompass. Handles all command-line arguments, etc. """
import sys
import os.path as op
@@ -225,10 +224,7 @@ def run():
for url in urls:
if "://" not in url:
# assumed to be file path
- if utils.is_win:
- url = 'file:///' + op.abspath(url)
- else:
- url = 'file://' + op.abspath(url)
+ url = utils.path2url(op.abspath(url))
if not open_store(url):
log.warning('Failed to open "%s"; no handlers' % url)
diff --git a/hdf_compass/compass_viewer/container/__init__.py b/hdf_compass/compass_viewer/container/__init__.py
index cd84308..c4076cc 100644
--- a/hdf_compass/compass_viewer/container/__init__.py
+++ b/hdf_compass/compass_viewer/container/__init__.py
@@ -161,10 +161,8 @@ class ContainerFrame(NodeFrame):
# --- End history support functions ---------------------------------------
-
def update_view(self):
- """ Refresh the entire contents of the frame according to self.node.
- """
+ """ Refresh the entire contents of the frame according to self.node. """
self.SetTitle(self.node.display_title)
self.view = type(self.view)(self, self.node)
self.update_info()
diff --git a/hdf_compass/filesystem_model/__init__.py b/hdf_compass/filesystem_model/__init__.py
index 5ea38fb..83a7d11 100644
--- a/hdf_compass/filesystem_model/__init__.py
+++ b/hdf_compass/filesystem_model/__init__.py
@@ -59,7 +59,9 @@ class Filesystem(compass_model.Store):
@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):
diff --git a/hdf_compass/utils/utils.py b/hdf_compass/utils/utils.py
index 4c918fb..8cae96b 100644
--- a/hdf_compass/utils/utils.py
+++ b/hdf_compass/utils/utils.py
@@ -31,6 +31,7 @@ def url2path(url):
else:
return url.replace('file://', '')
+
def path2url(path):
"""Helper function that returns the url from a file path, dealing with Windows peculiarities"""
if is_win:
--
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