def _get_rules(cls, version):
    """Initializes the _rule_set for the class which is used when parsing XML.

    This method is used internally for parsing and generating XML for an
    XmlElement. It is not recommended that you call this method directly.
    
    Returns:
      A tuple containing the XML parsing rules for the appropriate version.

      The tuple looks like: 
      (qname, {sub_element_qname: (member_name, member_class, repeating), ..},
       {attribute_qname: member_name})

      To give a couple of concrete example, the atom.data.Control _get_rules
      with version of 2 will return:
      ('{http://www.w3.org/2007/app}control',
       {'{http://www.w3.org/2007/app}draft': ('draft',
                                              <class 'atom.data.Draft'>,
                                              False)}, 
       {})
      Calling _get_rules with version 1 on gdata.data.FeedLink will produce:
      ('{http://schemas.google.com/g/2005}feedLink',
       {'{http://www.w3.org/2005/Atom}feed': ('feed',
                                              <class 'gdata.data.GDFeed'>,
                                              False)},
       {'href': 'href', 'readOnly': 'read_only', 'countHint': 'count_hint',
        'rel': 'rel'})
    """
    # Initialize the _rule_set to make sure there is a slot available to store
    # the parsing rules for this version of the XML schema.
    # Look for rule set in the class __dict__ proxy so that only the 
    # _rule_set for this class will be found. By using the dict proxy
    # we avoid finding rule_sets defined in superclasses.
    # The four lines below provide support for any number of versions, but it
    # runs a bit slower then hard coding slots for two versions, so I'm using
    # the below two lines.
    #if '_rule_set' not in cls.__dict__ or cls._rule_set is None:
    #  cls._rule_set = []
    #while len(cls.__dict__['_rule_set']) < version:
    #  cls._rule_set.append(None)
    # If there is no rule set cache in the class, provide slots for two XML
    # versions. If and when there is a version 3, this list will need to be
    # expanded.
    if '_rule_set' not in cls.__dict__ or cls._rule_set is None:
      cls._rule_set = [None, None]
    # If a version higher than 2 is requested, fall back to version 2 because
    # 2 is currently the highest supported version.
    if version > 2:
      return cls._get_rules(2)
    # Check the dict proxy for the rule set to avoid finding any rule sets 
    # which belong to the superclass. We only want rule sets for this class.
    if cls._rule_set[version-1] is None:
      # The rule set for each version consists of the qname for this element 
      # ('{namespace}tag'), a dictionary (elements) for looking up the 
      # corresponding class member when given a child element's qname, and a 
      # dictionary (attributes) for looking up the corresponding class member
      # when given an XML attribute's qname.
      elements = {}
      attributes = {}
      if ('_members' not in cls.__dict__ or cls._members is None):
        cls._members = tuple(cls._list_xml_members())
      for member_name, target in cls._members:
        if isinstance(target, list):
          # This member points to a repeating element.
          elements[_get_qname(target[0], version)] = (member_name, target[0], 
              True)
        elif isinstance(target, tuple):
          # This member points to a versioned XML attribute.
          if version <= len(target):
            attributes[target[version-1]] = member_name
          else:
            attributes[target[-1]] = member_name
        elif isinstance(target, (str, unicode)):
          # This member points to an XML attribute.
          attributes[target] = member_name
        elif issubclass(target, XmlElement):
          # This member points to a single occurance element.
          elements[_get_qname(target, version)] = (member_name, target, False)
      version_rules = (_get_qname(cls, version), elements, attributes)
      cls._rule_set[version-1] = version_rules
      return version_rules
    else:
      return cls._rule_set[version-1]
dataset/ETHPy150Open CollabQ/CollabQ/vendor/atom/core.py/XmlElement._get_rules/original