Beispiel #1
0
 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)  
Beispiel #2
0
 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)  
Beispiel #3
0
 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()
Beispiel #4
0
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)
Beispiel #5
0
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)
Beispiel #6
0
 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)
Beispiel #7
0
 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)
Beispiel #8
0
 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()
Beispiel #9
0
 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   
Beispiel #10
0
 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") 
Beispiel #11
0
 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")
Beispiel #12
0
class ApiVersions:

    required_fields = ['version', 'url']
    
    template = """<api_versions>
<api_version name="" version="" url="" />
</api_versions>""" 

    def __init__(self, string=None, filename=None, create=False):
        self.xml = None

        if create:
            self.create()
        elif string:
            self.load(string)
        elif filename:
            self.load(filename)
        else:
            # load the default file
            c = Config()
            api_versions_file = os.path.sep.join([c.config_path, 'api_versions.xml'])
            self.load(api_versions_file)
        
    def create(self):
        self.xml = XML(string=ApiVersions.template)

    def load(self, source):
        self.xml = XML(source)

    def get_versions(self):
        versions = {}
        for value in self.xml.todict().values():
            if not value:
                continue
            if isinstance(value, list):
                for item in value:
                    if isinstance(item, dict) and \
                       set(ApiVersions.required_fields).issubset(item.keys()) and \
                       item['version'] != '' and item['url'] != '':
                        versions[str(item['version'])] = item['url']
        return versions  
                
           
Beispiel #13
0
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)
Beispiel #14
0
class ApiVersions:

    required_fields = ['version', 'url']
    
    template = """<api_versions>
<api_version name="" version="" url="" />
</api_versions>""" 

    def __init__(self, string=None, filename=None, create=False):
        self.xml = None

        if create:
            self.create()
        elif string:
            self.load(string)
        elif filename:
            self.load(filename)
        else:
            # load the default file
            c = Config()
            api_versions_file = os.path.sep.join([c.config_path, 'api_versions.xml'])
            self.load(api_versions_file)
        
    def create(self):
        self.xml = XML(string=ApiVersions.template)

    def load(self, source):
        self.xml = XML(source)

    def get_versions(self):
        versions = {}
        for value in self.xml.todict().values():
            if not value:
                continue
            if isinstance(value, list):
                for item in value:
                    if isinstance(item, dict) and \
                       set(ApiVersions.required_fields).issubset(item.keys()) and \
                       item['version'] != '' and item['url'] != '':
                        versions[str(item['version'])] = item['url']
        return versions  
Beispiel #15
0
class Interfaces(dict):
    """
    Interfaces is a base class for managing information on the
    peers we are federated with. Provides connections (xmlrpc or soap) to federated peers
    """

    # fields that must be specified in the config file
    default_fields = {
        'hrn': '',
        'addr': '', 
        'port': '', 
    }

    # defined by the class 
    default_dict = {}

    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 server_proxy(self, hrn, key_file, cert_file, timeout=30):
        return self[hrn].server_proxy(key_file, cert_file, timeout)
Beispiel #16
0
 def create(self):
     self.xml = XML(string=ApiVersions.template)
