Example #1
0
def formatted (msg, encoding = "utf-8", body_only = False) :
    fmt_msg = u"\n".join (msg.formatted ())
    if body_only :
        h, _, t = split_hst (fmt_msg, "\n\n")   ### split off headers
        b, _, s = split_hst (t,       "\n--\n") ### split off signature
        fmt_msg = "\n" + b.lstrip ("-").strip () + "\n"
    return fmt_msg.encode (encoding, "replace")
Example #2
0
    def _header_fields(self, header, file_name, file_time):
        l, m, r = ":", ":", ":"
        if header:
            l, _, x = split_hst(header, "|")
            if _:
                m, _, r = split_hst(x, "|")
                if m == ":" and not _:
                    m, r = "", ":"
            else:
                l, m, r = "", l, ""

        def _gen(l, m, r, file_name, file_time):
            now = time.strftime(self.time_fmt, time.localtime())
            for x in (l, m, r):
                if x == "$b":
                    x = Filename(file_name).base
                elif x == "$n":
                    x = file_name
                elif x == "$t":
                    x = now
                yield x

        l, m, r = tuple(_gen(l, m, r, file_name, file_time))
        return \
            ( l if l != ":" else file_time
            , m if m != ":" else file_name
            , r if r != ":" else self._page_number_header ()
            )
Example #3
0
 def _header_fields (self, header, file_name, file_time) :
     l, m, r  = ":", ":", ":"
     if header :
         l, _, x = split_hst (header, "|")
         if _ :
             m, _, r = split_hst (x,  "|")
             if m == ":" and not _ :
                 m, r = "", ":"
         else :
             l, m, r = "", l, ""
     def _gen (l, m, r, file_name, file_time) :
         now = time.strftime (self.time_fmt, time.localtime ())
         for x in (l, m, r) :
             if x == "$b" :
                 x = Filename (file_name).base
             elif x == "$n" :
                 x = file_name
             elif x == "$t" :
                 x = now
             yield x
     l, m, r = tuple (_gen (l, m, r, file_name, file_time))
     return \
         ( l if l != ":" else file_time
         , m if m != ":" else file_name
         , r if r != ":" else self._page_number_header ()
         )
Example #4
0
def formatted(msg, encoding="utf-8", body_only=False):
    fmt_msg = u"\n".join(msg.formatted())
    if body_only:
        h, _, t = split_hst(fmt_msg, "\n\n")  ### split off headers
        b, _, s = split_hst(t, "\n--\n")  ### split off signature
        fmt_msg = "\n" + b.lstrip("-").strip() + "\n"
    return fmt_msg.encode(encoding, "replace")
Example #5
0
File: Enum.py Project: Tapyr/tapyr
 def _convert_values (self, values) :
     result = []
     if not values :
         raise ValueError \
            ("Enum declaration need at least one possible value")
     for v in values :
         item, _, comment = [s.strip () for s in split_hst (v, "//")]
         name, _, value   = [s.strip () for s in split_hst (item, "=")]
         result.append (Enum_Item (name, value, comment))
     return result
Example #6
0
 def _convert_values(self, values):
     result = []
     if not values:
         raise ValueError \
            ("Enum declaration need at least one possible value")
     for v in values:
         item, _, comment = [s.strip() for s in split_hst(v, "//")]
         name, _, value = [s.strip() for s in split_hst(item, "=")]
         result.append(Enum_Item(name, value, comment))
     return result
