Back to index

nordugrid-arc-nox  1.1.0~rc6
__init__.py
Go to the documentation of this file.
00001 def import_class_from_string(str):
00002     """ Import the class which name is in the string
00003     
00004     import_class_from_string(str)
00005 
00006     'str' is the name of the class in [package.]*module.classname format.
00007     """
00008     module = '.'.join(str.split('.')[:-1]) # cut off class name
00009     cl = __import__(module) # import module
00010     for name in str.split('.')[1:]: # start from the first package name
00011         # step to the next package or the module or the class
00012         cl = getattr(cl, name)
00013     return cl # return the class
00014 
00015 # for XMLNode
00016 
00017 def get_attributes(node):
00018     return dict([(attribute.Name(), str(attribute)) for attribute in [node.Attribute(i) for i in range(node.AttributesSize())]])
00019 
00020 def get_child_nodes(node):
00021     """ Get all the children nodes of an XMLNode
00022     
00023     get_child_nodes(node)
00024     
00025     node is the XMLNode whose children we need
00026     """
00027     # the node.Size() method returns the number of children
00028     return [node.Child(i) for i in range(node.Size())]
00029 
00030 def get_child_values_by_name(node, name):
00031     """ Get the non-empty children nodes with a given name.
00032     
00033     get_child_values_by_name(node, name):
00034     
00035     node is the XMLNode whose children we need
00036     name is the name of the children we need
00037     
00038     e.g.
00039     In [23]: print x.GetXML()
00040     <Service name="pythonservice" id="librarian">
00041         <ClassName>storage.librarian.librarian.LibrarianService</ClassName>
00042         <ISISURL>https://localhost:60000/ISIS1</ISISURL>
00043         <ISISURL>https://localhost:60000/ISIS2</ISISURL>
00044         <ISISURL/>
00045         <ISISURL/>
00046         <ISISURL/>
00047         <HeartbeatTimeout>60</HeartbeatTimeout>
00048         <CheckPeriod>20</CheckPeriod>
00049         <ClientSSLConfig>
00050             <KeyPath>/etc/grid-security/hostkey.pem</KeyPath>
00051             <CertificatePath>/etc/grid-security/hostcert.pem</CertificatePath>
00052             <CACertificatesDir>/etc/grid-security/certificates</CACertificatesDir>
00053         </ClientSSLConfig>
00054     </Service>
00055 
00056     In [24]: get_child_values_by_name(x,'ISISURL')
00057     Out[24]: ['https://localhost:60000/ISIS1', 'https://localhost:60000/ISIS2']    
00058     """
00059     return [str(child) for child in get_child_nodes(node) if child.Name() == name and len(str(child)) > 0]  
00060 
00061 # for DataHandle and DataPoint
00062 
00063 def datapoint_from_url(url_string, ssl_config = None):
00064     import arc
00065     user_config = arc.UserConfig(arc.initializeCredentialsType(arc.initializeCredentialsType.SkipCredentials))
00066     if ssl_config:
00067         if ssl_config.has_key('key_file'): user_config.KeyPath(ssl_config['key_file'])
00068         if ssl_config.has_key('cert_file'): user_config.CertificatePath(ssl_config['cert_file'])
00069         if ssl_config.has_key('proxy_file'): user_config.ProxyPath(ssl_config['proxy_file'])
00070         if ssl_config.has_key('ca_file'): user_config.CACertificatePath(ssl_config['ca_file'])
00071         if ssl_config.has_key('ca_dir'): user_config.CACertificatesDirectory(ssl_config['ca_dir'])
00072     url = arc.URL(url_string)
00073     handle = arc.DataHandle(url, user_config)
00074     point = handle.__ref__()
00075     # possible memory leaks - TODO: investigate
00076     url.thisown = False
00077     user_config.thisown = False
00078     handle.thisown = False
00079     return point
00080 
00081 # for the URL class
00082 
00083 def parse_url(url):
00084     import arc
00085     url = arc.URL(url)
00086     proto = url.Protocol()
00087     host = url.Host()
00088     port = url.Port()
00089     path = url.Path()
00090     return proto, host, int(port), path