Esempio n. 1
0
File: Node.py Progetto: Tapyr/tapyr
 def _init_kw (self, kw) :
     kw_err = {}
     for k, v in pyk.iteritems (self.init_arg_defaults) :
         if not hasattr (self, k) :
             setattr (self, k, v)
     for k, v in pyk.iteritems (kw) :
         if k in self.init_arg_defaults :
             ### protect `%` characters hidden inside `v` to avoid
             ### ValueErrors during formatting
             if isinstance (v, pyk.string_types) :
                 v = v.replace ("%", "%%")
             elif isinstance (v, (tuple, list, TFL.NO_List)) :
                 vin = v
                 v   = []
                 for x in vin :
                     if isinstance (x, pyk.string_types) :
                         x = x.replace ("%", "%%")
                     v.append (x)
             if k in self._autoconvert :
                 v = self._autoconvert [k] (self, k, v)
             setattr (self, k, v)
         else :
             kw_err [k] = v
     if kw_err :
         print (self.__class__, self.init_arg_defaults)
         raise TypeError ("unexpected keyword arguments: %s" % kw_err)
Esempio n. 2
0
    def __init__ (self, site, request, url = url) :
        self.site    = site
        self.request = request
        rtparm = 1
        if url.endswith ('cgi') :
            rtparm = 2
        if 'interfaces' in self.request or 'ips' in self.request :
            rt = Routes  (site = site, url = url + '?post_routes=%s' % rtparm)
            dt = Details (site = site, url = url + '?post_olsr=1')
            self.version = rt.version
            interfaces   = {}
            ips          = {}
            base         = 0
            for count, (ip, ifname) in enumerate (pyk.iteritems (rt.ip_dev)) :
                i4 = Inet4 (ip, None, None, iface = ifname)
                # ignore interfaces with unroutable IPs
                if unroutable (i4.ip) :
                    #print ("Unroutable: %s" % i4)
                    continue
                ips [i4] = 1
                iface = Interface (count, ifname, None)
                iface.is_wlan = False
                interfaces [ifname] = iface
                iface.append_inet4 (i4)
                base = count
            base += 1
            for count, (ip, ifname) in enumerate (pyk.iteritems (dt.ip_dev)) :
                i4 = Inet4 (ip, None, None, iface = ifname)
                is_wlan = sum (x == 1.0 for x in dt.metric [ip]) != 3
                #print ("ip", ip, dt.metric [ip])
                if unroutable (i4.ip) :
                    continue
                if i4 in ips :
                    if ifname not in interfaces :
                        iface = Interface (base + count, ifname, None)
                        interfaces [ifname] = iface
                        iface.append_inet4 (i4)
                    else :
                        iface = interfaces [ifname]
                        if i4 not in iface.inet4 :
                            #print ("Oops:", ifname, i4, iface.inet4 [0])
                            del iface.inet4 [0]
                            iface.append_inet4 (i4)
                    iface.is_wlan = is_wlan
                    continue
                ips [i4] = 1
                iface = Interface (base + count, ifname, None)
                iface.is_wlan = is_wlan
                interfaces [ifname] = iface
                iface.append_inet4 (i4)

            # check own ip
            n  = 'unknown'
            i4 = Inet4 (self.request ['ip'], None, None, iface = n)
            assert i4 in ips

            self.request ['ips']        = ips
            self.request ['interfaces'] = interfaces
            self.request ['version']    = rt.version
Esempio n. 3
0
File: Media.py Progetto: Tapyr/tapyr
 def from_list (cls, medias, ** kw) :
     to_add = dict ((n, v) for n, v in pyk.iteritems (kw) if v)
     if len (medias) == 1 and not to_add :
         result = medias [0]
     else :
         result = \
             CHJ.Media (children = medias) if (medias or to_add) else None
     for n, v in pyk.iteritems (to_add) :
         getattr (result, n).add (* v)
     return result