Example #7
0
def formatted_select(ETW, name="select", select=None, formatter=str):
    sep = nl + indent
    if select is None:
        select = getattr(ETW, name, None)
    if select is None:
        return "No '%s'" % (name, )
    text = formatter(select)

    def _gen(lines, indent=7):
        s = "," + sep + (" " * indent)
        inner_indent = " " * (indent - 7)
        inner_sep = "\n" + inner_indent
        for l in lines:
            if "," in l:
                comps = sorted(c.strip() for c in l.split(","))
                yield s.join(comps)
            elif "JOIN" in l:
                rerep = TFL.Re_Replacer \
                    ( "((?:[A-Z]+ )*JOIN)"
                    , "\n       \\1"
                    )
                p = rerep(l)
                ps = list(x.rstrip() for x in p.split("\n"))
                p = inner_indent + inner_sep.join(ps)
                yield p
            elif "WHERE" in l:
                rerep = TFL.Re_Replacer \
                    ( "((?:\s+)*(?:AND|OR)\s+)"
                    , "\n       \\1"
                    )
                p = rerep(l)
                ps = list(x.rstrip() for x in p.split("\n"))
                p = inner_indent + inner_sep.join(ps)
                yield p
            else:
                yield l

    gindent = 7
    head, _, tail = split_hst(text, " ")
    t_head, t_sep, t_tail = split_hst(tail, "\nFROM (SELECT")
    if t_sep:
        head = sep.join((" ".join((head, t_head)), t_sep.strip()))
        tail, t_sep, t_tail_tip = rsplit_hst(t_tail, ")")
        ttt_lines = t_tail_tip.split(nl)
        gindent += 6
    lines = tail.split(nl)
    result = " ".join \
        ((head, sep.join (x.rstrip () for x in _gen (lines, gindent))))
    if t_sep:
        r_tail = sep.join(x.rstrip() for x in _gen(ttt_lines))
        result = sep.join((result, " " * 5 + t_sep + r_tail))
    return result.rstrip()
Example #8
0
def formatted_select (ETW, name = "select", select = None, formatter = str) :
    sep = nl + indent
    if select is None :
        select = getattr (ETW, name, None)
    if select is None :
        return "No '%s'" % (name, )
    text = formatter (select)
    def _gen (lines, indent = 7) :
        s = "," + sep + (" " * indent)
        inner_indent = " " * (indent - 7)
        inner_sep    = "\n" + inner_indent
        for l in lines :
            if "," in l :
                comps = sorted (c.strip () for c in l.split (","))
                yield s.join (comps)
            elif "JOIN" in l :
                rerep = TFL.Re_Replacer \
                    ( "((?:[A-Z]+ )*JOIN)"
                    , "\n       \\1"
                    )
                p  = rerep (l)
                ps = list (x.rstrip () for x in p.split ("\n"))
                p  = inner_indent + inner_sep.join (ps)
                yield p
            elif "WHERE" in l :
                rerep = TFL.Re_Replacer \
                    ( "((?:\s+)*(?:AND|OR)\s+)"
                    , "\n       \\1"
                    )
                p  = rerep (l)
                ps = list (x.rstrip () for x in p.split ("\n"))
                p  = inner_indent + inner_sep.join (ps)
                yield p
            else :
                yield l
    gindent = 7
    head, _, tail  = split_hst  (text, " ")
    t_head, t_sep, t_tail = split_hst (tail, "\nFROM (SELECT")
    if t_sep :
        head = sep.join ((" ".join ((head, t_head)), t_sep.strip ()))
        tail, t_sep, t_tail_tip = rsplit_hst (t_tail, ")")
        ttt_lines = t_tail_tip.split (nl)
        gindent += 6
    lines  = tail.split (nl)
    result = " ".join \
        ((head, sep.join (x.rstrip () for x in _gen (lines, gindent))))
    if t_sep :
        r_tail = sep.join (x.rstrip () for x in _gen (ttt_lines))
        result = sep.join (( result, " " * 5 + t_sep + r_tail))
    return result.rstrip ()
Example #9
0
 def _convert_field (self, f) :
     if isinstance (f, TFL.SDG.C.Node) :
         return f
     m = self.field_pat.match (f)
     if not m :
         print (f)
         raise TFL.SDG.Invalid_Node (self, f)
     name   = m.group ("name").strip ()
     type   = m.group ("type").strip ()
     init   = (m.group ("init") or "").strip ()
     desc   = (m.group ("desc") or "").strip ()
     volat  = bool ((m.group ("volat")  or "").strip ())
     const  = bool ((m.group ("const")  or "").strip ())
     struct = bool ((m.group ("struct") or "").strip ())
     if init :
         init, _, desc = [x.strip () for x in split_hst (init, "//")]
     if not init :
         init = None
     if m.group ("bounds") :
         bounds = m.group ("bounds") [1:-1].split ("][")
         return TFL.SDG.C.Array \
             ( type, name, bounds, description = desc
             , new_line_col = self.desc_in_new_line
             , const        = const
             )
     else :
         return TFL.SDG.C.Var \
             ( type, name, description = desc
             , new_line_col = self.desc_in_new_line
             , const        = const
             , volatile     = volat
             , struct       = struct
             )
