コード例 #1
0
ファイル: mage.py プロジェクト: mikerobeson/pycogent
def MageGroupFromString(line):
    """Returns a new MageGroup, created from a string representation"""
    result = MageGroup([],RecessiveOn=False) 
    trans = {'off':('Off',True),\
            'on':('Off',False),\
            'recessiveon':('RecessiveOn',True),\
            'color':'Color',\
            'radius':'Radius',\
            'nobutton':('NoButton',True),\
            'dominant':('Dominant',True),\
            'lens':('Lens',True),
            'master':'Master',\
            'instance':'Instance',\
            'clone':'Clone'}
    
    #extract all delimited fields: label & KeyWordOptions (master, etc)
    delimited_fields = []
    while 1:
        part = extract_delimited(line,'{','}')
        if part is not None:
            delimited_fields.append(part)
            line = line.replace('{'+part+'}','')
        else:
            break
    #the first one is always the label
    label = delimited_fields[0] 
    #the later ones (starting with 1) are keyword options
    field_idx = 1
    #gather all left-over pieces
    pieces = line.split()
    
    if 'sub' in pieces[0]: #@(sub)group
        result.Subgroup = True
    result.Label = label

    #process all optional pieces
    for piece in pieces[1:]:
        try:
            #here we're finding the key. The value will be '', because it
            #is stored in delimited_fields (accesible by field_idx)
            key,value = piece.split('=')
            setattr(result,trans[key],delimited_fields[field_idx])
            field_idx += 1
        except ValueError:
            setattr(result,trans[piece][0],trans[piece][1])
    return result
コード例 #2
0
ファイル: mage.py プロジェクト: mikerobeson/pycogent
def MageListFromString(line):
    """Returns a new MageList, created from a string representation"""
    result = MageList()
    trans = {'off':('Off',True),\
            'on':('Off',False),\
            'color':'Color',\
            'radius':'Radius',\
            'nobutton':('NoButton',True),\
            'angle':'Angle',\
            'width':'Width',\
            'face':'Face',\
            'font':'Font',\
            'size':'Size'}

    line = re.sub('=\s*','=',line)
    label = extract_delimited(line, '{', '}')
    if label is not None:
        pieces = line.replace('{'+label+'}', '').split()
    else:
        pieces = line.split()
   
    style = pieces[0].strip('@')[:-4] #take off the 'list' part
    if style in MageList.KnownStyles:
        result.Style = style
    else:
        raise ValueError,"Unknown style: %s"%(style)
    result.Label = label 
    
    #process all optional pieces
    for piece in pieces[1:]:
        try:
            key,value = [item.strip() for item in piece.split('=')]
            key = trans[key]
        except ValueError: #unpack list of wrong size
            key,value = trans[piece][0],trans[piece][1]
        #KeyError will be raised in case of an unkown key
        setattr(result,key,value)
    return result
コード例 #3
0
ファイル: mage.py プロジェクト: mikerobeson/pycogent
def MagePointFromString(line):
    """Constructs a new MagePoint from a one-line string."""
    #handle the label if there is one: note that it might contain spaces
    line = line.strip()
    result = MagePoint()
    label = extract_delimited(line, '{', '}')
    if label:
        pieces = line.replace('{'+label+'}', '').split()
    else:
        pieces = line.split()
    fields = []
    #also have to take into account the possibility of comma-delimited parts
    for p in pieces:
        fields.extend(filter(None, p.split(',')))
    pieces = fields
    result.Label = label
    #get the coordinates and remove them from the list of items
    result.Coordinates = map(float, pieces[-3:])
    pieces = pieces[:-3]
    #parse the remaining attributes in more detail
    result.State = None
    result.Width = None
    result.Radius = None
    result.Color = None
    for attr in pieces:
        #handle radius
        if attr.startswith('r='):  #radius: note case sensitivity
            result.Radius = float(attr[2:])
        #handle single-character attributes
        elif len(attr) == 1:
            result.State = attr
        #handle line width
        elif attr.startswith('width'):
            result.Width = int(attr[5:])
        else:
            #otherwise assume it's a color label
            result.Color = attr
    return result