Exemplo n.º 1
0
    def js(self):
        """Combined Javascript code required by media fragments that can be
           loaded from a single file or included inline in a html <script>
           element.
        """
        env = self.env
        encoding = env.encoding
        handler = env.static_handler
        media = self._Media_R
        if handler and media:

            def _gen(handler, encoding, scripts, media):
                for s in sorted(scripts, key=TFL.Getter.rank):
                    p = handler.get_path(s.src)
                    if p:
                        with open(p, "rb") as file:
                            yield file.read().decode(encoding)
                    else:
                        try:
                            src, p, _ = env.loader.get_source(env, s.src)
                        except Exception:
                            pass
                        else:
                            if p:
                                yield src
                for s in sorted(media.script_files, key=TFL.Getter.rank):
                    yield pyk.decoded(s.body, encoding)
            result = "\n\n".join \
                (TFL.uniq (_gen (handler, encoding, self.scripts_c, media)))
            return result
Exemplo n.º 2
0
 def _e_type_attrs_q_able (self, ET) :
     attrs  = ET.q_able_no_edit
     _own   = ET._Attributes._own_names
     result = []
     if attrs :
         heading   = "Queryable attributes"
         leader    = None
         own_attrs = [a for a in attrs if a.name in _own]
         if len (attrs) > len (own_attrs) :
             def _gen_parents (ET) :
                 for p in ET.parents :
                     if p.q_able_no_edit :
                         ### filter out intermediate ancestors without
                         ### q_able_no_edit attributes of their own
                         ### --> refer to their parents instead
                         own = p._Attributes._own_names
                         if [a for a in p.q_able_no_edit if a.name in own] :
                             yield p
                         else :
                             for mp in _gen_parents (p) :
                                 yield mp
             parents = list (TFL.uniq (_gen_parents (ET)))
             if parents :
                 leader  = "For inherited %s see also %s" % \
                     ( heading.lower ()
                     , ", ".join
                         (   self._heading_ref (p, heading, p.type_name)
                         for p in parents
                         )
                     )
         result = self._e_type_attrs \
             ( ET, own_attrs if leader else attrs, heading, leader
             , electric = True
             )
     return result
Exemplo n.º 3
0
 def _e_type_attrs_q_able (self, ET) :
     attrs  = ET.q_able_no_edit
     _own   = ET._Attributes._own_names
     result = []
     if attrs :
         heading   = "Queryable attributes"
         leader    = None
         own_attrs = [a for a in attrs if a.name in _own]
         if len (attrs) > len (own_attrs) :
             def _gen_parents (ET) :
                 for p in ET.parents :
                     if p.q_able_no_edit :
                         ### filter out intermediate ancestors without
                         ### q_able_no_edit attributes of their own
                         ### --> refer to their parents instead
                         own = p._Attributes._own_names
                         if [a for a in p.q_able_no_edit if a.name in own] :
                             yield p
                         else :
                             yield from _gen_parents (p) 
             parents = list (TFL.uniq (_gen_parents (ET)))
             if parents :
                 leader  = "For inherited %s see also %s" % \
                     ( heading.lower ()
                     , ", ".join
                         (   self._heading_ref (p, heading, p.type_name)
                         for p in parents
                         )
                     )
         result = self._e_type_attrs \
             ( ET, own_attrs if leader else attrs, heading, leader
             , electric = True
             )
     return result
Exemplo n.º 4
0
 def js (self) :
     """Combined Javascript code required by media fragments that can be
        loaded from a single file or included inline in a html <script>
        element.
     """
     env      = self.env
     encoding = env.encoding
     handler  = env.static_handler
     media    = self._Media_R
     if handler and media :
         def _gen (handler, encoding, scripts, media) :
             for s in sorted (scripts, key = TFL.Getter.rank) :
                 p = handler.get_path (s.src)
                 if p :
                     with open (p, "rb") as file :
                         yield file.read ().decode (encoding)
                 else :
                     try :
                         src, p, _ = env.loader.get_source (env, s.src)
                     except Exception :
                         pass
                     else :
                         if p :
                             yield src
             for s in sorted (media.script_files, key = TFL.Getter.rank) :
                 yield pyk.decoded (s.body, encoding)
         result = "\n\n".join \
             (TFL.uniq (_gen (handler, encoding, self.scripts_c, media)))
         return result