Example #10
0
 def _convert_field (self, f) :
     if isinstance (f, TFL.SDG.C.Node) :
         return f
     m = self.field_pat.match (f)
     if not m :
         print (f)
         raise TFL.SDG.Invalid_Node (self, f)
     name   = m.group ("name").strip ()
     type   = m.group ("type").strip ()
     init   = (m.group ("init") or "").strip ()
     desc   = (m.group ("desc") or "").strip ()
     volat  = bool ((m.group ("volat")  or "").strip ())
     const  = bool ((m.group ("const")  or "").strip ())
     struct = bool ((m.group ("struct") or "").strip ())
     if init :
         init, _, desc = [x.strip () for x in split_hst (init, "//")]
     if not init :
         init = None
     if m.group ("bounds") :
         bounds = m.group ("bounds") [1:-1].split ("][")
         return TFL.SDG.C.Array \
             ( type, name, bounds, description = desc
             , new_line_col = self.desc_in_new_line
             , const        = const
             )
     else :
         return TFL.SDG.C.Var \
             ( type, name, description = desc
             , new_line_col = self.desc_in_new_line
             , const        = const
             , volatile     = volat
             , struct       = struct
             )
Example #11
0
 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 "", ""
Example #12
0
File: I18N.py Project: Tapyr/tapyr
 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 "", ""
Example #13
0
 def _footer_fields \
         (self, footer, file_name, file_time, subject, title, now) :
     l, m, r  = "", "", ""
     if footer.strip () :
         l, _, x = split_hst (footer, "|")
         if _ :
             m, _, r = split_hst (x,  "|")
             if m and not _ :
                 m, r = r, m
         l, m, r = tuple \
             ( self._fields__gen
                 (l, m, r, file_name, file_time, subject, title, now)
             )
     return \
         ( l if l != ":" else (Filename (file_name).base if title else "")
         , m if m != ":" else file_time
         , r if r != ":" else now
         )
Example #14
0
 def _header_fields \
         (self, header, file_name, file_time, subject, title, now) :
     l, m, r  = "", "", ""
     if header.strip () :
         l, _, x = split_hst (header, "|")
         if _ :
             m, _, r = split_hst (x,  "|")
             if m and not _ :
                 m, r = r, m
         l, m, r = tuple \
             ( self._fields__gen
                 (l, m, r, file_name, file_time, subject, title, now)
             )
     return \
         ( l if l != ":" else (title or file_name)
         , m if m != ":" else subject
         , r if r != ":" else "$p#"
         )
Example #15
0
 def __getitem__ (self, key) :
     map     = self.aliases
     rn, ea  = Lib.parseaddr (key)
     l, s, d = split_hst (ea, "@")
     if s :
         try :
             return map [l]
         except KeyError :
             pass
     return map [ea]
Example #16
0
File: Alias.py Project: Tapyr/tapyr
 def __getitem__ (self, key) :
     map     = self.aliases
     rn, ea  = Lib.parseaddr (key)
     l, s, d = split_hst (ea, "@")
     if s :
         try :
             return map [l]
         except KeyError :
             pass
     return map [ea]
Example #17
0
 def _gen_base_dirs (self, bds) :
     cwd = sos.getcwd ()
     for bd in bds :
         if isinstance (bd, pyk.string_types) and bd.startswith ("$") :
             h, _, t = split_hst (bd, "/")
             h       = getattr (self.cmd, h [1:])
             if h == "" :
                 h   = cwd
             bd = "/".join ((h, t)) if t else h
         if bd is not None :
             yield bd
Example #18
0
 def _gen_base_dirs(self, bds):
     cwd = sos.getcwd()
     for bd in bds:
         if isinstance(bd, pyk.string_types) and bd.startswith("$"):
             h, _, t = split_hst(bd, "/")
             h = getattr(self.cmd, h[1:])
             if h == "":
                 h = cwd
             bd = "/".join((h, t)) if t else h
         if bd is not None:
             yield bd
Example #19
0
File: ReST.py Project: Tapyr/tapyr
 def contents (self) :
     lang   = split_hst (getattr (self, "language", "en"), "_") [0]
     result = self._contents_by_lang.get (lang)
     if result is None :
         src = self.src_contents
         if src is not None :
             result = self._contents_by_lang [lang] = ReST.to_html \
                 ( src
                 , encoding = self.encoding
                 , language = lang
                 )
     return result
