Ignore:
Timestamp:
Jun 3, 2014 4:29:31 PM (10 years ago)
Author:
Ted Faber <faber@…>
Branches:
master
Children:
0b217d1
Parents:
ba07149
Message:

Service info in xml files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/federation/client_lib.py

    rba07149 rc259a77  
    44import pwd
    55import os
     6import re
    67import os.path
     8import xml.parsers.expat
    79
    810from string import join
    911from datetime import datetime
    10 
    1112
    1213from deter import fedid
     
    137138    return rv
    138139
     140ns_service_re = re.compile('^\\s*#\\s*SERVICE:\\s*([\\S]+)')
     141xml_service_re = re.compile('SERVICE:\\s*([\\S]+)\\s*$')
     142
     143def parse_service(svc):
     144    """
     145    Pasre a service entry into a hash representing a service entry in a
     146    message.  The format is:
     147        svc_name:exporter(s):importer(s):attr=val,attr=val
     148    The svc_name is the service name, exporter is the exporting testbeds
     149    (comma-separated) importer is the importing testbeds (if any) and the rest
     150    are attr=val pairs that will become attributes of the service.  These
     151    include parameters and other such stuff.
     152    """
     153
     154    terms = svc.split(':')
     155    svcd = { }
     156    if len(terms) < 2 or len(terms[0]) == 0 or len(terms[1]) == 0:
     157        sys.exit("Bad service description '%s': Not enough terms" % svc)
     158
     159    svcd['name'] = terms[0]
     160    svcd['export'] = terms[1].split(",")
     161    if len(terms) > 2 and len(terms[2]) > 0:
     162        svcd['import'] = terms[2].split(",")
     163    if len(terms) > 3 and len(terms[3]) > 0:
     164        svcd['fedAttr'] = [ ]
     165        for t in terms[3].split(";"):
     166            i = t.find("=")
     167            if i != -1 :
     168                svcd['fedAttr'].append(
     169                        {'attribute': t[0:i], 'value': t[i+1:]})
     170            else:
     171                sys.exit("Bad service attribute '%s': no equals sign" % t)
     172    return svcd
     173
     174def service_dict_to_line(d):
     175    """
     176    Convert a dict containing a service description into a colon separated
     177    service description suitable for inclusion in a file or command line.
     178    """
     179
     180    return 'SERVICE: %s' % (
     181            ':'.join([
     182                d.get('name', ''),
     183                ','.join(d.get('export','')),
     184                ','.join(d.get('import','')),
     185                ','.join([
     186                    '='.join((dd.get('attribute', ''), dd.get('value',''))) \
     187                        for dd in d.get('fedAttr', [])
     188                ])
     189            ]))
     190
     191def extract_services_from_xml(string=None, file=None, filename=None):
     192    class parser:
     193        def __init__(self):
     194            self.svcs = []
     195        def comment_handler(self, data):
     196            for l in data.split('\n'):
     197                if xml_service_re.match(l):
     198                    self.svcs.append(
     199                            parse_service(xml_service_re.match(l).group(1)))
     200
     201    p = parser()
     202    xp = xml.parsers.expat.ParserCreate()
     203
     204    xp.CommentHandler = p.comment_handler
     205
     206    num_set = len([ x for x in (string, filename, file)\
     207            if x is not None ])
     208
     209    if num_set != 1:
     210        raise RuntimeError("Exactly one one of file, filename and string " + \
     211                "must be set")
     212    elif filename:
     213        f = open(filename, "r")
     214        xp.ParseFile(f)
     215        f.close()
     216    elif file:
     217        xp.ParseFile(file)
     218    elif string:
     219        xp.Parse(string, True)
     220
     221    return p.svcs
     222
    139223def wrangle_standard_options(opts):
    140224    """
Note: See TracChangeset for help on using the changeset viewer.