Exemplo n.º 5
0
 def templates_e (self) :
     def _gen () :
         yield self
         if self.extends :
             for e in self.extends.templates_e :
                 yield e
     return tuple (TFL.uniq (_gen ()))
Exemplo n.º 6
0
    def templates_e(self):
        def _gen():
            yield self
            if self.extends:
                yield from self.extends.templates_e

        return tuple(TFL.uniq(_gen()))
Exemplo n.º 7
0
 def scripts (self) :
     """Scripts required by media fragments that need to be put into
        separate <script src="..."> elements.
     """
     media = self._Media
     if media :
         return tuple (TFL.uniq (media.scripts))
     return ()
Exemplo n.º 8
0
 def js_on_ready (self) :
     """Combined Javascript code required by media fragments to be
        executed when document is ready.
     """
     media = self._Media
     if media :
         return sorted (TFL.uniq (media.js_on_ready), key = TFL.Getter.rank)
     return ()
Exemplo n.º 9
0
 def js_on_ready(self):
     """Combined Javascript code required by media fragments to be
        executed when document is ready.
     """
     media = self._Media
     if media:
         return sorted(TFL.uniq(media.js_on_ready), key=TFL.Getter.rank)
     return ()
Exemplo n.º 10
0
    def templates_e(self):
        def _gen():
            yield self
            if self.extends:
                for e in self.extends.templates_e:
                    yield e

        return tuple(TFL.uniq(_gen()))
Exemplo n.º 11
0
 def scripts(self):
     """Scripts required by media fragments that need to be put into
        separate <script src="..."> elements.
     """
     media = self._Media
     if media:
         return tuple(TFL.uniq(media.scripts))
     return ()
Exemplo n.º 12
0
 def entities_transitive (self, ems) :
     def _gen (self, ems) :
         for e in self.entities (ems) :
             yield e
             for iea in e.id_entity_attr :
                 v = iea.get_value (e)
                 if v is not None :
                     yield v
     return TFL.uniq (_gen (self, ems))
Exemplo n.º 13
0
 def entities_transitive (self, ems) :
     def _gen (self, ems) :
         for e in self.entities (ems) :
             yield e
             for iea in e.id_entity_attr :
                 v = iea.get_value (e)
                 if v is not None :
                     yield v
     return TFL.uniq (_gen (self, ems))
Exemplo n.º 14
0
    def do_updates_pending(self, obj):
        if self.updates_pending:
            for a in TFL.uniq(self.updates_pending):
                try:
                    a.update(obj)
                except Exception as exc:
                    import sys, traceback

                    traceback.print_exc()
                    print("Error in `update` of attribute %s for %r" % (a, obj), file=sys.stderr)
                    print("   ", repr(a.get_raw(obj)), file=sys.stderr)
                    raise
            self.reset_updates_pending()
Exemplo n.º 15
0
 def do_updates_pending (self, obj) :
     if self.updates_pending :
         for a in TFL.uniq (self.updates_pending) :
             try :
                 a.update (obj)
             except Exception as exc :
                 import sys, traceback; traceback.print_exc ()
                 print \
                     ( "Error in `update` of attribute %s for %r" % (a, obj)
                     , file = sys.stderr
                     )
                 print ("   ", repr (a.get_raw (obj)), file = sys.stderr)
                 raise
         self.reset_updates_pending ()
Exemplo n.º 16
0
 def as_pickle_cargo (self, root) :
     if self.clear_dir :
         self._clear_dir ()
     css_map = {}
     js_map  = {}
     t_set   = set ()
     TEST    = root.TEST
     TT      = root.Templateer.Template_Type
     for t in TFL.uniq (root.template_iter ()) :
         t_set.update (t.templates)
         css_href = self._add_to_map (root, t, "CSS",   css_map)
         js_href  = self._add_to_map (root, t, "js", js_map)
         TT.Media_Map [t.name] = t.get_cached_media (css_href, js_href)
         if self.cache_filenames :
             self._add_filenames (t)
     self._create_cache ("CSS", css_map, None if TEST else CHJ.minified_css)
     self._create_cache ("js",  js_map,  None if TEST else CHJ.minified_js)
     TT.etag = self._get_etag (root, css_map, js_map, t_set)
     return dict \
         ( css_href_map = TT.css_href_map
         , etag         = TT.etag
         , Media_Map    = TT.Media_Map
         )