Esempio n. 4
0
 def _gen (ETW) :
     for k, q in sorted (pyk.iteritems (ETW.q_able_attrs)) :
         if not q.kind.show_in_ui : continue
         if k == q.attr.name :
             ### filter attribute-aliases
             yield str (q)
             if q.q_able_attrs :
                 for k, q in sorted (pyk.iteritems (q.q_able_attrs)) :
                     if not q.kind.show_in_ui : continue
                     if not q.columns :
                         yield str (q)
Esempio n. 5
0
 def __call__ (self, * args, ** kw) :
     _request  = self._request
     _response = self._response
     for c in list (_request.cookies_to_delete) :
         self.clear_cookie (c)
     _request.cookies_to_delete.clear ()
     for k, v in pyk.iteritems (self._auto_headers) :
         _response.add_header (k, v)
     for rel, (value, kw) in sorted (pyk.iteritems (self._links)) :
         _response.add_header ("link", value, rel = rel, ** kw)
     return _response.__call__ (* args, ** kw)
Esempio n. 6
0
File: Spec.py Progetto: Tapyr/tapyr
 def _create_properties (self, e_type) :
     for n, prop_type in pyk.iteritems (self._own_names) :
         if prop_type is not None :
             self._add_prop (e_type, n, prop_type)
     base_props = {}
     for b in e_type.__bases__ [::-1] :
         bps = getattr (b, self._prop_map_name, {})
         base_props.update (bps)
     for n, prop_type in pyk.iteritems (self._names) :
         if n not in self._own_names :
             ### Inherited property: include in `_prop_dict` and `_prop_kind`
             prop = base_props.get (n)
             if prop is not None :
                 self._setup_prop (e_type, n, prop.kind, prop)
Esempio n. 7
0
 def _setup_exprs (self, exprs, kw) :
     self.exprs = []
     add        = self.exprs.append
     for x in exprs :
         if isinstance (x, dict) :
             for f, e in sorted (pyk.iteritems (x)) :
                 add (Expression (f, e))
         else :
             if isinstance (x, pyk.string_types) :
                 x = (x, )
             if not isinstance (x, Expression) :
                 x = Expression (* x)
             add (x)
     for f, e in sorted (pyk.iteritems (kw)) :
         add (Expression (f, e))
Esempio n. 8
0
 def _items_iter (obj, level, compact, seen) :
     for k, v in sorted (pyk.iteritems (obj), key = SK) :
         rk = portable_repr (k)
         it = formatted_repr.iter (v, level + 1, compact, seen)
         for x in formatted_repr._structure_iter \
                 (it, level, rk + " : ", "", "", nl_after_open = True) :
             yield x
Esempio n. 9
0
 def row_as_pickle_cargo (self, row, db_attrs = None) :
     result = TFL.defaultdict (tuple)
     if db_attrs is None :
         db_attrs = self.db_attrs
     for name, kind_wrapper in pyk.iteritems (db_attrs) :
         result [name] = kind_wrapper.row_as_pickle_cargo (row)
     return result
Esempio n. 10
0
File: Q_Exp.py Progetto: Tapyr/tapyr
 def __init__ (cls, name, bases, dct) :
     cls.__m_super.__init__ (name, bases, dct)
     for map_name in "op_map", "rop_map" :
         map = getattr (cls, map_name, None)
         if map :
             for k, v in list (pyk.iteritems (map)) :
                 map.setdefault (normalized_op_name (k), v)
Esempio n. 11
0
File: Type.py Progetto: Tapyr/tapyr
    def satisfied (self, obj, attr_dict = {}) :
        """Checks if `obj' satisfies the predicate.
           `attr_dict' can provide values for `self.attributes'.

           If there is a `self.guard' the predicate is checked only if
           `self.guard' evaluates to true.
        """
        glob_dict = obj.globals ()
        if not self._guard_open (obj, attr_dict, glob_dict) :
            return True
        val_disp = self.val_disp = {}
        val_dict = self.val_dict = self._val_dict (obj, attr_dict)
        if not val_dict :
            return True
        for p in self.parameters :
            exc, val = self._eval_expr \
                (p, obj, glob_dict, val_dict, "parameter")
            if val is None or exc is not None :
                return True
            val_disp [p] = val if val is None else ui_display (val)
        for b, expr in pyk.iteritems (self.bindings) :
            if callable (expr) :
                val  = expr
                disp = "%s" % (ui_display (val), )
            else :
                exc, val = self._eval_expr \
                    (expr, obj, glob_dict, val_dict, "binding")
                if exc is not None :
                    return True
                disp = "%s << %s" % (ui_display (val), expr)
            val_dict [b] = val
            val_disp [b] = disp
        return self._satisfied (obj, glob_dict, val_dict)
