def convert_mods_name_to_contributor(mods_name, dai_dict):
    if mods_name is not None:
        contributor = Contributor()
        # extract properties
        name_type = mods_name.get("type")
        name_id = mods_name.get("ID")
        name_parts = mods_name.findall(prefixtag("mods", "namePart"))
        name_affiliations = mods_name.findall(prefixtag("mods", "affiliation"))
        name_roleterm = None
        name_role = mods_name.find(prefixtag("mods", "role"))
        if name_role is not None:
            name_roleterm = name_role.find(prefixtag("mods", "roleTerm"))
        name_descriptions = mods_name.findall(prefixtag("mods", "description"))

        if name_type == "personal":
            person = Person()

            if name_id is not None and dai_dict is not None and name_id in dai_dict:
                id_value = dai_dict[name_id]["authority"] + "/" + dai_dict[name_id]["value"]
                identifier = metajson.create_identifier("uri", id_value)
                person.add_item_to_key(identifier, "identifiers")

            if name_parts:
                for name_part in name_parts:
                    if name_part.get("type") == "given":
                        person["name_given"] = name_part.text
                    elif name_part.get("type") == "family":
                        person["name_family"] = name_part.text
                    elif name_part.get("type") == "date":
                        date = name_part.text.replace("(", "").replace(")", "")
                        minus_index = date.find("-")
                        if minus_index == -1:
                            person["date_of_birth"] = date
                        else:
                            person["date_of_birth"] = date[:minus_index]
                            person["date_of_death"] = date[minus_index+1:]
                    elif name_part.get("termsOfAddress") == "date":
                        person["name_terms_of_address"] = name_part.text

            contributor["person"] = person
        #print name_type, name_id, name_parts, name_affiliations, name_roleterm, name_descriptions
        return contributor
def convert_formatted_name_to_contributor(formatted_name, contributor_type, role):
    if formatted_name:
        formatted_name = formatted_name.strip()
        event = None
        family = None
        orgunit = None
        person = None

        #print("name: %s"%formatted_name)
        # contributor_type determination
        for event_term in contributor_event_terms:
            if event_term in formatted_name.lower():
                contributor_type = "event"
                break
        for orgunit_term in contributor_orgunit_terms:
            if orgunit_term in formatted_name.lower():
                contributor_type = "orgunit"
                break
        if contributor_type is None:
            contributor_type = "person"

        contributor = Contributor()
        if role:
            contributor["role"] = role

        if contributor_type == "event":
            event = Event()
            event["title"] = formatted_name
            contributor["event"] = event

        elif contributor_type == "orgunit":
            orgunit = Orgunit()
            orgunit["name"] = formatted_name
            contributor["orgunit"] = orgunit

        elif contributor_type == "person" or contributor_type == "family":
            #type is "person" or "family"

            name_given = ""
            name_middle = ""
            name_family = ""
            name_particule_non_dropping = ""
            name_terms_of_address = ""
            date_of_birth = ""
            date_of_death = ""

            parenthesis_index = formatted_name.rfind("(")
            if parenthesis_index != -1:
                #may be like: name (date_of_birth-date_of_death)
                dates_part = formatted_name[parenthesis_index + 1:-1].strip()
                date_of_birth = dates_part[:4]
                date_of_death = dates_part[5:]
                if date_of_death == "....":
                    date_of_death = ""
                formatted_name = formatted_name[:parenthesis_index].strip()

            slash_index = formatted_name.find("/")
            if slash_index != -1:
                #like: name/affiliation
                affiliation_name = formatted_name[slash_index + 1:].strip()
                formatted_name = formatted_name[:slash_index].strip()

            commaspacejrdot_index = formatted_name.rfind(", Jr.")
            if (commaspacejrdot_index != -1):
                #like "Paul B. Harvey, Jr."
                formatted_name = formatted_name[:commaspacejrdot_index].strip()
                name_middle = "Jr."

            #Is it formatted like "Family, Given" or "Given Family" ?
            comma_index = formatted_name.find(",")
            if comma_index == -1:
                space_index = formatted_name.rfind(" ")
                #print formatted_name
                #print space_index
                if space_index != -1:
                    #like Given Family
                    name_given = formatted_name[:space_index].strip()
                    name_family = formatted_name[space_index+1:].strip()
                else:
                    #like Family
                    name_family = formatted_name.strip()

            else:
                #like Family, Given
                name_family = formatted_name[:comma_index].strip()
                name_given = formatted_name[comma_index+1:].strip()

            # manage the terms_of_address and particule
            for term_of_address in contributor_person_terms_of_address:
                if name_family and name_family.lower().startswith(term_of_address+" "):
                    name_terms_of_address = name_family[:len(term_of_address)]
                    name_family = name_family[len(term_of_address):].strip()
                if name_given:
                    if name_given.lower().endswith(" "+term_of_address):
                        name_terms_of_address = name_given[-len(term_of_address):]
                        name_given = name_given[:-len(term_of_address)].strip()
                    if name_given.lower().startswith(term_of_address+" "):
                        name_terms_of_address = name_given[:len(term_of_address)]
                        name_given = name_given[len(term_of_address):].strip()
                    if name_given.lower() == term_of_address:
                        name_terms_of_address = name_given
                        name_given = None

            # Be careful with a particule inside the name like: Viveiros de Castro, Eduardo
            for particule in contributor_particule:
                if name_family and name_family.lower().startswith(particule+" "):
                    name_particule_non_dropping = name_family[0:len(particule)]
                    name_family = name_family[len(particule):].strip()
                if name_given:
                    if name_given.lower().endswith(" "+particule):
                        name_particule_non_dropping = name_given[-len(particule):]
                        name_given = name_given[:-len(particule)].strip()
                    if name_given.lower().startswith(particule+" "):
                        name_particule_non_dropping = name_given[:len(particule)]
                        name_given = name_given[len(particule):].strip()
                    if name_given.lower() == particule:
                        name_particule_non_dropping = name_given
                        name_given = None

            if contributor_type == "person":
                person = Person()
                person.set_key_if_not_none("name_family", name_family)
                person.set_key_if_not_none("name_given", name_given)
                person.set_key_if_not_none("name_middle", name_middle)
                person.set_key_if_not_none("name_terms_of_address", name_terms_of_address)
                person.set_key_if_not_none("name_particule_non_dropping", name_particule_non_dropping)
                person.set_key_if_not_none("date_of_birth", date_of_birth)
                person.set_key_if_not_none("date_of_death", date_of_death)
                if 'affiliation_name' in vars() and affiliation_name:
                    #todo manage as an object
                    person["affiliations"] = [{"type": "orgunit", "preferred": True, "name": affiliation_name}]

                contributor["person"] = person

            elif contributor_type == "family":
                family = Family()
                family.set_key_if_not_none("name_family", name_family)
                contributor["family"] = family

        #print json.dumps(contributor,ensure_ascii=False,indent=4,encoding="utf-8")
        return contributor