src/reportlab/graphics/widgetbase.py
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 3794 398ea04239b5
equal deleted inserted replaced
3722:29c11b905751 3723:99aa837b6703
    25         attribute has a valid value.  Either succeeds
    25         attribute has a valid value.  Either succeeds
    26         or raises an informative exception.
    26         or raises an informative exception.
    27         """
    27         """
    28 
    28 
    29         if self._attrMap is not None:
    29         if self._attrMap is not None:
    30             for key in list(self.__dict__.keys()):
    30             for key in self.__dict__.keys():
    31                 if key[0] != '_':
    31                 if key[0] != '_':
    32                     msg = "Unexpected attribute %s found in %s" % (key, self)
    32                     msg = "Unexpected attribute %s found in %s" % (key, self)
    33                     assert key in self._attrMap, msg
    33                     assert key in self._attrMap, msg
    34             for (attr, metavalue) in list(self._attrMap.items()):
    34             for attr, metavalue in self._attrMap.items():
    35                 msg = "Missing attribute %s from %s" % (attr, self)
    35                 msg = "Missing attribute %s from %s" % (attr, self)
    36                 assert hasattr(self, attr), msg
    36                 assert hasattr(self, attr), msg
    37                 value = getattr(self, attr)
    37                 value = getattr(self, attr)
    38                 args = (value, attr, self.__class__.__name__)
    38                 args = (value, attr, self.__class__.__name__)
    39                 assert metavalue.validate(value), "Invalid value %s for attribute %s in class %s" % args
    39                 assert metavalue.validate(value), "Invalid value %s for attribute %s in class %s" % args
    62 
    62 
    63         # TODO when we need it, but not before -
    63         # TODO when we need it, but not before -
    64         # expose sequence contents?
    64         # expose sequence contents?
    65 
    65 
    66         props = {}
    66         props = {}
    67         for name in list(self.__dict__.keys()):
    67         for name in self.__dict__.keys():
    68             if name[0:1] != '_':
    68             if name[0:1] != '_':
    69                 component = getattr(self, name)
    69                 component = getattr(self, name)
    70 
    70 
    71                 if recur and isValidChild(component):
    71                 if recur and isValidChild(component):
    72                     # child object, get its properties too
    72                     # child object, get its properties too
    73                     childProps = component.getProperties(recur=recur)
    73                     childProps = component.getProperties(recur=recur)
    74                     for (childKey, childValue) in list(childProps.items()):
    74                     for childKey, childValue in childProps.items():
    75                         #key might be something indexed like '[2].fillColor'
    75                         #key might be something indexed like '[2].fillColor'
    76                         #or simple like 'fillColor'; in the former case we
    76                         #or simple like 'fillColor'; in the former case we
    77                         #don't need a '.' between me and my child.
    77                         #don't need a '.' between me and my child.
    78                         if childKey[0] == '[':
    78                         if childKey[0] == '[':
    79                             props['%s%s' % (name, childKey)] = childValue
    79                             props['%s%s' % (name, childKey)] = childValue
    96         set before any of the children, which may be helpful to
    96         set before any of the children, which may be helpful to
    97         widget designers.
    97         widget designers.
    98         """
    98         """
    99 
    99 
   100         childPropDicts = {}
   100         childPropDicts = {}
   101         for (name, value) in list(propDict.items()):
   101         for name, value in propDict.items():
   102             parts = string.split(name, '.', 1)
   102             parts = string.split(name, '.', 1)
   103             if len(parts) == 1:
   103             if len(parts) == 1:
   104                 #simple attribute, set it now
   104                 #simple attribute, set it now
   105                 setattr(self, name, value)
   105                 setattr(self, name, value)
   106             else:
   106             else:
   109                     childPropDicts[childName][remains] = value
   109                     childPropDicts[childName][remains] = value
   110                 except KeyError:
   110                 except KeyError:
   111                     childPropDicts[childName] = {remains: value}
   111                     childPropDicts[childName] = {remains: value}
   112 
   112 
   113         # now assign to children
   113         # now assign to children
   114         for (childName, childPropDict) in list(childPropDicts.items()):
   114         for childName, childPropDict in childPropDicts.items():
   115             child = getattr(self, childName)
   115             child = getattr(self, childName)
   116             child.setProperties(childPropDict)
   116             child.setProperties(childPropDict)
   117 
   117 
   118 
   118 
   119     def dumpProperties(self, prefix=""):
   119     def dumpProperties(self, prefix=""):
   134     """Base for all user-defined widgets.  Keep as simple as possible. Does
   134     """Base for all user-defined widgets.  Keep as simple as possible. Does
   135     not inherit from Shape so that we can rewrite shapes without breaking
   135     not inherit from Shape so that we can rewrite shapes without breaking
   136     widgets and vice versa."""
   136     widgets and vice versa."""
   137 
   137 
   138     def _setKeywords(self,**kw):
   138     def _setKeywords(self,**kw):
   139         for k,v in list(kw.items()):
   139         for k,v in kw.items():
   140             if k not in self.__dict__:
   140             if k not in self.__dict__:
   141                 setattr(self,k,v)
   141                 setattr(self,k,v)
   142 
   142 
   143     def draw(self):
   143     def draw(self):
   144         msg = "draw() must be implemented for each Widget!"
   144         msg = "draw() must be implemented for each Widget!"
   271     def getProperties(self,recur=1):
   271     def getProperties(self,recur=1):
   272         # return any children which are defined and whatever
   272         # return any children which are defined and whatever
   273         # differs from the parent
   273         # differs from the parent
   274         props = {}
   274         props = {}
   275 
   275 
   276         for (key, value) in list(self._value.getProperties(recur=recur).items()):
   276         for key, value in self._value.getProperties(recur=recur).items():
   277             props['%s' % key] = value
   277             props['%s' % key] = value
   278 
   278 
   279         for idx in list(self._children.keys()):
   279         for idx in self._children.keys():
   280             childProps = self._children[idx].getProperties(recur=recur)
   280             childProps = self._children[idx].getProperties(recur=recur)
   281             for (key, value) in list(childProps.items()):
   281             for key, value in childProps.items():
   282                 if not hasattr(self,key) or getattr(self, key)!=value:
   282                 if not hasattr(self,key) or getattr(self, key)!=value:
   283                     newKey = '[%s].%s' % (idx, key)
   283                     newKey = '[%s].%s' % (idx, key)
   284                     props[newKey] = value
   284                     props[newKey] = value
   285         return props
   285         return props
   286 
   286 
   287     def setVector(self,**kw):
   287     def setVector(self,**kw):
   288         for name, value in list(kw.items()):
   288         for name, value in kw.items():
   289             for i in range(len(value)):
   289             for i in range(len(value)):
   290                 setattr(self[i],name,value[i])
   290                 setattr(self[i],name,value[i])
   291 
   291 
   292     def __getattr__(self,name):
   292     def __getattr__(self,name):
   293         return getattr(self._value,name)
   293         return getattr(self._value,name)
   331         )
   331         )
   332 
   332 
   333     def __init__(self, **kwargs):
   333     def __init__(self, **kwargs):
   334         "Initialize with attributes if any."
   334         "Initialize with attributes if any."
   335 
   335 
   336         for k, v in list(kwargs.items()):
   336         for k, v in kwargs.items():
   337             setattr(self, k, v)
   337             setattr(self, k, v)
   338 
   338 
   339 
   339 
   340     def __setattr__(self, name, value):
   340     def __setattr__(self, name, value):
   341         "Verify attribute name and value, before setting it."
   341         "Verify attribute name and value, before setting it."