Changeset 8a0c67f for fedd


Ignore:
Timestamp:
May 19, 2010 4:16:56 AM (15 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-3.01, version-3.02
Children:
7ee16b3
Parents:
89e2138
Message:

comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/compose.py

    r89e2138 r8a0c67f  
    1616class constraint:
    1717    """
    18     This is just a struct to hold constraint fields, really
     18    This is mainly a struct to hold constraint fields and convert to XML output
    1919    """
    2020    def __init__(self, name=None, required=False, accepts=None, provides=None,
     
    4444def constraints_from_xml(string=None, file=None, filename=None,
    4545        top="constraints"):
     46    """
     47    Pull constraints from an xml file.  Only constraints in the top element are
     48    recognized.  A constraint consists of a constraint element with one name,
     49    one required, and multiple accepts nd provides elements.  Each contains a
     50    string.  A list of constraints is returned.
     51    """
    4652    class parser:
     53        """
     54        A little class to encapsulate some state used to parse the constraints.
     55        The methods are all bound to the analogous handlers in an XML parser.
     56        It collects the constraints in self.constraint.
     57        """
    4758        def __init__(self, top):
    4859            self.top = top
     
    5364       
    5465        def start_element(self, name, attrs):
     66            # Clear any collected strings (from inside elements)
    5567            self.chars = None
    5668            self.key = str(name)
    57 
     69           
     70            # See if we've entered the containing context
    5871            if name == self.top:
    5972                self.in_top = True
    6073
     74            # Entering a constraint, create the object which also acts as a
     75            # flag to indicate we're collecting constraint data.
    6176            if self.in_top:
    6277                if name == 'constraint':
     
    6580        def end_element(self, name):
    6681            if self.current:
     82                # In a constraint and leaving an element.  Clean up any string
     83                # we've collected and process elements we know.
    6784                if self.chars is not None:
    6885                    self.chars = self.chars.strip()
     
    8097                    self.current.provides.append(self.chars)
    8198                elif name == 'constraint':
     99                    # Leaving this constraint.  Record it and clear the flag
    82100                    self.constraints.append(self.current)
    83101                    self.current = None
     
    86104                            "Unexpected element in constraint: %s" % name
    87105            elif name == self.top:
     106                # We've left the containing context
    88107                self.in_top = False
    89108
    90109
    91110        def char_data(self, data):
     111            # Collect strings if we're in the overall context
    92112            if self.in_top:
    93113                if self.chars is None: self.chars = data
    94114                else: self.chars += data
    95115
     116    # Beginning of constraints_from_xml.  Connect up the parser and call it
     117    # properly for the kind of input supplied.
    96118    p = parser(top=top)
    97119    xp = xml.parsers.expat.ParserCreate()
Note: See TracChangeset for help on using the changeset viewer.