Exemplo n.º 17
0
 def as_pickle_cargo (self, root) :
     if self.clear_dir :
         self._clear_dir ()
     css_map = {}
     js_map  = {}
     t_set   = set ()
     TEST    = root.TEST
     TT      = root.Templateer.Template_Type
     for t in TFL.uniq (root.template_iter ()) :
         t_set.update (t.templates)
         css_href = self._add_to_map (root, t, "CSS",   css_map)
         js_href  = self._add_to_map (root, t, "js", js_map)
         TT.Media_Map [t.name] = t.get_cached_media (css_href, js_href)
         if self.cache_filenames :
             self._add_filenames (t)
     self._create_cache ("CSS", css_map, None if TEST else CHJ.minified_css)
     self._create_cache ("js",  js_map,  None if TEST else CHJ.minified_js)
     TT.etag = self._get_etag (root, css_map, js_map, t_set)
     return dict \
         ( css_href_map = TT.css_href_map
         , etag         = TT.etag
         , Media_Map    = TT.Media_Map
         )
Exemplo n.º 18
0
    def matching_rules(self, *patterns):
        """Return all rules matching any of the `patterns`.

        Patterns are strings that are matched as follows:

        - `:<name>` : matches all rules that are instances of class `<name>`

        - `~regexp` : matches all rules which `name` matches `regexp`

        - `pattern` : matches all rules which `name` starts with `pattern`

        - `*`       : matches all rules
        """
        def _gen():
            for p in patterns:
                if p == "*":
                    yield self._rules
                elif p.startswith(":"):
                    for scope in (CAL.Day_Rule, CAL):
                        cls = getattr(scope, p[1:], None)
                        if cls is not None:
                            break
                    else:
                        raise NameError(p)
                    yield (r for r in self._rules if isinstance(r, cls))
                elif p.startswith("~"):
                    from _TFL.Regexp import Regexp
                    re = Regexp(p[1:])
                    yield (r for r in self._rules if re.search(r.name))
                else:
                    rbn = self.rules_by_name
                    yield \
                        ( rbn [n]
                        for n in self.rule_name_trie.completions (p) [0]
                        )

        return tuple(TFL.uniq(itertools.chain(*_gen())))
Exemplo n.º 19
0
 def _gen_all(self):
     return TFL.uniq(self.__super._gen_all())
Exemplo n.º 20
0
 def get_Media_R(cls, env, templates):
     P = env.Media_Parameters
     media_fragment_pathes = tuple \
         (TFL.uniq (t.media_path for t in templates if t.media_path))
     if media_fragment_pathes:
         return cls._eval_fragments(media_fragment_pathes, P, env)
Exemplo n.º 21
0
 def scripts_x(self):
     """Scripts required by media fragments that can not be cached."""
     media = self._Media_R
     if media:
         return tuple(s for s in TFL.uniq(media.scripts) if not s.cache_p)
     return ()
Exemplo n.º 22
0
 def rel_links(self):
     media = self._Media
     if media:
         return sorted(TFL.uniq(media.rel_links), key=TFL.Getter.rank)
     return ()
Exemplo n.º 23
0
 def rel_links (self) :
     media = self._Media
     if media :
         return sorted (TFL.uniq (media.rel_links), key = TFL.Getter.rank)
     return ()
Exemplo n.º 24
0
 def scripts_x (self) :
     """Scripts required by media fragments that can not be cached."""
     media = self._Media_R
     if media :
         return tuple (s for s in TFL.uniq (media.scripts) if not s.cache_p)
     return ()
Exemplo n.º 25
0
Arquivo: Media.py Projeto: Tapyr/tapyr
 def _gen_all (self) :
     return TFL.uniq (self.__super._gen_all ())
Exemplo n.º 26
0
 def get_Media_R (cls, env, templates) :
     P = env.Media_Parameters
     media_fragment_pathes = tuple \
         (TFL.uniq (t.media_path for t in templates if t.media_path))
     if media_fragment_pathes :
         return cls._eval_fragments (media_fragment_pathes, P, env)