nodes: 0, module; 1, function_definition; 2, function_name:add_identity; 3, parameters; 4, block; 5, identifier:cls; 6, identifier:db; 7, identifier:identity; 8, identifier:backend; 9, expression_statement; 10, expression_statement; 11, try_statement; 12, if_statement; 13, return_statement; 14, comment:""" Load and identity list from backend in Sorting Hat """; 15, assignment; 16, block; 17, except_clause; 18, except_clause; 19, except_clause; 20, except_clause; 21, boolean_operator; 22, block; 23, identifier:uuid; 24, identifier:uuid; 25, None; 26, expression_statement; 27, expression_statement; 28, expression_statement; 29, expression_statement; 30, as_pattern; 31, block; 32, as_pattern; 33, block; 34, as_pattern; 35, block; 36, as_pattern; 37, block; 38, comparison_operator:'company' in identity; 39, comparison_operator:identity['company'] is not None; 40, try_statement; 41, assignment; 42, call; 43, assignment; 44, call; 45, identifier:AlreadyExistsError; 46, as_pattern_target; 47, expression_statement; 48, identifier:InvalidValueError; 49, as_pattern_target; 50, expression_statement; 51, identifier:UnicodeEncodeError; 52, as_pattern_target; 53, expression_statement; 54, identifier:Exception; 55, as_pattern_target; 56, expression_statement; 57, string; 58, identifier:identity; 59, subscript; 60, None; 61, block; 62, except_clause; 63, identifier:uuid; 64, call; 65, attribute; 66, argument_list; 67, identifier:profile; 68, dictionary; 69, attribute; 70, argument_list; 71, identifier:ex; 72, assignment; 73, identifier:ex; 74, call; 75, identifier:ex; 76, call; 77, identifier:ex; 78, call; 79, string_content:company; 80, identifier:identity; 81, string; 82, expression_statement; 83, expression_statement; 84, identifier:AlreadyExistsError; 85, block; 86, attribute; 87, argument_list; 88, identifier:logger; 89, identifier:debug; 90, string:"New sortinghat identity %s %s,%s,%s "; 91, identifier:uuid; 92, subscript; 93, subscript; 94, subscript; 95, pair; 96, pair; 97, identifier:api; 98, identifier:edit_profile; 99, identifier:db; 100, identifier:uuid; 101, dictionary_splat; 102, identifier:uuid; 103, attribute; 104, attribute; 105, argument_list; 106, attribute; 107, argument_list; 108, attribute; 109, argument_list; 110, string_content:company; 111, call; 112, call; 113, pass_statement; 114, identifier:api; 115, identifier:add_identity; 116, identifier:db; 117, identifier:backend; 118, subscript; 119, subscript; 120, subscript; 121, identifier:identity; 122, string; 123, identifier:identity; 124, string; 125, identifier:identity; 126, string; 127, string:"name"; 128, conditional_expression:identity['name'] if identity['name'] else identity['username']; 129, string:"email"; 130, subscript; 131, identifier:profile; 132, identifier:ex; 133, identifier:eid; 134, identifier:logger; 135, identifier:warning; 136, string:"Trying to add a None identity. Ignoring it."; 137, identifier:logger; 138, identifier:warning; 139, string:"UnicodeEncodeError. Ignoring it. %s %s %s"; 140, subscript; 141, subscript; 142, subscript; 143, identifier:logger; 144, identifier:warning; 145, string:"Unknown exception adding identity. Ignoring it. %s %s %s"; 146, subscript; 147, subscript; 148, subscript; 149, keyword_argument; 150, attribute; 151, argument_list; 152, attribute; 153, argument_list; 154, identifier:identity; 155, string; 156, identifier:identity; 157, string; 158, identifier:identity; 159, string; 160, string_content:username; 161, string_content:name; 162, string_content:email; 163, subscript; 164, subscript; 165, subscript; 166, identifier:identity; 167, string; 168, identifier:identity; 169, string; 170, identifier:identity; 171, string; 172, identifier:identity; 173, string; 174, identifier:identity; 175, string; 176, identifier:identity; 177, string; 178, identifier:identity; 179, string; 180, identifier:exc_info; 181, True; 182, identifier:api; 183, identifier:add_organization; 184, identifier:db; 185, subscript; 186, identifier:api; 187, identifier:add_enrollment; 188, identifier:db; 189, identifier:uuid; 190, subscript; 191, call; 192, call; 193, string_content:email; 194, string_content:name; 195, string_content:username; 196, identifier:identity; 197, string; 198, identifier:identity; 199, string; 200, identifier:identity; 201, string; 202, string_content:email; 203, string_content:email; 204, string_content:name; 205, string_content:username; 206, string_content:email; 207, string_content:name; 208, string_content:username; 209, identifier:identity; 210, string; 211, identifier:identity; 212, string; 213, identifier:datetime; 214, argument_list; 215, identifier:datetime; 216, argument_list; 217, string_content:name; 218, string_content:name; 219, string_content:username; 220, string_content:company; 221, string_content:company; 222, integer:1900; 223, integer:1; 224, integer:1; 225, integer:2100; 226, integer:1; 227, integer:1
code: def add_identity(cls, db, identity, backend):
          """ Load and identity list from backend in Sorting Hat """
          uuid = None
  
          try:
              uuid = api.add_identity(db, backend, identity['email'],
                                      identity['name'], identity['username'])
  
              logger.debug("New sortinghat identity %s %s,%s,%s ",
                           uuid, identity['username'], identity['name'], identity['email'])
  
              profile = {"name": identity['name'] if identity['name'] else identity['username'],
                         "email": identity['email']}
  
              api.edit_profile(db, uuid, **profile)
  
          except AlreadyExistsError as ex:
              uuid = ex.eid
          except InvalidValueError as ex:
              logger.warning("Trying to add a None identity. Ignoring it.")
          except UnicodeEncodeError as ex:
              logger.warning("UnicodeEncodeError. Ignoring it. %s %s %s",
                             identity['email'], identity['name'],
                             identity['username'])
          except Exception as ex:
              logger.warning("Unknown exception adding identity. Ignoring it. %s %s %s",
                             identity['email'], identity['name'],
                             identity['username'], exc_info=True)
  
          if 'company' in identity and identity['company'] is not None:
              try:
                  api.add_organization(db, identity['company'])
                  api.add_enrollment(db, uuid, identity['company'],
                                     datetime(1900, 1, 1),
                                     datetime(2100, 1, 1))
              except AlreadyExistsError:
                  pass
  
          return uuid