Beispiel #1
0
    def _make_person_name(self, name_element):
        """Make a list out of <name>. Names are type+value in
        Cerebrum."""

        result = []
        for sub in name_element.getiterator():
            if not sub.text:
                continue
            value = sub.text.strip().encode("latin1")
            for tag in ("fn", "sort", "nickname"):
                if not tag == sub.tag:
                    continue
                if value:
                    result.append((ABCTypes.get_name_type(tag), value))
            if sub.tag == "n":
                for n in sub.getiterator():
                    if not n.text:
                        continue
                    value = n.text.strip().encode("latin1")
                    for tag in ("family", "given", "other", "prefix",
                                "suffix"):
                        if not tag == n.tag:
                            continue
                        if value:
                            result.append((ABCTypes.get_name_type(tag), value))
                        if n.tag in ("partname", ):
                            if len(n.attrib) != 1:
                                raise ABCTypesError, "error in partname: %s" % value
                            type = ABCTypes.get_type(
                                "partname", (n.attrib.get("partnametype"), ))
                            if value:
                                result.append((type, value))
        return result
Beispiel #2
0
    def _make_person_name(name_element):
        """Make a list out of <name>. Names are type+value in
        Cerebrum."""

        result = []
        for sub in name_element.getiterator():
            if not sub.text:
                continue
            value = sub.text.strip()
            for tag in ("fn", "sort", "nickname"):
                if not tag == sub.tag:
                    continue
                if value:
                    result.append((ABCTypes.get_name_type(tag), value))
            if sub.tag == "n":
                for n in sub.getiterator():
                    if not n.text:
                        continue
                    value = n.text.strip()
                    for tag in ("family", "given", "other",
                                "prefix", "suffix"):
                        if not tag == n.tag:
                            continue
                        if value:
                            result.append((ABCTypes.get_name_type(tag), value))
                        if n.tag in ("partname",):
                            if len(n.attrib) != 1:
                                raise ABCTypesError(
                                    "error in partname: {}".format(value))
                            _type = ABCTypes.get_type(
                                "partname",
                                (n.attrib.get("partnametype"),))
                            if value:
                                result.append((_type, value))
        return result
Beispiel #3
0
    def next(self):
        """Return the next DataOU object. Returns a ABCTypesError
        exception if object has errors in types. These objects should be
        skipped.

        Consume the next XML-element describing an organization, and
        return a suitable representation (DataOU).
        """

        # This call with propagate StopIteration when all the (XML) elements
        # are exhausted.
        element = super(XMLOrg2Object, self).next()
        result = DataOU()

        # Iterate over *all* subelements
        for sub in element:
            value = None
            if sub.text:
                value = sub.text.strip().encode("latin1")
            if sub.tag == "orgid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "wrong number of arguments: %s" % value
                type = sub.attrib.get("orgidtype")
                result.add_id(ABCTypes.get_type("orgidtype", (type, )), value)
            elif sub.tag == "orgname":
                if len(sub.attrib) != 2:
                    raise ABCTypesError, "not 2 attributes: %s" % value
                type = sub.attrib.get("orgnametype")
                # TODO: support lang
                lang = sub.attrib.get("lang")
                result.add_name(ABCTypes.get_type("orgnametype", (type, )),
                                value)
            elif sub.tag == "realm":
                result.realm = value
            elif sub.tag == "address":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in address: %s" % value
                type = sub.attrib.get("addresstype")
                addr_type = ABCTypes.get_type("addresstype",
                                              ("organization", type))
                result.add_address(addr_type, self._make_address(sub))
            elif sub.tag == "contactinfo":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in contact: %s" % value
                if not sub.text:
                    continue
                type = sub.attrib.get("contacttype")
                result.add_contact(
                    ABCTypes.get_type("contacttype", (
                        "organization",
                        type,
                    )), value)
            elif sub.tag == "ou" and result.ou is None:
                # Rather tricky. We have to represent the trailing OUs with
                # something. Returning an iterator seems viable.
                result.ou = ABCFactory.get('OUParser')(iter(
                    element.getiterator("ou")))

        return result
