81 |
81 |
82 def match_name(self, name): |
82 def match_name(self, name): |
83 """ See if the given name matches the name of this configuration. """ |
83 """ See if the given name matches the name of this configuration. """ |
84 return self.name == name |
84 return self.name == name |
85 |
85 |
86 def get(self, key, default_value): |
86 def get(self, key, failobj=None): |
87 """ Get an item from the configuration. """ |
87 """ Get an item from the configuration. """ |
88 try: |
88 try: |
89 return self.__getitem__(key) |
89 return self.__getitem__(key) |
90 except KeyError: |
90 except KeyError: |
91 return default_value |
91 return failobj |
92 |
92 |
93 def get_list(self, key, default_value): |
93 def get_list(self, key, default_value): |
94 """ Get a value as a list. """ |
94 """ Get a value as a list. """ |
95 try: |
95 try: |
96 itemlist = self.__getitem__(key) |
96 itemlist = self.__getitem__(key) |
97 if not isinstance(itemlist, types.ListType): |
97 if not isinstance(itemlist, types.ListType): |
400 |
400 |
401 def isBuildable(self): |
401 def isBuildable(self): |
402 """ Is this a buildable configuration? """ |
402 """ Is this a buildable configuration? """ |
403 return self.abstract == None |
403 return self.abstract == None |
404 |
404 |
405 def _addPropertyValue(self, key, value, parseList=True): |
405 def add_property_value(self, key, value, parseList=True): |
406 """Adds a property value to the configuration. |
406 """Adds a property value to the configuration. |
407 |
407 |
408 If the property does not exist, it is added without modification. |
408 If the property does not exist, it is added without modification. |
409 If there is already a single value matching the key, the value is replaced by a list |
409 If there is already a single value matching the key, the value is replaced by a list |
410 containing the original and new values. |
410 containing the original and new values. |
517 def __init__(self, configs): |
517 def __init__(self, configs): |
518 """ Initialization. """ |
518 """ Initialization. """ |
519 Configuration.__init__(self) |
519 Configuration.__init__(self) |
520 self._configs = configs |
520 self._configs = configs |
521 |
521 |
522 def getConfigurations(self, name=None, type=None): |
522 def getConfigurations(self, name=None, type_=None): |
523 """ Return a list of configs that matches the name and type specified. |
523 """ Return a list of configs that matches the name and type specified. |
524 |
524 |
525 This can be queried multiple times to retrieve different named configurations. |
525 This can be queried multiple times to retrieve different named configurations. |
526 """ |
526 """ |
527 result = [] |
527 result = [] |
528 for conf in self._configs: |
528 for conf in self._configs: |
529 if ((name != None and conf.match_name(name)) or name == None) and ((type != None and conf.type == type) or type == None): |
529 if ((name != None and conf.match_name(name)) or name == None) and ((type_ != None and conf.type == type_) or type_ == None): |
530 result.append(conf) |
530 result.append(conf) |
531 return result |
531 return result |
532 |
532 |
533 |
533 |
534 class ConfigurationBuilder(object): |
534 class ConfigurationBuilder(object): |
544 |
544 |
545 _constructors = {'spec':Specification, 'config':NestedConfiguration} |
545 _constructors = {'spec':Specification, 'config':NestedConfiguration} |
546 |
546 |
547 def __init__(self, inputfile, configname=''): |
547 def __init__(self, inputfile, configname=''): |
548 """ Initialization. """ |
548 """ Initialization. """ |
|
549 ConfigurationBuilder.__init__(self) |
549 self.inputfile = inputfile |
550 self.inputfile = inputfile |
550 self.configname = configname |
551 self.configname = configname |
551 self._warn_on_deprecated_spec = False |
552 self._warn_on_deprecated_spec = False |
|
553 self.rootNode = None |
552 |
554 |
553 def getConfiguration(self): |
555 def getConfiguration(self): |
554 """ Returns a ConfigurationSet object. |
556 """ Returns a ConfigurationSet object. |
555 |
557 |
556 A ConfigurationSet represents a number of Configuration objects |
558 A ConfigurationSet represents a number of Configuration objects |
622 setNode.setAttribute( 'value', config.__getitem__(key)) |
624 setNode.setAttribute( 'value', config.__getitem__(key)) |
623 out = open(output, 'w+') |
625 out = open(output, 'w+') |
624 out.write(doc.toprettyxml()) |
626 out.write(doc.toprettyxml()) |
625 out.close() |
627 out.close() |
626 |
628 |
627 |
629 def getConfigurations(self, name=None, type_=None): |
628 def getConfigurations(self, name=None, type=None): |
|
629 """ Get a list of the individual configurations. |
630 """ Get a list of the individual configurations. |
630 |
631 |
631 Once read a new builder must be opened to retrieve a differently filtered set of configurations. |
632 Once read a new builder must be opened to retrieve a differently filtered set of configurations. |
632 """ |
633 """ |
633 config_set = self.getConfiguration() |
634 config_set = self.getConfiguration() |
634 return config_set.getConfigurations(name, type) |
635 return config_set.getConfigurations(name, type_) |
635 |
636 |
636 def getReferences(self): |
637 def getReferences(self): |
637 """get references""" |
638 """get references""" |
638 references = [] |
639 references = [] |
639 for rootNode in self.rootNode.childNodes: |
640 for rootNode in self.rootNode.childNodes: |
680 if parentConfig != None and parentConfig.has_key(name): |
681 if parentConfig != None and parentConfig.has_key(name): |
681 parent_value = parentConfig.__getitem__(name, False) |
682 parent_value = parentConfig.__getitem__(name, False) |
682 if not isinstance(parent_value, types.ListType): |
683 if not isinstance(parent_value, types.ListType): |
683 parent_value = [parent_value] |
684 parent_value = [parent_value] |
684 for value in parent_value: |
685 for value in parent_value: |
685 config._addPropertyValue(name, value) |
686 config.add_property_value(name, value) |
686 |
687 |
687 if child.nodeName == 'set' or child.nodeName == 'append': |
688 if child.nodeName == 'set' or child.nodeName == 'append': |
688 name = child.getAttribute('name') |
689 name = child.getAttribute('name') |
689 if child.hasAttribute('value'): |
690 if child.hasAttribute('value'): |
690 value = child.getAttribute('value') |
691 value = child.getAttribute('value') |
691 config._addPropertyValue(name, value) |
692 config.add_property_value(name, value) |
692 elif child.hasChildNodes(): |
693 elif child.hasChildNodes(): |
693 value = "" |
694 value = "" |
694 for textchild in child.childNodes: |
695 for textchild in child.childNodes: |
695 value += textchild.data |
696 value += textchild.data |
696 config._addPropertyValue(name, value, False) |
697 config.add_property_value(name, value, False) |
697 elif child.nodeName == 'specRef': |
698 elif child.nodeName == 'specRef': |
698 for ref in child.getAttribute('ref').split(','): |
699 for ref in child.getAttribute('ref').split(','): |
699 node = self.getNodeByReference(ref) |
700 node = self.getNodeByReference(ref) |
700 if not node: |
701 if not node: |
701 raise Exception('Referenced spec not found: ' + ref) |
702 raise Exception('Referenced spec not found: ' + ref) |
756 |
757 |
757 def to_xpath(self): |
758 def to_xpath(self): |
758 """ Convert the key to XPath syntax. """ |
759 """ Convert the key to XPath syntax. """ |
759 return self.string.replace('.', '/') |
760 return self.string.replace('.', '/') |
760 |
761 |
761 |
|
762 class XMLConfiguration(HierarchicalConfiguration): |
|
763 """ A XML-based hierarchical configuration. """ |
|
764 |
|
765 def __init__(self, file_): |
|
766 """ Initialization. """ |
|
767 from lxml import etree |
|
768 HierarchicalConfiguration.__init__(self) |
|
769 |
|
770 self._root = etree.parse(file_) |
|