Example #20
0
 def contents(self):
     lang = split_hst(getattr(self, "language", "en"), "_")[0]
     result = self._contents_by_lang.get(lang)
     if result is None:
         src = self.src_contents
         if src is not None:
             result = self._contents_by_lang [lang] = ReST.to_html \
                 ( src
                 , encoding = self.encoding
                 , language = lang
                 )
     return result
Example #21
0
 def _get_etm(self, name):
     try:
         result = self._etm[name]
     except KeyError:
         pn, _, rest = split_hst(name, ".")
         try:
             result = self._pkg_ns[pn]
         except KeyError:
             raise AttributeError(name)
         for k in rest.split("."):
             result = getattr(result, k)
         self._etm[name] = result
     return result
Example #22
0
File: Scope.py Project: Tapyr/tapyr
 def _get_etm (self, name) :
     try :
         result = self._etm [name]
     except KeyError :
         pn, _, rest = split_hst (name, ".")
         try :
             result = self._pkg_ns [pn]
         except KeyError :
             raise AttributeError (name)
         for k in rest.split (".") :
             result = getattr (result, k)
         self._etm [name] = result
     return result
Example #23
0
 def _pepk_filter (cls, E_Type, key, name, typ, tail, value, default_op) :
     try :
         k, op, v = key.split (",", 3)
     except (ValueError, TypeError) :
         k, _, op = split_hst (key, cls._op_sep)
     else :
         if value and v != value :
             logging.error \
                 ( "Got two different values for %s: %r vs. %r"
                 % (key, v, value)
                 )
     qa    = getattr (E_Type.AQ, k)
     f, fq = cls._setup_attr (E_Type, key, name, op or default_op, value, qa)
     return f
Example #24
0
 def _pepk_filter (cls, E_Type, key, name, typ, tail, value, default_op) :
     try :
         k, op, v = key.split (",", 3)
     except (ValueError, TypeError) :
         k, _, op = split_hst (key, cls._op_sep)
     else :
         if value and v != value :
             logging.error \
                 ( "Got two different values for %s: %r vs. %r"
                 % (key, v, value)
                 )
     qa    = getattr (E_Type.AQ, k)
     f, fq = cls._setup_attr (E_Type, key, name, op or default_op, value, qa)
     return f
Example #25
0
 def __init__ (self, line, anlagenverzeichnis) :
     try :
         ( self.desc, self.supplier, self.flags
         , self.birth_date, self.a_value, self.afa_spec, ifb
         , self.death_date
         )                = split_pat.split     (line, 8)
     except ValueError as exc :
         print (line)
         raise
     death, _, d_reason   = split_hst (self.death_date, "#")
     final                = "31.12.2037"
     self.p_konto         = self._get_p_konto (self.flags)
     self.birth_time      = Date (self.birth_date)
     self.death_time      = Date (death.strip () or final)
     self.death_reason    = d_reason.strip () if d_reason else ""
     self.alive           = self.death_time > anlagenverzeichnis.tail_time
     self.contemporary    = \
         (   self.birth_time <= anlagenverzeichnis.tail_time
         and self.death_time >= anlagenverzeichnis.head_time
         )
     if int (self.death_time.year) < int (anlagenverzeichnis.year) :
         self._setup_dates (self.death_time.year)
     else :
         self._setup_dates (anlagenverzeichnis.year)
     self.half_date       = "1.7.%s" % (self.birth_time.year, )
     if "~" in self.flags :
         self.half_date   = "1.1.%s" % (self.birth_time.year + 1, )
     self.half_time       = Date (self.half_date)
     self.desc            = desc_cleaner        (self.desc)
     currency_match       = currency_pat.search (self.a_value)
     a_value              = self.a_value
     source_currency      = anlagenverzeichnis.source_currency
     if currency_match :
         source_currency  = EU_Currency.Table   [currency_match.group (1)]
         a_value          = currency_pat.sub    ("", a_value)
     if EUC.target_currency is not ATS :
         self.zero        = source_currency     (0.0)
     else :
         self.zero        = source_currency     (1.0)
     self.source_currency = source_currency
     self.birth_value     = source_currency     (TFL.r_eval (a_value))
     self.new_value       = source_currency     (0.0)
     self.out_value       = source_currency     (0.0)
     if "G" in self.flags :
         self.ifb         = FBiG (self, ifb, source_currency)
     else :
         self.ifb         = IFB  (self, ifb, source_currency)
     self._set_cat (self.flags)
