def has_children (self) : try : first (self.own_links) except IndexError : return False else : return True
def attr_filters \ ( soc, E_Type, request, data, scope , a_pat = None , default_op = "EQ" ) : filters = [] filters_q = [] map = TFL.mm_dict_mm_list () af_args = ichain \ ( soc.af_args_api (request.req_data_list, default_op) , soc.af_args_fif (data, a_pat, default_op) ) for fn, name, typ, tail, op, value in af_args : if typ : map [name] [typ].append ((tail, op, value)) else : f, fq = soc._setup_attr (E_Type, fn, name, op, value) filters.append (f) filters_q.append (fq) for name, t_map in pyk.iteritems (map) : if len (t_map) > 1 : raise ValueError \ ( "Got types %s instead of exactly one type" % (sorted (t_map), ) ) typ = first (t_map) nqr = soc.nested (scope, scope [typ], t_map [typ]) fs, fq = soc._setup_attr_pepk (E_Type, name, nqr) filters.extend (fs) filters_q.append (fq) return tuple (filters), tuple (filters_q)
def singleton (self) : Type = self._etype if Type.max_count == 1 : try : return first (self.query ().limit (2)) except IndexError : pass
def singleton(self): Type = self._etype if Type.max_count == 1: try: return first(self.query().limit(2)) except IndexError: pass
def _get_sender_name(self): try: result = first(s for s in self._get_sender_() if s) except IndexError: result = None if result is not None: return decoded_header(result) return ""
def __init__ (self, e_type, parent) : self.__super.__init__ (e_type) self.p_parent = parent self.parent = \ ( parent if parent.sa_table is not None else first (p for p in parent.ancestors if p.sa_table is not None) )
def _get_sender_name (self) : try : result = first (s for s in self._get_sender_ () if s) except IndexError : result = None if result is not None : return decoded_header (result) return ""
def one (self) : """Return first and only element of query result. Raise IndexError if query result contains more than one element. """ result = first (self) if len (self._cache) > 1 : raise IndexError \ ("Query result contains %s entries" % len (self._cache)) return result
def one(self): """Return first and only element of query result. Raise IndexError if query result contains more than one element. """ result = first(self) if len(self._cache) > 1: raise IndexError \ ("Query result contains %s entries" % len (self._cache)) return result
def _get_pages (self) : np = _T ("Participants") nr = _T ("Results") obj = self.obj result = [] scope = self.scope sk = TFL.Sorted_By \ ("skipper.person.last_name", "skipper.person.first_name") Result_Type = None if obj.is_team_race : Registration_Type = self.Registration_Teamrace try : place = first (obj.teams).place except IndexError : pass else : if place : Result_Type = self.Result_Teamrace else : Registration_Type = self.Registration if obj.races : Result_Type = self.Result if Result_Type : result.append \ ( Result_Type ( ETM = obj.ETM , name = nr.lower () , obj = obj , parent = self , regatta = obj , short_title = nr , title = "%s %s" % ( _T ("Results for"), self.short_title) ) ) head = _T ("List of participants for") result.append \ ( Registration_Type ( ETM = obj.ETM , head_line = "%s %s<br />%s, %s" % ( _T ("Registration list"), obj.name , obj.event.FO.short_title, obj.event.ui_date ) , name = np.lower () , nav_off_canvas = False , obj = obj , parent = self , regatta = obj , short_title = np , title = "%s %s" % (head, self.short_title) ) ) bir_admin = self._get_bir_admin () if bir_admin : result.append (bir_admin) return result
def choose (* lang) : def _gen (lang) : for l in lang : yield l, l for l in lang : if l : a, _, b = split_hst (l, "_") yield a, b or a yield "", "" return first (l for l in _gen (lang) if l [0] in Config.Languages)
def choose(*lang): def _gen(lang): for l in lang: yield l, l for l in lang: if l: a, _, b = split_hst(l, "_") yield a, b or a yield "", "" return first(l for l in _gen(lang) if l[0] in Config.Languages)
def distinct(self, *criteria): """Restrict query result to distinct elements.""" n = len(criteria) if n == 0: _distinct = uniq else: if n == 1: criterion = first(criteria) elif criteria: criterion = TFL.Filter_And(*criteria) _distinct = _Q_Filter_Distinct_(criterion) return _Q_Result_(self, _distinct=_distinct)
def distinct (self, * criteria) : """Restrict query result to distinct elements.""" n = len (criteria) if n == 0 : _distinct = uniq else : if n == 1 : criterion = first (criteria) elif criteria : criterion = TFL.Filter_And (* criteria) _distinct = _Q_Filter_Distinct_ (criterion) return _Q_Result_ (self, _distinct = _distinct)
def _polymorphic(self): ET = self.E_Type cnp = getattr(self._attr, "selectable_e_types", None) result = \ ( ET is not None and ET.has_identity and ET.polymorphic_epk and cnp and not (len (cnp) == 1 and first (cnp) == ET.type_name) ### if there aren't any selectable_e_types except `ET` ### --> not polymorphic in the context of `self._attr` ) return result
def _polymorphic (self) : ET = self.E_Type cnp = getattr (self._attr, "selectable_e_types", None) result = \ ( ET is not None and ET.has_identity and ET.polymorphic_epk and cnp and not (len (cnp) == 1 and first (cnp) == ET.type_name) ### if there aren't any selectable_e_types except `ET` ### --> not polymorphic in the context of `self._attr` ) return result
def filter(self, *criteria, **kw): """Restrict query result to elements matching the `criteria`.""" if kw: criteria = list(criteria) Q = self.Q for k, v in pyk.iteritems(kw): criteria.append(getattr(Q, k) == v) criteria = tuple(criteria) assert criteria if len(criteria) == 1: criterion = first(criteria) else: criterion = TFL.Filter_And(*criteria) return self._Q_Result_Filtered_(self, criterion, self._distinct)
def group_by(self, *criteria, **kw): """Group query result by `criteria`.""" if kw: criteria = list(criteria) Q = self.Q for k, v in pyk.iteritems(kw): criteria.append(getattr(Q, k) == v) criteria = tuple(criteria) assert criteria if len(criteria) == 1: criterion = first(criteria) else: criterion = TFL.Filter_And(*criteria) return self._Q_Result_Group_By_(self, criterion, self._distinct)
def filter (self, * criteria, ** kw) : """Restrict query result to elements matching the `criteria`.""" if kw : criteria = list (criteria) Q = self.Q for k, v in pyk.iteritems (kw) : criteria.append (getattr (Q, k) == v) criteria = tuple (criteria) assert criteria if len (criteria) == 1 : criterion = first (criteria) else : criterion = TFL.Filter_And (* criteria) return self._Q_Result_Filtered_ (self, criterion, self._distinct)
def group_by (self, * criteria, ** kw) : """Group query result by `criteria`.""" if kw : criteria = list (criteria) Q = self.Q for k, v in pyk.iteritems (kw) : criteria.append (getattr (Q, k) == v) criteria = tuple (criteria) assert criteria if len (criteria) == 1 : criterion = first (criteria) else : criterion = TFL.Filter_And (* criteria) return self._Q_Result_Group_By_ (self, criterion, self._distinct)
def _effective_entry (self) : if self.hidden : entries = self.entries else : entries = (e for e in self.entries if not e.hidden) if self.request : method = self.request.method user = self.user entries = (e for e in entries if e.allow_method (method, user)) try : page = first (entries) except IndexError : return self else : return page._effective
def __init__(cls, name, bases, dct): cls.__m_super.__init__(name, bases, dct) _own_names = set(k for k in dct if not k.startswith("__")) _names = set(_own_names) for b in cls.__bases__: _names.update(getattr(b, "_names", ())) for k in _own_names: v = dct[k] if v: v = TFL.normalized_indent(v) setattr(cls, k, v) setattr(cls, "_names", _names) setattr(cls, "_own_names", _own_names) if not cls.__doc__: setattr(cls, "__doc__", first(b.__doc__ for b in cls.__bases__ if b.__doc__)) cls._OWN = cls._ALL = None
def name_skip_pat (self) : """Multi/Regexp matching all attribute names that are **not** selected. Use negative lookahead assertions to skip all names belonging to E_Types not in the set of `e_type_selectors` selected by the client. """ ets = self.e_type_selectors def _gen (self, ets) : def _sk (x) : k, _ = x return - len (k), k ### longest first, deterministic for k, v in sorted (pyk.iteritems (ets), key = _sk) : head = k.split ("/") [0] if "/" in k else "" yield Regexp \ ("%s\[(?!%s\])" % (re.escape (head), re.escape (v))) pats = _gen (self, ets) result = first (pats) if len (ets) == 1 else Multi_Regexp (* pats) return result
def __init__(cls, name, bases, dct): cls.__m_super.__init__(name, bases, dct) _own_names = set(k for k in dct if not k.startswith("__")) _names = set(_own_names) for b in cls.__bases__: _names.update(getattr(b, "_names", ())) for k in _own_names: v = dct[k] if v: v = TFL.normalized_indent(v) setattr(cls, k, v) setattr(cls, "_names", _names) setattr(cls, "_own_names", _own_names) if not cls.__doc__: setattr \ ( cls, "__doc__" , first (b.__doc__ for b in cls.__bases__ if b.__doc__) ) cls._OWN = cls._ALL = None
def name_skip_pat(self): """Multi/Regexp matching all attribute names that are **not** selected. Use negative lookahead assertions to skip all names belonging to E_Types not in the set of `e_type_selectors` selected by the client. """ ets = self.e_type_selectors def _gen(self, ets): def _sk(x): k, _ = x return -len(k), k ### longest first, deterministic for k, v in sorted(pyk.iteritems(ets), key=_sk): head = k.split("/")[0] if "/" in k else "" yield Regexp \ ("%s\[(?!%s\])" % (re.escape (head), re.escape (v))) pats = _gen(self, ets) result = first(pats) if len(ets) == 1 else Multi_Regexp(*pats) return result
def _pid_query_indirect (self, session, pid) : pqs = self.pid_query_stmt tnq = session.connection.execute (self.type_name_select_stmt, pid = pid) tn = first (tnq) [0] ETW = session.scope.entity_type (tn)._SAW return ETW._pid_query_direct (session, pid)
def _pid_query_direct (self, session, pid) : result = first (self.pid_query_stmt.bind (session, pid = pid)) return result
def first (self) : """Return first element of query result.""" try : return first (self) except IndexError : return None
def message_from_arg (cmd, arg) : try : return first (PMA.messages_from_args ([arg], cmd.msg_base_dirs)) except LookupError : raise SystemExit (1)
def __init__(self, e_type): if self.ckd_name_eq_name: self._fix_ckd_names() sk = TFL.Sorted_By("rank", "name") self._syncable = [] self._db_attr = e_type.db_attr = [] self._user_attr = e_type.user_attr = [] self.__super.__init__(e_type) e_type.db_attr.sort(key=sk) e_type.user_attr.sort(key=sk) self._setup_attrs(e_type) e_type.q_able = tuple \ ( sorted ( (a for a in pyk.itervalues (e_type.attributes) if a.q_able) , key = sk ) ) e_type.edit_attr = tuple(MOM.Attr.Selector.editable(e_type)) ea_set = set(e_type.edit_attr) e_type.q_able_no_edit = tuple \ (a for a in e_type.q_able if a not in ea_set) e_type.id_entity_attr = tuple \ ( a for a in e_type.edit_attr if isinstance (a, MOM.Attr._EPK_Mixin_) ) e_type.link_ref_attr = lra = tuple \ ( sorted ( MOM.Attr.Selector.A_Type (MOM.Attr.A_Link_Ref_List) (e_type) , key = sk ) ) e_type.link_ref_map = dict((a.E_Type, a) for a in lra) e_type.polish_attr = tuple \ ( sorted ( (a for a in e_type.edit_attr if a.attr.polisher is not None) , key = TFL.Sorted_By ("ui_rank") ) ) e_type.primary_required = pr = list \ (p for p in e_type.primary if p.is_required) e_type.primary_optional = list \ (p for p in e_type.primary [len (pr): ] if not p.electric) e_type.rev_ref_attr = tuple \ ( sorted ( MOM.Attr.Selector.A_Type (MOM.Attr._A_Rev_Ref_) (e_type) , key = sk ) ) e_type.surrogate_attr = tuple \ ( a for a in e_type.db_attr if isinstance (a.attr, MOM.Attr.A_Surrogate) ) e_type.ui_attr = tuple(MOM.Attr.Selector.ui_attr(e_type)) own_surrogates = tuple \ (a for a in e_type.surrogate_attr if a.name in self._own_names) if len(own_surrogates) > 1: raise TypeError \ ( "%s cannot have more than 1 own surrogate attributes; got %s" "\n %s" % ( e_type.type_name, len (own_surrogates) , ", ".join (repr (a.name) for a in own_surrogates) ) ) else: try: e_type.own_surrogate = s = first(own_surrogates) except LookupError: e_type.own_surrogate = None else: suid = s.surrogate_id app_type = e_type.app_type if suid not in app_type.surrogate_map: app_type.surrogate_t_map[suid] = e_type app_type.surrogate_map[s.q_name] = s
def first(self): """Return first element of query result.""" try: return first(self) except IndexError: return None
def __init__ (self, e_type) : if self.ckd_name_eq_name : self._fix_ckd_names () sk = TFL.Sorted_By ("rank", "name") self._syncable = [] self._db_attr = e_type.db_attr = [] self._user_attr = e_type.user_attr = [] self.__super.__init__ (e_type) e_type.db_attr.sort (key = sk) e_type.user_attr.sort (key = sk) self._setup_attrs (e_type) e_type.q_able = tuple \ ( sorted ( (a for a in pyk.itervalues (e_type.attributes) if a.q_able) , key = sk ) ) e_type.edit_attr = tuple (MOM.Attr.Selector.editable (e_type)) ea_set = set (e_type.edit_attr) e_type.q_able_no_edit = tuple \ (a for a in e_type.q_able if a not in ea_set) e_type.id_entity_attr = tuple \ ( a for a in e_type.edit_attr if isinstance (a, MOM.Attr._EPK_Mixin_) ) e_type.link_ref_attr = lra = tuple \ ( sorted ( MOM.Attr.Selector.A_Type (MOM.Attr.A_Link_Ref_List) (e_type) , key = sk ) ) e_type.link_ref_map = dict ((a.E_Type, a) for a in lra) e_type.polish_attr = tuple \ ( sorted ( (a for a in e_type.edit_attr if a.attr.polisher is not None) , key = TFL.Sorted_By ("ui_rank") ) ) e_type.primary_required = pr = list \ (p for p in e_type.primary if p.is_required) e_type.primary_optional = list \ (p for p in e_type.primary [len (pr): ] if not p.electric) e_type.rev_ref_attr = tuple \ ( sorted ( MOM.Attr.Selector.A_Type (MOM.Attr._A_Rev_Ref_) (e_type) , key = sk ) ) e_type.surrogate_attr = tuple \ ( a for a in e_type.db_attr if isinstance (a.attr, MOM.Attr.A_Surrogate) ) e_type.ui_attr = tuple (MOM.Attr.Selector.ui_attr (e_type)) own_surrogates = tuple \ (a for a in e_type.surrogate_attr if a.name in self._own_names) if len (own_surrogates) > 1 : raise TypeError \ ( "%s cannot have more than 1 own surrogate attributes; got %s" "\n %s" % ( e_type.type_name, len (own_surrogates) , ", ".join (repr (a.name) for a in own_surrogates) ) ) else : try : e_type.own_surrogate = s = first (own_surrogates) except LookupError : e_type.own_surrogate = None else : suid = s.surrogate_id app_type = e_type.app_type if suid not in app_type.surrogate_map : app_type.surrogate_t_map [suid] = e_type app_type.surrogate_map [s.q_name] = s