Beispiel #17
0
class RSpec:
    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 create(self, version=None):
        """
        Create root element
        """
        self.version = self.version_manager.get_version(version)
        self.namespaces = self.version.namespaces
        self.parse_xml(self.version.template, self.version)
        # eg. 2011-03-23T19:53:28Z
        date_format = "%Y-%m-%dT%H:%M:%SZ"
        now = datetime.utcnow()
        generated_ts = now.strftime(date_format)
        expires_ts = (now + timedelta(hours=1)).strftime(date_format)
        self.xml.set("expires", expires_ts)
        self.xml.set("generated", generated_ts)

    def parse_xml(self, xml, version=None):
        self.xml.parse_xml(xml)
        if not version:
            if self.xml.schema:
                self.version = self.version_manager.get_version_by_schema(self.xml.schema)
            else:
                # raise InvalidRSpec('unknown rspec schema: %s' % schema)
                # TODO: Should start raising an exception once SFA defines a schema.
                # for now we just  default to sfa
                self.version = self.version_manager.get_version({"type": "sfa", "version": "1"})
        self.version.xml = self.xml
        self.namespaces = self.xml.namespaces

    def load_rspec_elements(self, rspec_elements):
        self.elements = {}
        for rspec_element in rspec_elements:
            if isinstance(rspec_element, RSpecElement):
                self.elements[rspec_element.type] = rspec_element

    def register_rspec_element(self, element_type, element_name, element_path):
        if element_type not in RSpecElements:
            raise InvalidRSpecElement(
                element_type, extra="no such element type: %s. Must specify a valid RSpecElement" % element_type
            )
        self.elements[element_type] = RSpecElement(element_type, element_name, element_path)

    def get_rspec_element(self, element_type):
        if element_type not in self.elements:
            msg = "ElementType %s not registerd for this rspec" % element_type
            raise InvalidRSpecElement(element_type, extra=msg)
        return self.elements[element_type]

    def get(self, element_type, filter={}, depth=0):
        elements = self.get_elements(element_type, filter)
        elements = [self.xml.get_element_attributes(elem, depth=depth) for elem in elements]
        return elements

    def get_elements(self, element_type, filter={}):
        """
        search for a registered element
        """
        if element_type not in self.elements:
            msg = "Unable to search for element %s in rspec, expath expression not found." % element_type
            raise InvalidRSpecElement(element_type, extra=msg)
        rspec_element = self.get_rspec_element(element_type)
        xpath = rspec_element.path + XpathFilter.xpath(filter)
        return self.xml.xpath(xpath)

    def merge(self, in_rspec):
        self.version.merge(in_rspec)

    def filter(self, filter):
        if "component_manager_id" in filter:
            nodes = self.version.get_node_elements()
            for node in nodes:
                if (
                    "component_manager_id" not in node.attrib
                    or node.attrib["component_manager_id"] != filter["component_manager_id"]
                ):
                    parent = node.getparent()
                    parent.remove(node)

    def toxml(self, header=True):
        if header:
            return self.header + self.xml.toxml()
        else:
            return self.xml.toxml()

    def save(self, filename):
        return self.xml.save(filename)
Beispiel #18
0
 def write(self):
     xml = XML()
     xml.parseDict(self)
     db_file = open(self.db_filename, 'w')
     db_file.write(data.toprettyxml())
     db_file.close()
Beispiel #19
0
 def write(self):
     xml = XML()
     xml.parseDict(self)
     db_file = open(self.db_filename, 'w')
     db_file.write(data.toprettyxml())
     db_file.close()
Beispiel #20
0
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)
Beispiel #21
0
class RSpec:
 
    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 create(self, version=None):
        """
        Create root element
        """
        self.version = self.version_manager.get_version(version)
        self.namespaces = self.version.namespaces
        self.parse_xml(self.version.template, self.version) 
        # eg. 2011-03-23T19:53:28Z 
        date_format = '%Y-%m-%dT%H:%M:%SZ'
        now = datetime.utcnow()
        generated_ts = now.strftime(date_format)
        expires_ts = (now + timedelta(hours=1)).strftime(date_format) 
        self.xml.set('expires', expires_ts)
        self.xml.set('generated', generated_ts)

    def parse_xml(self, xml, version=None):
        self.xml.parse_xml(xml)
        if not version:
            if self.xml.schema:
                self.version = self.version_manager.get_version_by_schema(self.xml.schema)
            else:
                #raise InvalidRSpec('unknown rspec schema: %s' % schema)
                # TODO: Should start raising an exception once SFA defines a schema.
                # for now we just  default to sfa 
                self.version = self.version_manager.get_version({'type':'sfa','version': '1'})
        self.version.xml = self.xml    
        self.namespaces = self.xml.namespaces
    
    def load_rspec_elements(self, rspec_elements):
        self.elements = {}
        for rspec_element in rspec_elements:
            if isinstance(rspec_element, RSpecElement):
                self.elements[rspec_element.type] = rspec_element

    def register_rspec_element(self, element_type, element_name, element_path):
        if element_type not in RSpecElements:
            raise InvalidRSpecElement(element_type, extra="no such element type: %s. Must specify a valid RSpecElement" % element_type)
        self.elements[element_type] = RSpecElement(element_type, element_name, element_path)

    def get_rspec_element(self, element_type):
        if element_type not in self.elements:
            msg = "ElementType %s not registerd for this rspec" % element_type
            raise InvalidRSpecElement(element_type, extra=msg)
        return self.elements[element_type]

    def get(self, element_type, filter={}, depth=0):
        elements = self.get_elements(element_type, filter)
        elements = [self.xml.get_element_attributes(elem, depth=depth) for elem in elements]
        return elements

    def get_elements(self, element_type, filter={}):
        """
        search for a registered element
        """
        if element_type not in self.elements:
            msg = "Unable to search for element %s in rspec, expath expression not found." % \
                   element_type
            raise InvalidRSpecElement(element_type, extra=msg)
        rspec_element = self.get_rspec_element(element_type)
        xpath = rspec_element.path + XpathFilter.xpath(filter)
        return self.xml.xpath(xpath)

    def merge(self, in_rspec):
        self.version.merge(in_rspec)

    def filter(self, filter):
        if 'component_manager_id' in filter:    
            nodes = self.version.get_nodes()
            for node in nodes:
                if 'component_manager_id' not in node.attrib or \
                  node.attrib['component_manager_id'] != filter['component_manager_id']:
                    parent = node.getparent()
                    parent.remove(node.element) 
        

    def toxml(self, header=True):
        if header:
            return self.header + self.xml.toxml()
        else:
            return self.xml.toxml()
    

    def save(self, filename):
        return self.xml.save(filename)