Example #26
0
 def __init__ (self, line, anlagenverzeichnis) :
     try :
         ( self.desc, self.supplier, self.flags
         , self.birth_date, self.a_value, self.afa_spec, ifb
         , self.death_date
         )                = split_pat.split     (line, 8)
     except ValueError as exc :
         print (line)
         raise
     death, _, d_reason   = split_hst (self.death_date, "#")
     final                = "31.12.2037"
     self.p_konto         = self._get_p_konto (self.flags)
     self.birth_time      = Date (self.birth_date)
     self.death_time      = Date (death.strip () or final)
     self.death_reason    = d_reason.strip () if d_reason else ""
     self.alive           = self.death_time > anlagenverzeichnis.tail_time
     self.contemporary    = \
         (   self.birth_time <= anlagenverzeichnis.tail_time
         and self.death_time >= anlagenverzeichnis.head_time
         )
     if int (self.death_time.year) < int (anlagenverzeichnis.year) :
         self._setup_dates (self.death_time.year)
     else :
         self._setup_dates (anlagenverzeichnis.year)
     self.half_date       = "1.7.%s" % (self.birth_time.year, )
     if "~" in self.flags :
         self.half_date   = "1.1.%s" % (self.birth_time.year + 1, )
     self.half_time       = Date (self.half_date)
     self.desc            = desc_cleaner        (self.desc)
     currency_match       = currency_pat.search (self.a_value)
     a_value              = self.a_value
     source_currency      = anlagenverzeichnis.source_currency
     if currency_match :
         source_currency  = EU_Currency.Table   [currency_match.group (1)]
         a_value          = currency_pat.sub    ("", a_value)
     if EUC.target_currency is not ATS :
         self.zero        = source_currency     (0.0)
     else :
         self.zero        = source_currency     (1.0)
     self.source_currency = source_currency
     self.birth_value     = source_currency     (TFL.r_eval (a_value))
     self.new_value       = source_currency     (0.0)
     self.out_value       = source_currency     (0.0)
     if "G" in self.flags :
         self.ifb         = FBiG (self, ifb, source_currency)
     else :
         self.ifb         = IFB  (self, ifb, source_currency)
     self._set_cat (self.flags)
Example #27
0
 def _getattr_transitive(self, name):
     name, _, op = split_hst(name, op_sep)
     tr = regexp.type_restriction
     if tr.search(name):
         head, typ, tail = tr.split(name, 1, 2)
         result = self._getattr_transitive_inner(head) if head else self
         try:
             result = result[typ]
         except LookupError:
             raise ValueError \
                 ( _T ("Unknown type %s for attribute %s.%s")
                 % (typ, self.E_Type.type_name, name)
                 )
         if tail:
             result = getattr(result, tail)
     else:
         result = self._getattr_transitive_inner(name)
     return result
Example #28
0
 def _getattr_transitive (self, name) :
     name, _, op = split_hst (name, op_sep)
     tr = regexp.type_restriction
     if tr.search (name) :
         head, typ, tail = tr.split (name, 1, 2)
         result = self._getattr_transitive_inner (head) if head else self
         try :
             result = result [typ]
         except LookupError :
             raise ValueError \
                 ( _T ("Unknown type %s for attribute %s.%s")
                 % (typ, self.E_Type.type_name, name)
                 )
         if tail :
             result = getattr (result, tail)
     else :
         result = self._getattr_transitive_inner (name)
     return result
Example #29
0
 def __init__ (self, key, format, head_form, tail_form, anchor) :
     key, _, rec_form = split_hst (key, ".")
     self.rec_form    = rec_form or None
     self.__super.__init__ (key, format, head_form, tail_form, anchor)
Example #30
0
 def __init__(self, key, format, head_form, tail_form, anchor):
     key, _, rec_form = split_hst(key, ".")
     self.rec_form = rec_form or None
     self.__super.__init__(key, format, head_form, tail_form, anchor)