Esempio n. 12
0
 def _gen (self, ET, bindings) :
     a_doc   = self._e_type_attr_doc
     dle     = self._dl_element
     for k, v in sorted (pyk.iteritems (bindings)) :
         vs  = a_doc (v, indent)
         for l in dle (":%s:" % k, * vs, indent = indent) :
             yield l
Esempio n. 13
0
 def check_attr_conflicts (self, entity, initial_values) :
     result = False
     for name, acs in pyk.iteritems (self.attribute_changes) :
         attr    = entity.attr_prop   (name)
         ini     = initial_values.get (name)
         result += acs.check_conflict (attr, entity, ini)
     return result
Esempio n. 14
0
File: Attr.py Progetto: Tapyr/tapyr
 def col_values_from_cargo (self, pickle_cargos) :
     pickle_cargo = pickle_cargos [0]
     for name, kind_wrapper in pyk.iteritems (self.db_attrs) :
         attr_pc = pickle_cargo.get (name)
         if attr_pc is not None :
             for k, v in kind_wrapper.col_values_from_cargo (attr_pc) :
                 yield k, v
Esempio n. 15
0
def undotted_dict (d, sep = ".") :
    """Return a dict with un-dotted keys. Dotted keys in `d` are converted
       to nested dictionaries with un-dotted keys in the result.

       >>> def show (d) :
       ...     def gen (d) :
       ...         for k, v in sorted (pyk.iteritems (d)) :
       ...             if isinstance (v, dict) :
       ...                 v = show (v)
       ...             yield "%r : %s" % (k, v)
       ...     return "{%s}" % (", ".join (gen (d)))
       ...
       >>> show (undotted_dict({"a" : 1, "ab.b" : 2}))
       "{'a' : 1, 'ab' : {'b' : 2}}"
       >>> show (undotted_dict({"a" : 1, "ab.b" : 2, "ab.c.d" : 42}))
       "{'a' : 1, 'ab' : {'b' : 2, 'c' : {'d' : 42}}}"
    """
    result = {}
    for k, v in sorted (pyk.iteritems (d)) :
        n = k
        target = result
        if sep in k :
            os, _, n = rsplit_hst (k, sep)
            for o in os.split (sep) :
                target = target.setdefault (o, {})
                assert isinstance (target, dict), "Need a dict: %s, %s" % (o, k)
        target [n] = v
    return result
Esempio n. 16
0
 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)
Esempio n. 17
0
def _setup_all () :
    global __all__
    _g = globals ()
    _g.update (Type.Table)
    _g.update (M_Media.Nick)
    __all__ = tuple \
        (k for (k, v) in pyk.iteritems (_g) if isinstance (v, (M_Media, Type)))
Esempio n. 18
0
 def create_test_dict(self, test_spec, backends=None, bpt=1, combiner=None, ignore=set()):
     result = {}
     if backends is None:
         backends = sos.environ.get("GTW_test_backends", ("HPS:SQL"))
         if backends == "*":
             backends = sorted(self.Backend_Parameters)
         else:
             backends = list(p.strip() for p in backends.split(":"))
     if combiner is None:
         combiner = self.combiner
     if isinstance(ignore, pyk.string_types):
         ignore = set((ignore,))
     elif not isinstance(ignore, set):
         ignore = set(ignore)
     if not isinstance(test_spec, dict):
         test_spec = {"": test_spec}
     for w in combiner((b for b in backends if b not in ignore), bpt):
         for name, code in pyk.iteritems(test_spec):
             key = "_".join(p for p in (name,) + w if p)
             bsd = dict(self._backend_spec(w))
             test = "%s\n\n    %s\n" % (
                 code % bsd,
                 "\n    ".join((">>> try :", "...     Scaffold.reset ()", "... except Exception :", "...     pass")),
             )
             result[key] = test
     return result