Beispiel #4
0
    def next(self):
        """Return the next DataOU object. Returns a ABCTypesError
        exception if object has errors in types. These objects should be
        skipped.

        Consume the next XML-element describing an organization, and
        return a suitable representation (DataOU).
        """

        # This call with propagate StopIteration when all the (XML) elements
        # are exhausted.
        element = super(XMLOrg2Object, self).next()
        result = DataOU()

        # Iterate over *all* subelements
        for sub in element:
            value = None
            if sub.text:
                value = sub.text.strip()
            if sub.tag == "orgid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError(
                        "wrong number of arguments: {}".format(value))
                _type = sub.attrib.get("orgidtype")
                result.add_id(ABCTypes.get_type("orgidtype", (_type,)),
                              value)
            elif sub.tag == "orgname":
                if len(sub.attrib) != 2:
                    raise ABCTypesError("not 2 attributes: {}".format(value))
                _type = sub.attrib.get("orgnametype")
                result.add_name(ABCTypes.get_type("orgnametype", (_type,)),
                                value)
            elif sub.tag == "realm":
                result.realm = value
            elif sub.tag == "address":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in address: {}".format(value))
                _type = sub.attrib.get("addresstype")
                addr_type = ABCTypes.get_type("addresstype",
                                              ("organization", _type))
                result.add_address(addr_type, self._make_address(sub))
            elif sub.tag == "contactinfo":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in contact: {}".format(value))
                if not sub.text:
                    continue
                _type = sub.attrib.get("contacttype")
                result.add_contact(ABCTypes.get_type("contacttype",
                                                     ("organization", _type,)),
                                   value)
            elif sub.tag == "ou" and result.ou is None:
                # Rather tricky. We have to represent the trailing OUs with
                # something. Returning an iterator seems viable.
                result.ou = ABCFactory.get('OUParser')(
                    iter(element.getiterator("ou")))

        return result
 def _get_subvalues(_iter):
     value = None
     result = []
     for s in _iter:
         if s.text:
             value = s.text.strip()
         if s.tag == "personid":
             if len(s.attrib) != 1:
                 raise ABCTypesError("error in personid: {}".format(value))
             _type = s.attrib.get("personidtype")
             result.append(("person",
                            ABCTypes.get_type("personidtype",
                                              (_type,)),
                            value))
         elif s.tag == "groupid":
             if len(s.attrib) != 1:
                 raise ABCTypesError("error in groupid: {}".format(value))
             _type = s.attrib.get("groupidtype")
             result.append(("group", ABCTypes.get_type("groupidtype",
                                                       (_type,)),
                            value))
         elif s.tag == "org":
             org = ou = None
             for o in s.getiterator():
                 if o.text:
                     value = o.text.strip()
                 if o.tag == "orgid":
                     if len(o.attrib) != 1:
                         raise ABCTypesError("error in org: {}"
                                             .format(value))
                     _type = o.attrib.get("orgidtype")
                     org = (ABCTypes.get_type("orgidtype",
                                              (_type,)),
                            value)
                 elif o.tag == "ouid":
                     if len(o.attrib) != 1:
                         raise ABCTypesError("error in ouid: {}"
                                             .format(value))
                     _type = o.attrib.get("ouidtype")
                     ou = (ABCTypes.get_type("ouidtype",
                                             (_type,)),
                           value)
             # Org is required
             if not org:
                 # raise ABCTypesError, "no org"
                 # TODO: enable again.
                 pass
             if org and ou:
                 result.append(("org", org, ou))
             elif org:
                 result.append(("org", org))
             else:
                 result.append(("ou", ou))
     return result
Beispiel #6
0
    def next(self):
        """Return the next DataGroup object. Returns a ABCTypesError
        exception if object has errors in types. These objects should be
        skipped.

        Consume the next XML-element describing an OU, and return a
        suitable representation (DataGroup).
        """

        # This call with propagate StopIteration when all the (XML) elements
        # are exhausted.
        element = super(XMLGroup2Object, self).next()
        result = DataGroup()

        # Iterate over *all* subelements
        for sub in element.getiterator():
            value = None
            if sub.text:
                value = sub.text.strip().encode("latin1")

            if sub.tag == "groupid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in groupid: %s" % value
                type = sub.attrib.get("groupidtype")
                result.add_id(ABCTypes.get_type("groupidtype", (type, )),
                              value)
            elif sub.tag == "tag":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in tag: %s" % value
                type = sub.attrib.get("tagtype")
                result.add_tag(ABCTypes.get_type("tagtype", ("group", type)),
                               value)
            elif sub.tag == "description":
                result.desc = value

        # NB! This is crucial to save memory on XML elements
        element.clear()
        return result
