8000 added traits null ui · matplotlib/matplotlib@89ae6cd · GitHub
[go: up one dir, main page]

Skip to content

Commit 89ae6cd

Browse files
committed
added traits null ui
svn path=/trunk/matplotlib/; revision=977
1 parent e945059 commit 89ae6cd

34 files changed

+5120
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from resource_factory import ResourceFactory
2+
from resource_manager import ResourceManager
3+
from resource_path import resource_path
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
""" Default base-class for resource factories. """
2+
3+
4+
class ResourceFactory:
5+
""" Default base-class for resource factories. """
6+
7+
###########################################################################
8+
# 'ResourceFactory' interface.
9+
###########################################################################
10+
11+
def image_from_file(self, filename):
12+
""" Creates an image from the data in the specified filename. """
13+
14+
raise NotImplemented
15+
16+
def image_from_data(self, data):
17+
""" Creates an image from the specified data. """
18+
19+
raise NotImplemented
20+
21+
#### EOF ######################################################################
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
""" The default resource manager.
2+
3+
A resource manager locates and loads application resources such as images and
4+
sounds etc.
5+
6+
"""
7+
8+
9+
# Standard library imports.
10+
import glob, inspect, operator, os
11+
from os.path import join
12+
from zipfile import ZipFile
13+
14+
# Enthought library imports.
15+
from matplotlib.enthought.traits import HasTraits, Instance
16+
from matplotlib.enthought.util.resource import get_path
17+
18+
# Local imports.
19+
from resource_factory import ResourceFactory
20+
from resource_reference import ImageReference
21+
22+
class ResourceManager(HasTraits):
23+
""" The default resource manager.
24+
25+
A resource manager locates and loads application resources such as images
26+
and sounds etc.
27+
28+
"""
29+
30+
# Allowed extensions for image resources.
31+
IMAGE_EXTENSIONS = ['.png', '.jpg', '.bmp', '.gif', '.ico']
32+
33+
# The resource factory is responsible for actually creating resources.
34+
# This is used so that (for example) different GUI toolkits can create
35+
# a images in the format that they require.
36+
resource_factory = Instance(ResourceFactory)
37+
38+
39+
###########################################################################
40+
# 'ResourceManager' interface.
41+
###########################################################################
42+
43+
def locate_image(self, image_name, path):
44+
""" Locates an image. """
45+
46+
if not operator.isSequenceType(path):
47+
path = [path]
48+
49+
resource_path = []
50+
for item in path:
51+
if type(item) is str:
52+
resource_path.append(item)
53+
else:
54+
resource_path.extend(self._get_resource_path(item))
55+
56+
return self._locate_image(image_name, resource_path)
57+
58+
def load_image(self, image_name, path):
59+
""" Loads an image. """
60+
61+
reference = self.locate_image(image_name, path)
62+
if reference is not None:
63+
image = reference.load()
64+
65+
else:
66+
image = None
67+
68+
return image
69+
70+
###########################################################################
71+
# Private interface.
72+
###########################################################################
73+
74+
def _locate_image(self, image_name, resource_path):
75+
""" Attempts to locate an image resource.
76+
77+
If the image is found, an image resource reference is returned.
78+
If the image is NOT found None is returned.
79+
80+
"""
81+
82+
# If the image name contains a file extension (eg. '.jpg') then we will
83+
# only accept an an EXACT filename match.
84+
basename, extension = os.path.splitext(image_name)
85+
if len(extension) > 0:
86+
extensions = [extension]
87+
pattern = image_name
88+
89+
# Otherwise, we will search for common image suffixes.
90+
else:
91+
extensions = self.IMAGE_EXTENSIONS
92+
pattern = image_name + '.*'
93+
94+
for dirname in resource_path:
95+
# Try the 'images' sub-directory first (since that is commonly
96+
# where we put them!). If the image is not found there then look
97+
# in the directory itself.
98+
for path in ['images', '']:
99+
# Is there anything resembling the image name in the directory?
100+
filenames = glob.glob(join(dirname, path, pattern))
101+
for filename in filenames:
102+
not_used, extension = os.path.splitext(filename)
103+
if extension in extensions:
104+
reference = ImageReference(
105+
self.resource_factory, filename=filename
106+
)
107+
108+
return reference
109+
110+
# Is there an 'images' zip file in the directory?
111+
zip_filename = join(dirname, 'images.zip')
112+
if os.path.isfile(zip_filename):
113+
zip_file = ZipFile(zip_filename, 'r')
114+
# Try the image name itself, and then the image name with
115+
# common images suffixes.
116+
for extension in extensions:
117+
try:
118+
image_data = zip_file.read(basename + extension)
119+
reference = ImageReference(
120+
self.resource_factory, data=image_data
121+
)
122+
123+
return reference
124+
125+
except:
126+
pass
127+
128+
return None
129+
130+
def _get_resource_path(self, object):
131+
""" Returns the resource path for an object. """
132+
133+
if hasattr(object, 'resource_path'):
134+
resource_path = object.resource_path
135+
136+
else:
137+
resource_path = self._get_default_resource_path(object)
138+
139+
return resource_path
140+
141+
def _get_default_resource_path(self, object):
142+
""" Returns the default resource path for an object. """
143+
144+
resource_path = []
145+
for klass in inspect.getmro(object.__class__):
146+
try:
147+
resource_path.append(get_path(klass))
148+
149+
# We get an attribute error when we get to a C extension type (in
150+
# our case it will most likley be 'CHasTraits'. We simply ignore
151+
# everything after this point!
152+
except AttributeError:
153+
break
154+
155+
return resource_path
156+
157+
#### EOF ######################################################################
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
""" Returns a resource path based on the call stack.
2+
3+
This type of resource path is normally requested from the constructor for an
4+
object whose resources are relative to the module constructing the object.
5+
"""
6+
7+
# Standard library imports.
8+
import sys
9+
10+
from inspect import stack
11+
from os.path import dirname, exists
12+
from os import getcwd
13+
14+
def resource_path ( level = 2 ):
15+
"""Returns a resource path calculated from the caller's stack.
16+
"""
17+
module = stack( level )[ level ][0].f_globals[ '__name__' ]
18+
19+
if module != '__main__':
20+
# Return the path to the module:
21+
return dirname( getattr( sys.modules.get( module ), '__file__' ) )
22+
23+
# '__main__' is not a real module, so we need a work around:
24+
for path in [ dirname( sys.argv[0] ), getcwd() ]:
25+
if exists( path ):
26+
break
27+
28+
return path
29+
30+
#### EOF ######################################################################
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
""" Resource references. """
2+
3+
4+
# Enthought library imports.
5+
from matplotlib.enthought.traits import Any, HasTraits, Instance
6+
7+
# Local imports.
8+
from resource_factory import ResourceFactory
9+
10+
11+
class ResourceReference(HasTraits):
12+
""" Abstract base class for resource references.
13+
14+
Resource references are returned from calls to 'locate_reference' on the
15+
resource manager.
16+
17+
"""
18+
19+
# The resource factory that will be used to load the resource.
20+
resource_factory = Instance(ResourceFactory) # ReadOnly
21+
22+
###########################################################################
23+
# 'ResourceReference' interface.
24+
###########################################################################
25+
26+
def load(self):
27+
""" Loads the resource. """
28+
29+
raise NotImplementedError
30+
31+
32+
class ImageReference(ResourceReference):
33+
""" A reference to an image resource. """
34+
35+
# Iff the image was found in a file then this is the name of that file.
36+
filename = Any # ReadOnly
37+
38+
# Iff the image was found in a zip file then this is the image data that
39+
# was read from the zip file.
40+
data = Any # ReadOnly
41+
42+
43+
def __init__(self, resource_factory, filename=None, data=None):
44+
""" Creates a new image reference. """
45+
46+
self.resource_factory = resource_factory
47+
self.filename = filename
48+
self.data = data
49+
50+
return
51+
52+
###########################################################################
53+
# 'ResourceReference' interface.
54+
###########################################################################
55+
56+
def load(self):
57+
""" Loads the resource. """
58+
59+
if self.filename is not None:
60+
image = self.resource_factory.image_from_file(self.filename)
61+
62+
elif self.data is not None:
63+
image = self.resource_factory.image_from_data(self.data)
64+
65+
else:
66+
raise ValueError("Image reference has no filename OR data")
67+
68+
return image
69+
70+
#### EOF ######################################################################
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#-------------------------------------------------------------------------------
2+
#
3+
# Export the symbols defined by the traits.ui package.
4+
#
5+
# Written by: David C. Morrill
6+
#
7+
# Date: 10/07/2004
8+
#
9+
# (c) Copyright 2004 by Enthought, Inc.
10+
#
11+
#-------------------------------------------------------------------------------
12+
13+
#-------------------------------------------------------------------------------
14+
# Imports:
15+
#-------------------------------------------------------------------------------
16+
17+
from handler import Handler, default_handler
18+
from view import View
19+
from group import Group
20+
from ui_info import UIInfo
21+
from help import on_help_call
22+
from include import Include
23+
from item import Item
24+
from editor_factory import EditorFactory
25+
from editor import Editor
26+
from toolkit import toolkit
27+
from view_element import ViewElement, ViewSubElement
28+
from help_template import help_template
29+
from tree_node import TreeNode, ObjectTreeNode, TreeNodeObject
30+
from editors import BooleanEditor, ButtonEditor, CheckListEditor, \
31+
CodeEditor, ColorEditor, RGBColorEditor, RGBAColorEditor, CompoundEditor, \
32+
DirectoryEditor, EnumEditor, FileEditor, FontEditor, KivaFontEditor, \
33+
ImageEnumEditor, InstanceEditor, ListEditor, PlotEditor, RangeEditor, \
34+
TextEditor, TreeEditor, TupleEditor
35+
from editors import ColorTrait, RGBColorTrait, RGBAColorTrait, \
36+
EnableRGBAColorEditor, FontTrait, KivaFontTrait
37+
38+
import view_elements
39+

0 commit comments

Comments
 (0)
0