Esempio n. 19
0
 def averager (self) :
     result = self._averager
     if result is None :
         result = TFL.DRA.Averager ()
         for i, f in pyk.iteritems (self.distribution) :
             result.add (i for k in range (f))
     return result
Esempio n. 20
0
File: Babel.py Progetto: Tapyr/tapyr
 def compile (self, cmd) :
     for lang, files in pyk.iteritems (self.files_per_language) :
         for po_file_n in files :
             po_file    = TFL.Babel.PO_File.load (po_file_n)
             if po_file.fuzzy and not cmd.use_fuzzy :
                 print \
                     ( "Catalog %r is marked as fuzzy, skipping"
                     % (po_file_n, )
                     )
                 continue
             for message, errors in po_file.catalog.check ():
                 for error in errors :
                     print \
                         ( "Error: %s:%d: %s"
                         % (po_file_n, message.lineno, error)
                         , file = sys.stderr
                         )
             if cmd.javascript :
                 js_file_n  = self._output_file_name (cmd, lang, po_file_n)
                 print \
                     ("compiling catalog %r to %r" % (po_file_n, js_file_n))
                 po_file.generate_js (lang, js_file_n)
             else :
                 mo_file_n  = self._output_file_name \
                     (cmd, lang, po_file_n, suffix = cmd.file_suffix)
                 print \
                     ("compiling catalog %r to %r" % (po_file_n, mo_file_n))
                 po_file.generate_mo (mo_file_n)
Esempio n. 21
0
File: Scope.py Progetto: Tapyr/tapyr
 def destroy_all (cls) :
     """Destroy all scopes."""
     for i, s in sorted (pyk.iteritems (Scope.Table), reverse = True) :
         try :
             s.destroy ()
         except Exception :
             pass
Esempio n. 22
0
 def __init__ (cls, name, bases, dct) :
     cls.__m_super.__init__ (name, bases, dct)
     bn = tuple (reversed ([getattr (b, "_nested_", {}) for b in bases]))
     cls._nested_ = _nested_ = ddict (* bn)
     Q_Root = TFL.Q_Exp.Q_Root
     for k, v in pyk.iteritems (dct) :
         cls._setup_prop (k, v, Q_Root, _nested_)
Esempio n. 23
0
 def _fix_keys_dict_iter (dct) :
     for k, v in pyk.iteritems (dct) :
         if isinstance (k, (int, float)) :
             k = str (k).lower () ### `.lower` because `bool`
         elif k is None :
             k = "null"
         yield k, _fix_keys (v)
Esempio n. 24
0
 def _record_iter (obj, level, compact, seen) :
     for k, v in sorted (pyk.iteritems (obj._kw), key = SK) :
         rk = pyk.text_type (k)
         it = formatted_repr.iter (v, level + 1, compact, seen)
         for x in formatted_repr._structure_iter \
                 (it, level, rk + " = ", "", "", nl_after_open = True) :
             yield x
Esempio n. 25
0
def Elem_Type \
        ( elem_type
        , bases             = None
        , front_args        = ()
        , rest_args         = None
        , init_arg_defaults = {}
        , _autoconvert      = {}
        , ** attributes
        ) :
    """Return a new subclass of XML.Element"""
    if bases is None :
        bases         = (TFL.SDG.XML.Element, )
    attr_names        = []
    front_dict        = dict_from_list (front_args)
    init_arg_defaults = dict (init_arg_defaults)
    for k, v in pyk.iteritems (attributes) :
        init_arg_defaults [k] = v
        if not (k in front_dict or k == rest_args) :
            attr_names.append (k)
    return TFL.SDG.XML.Element.__class__ \
        ( elem_type, bases
        , dict
            ( attr_names        = tuple (sorted (attr_names))
            , _autoconvert      = _autoconvert
            , elem_type         = elem_type
            , front_args        = front_args
            , init_arg_defaults = init_arg_defaults
            , rest_args         = rest_args
            , __module__        = TFL.Caller.globals () ["__name__"]
            )
        )
