Changeset 2
- Timestamp:
- 11/13/08 12:33:47 (8 weeks ago)
- Location:
- gnrpy
- Files:
-
- 2 modified
-
gnr/core/gnrbag.py (modified) (52 diffs)
-
test_gnr/core/data/testbag.xml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
gnrpy/gnr/core/gnrbag.py
r1 r2 22 22 23 23 """ 24 25 24 The gnrbag module contains a single class intended to be used: Bag 26 27 25 A Bag is a generic container object, similar to a dictionary, 28 26 with some useful properties: 29 - ordered 30 - accessible by key 31 - iterable 32 - hierarchical 27 28 - ordered 29 - accessible by key 30 - iterable 31 - hierarchical 33 32 34 33 A Bag can store any kind of python object with a label in an ordered list. … … 37 36 38 37 A Bag can be loaded and saved in some ways: 39 - saved to and loaded from pikle files or strings:40 the object stored in the bag must be picklable of course.41 - saved to and loaded from xml files or strings with a specific syntax:42 the object stored in the bag must be strings, numbers or dates.43 - loaded from a generic xml file preserving the whole hierarchical structure and44 the attributes: all values will be of type string of course.45 - loaded from a generic html file: requires tidy.38 - saved to and loaded from pikle files or strings: 39 the object stored in the bag must be picklable of course. 40 - saved to and loaded from xml files or strings with a specific syntax: 41 the object stored in the bag must be strings, numbers or dates. 42 - loaded from a generic xml file preserving the whole hierarchical structure and 43 the attributes: all values will be of type string of course. 44 - loaded from a generic html file: requires tidy. 46 45 47 46 Another class you could have to deal with is BagNode: they are Bag elements, … … 88 87 """ 89 88 BagNode is the element type which a Bag is composed of. That's why it's 90 possible to say that a Bag is a collection of BagNodes. A BagNode is an object 91 that gather within itself, three main things: 89 possible to say that a Bag is a collection of BagNodes. A BagNode is an 90 object that gather within itself, three main things: 91 92 92 -label: can be only a string. 93 93 -value: can be anything, but a BagNode. Often value is a Bag. 94 94 -attributes: dictionary that contains node's metadata 95 95 96 """ 96 def __init__(self, parentbag, label, value=None, attr=None, resolver=None, validators=None,_removeNullAttributes=True): 97 """ 98 @param parentbag: Bag than contains the node 99 @param label: label that identifies the node 100 @param value: value of the node 101 @param attr: node's attributes 102 @param resolver: a BagResolver 103 @param validators: a dict with all validators pairs 97 def __init__(self, parentbag, label, value=None, attr=None, resolver=None, 98 validators=None,_removeNullAttributes=True): 99 """ 100 * `parentbag`: Bag than contains the node 101 * `label`: label that identifies the node 102 * `value`: value of the node 103 * `attr`: node's attributes 104 * `resolver`: a BagResolver 105 * `validators`: a dict with all validators pairs 104 106 """ 105 107 self.label = label … … 112 114 self.attr = {} 113 115 if attr: 114 self.setAttr(attr, trigger = False,_removeNullAttributes=_removeNullAttributes) 116 self.setAttr(attr, trigger = False, 117 _removeNullAttributes = _removeNullAttributes) 115 118 if validators: 116 119 self.setValidators(validators) … … 169 172 Returns the value of the BagNode. 170 173 This method is called by the property .value 171 @param mode: can be one or more of: 172 - static: to get the resolver instance instead of the calculated value 173 - weak: to get a weak ref stored in the node instead of the actual object 174 @return: node's value 174 175 * `mode`: can be one or more of: 176 - static: to get the resolver instance instead of the 177 calculated value 178 - weak: to get a weak ref stored in the node instead of 179 the actual object 180 181 Return: node's value 175 182 """ 176 183 if not self._resolver == None: … … 187 194 return self._value 188 195 189 def setValue(self, value, trigger=True, _attributes=None, _updattr=None,_removeNullAttributes=True): 196 def setValue(self, value, trigger=True, _attributes = None, 197 _updattr = None,_removeNullAttributes = True): 190 198 """ 191 199 Set the node's value, unless the node is locked. 192 200 This method is called by the property .value 193 @param value: the value to set the new bag inherits the trigger of the 194 parentbag and calls it sending an update event 201 202 * `value`: the value to set the new bag inherits the trigger of the 203 parentbag and calls it sending an update event 195 204 """ 196 205 if self.locked: … … 214 223 if _attributes != None: 215 224 evt='upd_value_attr' 216 self.setAttr(_attributes, trigger=False, _updattr=_updattr,_removeNullAttributes=_removeNullAttributes) 225 self.setAttr(_attributes, trigger = False, _updattr = _updattr, 226 _removeNullAttributes=_removeNullAttributes) 217 227 if trigger: 218 228 for subscriber in self._node_subscribers.values(): … … 222 232 value.setBackRef(node=self,parent=self.parentbag) 223 233 if trigger: 224 self.parentbag._onNodeChanged(self, [self.label], oldvalue=oldvalue, evt=evt ) 234 self.parentbag._onNodeChanged(self, [self.label], 235 oldvalue=oldvalue, evt=evt) 225 236 226 237 value = property(getValue,setValue) … … 262 273 this method returns the value of an attribute given it's label. 263 274 If it doesn't exists returns a default value. 264 @param label: the label of the attribute to get.275 * `label`: the label of the attribute to get. 265 276 """ 266 277 if label: return self.attr.get(label,default) … … 283 294 return True 284 295 285 def setAttr(self, attr=None, trigger=True, _updattr=True, _removeNullAttributes=True,**kwargs): 296 def setAttr(self, attr = None, trigger = True, _updattr = True, 297 _removeNullAttributes = True,**kwargs): 286 298 """this method receives one or more key-value couple, passed as a dict or 287 299 as named parameters, and sets them as attributes of the node 288 @param attr the dict of attributes to set into the node. 300 301 * `attr`: the dict of attributes to set into the node. 302 289 303 """ 290 304 if not _updattr: … … 334 348 If there are no validators into the node then addValidator instantiate 335 349 a new BagValidationList and append the validator to it. 336 @param validator: the type of validation to set into the list of the node.337 @param paremeterString: the parameters for a single validation type.350 * `validator`: the type of validation to set into the list of the node. 351 * `paremeterString`: the parameters for a single validation type. 338 352 """ 339 353 if self._validators is None: … … 460 474 The "in" operator can be used to test the existence of a key in a 461 475 bag. Also nested keys are allowed. 462 @param what: the key path to test.476 * `what`: the key path to test. 463 477 @return: a boolean value, True if the key exists in the bag, False otherwise. 464 478 … … 485 499 A path can also ba a list of keys. 486 500 487 @param path: the item's path 488 @param default: an optional default value, default is 'None'. 489 @return: the value of the given item 501 * `path`: the item's path 502 * `default`: an optional default value, default is 'None'. 503 504 Return: the value of the given item 490 505 491 506 >>> mybag=Bag() … … 592 607 one step of the path, calling itself recursively. If autocreate mode is True, 593 608 the method creates not existing nodes of the pathlist. 594 @param pathlist: list of nodes'labels609 * `pathlist`: list of nodes'labels 595 610 @return: current node's value 596 611 """ … … 690 705 This method calls the __str__ method: 691 706 asString() returns an ascii encoded formatted representation of the bag. 692 @param encoding: default is 'UTF-8'707 * `encoding`: default is 'UTF-8' 693 708 @return: a formatted representation of the bag contents (ascii) 694 709 … … 735 750 long as the Bag containing the requested values. 736 751 737 @param what: this param is a comma separated string of special keys. 738 Special keys are: 752 * `what`: this param is a comma separated string of special keys. 753 Special keys are: 754 739 755 - #k: the label of each node 740 756 - #v: the value of each node … … 743 759 - #a.attrname: the attribute 'attrname' of each node 744 760 - subpath: the value of this subpath of each node 745 this parameter can start with a path before the list of746 special keys to apply the digest to a subpath of this747 Bag. Path and special keys are separated by ':'.748 @param condition: set a condition for digest process761 this parameter can start with a path before the list of 762 special keys to apply the digest to a subpath of this 763 Bag. Path and special keys are separated by ':'. 764 * `condition`: set a condition for digest process 749 765 750 766 """ … … 785 801 """ 786 802 This method is analog to dictionary's has_key() method. 787 @param path: path of the given item.803 * `path`: path of the given item. 788 804 @return: a boolean value: True if the given item has a key, False otherwise. 789 805 … … 810 826 This method is analog to dictionary's pop() method. It pops the given item 811 827 from the Bag; it returns the given item. 812 @param path: path of the given item.828 * `path`: path of the given item. 813 829 @return: the given item. 814 830 """ … … 843 859 """ 844 860 this method merge a Bag into the current one. 845 @param otherbag: a Bag to merge into.861 * `otherbag`: a Bag to merge into. 846 862 """ 847 863 if isinstance(otherbag, basestring): … … 934 950 equal to 'value'. E.g. searching a node with a given 'id' in a Bag build from html. 935 951 936 @param attr: path of the given item. 937 @param value: path of the given item. 938 @param path: optional, an empty list that will be filled with the path of the found node. 939 @return: a BagNode with the requested attribute""" 952 * `attr`: path of the given item. 953 * `value`: path of the given item. 954 * `path`: optional, an empty list that will be filled with the path of the found node. 955 956 Return: a BagNode with the requested attribute 957 """ 940 958 bags=[] 941 959 if path == None: path = [] … … 957 975 """ 958 976 This method returns the BagNode stored at this path. 959 @param path: path of the given item.977 * `path`: path of the given item. 960 978 """ 961 979 if not path: … … 991 1009 """ 992 1010 This method set attributes into the node at the given path 993 @param _path: path of the target item.994 @param _attributes:a dict of attributes to set into the node.1011 * `_path`: path of the target item. 1012 * `_attributes`:a dict of attributes to set into the node. 995 1013 996 1014 """ … … 1000 1018 """ 1001 1019 This method get the value of the attribute of the node at the given path 1002 @param path: path of the given item.1003 @param _atts: the label of the attribute to get.1020 * `path`: path of the given item. 1021 * `_atts`: the label of the attribute to get. 1004 1022 """ 1005 1023 node = self.getNode(path) … … 1015 1033 """This method splits a path string it at each '.' and returns a list of 1016 1034 nodes' labels and the label of the first list's element. 1017 @param path: the given path.1035 * `path`: the given path. 1018 1036 @return label: the first label 1019 1037 @return pathlist: the list result of path splitting. … … 1054 1072 If the path already exists, this method replicates the path keeping old 1055 1073 values and the new value. 1056 @param item_path: the path of the given item. 1057 @param item_value: the value to set. 1058 @param _attributes: an optional parameter, it specifies the attributes 1059 of the value to set. Default is 'None'. 1060 @param _position: specifies the position where to add the new item. 1061 It can be "<" or ">" followed by "#n" or "label". Default is append 1062 after last item. 1063 @return: the current bag. 1074 Parameters: 1075 1076 * item_path: the path of the given item. 1077 * item_value: the value to set. 1078 * _attributes: an optional parameter, it specifies the attributes 1079 of the value to set. Default is 'None'. 1080 * _position: specifies the position where to add the new item. 1081 It can be "<" or ">" followed by "#n" or "label". Default is append 1082 after last item. 1083 1084 Return:the current bag. 1064 1085 1065 1086 """ … … 1074 1095 in the form "label1.label2...labelN".It returns the current bag. 1075 1096 If the path already exists, it overwrites the value at the given path. 1076 @param item_path: the path of the given item. 1077 @param item_value: the value to set. 1078 @param _attributes: an optional parameter, it specified the attributes of 1079 the value to set. Default is 'None'. 1080 @param _position: an optional parameter, if specified the method setItem() 1081 behaves like addItem(). Default is 'None'. 1082 @param _duplicate: specifies if a node with an existing path overwrite 1083 the value or append it. 1084 @param _validators: an optional parameter, it specified the validarors of 1085 the value to set. Default is 'None'. 1086 @param kwargs: all remaining kwargs can be attributes AND/OR validators . 1087 @return: the current bag. 1097 Parameters: 1098 1099 * `item_path`: the path of the given item. 1100 * `item_value`: the value to set. 1101 * `_attributes`: an optional parameter, it specified the attributes of 1102 the value to set. Default is 'None'. 1103 * `_position`: an optional parameter, if specified the method setItem() 1104 behaves like addItem(). Default is 'None'. 1105 * `_duplicate`: specifies if a node with an existing path overwrite 1106 the value or append it. 1107 * `_validators`: an optional parameter, it specified the validarors of 1108 the value to set. Default is 'None'. 1109 * `kwargs`: all remaining kwargs can be attributes AND/OR validators . 1110 1111 Return: the current bag. 1088 1112 """ 1089 1113 … … 1110 1134 obj, label = self._htraverse(item_path, autocreate=True) 1111 1135 obj._set(label, item_value, _attributes=_attributes, _position=_position, 1112 _duplicate=_duplicate, _updattr=_updattr,_validators = _validators,_removeNullAttributes=_removeNullAttributes) 1136 _duplicate=_duplicate, _updattr=_updattr, 1137 _validators = _validators,_removeNullAttributes=_removeNullAttributes) 1113 1138 __setitem__ = setItem 1114 1139 1115 def _set(self, label, value, _attributes=None, _position=None, _duplicate=False, _updattr=False, _validators=None,_removeNullAttributes=True): 1140 def _set(self, label, value, _attributes=None, _position=None, 1141 _duplicate=False, _updattr=False, _validators=None,_removeNullAttributes=True): 1116 1142 resolver = None 1117 1143 if isinstance(value, BagResolver): … … 1125 1151 raise BagException ('Not existing index in #n syntax') 1126 1152 else: 1127 self._insertNode(BagNode(self,label=label,value=value,attr=_attributes,resolver=resolver, validators=_validators,_removeNullAttributes=_removeNullAttributes), _position) 1153 self._insertNode(BagNode(self,label=label,value=value,attr=_attributes, 1154 resolver=resolver, validators=_validators, 1155 _removeNullAttributes=_removeNullAttributes), _position) 1128 1156 else: 1129 1157 node=self._nodes[i] … … 1132 1160 if _validators: 1133 1161 node.setValidators(_validators) 1134 node.setValue(value, _attributes=_attributes, _updattr=_updattr,_removeNullAttributes=_removeNullAttributes) 1162 node.setValue(value, _attributes=_attributes, _updattr=_updattr, 1163 _removeNullAttributes=_removeNullAttributes) 1135 1164 def defineSymbol(self,**kwargs): 1136 1165 """ 1137 1166 Define a variable and link it to a value at the specified path. The value 1138 1167 linked is a BagFormula Resolver. 1139 @param kwargs: a dict of symbol to define for a formula.1168 * `kwargs`: a dict of symbol to define for a formula. 1140 1169 """ 1141 1170 if self._symbols == None: … … 1146 1175 """ 1147 1176 Define a formula that uses defined symbols. 1148 @param kwargs: a pair of key-value which rapresent the formula and the1177 * `kwargs`: a pair of key-value which rapresent the formula and the 1149 1178 string that describes it. 1150 1179 """ … … 1157 1186 """ 1158 1187 Sets a BagFormula resolver. 1159 @param formula: a string that represents the expression with symbolic vars1160 @param kwargs: links between symbols and paths associated to their values1188 * `formula`: a string that represents the expression with symbolic vars 1189 * `kwargs`: links between symbols and paths associated to their values 1161 1190 """ 1162 1191 self.setBackRef() … … 1171 1200 """ 1172 1201 This method get the resolver of the node at the given path. 1173 @param path: path of the node.1202 * `path`: path of the node. 1174 1203 """ 1175 1204 return self.getNode(path).getResolver() … … 1179 1208 """ 1180 1209 This method set a resolver into the node at the given path. 1181 @param path: path of the node.1210 * `path`: path of the node. 1182 1211 """ 1183 1212 return self.setItem(path,None,resolver=resolver) … … 1189 1218 and it knows has a reference to its Parent. 1190 1219 1191 @param node: not required1192 @param parent: not required1220 * `node`: not required 1221 * `parent`: not required 1193 1222 """ 1194 1223 if self.backref != True: … … 1286 1315 If the label doesn't exist it returns -1. 1287 1316 The mach is not case sensitive. 1288 @param label: a not hierarchical label.1317 * `label`: a not hierarchical label. 1289 1318 @return: position into the bag. 1290 1319 … … 1324 1353 """ 1325 1354 This method returns a pickled Bag. 1326 @param destination: an optional parameter; it is the destination path; 1327 default is 'None'. 1328 @param bin: a boolean optional parameter, if set to 'False' the Bag is 1329 pickled in ASCII code, if set to 'True' is pickled in binary format. 1330 Default is 'True'. 1331 @return: the pickled Bag. 1355 1356 * `destination`: an optional parameter; it is the destination path; 1357 default is 'None'. 1358 * `bin`: a boolean optional parameter, if set to 'False' the Bag is 1359 pickled in ASCII code, if set to 'True' is pickled in binary format. 1360 Default is 'True'. 1361 1362 Return: the pickled Bag. 1332 1363 1333 1364 """ … … 1346 1377 """ 1347 1378 This method unpickles a pickled Bag. 1348 @param source: the source path.1379 * `source`: the source path. 1349 1380 @return: the unpickled Bag. 1350 1381 … … 1371 1402 #-------------------- toXml -------------------------------- 1372 1403 def toXml(self,filename=None,encoding='UTF-8',typeattrs=True, unresolved=False, 1373 autocreate=False, jsonmode=None, jsonkey=None, translate_cb=None, omitUnknownTypes=False, catalog=None): 1404 autocreate=False, jsonmode=None, jsonkey=None, translate_cb=None, 1405 omitUnknownTypes=False, catalog=None): 1374 1406 """ 1375 1407 This method returns a complete standard XML version of the Bag, … … 1380 1412 Is also possible to write the result on a file, passing the path of the file 1381 1413 as the 'filename' parameter. 1382 @param filename: an optional parameter, it is the path of the output file; 1383 default value is 'None' 1384 @param encoding: an optional parameter, is used to set the XML encoding; 1385 default value is UTF-8. 1386 @return: an XML version of the bag. 1414 1415 * `filename`: an optional parameter, it is the path of the output file; 1416 default value is 'None' 1417 * `encoding`: an optional parameter, is used to set the XML encoding; 1418 default value is UTF-8. 1419 1420 Return: an XML version of the bag. 1387 1421 1388 1422 >>> mybag=Bag() … … 1404 1438 This method fills a void Bag from: basestring, bag, list. 1405 1439 1406 @param source: the source for the Bag.1440 * `source`: the source for the Bag. 1407 1441 1408 1442 """ … … 1431 1465 def _fromSource(self, source, fromFile, mode): 1432 1466 """ 1433 @param source: the source string or source URI1434 @param fromFile: flag that says if source is eventually an URI1435 @param mode: flag that means the importation mode (XML, pickle or VCARD)1467 * `source`: the source string or source URI 1468 * `fromFile`: flag that says if source is eventually an URI 1469 * `mode`: flag that means the importation mode (XML, pickle or VCARD) 1436 1470 @return: a bag from unpickle or from fromXml 1437 1471 … … 1460 1494 """ 1461 1495 Generate a Bag from a generic xml 1462 @param source: an xml string or a path to an xml document.1496 * `source`: an xml string or a path to an xml document. 1463 1497 @type source: basestring 1464 1498 @return: source, fromFile, mode … … 1513 1547 """ 1514 1548 This method fills the Bag with values read from an XML string or file or URL. 1515 @param source: the XML source to be loaded in the Bag.1516 @param catalog:1517 @param bagcls:1549 * `source`: the XML source to be loaded in the Bag. 1550 * `catalog`: 1551 * `bagcls`: 1518 1552 bagcls empty: 1519 1553 """ … … 1559 1593 """ 1560 1594 This method add a validator into the node at the given path 1561 @param path: path of the node.1562 @param validator: the type of validation.1563 @param parameterString: string which contains the params for validation.1595 * `path`: path of the node. 1596 * `validator`: the type of validation. 1597 * `parameterString`: string which contains the params for validation. 1564 1598 """ 1565 1599 self.getNode(path,autocreate=True).addValidator(validator, parameterString) … … 1614 1648 this means that I can define many eventhandlers for the same event. 1615 1649 1616 @param subscriberId: an ID can be assigned for a subscription1617 @param update: the eventhandler function linked to update event.1618 @param insert: the eventhandler function linked to insert event.1619 @param delete: the eventhandler function linked to delete event.1620 @param any: the eventhandler function linked to do whenever something happens.1650 * `subscriberId`: an ID can be assigned for a subscription 1651 * `update`: the eventhandler function linked to update event. 1652 * `insert`: the eventhandler function linked to insert event. 1653 * `delete`: the eventhandler function linked to delete event. 1654 * `any`: the eventhandler function linked to do whenever something happens. 1621 1655 """ 1622 1656 if self.backref == False: … … 1631 1665 delete a subscription of an event of given subscriberId. 1632 1666 1633 @param subscriberId: an ID can be assigned for a subscription1634 @param update: the eventhandler function to remove1635 @param insert: the eventhandler function to remove1636 @param delete: the eventhandler function to remove1637 @param any: the eventhandler function to remove1667 * `subscriberId`: an ID can be assigned for a subscription 1668 * `update`: the eventhandler function to remove 1669 * `insert`: the eventhandler function to remove 1670 * `delete`: the eventhandler function to remove 1671 * `any`: the eventhandler function to remove 1638 1672 1639 1673 """ … … 1672 1706 """ 1673 1707 Calls a function for each node of the Bag. 1674 @param callback: the function which is called.1708 * `callback`: the function which is called. 1675 1709 """ 1676 1710 result=None … … 1700 1734 def