Beispiel #22
0
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)
Beispiel #23
0
 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()
Beispiel #24
0
 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()
Beispiel #25
0
class RSpec:
 
    def __init__(self, rspec="", version=None, user_options=None, ttl=None, expires=None):
        if user_options is 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.ttl = ttl
        self.expires = expires
        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, ttl, expires)
        else:
            raise InvalidRSpec("No RSpec or version specified. Must specify a valid rspec string or a valid version") 

    def create(self, version=None, ttl=None, expires=None):
        """
        Create root element
        ttl: time to live in minutes, this will determine the expires tag of the RSpec
        """
        self.version = self.version_manager.get_version(version)
        self.namespaces = self.version.namespaces
        self.parse_xml(self.version.template, self.version) 
        now = datetime.utcnow()
        generated_ts = now.strftime(SFATIME_FORMAT)
        if ttl is None:
            ttl = 60
        if expires is None:
            expires_ts = (now + timedelta(minutes=ttl)).strftime(SFATIME_FORMAT)
        else:
            if isinstance(expires,int):
                expires_date = datetime.fromtimestamp(expires)
            else:
                expires_date = expires
            expires_ts = expires_date.strftime(SFATIME_FORMAT)
        self.xml.set('expires', expires_ts)
        self.xml.set('generated', generated_ts)

    def parse_xml(self, xml, version=None):
        self.xml.parse_xml(xml)
        if not version:
            if self.xml.schema:
                self.version = self.version_manager.get_version_by_schema(self.xml.schema)
            else:
                #raise InvalidRSpec('unknown rspec schema: %s' % schema)
                # TODO: Should start raising an exception once SFA defines a schema.
                # for now we just  default to sfa 
                self.version = self.version_manager.get_version({'type':'sfa','version': '1'})
        self.version.xml = self.xml    
        self.namespaces = self.xml.namespaces
    
    def load_rspec_elements(self, rspec_elements):
        self.elements = {}
        for rspec_element in rspec_elements:
            if isinstance(rspec_element, RSpecElement):
                self.elements[rspec_element.type] = rspec_element

    def register_rspec_element(self, element_type, element_name, element_path):
        if element_type not in RSpecElements:
            raise InvalidRSpecElement(element_type, extra="no such element type: %s. Must specify a valid RSpecElement" % element_type)
        self.elements[element_type] = RSpecElement(element_type, element_name, element_path)

    def get_rspec_element(self, element_type):
        if element_type not in self.elements:
            msg = "ElementType %s not registerd for this rspec" % element_type
            raise InvalidRSpecElement(element_type, extra=msg)
        return self.elements[element_type]

    def get(self, element_type, filter=None, depth=0):
        if filter is None: filter={}
        elements = self.get_elements(element_type, filter)
        elements = [self.xml.get_element_attributes(elem, depth=depth) for elem in elements]
        return elements

    def get_elements(self, element_type, filter=None):
        """
        search for a registered element
        """
        if filter is None: filter={}
        if element_type not in self.elements:
            msg = "Unable to search for element %s in rspec, expath expression not found." % \
                   element_type
            raise InvalidRSpecElement(element_type, extra=msg)
        rspec_element = self.get_rspec_element(element_type)
        xpath = rspec_element.path + XpathFilter.xpath(filter)
        return self.xml.xpath(xpath)

    def merge(self, in_rspec):
        self.version.merge(in_rspec)

    def filter(self, filter):
        if 'component_manager_id' in filter:    
            nodes = self.version.get_nodes()
            for node in nodes:
                if 'component_manager_id' not in node.attrib or \
                  node.attrib['component_manager_id'] != filter['component_manager_id']:
                    parent = node.getparent()
                    parent.remove(node.element) 
        

    def toxml(self, header=True):
        if header:
            return self.header + self.xml.toxml()
        else:
            return self.xml.toxml()
    

    def save(self, filename):
        return self.xml.save(filename)
Beispiel #26
0
 def load(self, source):
     self.xml = XML(source)