Esempio n. 26
0
 def _fill_cache (self) :
     if self._cache is None :
         pred        = self._criterion
         result      = dict        ()
         sums        = _Sum_Aggr_  ()
         sum_col     = None
         for row in self.iterable :
             key    = pred (row)
             is_sum = getattr (row, "_IS_SUM", None)
             if is_sum is not None :
                 sums [key] = is_sum (row)
                 sum_col    = getattr (row, "_SUM_CO", None)
             result [key]   = row
         if sums :
             sum_fixed      = []
             for key, row in pyk.iteritems (result) :
                 if sum_col is None :
                     sum_fixed.append (sums [key])
                 else :
                     sum_fixed.append \
                         ( row [:sum_col]
                         + (sums [key], )
                         + row [sum_col + 1:]
                         )
             result         = sum_fixed
         else :
             result         = pyk.itervalues (result)
         if self._distinct and not self.iterable._distinct :
             result  = self._distinct (result)
         self._cache = list (result)
Esempio n. 27
0
File: Attr.py Progetto: Tapyr/tapyr
 def _setup_QC (self, QC, col_map) :
     self.QC        = QC
     QC.MOM_Wrapper = self
     col_map.update ((qn, QC) for qn in self.q_able_names)
     for k, akw in pyk.iteritems (self.q_able_attrs) :
         if k not in self.db_attrs :
             col_map [akw.name] = akw
Esempio n. 28
0
 def __init__ \
     ( self
     , olsr_file
     , processes =    20
     , N         =     0
     , timeout   =   180
     , ip_port   =    {}
     , debug     = False
     , ** kw
     ) :
     self.__super.__init__ (**kw)
     olsr = get_olsr_container (olsr_file)
     self.olsr_nodes = {}
     assert len (olsr.topo.forward)
     for t in pyk.iterkeys (olsr.topo.forward) :
         self.olsr_nodes [t] = True
     for t in pyk.iterkeys (olsr.topo.reverse) :
         self.olsr_nodes [t] = True
     # limit to N elements
     if N :
         self.olsr_nodes = dict \
             ((k, v) for k, v in islice (pyk.iteritems (self.olsr_nodes), N))
     self.pool        = Pool (processes = processes)
     self.mgr         = Manager ()
     self.result_dict = self.mgr.dict ()
     self.timeout     = timeout
     self.ip_port     = ip_port
     self.debug       = debug
     olsr_nodes       = None
     if not debug :
         self.log.setLevel (INFO)
     self.log.debug ("Starting ...")
Esempio n. 29
0
 def __init__ (self, source, target, * args, ** kw) :
     self.pop_to_self (kw, * self._attr_names)
     if kw :
         raise TypeError \
             ("Unknown arguments %s" % (sorted (pyk.iteritems (kw))))
     self.source   = source
     self.target   = target
Esempio n. 30
0
File: SQ.py Progetto: Tapyr/tapyr
 def __str__ (self) :
     result = ["SQ [%s]" % (self.type_name)]
     if self._filters or self._kw :
         args = list (str (f) for f in self._filters)
         args.extend \
             (   "%s = %r" % (k, v)
             for k, v in sorted (pyk.iteritems (self._kw))
             )
         result.append ("filter (%s)" % ", ".join (args))
     if self._group_by :
         args = list (str (f) for f in self._group_by)
         result.append ("group_by (%s)" % ", ".join (args))
     if self._distinct is not None :
         result.append ("distinct (%s)" % self._distinct)
     if self._order_by :
         args = list (str (f) for f in self._order_by)
         result.append ("order_by (%s)" % ", ".join (args))
     if self._limit is not None :
         result.append ("limit (%s)" % (self._limit, ))
     if self._offset is not None :
         result.append ("limit (%s)" % (self._offset, ))
     if self._attr is not None :
         result.append ("attr (%s)" % (self._attr, ))
     if self._attrs :
         result.append ("attrs (%s)" % (self._attrs, ))
     sep = "\n    ." if len (result) > 2 else "."
     return sep.join (result)