def attributes2key(self, group_kind, attributes): """Construct a Cerebrum group_name/CF group id, given a bunch of attributes. This function is useful to map various information tidbits on groups, roles, etc to a unique ID in Cerebrum for that particular kind of attributes. Each L{group_kind} results in a group in Cerebrum and this function calculates that group's id (not entity_id, but the id used in CF). @type group_kind: basestring @param group_kind: A tag that describes what kind of information is to be expected in L{attributes}. The ONLY legal values are: - 'stprog', 'kull', 'kullklasse', 'undenh', 'undakt' - 'student-undenh', 'student-undakt', 'student-kullklasse', 'student-kull' Each kind has a different set of keys in attributes that MUST be present. @type attributes: dict (of basestring -> basestring) @param attributes: A collection of attributes. This collection may contain more than what is required by L{group_kind}. @rtype: basestring or None @return: None if an id could not be constructed. The id itself otherwise. """ # easiest way to copy, since we modify these destructively attrs = lower(attributes) if group_kind not in self.group_kind2required_keys: logger.warn("Don't know how to process attributes " "belonging to '%s' (%s)", group_kind, repr(attrs)) return None keys = self.group_kind2required_keys[group_kind] if not all(x in attrs for x in keys): logger.warn("Missing essential keys for kind=%s. " "Required=%s, available=%s", group_kind, sorted(keys), sorted(attrs)) return None # Now, those that have "terminnr" > 1 MUST be remapped. if "terminnr" in keys: attrs = count_back_semesters(attrs) result_id = self.group_kind2name_template[group_kind] result_id = result_id % attrs return result_id
def _attributes2exportable_key(self, attr_kind, attributes): """A help method to create an internal lookup key. This is for internal usage only. The key is similar to group.group_name for the groups we create (but not quite the same). @type attr_kind: basestring @param attr_kind: String tagging the attributes. @type attributes: dict (basestring -> basestring) @param attributes: Attributes from which a key is derived @rtype: basestring @return: Key calculated from L{attributes}. """ key = None attrs = lower(attributes) if attr_kind == "undenh": attrs = count_back_semesters(attrs) key = ":".join((attrs[x] for x in ("arstall", "terminkode", "emnekode", "versjonskode", "terminnr"))) elif attr_kind == "undakt": attrs = count_back_semesters(attrs) key = ":".join((attrs[x] for x in ("arstall", "terminkode", "emnekode", "versjonskode", "terminnr", "aktivitetkode"))) elif attr_kind in ("stprog", "kull", "kullklasse"): key = attributes["studieprogramkode"] else: assert False, "NOTREACHED" return key