Beispiel #7
0
    def next(self):
        """Return the next DataGroup object. Returns a ABCTypesError
        exception if object has errors in types. These objects should be
        skipped.

        Consume the next XML-element describing an OU, and return a
        suitable representation (DataGroup).
        """

        # This call with propagate StopIteration when all the (XML) elements
        # are exhausted.
        element = super(XMLGroup2Object, self).next()
        result = DataGroup()

        # Iterate over *all* subelements
        for sub in element.getiterator():
            value = None
            if sub.text:
                value = sub.text.strip()

            if sub.tag == "groupid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in groupid: {}".format(value))
                _type = sub.attrib.get("groupidtype")
                result.add_id(ABCTypes.get_type("groupidtype", (_type,)),
                              value)
            elif sub.tag == "tag":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in tag: {}".format(value))
                _type = sub.attrib.get("tagtype")
                result.add_tag(ABCTypes.get_type("tagtype", ("group", _type)),
                               value)
            elif sub.tag == "description":
                result.desc = value

        # NB! This is crucial to save memory on XML elements
        element.clear()
        return result
Beispiel #8
0
 def _check_type(self, type, args):
     # Should throw exception if anything is fishy
     ABCTypes.get_type(type, args)
Beispiel #9
0
    def next(self):
        """Return the next DataPerson object. Returns a ABCTypesError
        exception if object has errors in types. These objects should be
        skipped.

        Consume the next XML-element describing a person, and return a
        suitable representation (DataPerson).
        """

        # This call with propagate StopIteration when all the (XML) elements
        # are exhausted.
        element = super(XMLPerson2Object, self).next()
        result = DataPerson()

        # Iterate over *all* subelements
        for sub in element.getiterator():
            value = None
            if sub.text:
                value = sub.text.strip().encode("latin1")

            if sub.tag == "personid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in personid: %s" % value
                type = sub.attrib.get("personidtype")
                result.add_id(ABCTypes.get_type("personidtype", (type, )),
                              value)
            elif sub.tag == "tag":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in tag: %s" % value
                type = sub.attrib.get("tagtype")
                result.add_tag(ABCTypes.get_type("tagtype", ("person", type)),
                               value)
            elif sub.tag == "name":
                for t, v in self._make_person_name(sub):
                    result.add_name(t, v)
            elif sub.tag == "birthdate":
                # if len == 6 we asume ddmmyy
                if value == None:
                    result.birth_date = None
                else:
                    if len(value) == 6:
                        value = "19%s-%s-%s" % (value[4:6], value[2:4],
                                                value[0:2])
                    result.birth_date = self._make_mxdate(value)
            elif sub.tag == "gender":
                result.gender = value
            elif sub.tag == "address":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in address: %s" % value
                type = sub.attrib.get("addresstype")
                addr_type = ABCTypes.get_type("addresstype", ("person", type))
                result.add_address(addr_type, self._make_address(sub))
            elif sub.tag == "contactinfo":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in contactinfo: %s" % value
                if not sub.text:
                    continue
                type = sub.attrib.get("contacttype")
                result.add_contact(
                    ABCTypes.get_type("contacttype", (
                        "person",
                        type,
                    )), value)

        # NB! This is crucial to save memory on XML elements
        element.clear()
        return result
Beispiel #10
0
    def next(self):
        """Return the next DataOU object. Returns a ABCTypesError
        exception if object has errors in types. These objects should be
        skipped.

        Consume the next XML-element describing an OU, and return a
        suitable representation (DataOU).
        """

        # This call with propagate StopIteration when all the (XML) elements
        # are exhausted.
        element = super(XMLOU2Object, self).next()
        result = DataOU()

        # Iterate over *all* subelements
        for sub in element.getiterator():
            value = None
            if sub.text:
                value = sub.text.strip().encode("latin1")

            if sub.tag == "ouid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in ouid: %s" % value
                type = sub.attrib.get("ouidtype")
                result.add_id(ABCTypes.get_type("ouidtype", (type, )), value)
            elif sub.tag == "tag":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in tag: %s" % value
                type = sub.attrib.get("tagtype")
                result.add_tag(ABCTypes.get_type("tagtype", ("ou", type)),
                               value)
            elif sub.tag == "ouname":
                if len(sub.attrib) != 2:
                    raise ABCTypesError, "error in ouname: %s" % value
                type = sub.attrib.get("ounametype")
                # TODO: support lang
                lang = sub.attrib.get("lang")
                result.add_name(ABCTypes.get_type("ounametype", (type, )),
                                value)
            elif sub.tag == "parentid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in parentid: %s" % value
                type = sub.attrib.get("ouidtype")
                result.parent = (ABCTypes.get_type("ouidtype",
                                                   (type, )), value)
            elif sub.tag == "address":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error in address: %s" % value
                type = sub.attrib.get("addresstype")
                addr_type = ABCTypes.get_type("addresstype", ("ou", type))
                result.add_address(addr_type, self._make_address(sub))
            elif sub.tag == "contactinfo":
                if len(sub.attrib) != 1:
                    raise ABCTypesError, "error on contactinfo: %s" % value
                if not sub.text:
                    continue
                type = sub.attrib.get("contacttype")
                result.add_contact(
                    ABCTypes.get_type("contacttype", (
                        "ou",
                        type,
                    )), value)

        # NB! This is crucial to save memory on XML elements
        element.clear()
        return result
