def __init__(self, dict=None, xml=None): if dict: self.load_from_dict(dict) elif xml: xml_record = XML(xml) xml_dict = xml_record.todict() self.load_from_dict(xml_dict)
def main(): parser = create_parser(); (options, args) = parser.parse_args() stdin = sys.stdin.read() record = XML(stdin) record_dict = record.todict() if options.DEBUG: pprint(record.toxml()) print "#####################################################" printRec(record_dict, args, options)
def main(): parser = create_parser() (options, args) = parser.parse_args() record = XML(sys.stdin.read()) record_dict = record.todict() if args: editDict(args, record_dict, options) if options.DEBUG: print "New Record:\n%s" % record_dict record.parse_dict(record_dict) s = record.toxml() sys.stdout.write(s)
def load_xml(self, filename): xml = XML(filename) categories = xml.xpath('//configuration/variables/category') for category in categories: section_name = category.get('id') if not self.config.has_section(section_name): self.config.add_section(section_name) options = category.xpath('./variablelist/variable') for option in options: option_name = option.get('id') value = option.xpath('./value')[0].text if not value: value = "" self.config.set(section_name, option_name, value)
def load(self): """ Parse an xml file and store it as a dict """ if os.path.exists(self.db_filename) and os.path.isfile( self.db_filename): xml = XML(self.db_filename) dict.__init__(self, xml.todict()) elif os.path.exists( self.db_filename) and not os.path.isfile(self.db_filename): raise IOError, '%s exists but is not a file. please remove it and try again' \ % self.db_filename else: self.write() self.load()
def __init__(self, conf_file): dict.__init__(self, {}) # load config file required_fields = set(self.default_fields.keys()) self.interface_info = XML(conf_file).todict() for value in self.interface_info.values(): if isinstance(value, list): for record in value: if isinstance(record, dict) and \ required_fields.issubset(record.keys()): hrn, address, port = record['hrn'], record['addr'], record['port'] # sometime this is called at a very early stage with no config loaded # avoid to remember this instance in such a case if not address or not port: continue interface = Interface(hrn, address, port) self[hrn] = interface
def __init__(self, rspec="", version=None, user_options={}): self.header = '<?xml version="1.0"?>\n' self.template = """<RSpec></RSpec>""" self.version = None self.xml = XML() self.version_manager = VersionManager() self.user_options = user_options self.elements = {} if rspec: if version: self.version = self.version_manager.get_version(version) self.parse_xml(rspec, version) else: self.parse_xml(rspec) elif version: self.create(version) else: raise InvalidRSpec("No RSpec or version specified. Must specify a valid rspec string or a valid version")
def write(self): xml = XML() xml.parseDict(self) db_file = open(self.db_filename, 'w') db_file.write(data.toprettyxml()) db_file.close()
def make_record_xml(xml): xml_record = XML(xml) xml_dict = xml_record.todict() logger.info("load from xml, keys=%s" % xml_dict.keys()) return make_record_dict(xml_dict)
def save_as_xml (self): # xxx not sure about the scope here input_dict = dict( [ (key, getattr(self,key)) for key in self.fields() if getattr(self,key,None) ] ) xml_record=XML("<record />") xml_record.parse_dict (input_dict) return xml_record.toxml()
def load(self, source): self.xml = XML(source)
def create(self): self.xml = XML(string=ApiVersions.template)