Beispiel #11
0
 def _check_type(_type, args):
     # Should throw exception if anything is fishy
     ABCTypes.get_type(_type, args)
Beispiel #12
0
    def next(self):
        """Return the next DataPerson object. Returns a ABCTypesError
        exception if object has errors in types. These objects should be
        skipped.

        Consume the next XML-element describing a person, and return a
        suitable representation (DataPerson).
        """

        # This call with propagate StopIteration when all the (XML) elements
        # are exhausted.
        element = super(XMLPerson2Object, self).next()
        result = DataPerson()

        # Iterate over *all* subelements
        for sub in element.getiterator():
            value = None
            if sub.text:
                value = sub.text.strip()

            if sub.tag == "personid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in personid: {}".format(value))
                _type = sub.attrib.get("personidtype")
                result.add_id(ABCTypes.get_type("personidtype", (_type,)),
                              value)
            elif sub.tag == "tag":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in tag: {}".format(value))
                _type = sub.attrib.get("tagtype")
                result.add_tag(ABCTypes.get_type("tagtype", ("person", _type)),
                               value)
            elif sub.tag == "name":
                for t, v in self._make_person_name(sub):
                    result.add_name(t, v)
            elif sub.tag == "birthdate":
                # if len == 6 we asume ddmmyy
                if value is None:
                    result.birth_date = None
                else:
                    if len(value) == 6:
                        value = "19{}-{}-{}".format(value[4:6],
                                                    value[2:4],
                                                    value[0:2])
                    result.birth_date = self._make_mxdate(value)
            elif sub.tag == "gender":
                result.gender = value
            elif sub.tag == "address":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in address: {}".format(value))
                _type = sub.attrib.get("addresstype")
                addr_type = ABCTypes.get_type("addresstype", ("person", _type))
                result.add_address(addr_type, self._make_address(sub))
            elif sub.tag == "contactinfo":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in contactinfo: {}"
                                        .format(value))
                if not sub.text:
                    continue
                _type = sub.attrib.get("contacttype")
                result.add_contact(ABCTypes.get_type("contacttype",
                                                     ("person", _type,)),
                                   value)

        # NB! This is crucial to save memory on XML elements
        element.clear()
        return result
Beispiel #13
0
    def next(self):
        """Return the next DataOU object. Returns a ABCTypesError
        exception if object has errors in types. These objects should be
        skipped.

        Consume the next XML-element describing an OU, and return a
        suitable representation (DataOU).
        """

        # This call with propagate StopIteration when all the (XML) elements
        # are exhausted.
        element = super(XMLOU2Object, self).next()
        result = DataOU()

        # Iterate over *all* subelements
        for sub in element.getiterator():
            value = None
            if sub.text:
                value = sub.text.strip()

            if sub.tag == "ouid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in ouid: {}".format(value))
                _type = sub.attrib.get("ouidtype")
                result.add_id(ABCTypes.get_type("ouidtype", (_type,)),
                              value)
            elif sub.tag == "tag":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in tag: {}".format(value))
                _type = sub.attrib.get("tagtype")
                result.add_tag(ABCTypes.get_type("tagtype", ("ou", _type)),
                               value)
            elif sub.tag == "ouname":
                if len(sub.attrib) != 2:
                    raise ABCTypesError("error in ouname: {}".format(value))
                _type = sub.attrib.get("ounametype")
                result.add_name(ABCTypes.get_type("ounametype", (_type,)),
                                value)
            elif sub.tag == "parentid":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in parentid: {}".format(value))
                _type = sub.attrib.get("ouidtype")
                result.parent = (ABCTypes.get_type("ouidtype", (_type,)),
                                 value)
            elif sub.tag == "address":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error in address: {}".format(value))
                _type = sub.attrib.get("addresstype")
                addr_type = ABCTypes.get_type("addresstype",
                                              ("ou", _type))
                result.add_address(addr_type, self._make_address(sub))
            elif sub.tag == "contactinfo":
                if len(sub.attrib) != 1:
                    raise ABCTypesError("error on contactinfo: {}"
                                        .format(value))
                if not sub.text:
                    continue
                _type = sub.attrib.get("contacttype")
                result.add_contact(ABCTypes.get_type("contacttype",
                                                     ("ou", _type,)),
                                   value)

        # NB! This is crucial to save memory on XML elements
        element.clear()
        return result