Example #1
0
 def hashed(cls, clear_password, salt=None):
     """Hashed value of `clear_password` using `salt`"""
     if salt is None:
         salt = bcrypt.gensalt(cls.default_rounds)
     else:
         salt = pyk.encoded(salt)
     result = bcrypt.hashpw(pyk.encoded(clear_password, "ascii"), salt)
     return pyk.decoded(result, "ascii")
Example #2
0
 def verify (cls, clear_password, hashed_password) :
     """True if `clear_password` and `hashed_password` match"""
     try :
         cp = pyk.encoded (clear_password,  "ascii")
         hp = pyk.encoded (hashed_password, "ascii")
         return bcrypt.checkpw (cp, hp)
     except Exception :
         return False
Example #3
0
 def verify(cls, clear_password, hashed_password):
     """True if `clear_password` and `hashed_password` match"""
     try:
         cp = pyk.encoded(clear_password, "ascii")
         hp = pyk.encoded(hashed_password, "ascii")
         return bcrypt.checkpw(cp, hp)
     except Exception:
         return False
Example #4
0
 def hashed (cls, clear_password, salt = None) :
     """Hashed value of `clear_password` using `salt`"""
     if salt is None :
         salt = bcrypt.gensalt (cls.default_rounds)
     else :
         salt = pyk.encoded (salt)
     result = bcrypt.hashpw (pyk.encoded (clear_password, "ascii"), salt)
     return pyk.decoded (result, "ascii")
Example #5
0
def _main (cmd) :
    if cmd.encoding :
        encoding = cmd.encoding
    else :
        encoding = "iso8859-1" if cmd.Print else "utf-8"
    PMA.default_encoding = encoding
    for msg in messages_from_args (cmd.argv, cmd.msg_base_dirs) :
        txt = formatted (msg, encoding = encoding)
        if cmd.Print :
            from plumbum  import local as pbl
            from _TFL.FCM import open_tempfile
            subject = msg.scope.subject
            pbl.env ["LC_ALL"] = "en_US.%s" % encoding.replace ("-", "")
            with open_tempfile () as (file, temp_name) :
                file.write (txt)
                file.close ()
                a2ps = pbl ["a2ps"] \
                    [ "-s", "-8", "-nL", "-nu", "-nS"
                    , pyk.encoded ("-H%s" % (subject, ), encoding)
                    , "-P%s" % (cmd.printer_name, )
                    , temp_name
                    ]
                if cmd.verbose :
                    print (a2ps)
                a2ps ()
        else :
            print (txt)
Example #6
0
 def b64digest(self, altchars="_-", strip=False):
     if isinstance(altchars, str):
         altchars = pyk.encoded(altchars)
     result = base64.b64encode(self.digest(), altchars)
     if strip:
         result = result.rstrip(b"=")
     return str(result, "ASCII")
Example #7
0
 def b64digest (self, altchars = "_-", strip = False) :
     if isinstance (altchars, pyk.text_type) :
         altchars = pyk.encoded (altchars)
     result = base64.b64encode (self.digest (), altchars)
     if strip :
         result = result.rstrip (b"=")
     return pyk.text_type (result, "ASCII")
Example #8
0
 def _handle_pns \
         (self, outer, inner, b_name, doc_base) :
     n_mod, n_pns = self._handle_package_inner (inner, doc_base)
     scope        = Scope \
         ( b_name = b_name
         , m      = inner
         , q_name = "_".join (inner.__name__.split ("."))
         )
     parts        = [self._stub_pns_template % scope]
     doc_path     = sos.path.join \
         (sos.path.dirname (inner.__file__), "__doc__.py")
     graph_mod    = getattr (inner, "graph", None)
     if graph_mod is not None :
         try :
             self._make_graph (inner, graph_mod, scope.q_name, doc_base)
         except Exception as exc :
             logging.exception (doc_path)
         else :
             parts [0] += (self._stub_pns_template_graph % scope)
     if sos.path.isfile (doc_path) :
         parts [0] += (self._stub_pns_template_doc % scope)
     if n_mod + n_pns :
         parts.append (self._stub_pns_template_toc)
         if n_mod :
             parts.append ("  %s/M_[0-9]*\n" % b_name)
         if n_pns :
             parts.append ("  %s/P_[0-9]*\n" % b_name)
     k      = inner.__name__.split (".") [-1]
     s_body = "\n".join (parts)
     s_name = self._ranked_file_name (outer, k, "P")
     with self._open_stub (doc_base, outer, s_name) as f :
         f.write (pyk.encoded (s_body))
Example #9
0
def _main(cmd):
    if cmd.encoding:
        encoding = cmd.encoding
    else:
        encoding = "iso8859-1" if cmd.Print else "utf-8"
    PMA.default_encoding = encoding
    for msg in messages_from_args(cmd.argv, cmd.msg_base_dirs):
        txt = formatted(msg, encoding=encoding)
        if cmd.Print:
            from plumbum import local as pbl
            from _TFL.FCM import open_tempfile
            subject = msg.scope.subject
            pbl.env["LC_ALL"] = "en_US.%s" % encoding.replace("-", "")
            with open_tempfile() as (file, temp_name):
                file.write(txt)
                file.close()
                a2ps = pbl ["a2ps"] \
                    [ "-s", "-8", "-nL", "-nu", "-nS"
                    , pyk.encoded ("-H%s" % (subject, ), encoding)
                    , "-P%s" % (cmd.printer_name, )
                    , temp_name
                    ]
                if cmd.verbose:
                    print(a2ps)
                a2ps()
        else:
            print(txt)
Example #10
0
 def _stub_mod(self, pns, m, b_name, doc_base):
     if self.mod_skip_pat.search(m.__name__) or m.__name__ in self.mod_skip:
         return False
     x_members = ", ".join(getattr(m, "__sphinx__members", ()))
     s_body = self._stub_mod_template % Scope(m=m, b_name=b_name, x_members=x_members)
     s_name = self._ranked_file_name(pns, b_name, "M")
     with self._open_stub(doc_base, pns, s_name) as f:
         f.write(pyk.encoded(s_body))
     return True
Example #11
0
 def diff(self, new_content):
     if self != new_content:
         old = pyk.text_type(self)
         new = pyk.decoded(new_content, *self.encodings)
         diffs   = self._diffs \
             (old, new, fromfile = self.file_name, tofile = "Result of test")
         diff_no = len(diffs)
         d_name = Filename(".diff", self.file_name).name
         with open(d_name, "wb") as f:
             f.write(pyk.encoded("\n".join(diffs)))
         return "%d difference%s (see %s)" % \
             (diff_no, "" if diff_no == 1 else "s", d_name)
Example #12
0
 def diff (self, new_content) :
     if self != new_content :
         old     = pyk.text_type (self)
         new     = pyk.decoded   (new_content, * self.encodings)
         diffs   = self._diffs \
             (old, new, fromfile = self.file_name, tofile = "Result of test")
         diff_no = len (diffs)
         d_name  = Filename (".diff", self.file_name).name
         with open (d_name, "wb") as f :
             f.write (pyk.encoded ("\n".join (diffs)))
         return "%d difference%s (see %s)" % \
             (diff_no, "" if diff_no == 1 else "s", d_name)
Example #13
0
 def __init__(self, request, data, **kw):
     time = request.current_time
     b64ed = lambda x : \
         pyk.decoded (base64.b64encode (pyk.encoded (x, self.encoding)))
     self.__dict__.update \
         ( cargo     = b64ed (data)
         , data      = data
         , request   = request
         , time      = time
         , timestamp = b64ed (int (time))
         , ** kw
         )
Example #14
0
 def __init__ (self, request, data, ** kw) :
     time  = request.current_time
     b64ed = lambda x : \
         pyk.decoded (base64.b64encode (pyk.encoded (x, self.encoding)))
     self.__dict__.update \
         ( cargo     = b64ed (data)
         , data      = data
         , request   = request
         , time      = time
         , timestamp = b64ed (int (time))
         , ** kw
         )
Example #15
0
 def _generate (cao, p, root, tail = None) :
     name = pjoin (root, tail or p.href_static.lstrip ("/"))
     dir  = sos.path.dirname (name)
     if not sos.path.exists (dir) :
         sos.mkdir_p  (dir)
     if cao.verbose :
         print (name, "...", end = " ")
     asp = p.as_static_page ()
     if asp is not None :
         with open (name, "wb") as f :
             f.write (pyk.encoded (asp))
     if cao.verbose :
         print ("done")
Example #16
0
 def file_info (self, file_name) :
     stat_result    = os.stat (file_name)
     mtime          = stat_result [stat.ST_MTIME]
     size           = stat_result [stat.ST_SIZE]
     last_modified  = datetime.fromtimestamp (mtime)
     offset         = TFL.user_config.time_zone.utcoffset (last_modified)
     last_modified -= offset
     fn_hash        = zlib.adler32 (pyk.encoded (file_name)) & 0xffffffff
     return TFL.Record \
         ( etag           = "sf-%s-%s-%s" % (mtime, size, fn_hash)
         , last_modified  = last_modified
         , content_length = size
         )
Example #17
0
 def _generate (cao, p, root, tail = None) :
     name = pjoin (root, tail or p.href_static.lstrip ("/"))
     dir  = sos.path.dirname (name)
     if not sos.path.exists (dir) :
         sos.mkdir_p  (dir)
     if cao.verbose :
         print (name, "...", end = " ")
     asp = p.as_static_page ()
     if asp is not None :
         with open (name, "wb") as f :
             f.write (pyk.encoded (asp))
     if cao.verbose :
         print ("done")
Example #18
0
 def _stub_mod (self, pns, m, b_name, doc_base) :
     if self.mod_skip_pat.search (m.__name__) or m.__name__ in self.mod_skip:
         return False
     x_members = ", ".join (getattr (m, "__sphinx__members", ()))
     s_body    = self._stub_mod_template % Scope \
         ( m         = m
         , b_name    = b_name
         , x_members = x_members
         )
     s_name    = self._ranked_file_name (pns, b_name, "M")
     with self._open_stub (doc_base, pns, s_name) as f :
         f.write (pyk.encoded (s_body))
     return True
Example #19
0
def kassabuch (account, file) :
    """Write entries in `account' to kassabuch `file'"""
    for e in account.entries :
        if not isinstance (e, Account_Entry) :
            continue
        e.desc = par_sub_pat.sub ("[\\1]", e.desc)
        if "-" in e.dir :
            file.write \
                ( pyk.encoded
                    ( "\\A %6s %-50s (%8s) [%s]\n"
                    % (e.date, e.desc, e.gross.as_string (), e.number)
                    )
                )
        elif "+" in e.dir :
            print ("Income entries not yet implemented", e)
Example #20
0
def kassabuch(account, file):
    """Write entries in `account` to kassabuch `file`"""
    for e in account.entries:
        if not isinstance(e, Account_Entry):
            continue
        e.desc = par_sub_pat.sub("[\\1]", e.desc)
        if "-" in e.dir:
            file.write \
                ( pyk.encoded
                    ( "\\A %6s %-50s (%8s) [%s]\n"
                    % (e.date, e.desc, e.gross.as_string (), e.number)
                    )
                )
        elif "+" in e.dir:
            print("Income entries not yet implemented", e)
Example #21
0
def is_non_ascii(x):
    """Return true if byte-string `x` contains non-ascii characters.

    >>> is_non_ascii ("abc")
    False

    >>> is_non_ascii ("äbc")
    True

    """
    y = pyk.encoded(x, "utf-8")
    try:
        y.decode("ascii")
    except UnicodeDecodeError:
        return True
    else:
        return False
Example #22
0
def _main(cmd):
    """For each argument, write sanitized_filename to stdout. If `-rename` is
       specified, the files are renamed to the sanitized_filenames instead.
    """
    from _TFL import sos
    from _TFL.pyk import pyk
    for f_b in cmd.argv:
        f = pyk.decoded(f_b)
        sf = sanitized_filename(f)
        sf_b = pyk.encoded(sf)
        if cmd.rename:
            if f_b != sf_b and sos.path.exists(f_b):
                sos.rename(f_b, sf_b)
                if cmd.verbose:
                    print("Renamed", f, "to", sf)
        else:
            print(sf, end=cmd.separator)
Example #23
0
File: Ascii.py Project: Tapyr/tapyr
def _main (cmd) :
    """For each argument, write sanitized_filename to stdout. If `-rename` is
       specified, the files are renamed to the sanitized_filenames instead.
    """
    from _TFL     import sos
    from _TFL.pyk import pyk
    for f_b in cmd.argv :
        f    = pyk.decoded (f_b)
        sf   = sanitized_filename (f)
        sf_b = pyk.encoded (sf)
        if cmd.rename :
            if f_b != sf_b and sos.path.exists (f_b) :
                sos.rename (f_b, sf_b)
                if cmd.verbose :
                    pyk.fprint ("Renamed", f, "to", sf)
        else :
            pyk.fprint (sf, end = cmd.separator)
Example #24
0
File: Ascii.py Project: Tapyr/tapyr
def is_non_ascii (x) :
    """Return true if byte-string `x` contains non-ascii characters.

    >>> is_non_ascii ("abc")
    False

    >>> is_non_ascii ("äbc")
    True

    """
    y = pyk.encoded (x, "utf-8")
    try :
        y.decode ("ascii")
    except UnicodeDecodeError :
        return True
    else :
        return False
Example #25
0
 def _handler (self, sub, cmd) :
     time_format        = _Sub_Command_.time_fmt = cmd.time_format
     i_enc              = cmd.input_encoding
     o_enc              = cmd.output_encoding
     pbl.env ["LC_ALL"] = "en_US.%s" % o_enc.replace ("-", "")
     for arg in cmd.argv :
         if arg in ("-", "STDIN") :
             file_time = time.localtime ()
             fn        = "STDIN"
             txt_in    = sys.stdin.read ()
         else :
             file_time = time.localtime (sos.path.getmtime (arg))
             fn        = Filename (arg).relative_to ("~/")
             with open (arg, "rb") as fi :
                 txt_in = fi.read ()
         txt     = txt_in.decode  (i_enc)
         ft      = time.strftime  (time_format, file_time)
         txt_out = txt.encode     (o_enc, "replace")
         options = tuple \
             (pyk.encoded (o) for o in sub.options (cmd, fn, ft))
         pbl_cmd = sub.pbl_cmd.__getitem__ (options)
         with open_tempfile () as (fo, no) :
             fo.write (txt_out)
             fo.close ()
             cx  = pbl_cmd [no]
             if cmd.verbose or cmd.dry_run :
                 print (pbl_cmd, arg, "[%s]" % no)
             if not cmd.dry_run :
                 if cmd.Display :
                     if cmd.Print :
                         print \
                             ( "Specify either -Display or -Print, "
                               "but not both"
                             )
                         raise SystemExit (10)
                     with open_tempfile () as (fd, nd) :
                         fd.write (cx ())
                         fd.close ()
                         pbl [cmd.display_program] [nd] ()
                 else :
                     cx ()
Example #26
0
 def _handler(self, sub, cmd):
     time_format = _Sub_Command_.time_fmt = cmd.time_format
     i_enc = cmd.input_encoding
     o_enc = cmd.output_encoding
     pbl.env["LC_ALL"] = "en_US.%s" % o_enc.replace("-", "")
     for arg in cmd.argv:
         if arg in ("-", "STDIN"):
             file_time = time.localtime()
             fn = "STDIN"
             txt_in = sys.stdin.read()
         else:
             file_time = time.localtime(sos.path.getmtime(arg))
             fn = Filename(arg).relative_to("~/")
             with open(arg, "rb") as fi:
                 txt_in = fi.read()
         txt = txt_in.decode(i_enc).lstrip("\n").rstrip()
         ft = time.strftime(time_format, file_time)
         txt_out = self._encoded(txt, o_enc)
         options = tuple \
             (pyk.encoded (o) for o in sub.options (cmd, fn, ft))
         pbl_cmd = sub.pbl_cmd.__getitem__(options)
         with open_tempfile() as (fo, no):
             fo.write(txt_out)
             fo.close()
             cx = pbl_cmd[no]
             if cmd.verbose or cmd.dry_run:
                 print(pbl_cmd, arg, "[%s]" % no)
             if not cmd.dry_run:
                 if cmd.Display:
                     if cmd.Print:
                         print \
                             ( "Specify either -Display or -Print, "
                               "but not both"
                             )
                         raise SystemExit(10)
                     with open_tempfile() as (fd, nd):
                         fd.write(cx())
                         fd.close()
                         pbl[cmd.display_program][nd]()
                 else:
                     cx()
Example #27
0
 def _create_config_uwsgi (self, cao, config_options, templateer) :
     P            = self._P (cao)
     app_dir      = sos.path.dirname  (self._app_path (cao, P))
     macro        = templateer.GTW.get_macro \
         ("uwsgi_config_ini", templ_name = config_options ["templ_name"])
     script_name  = Filename (".ini", cao.script_path, absolute = True)
     c_path       = script_name.name \
         if script_name.base not in ("-", "stdout") else ""
     socket       = config_options ["socket"]
     if socket.startswith ("unix:") :
         socket   = socket [5:]
     stats_server = config_options ["stats_server"]
     if stats_server.startswith ("unix:") :
         stats_server     = stats_server [5:]
     kw     = dict \
         ( chdir          = script_name.directory
         , chmod_socket   = "660"
         , chown_socket   = "%s:%s"
             % (cao.user or cao.http_user, cao.http_user)
         , config_name    = c_path
         , gid            = cao.group or cao.http_user
         , lazy_apps      = ["false", "true"] [cao.lazy_apps]
         , master         = "true"
         , module         = script_name.base
         , need_threads   = "true"
         , processes      = cao.processes
         , py_path        = reversed
               ([pyk.encoded (app_dir)] + P.py_path.split (":"))
         , server_name    = config_options ["server_name"]
         , socket         = socket
         , stats_server   = stats_server
         , threads        = 0
         , uid            = cao.user or cao.http_user
         , virtualenv     = cao.virtualenv
         )
     config   = macro (** kw)
     config_s = strip_empty_lines (config)
     self._write_config \
         (cao, c_path, config_s, "Created uwsgi config")
Example #28
0
 def _create_wsgi_script \
         (self, cao, argv = (), script_path = None, script_format = None) :
     P      = self._P (cao)
     a_conf = cao.app_config
     try :
         h_conf = cao._spec ["HTTP_Config"].pathes
     except KeyError :
         h_conf = []
     config   = self.App_Config.auto_split.join (a_conf)
     app_dir  = sos.path.dirname  (self._app_path (cao, P))
     app_mod  = sos.path.splitext (sos.path.basename (cao.app_module)) [0]
     args     = ("wsgi", "-config", config) + tuple (argv)
     s_path   = script_path or cao.script_path
     if script_format is None :
         script_format = self._wsgi_script_format
     script = script_format % dict \
         ( app_dir     = app_dir
         , app_mod     = app_mod
         , args        = list (args) if args else ""
         , py_path     = [pyk.encoded (app_dir)] + P.py_path.split (":")
         , server_name = cao.server_name
         )
     self._write_config (cao, s_path, script, "Created wsgi  script")
Example #29
0
def save(filename, body):
    if body:
        with open(filename, "wb") as f:
            f.write(pyk.encoded(body, PMA.default_encoding))
Example #30
0
def safe_str_cmp (lhs, rhs) :
    l = pyk.encoded (lhs, "iso-8859-1")
    r = pyk.encoded (rhs, "iso-8859-1")
    return _wz_safe_str_cmp (l, r)
Example #31
0
 def __eq__ (self, other) :
     return self.encoded == pyk.encoded (other)
Example #32
0
 def _encoded (cls, obj) :
     return pyk.encoded (portable_repr (obj).strip (""""'"""), "utf-8")
Example #33
0
def save (filename, body) :
    if body :
        with open (filename, "wb") as f :
            f.write (pyk.encoded (body, PMA.default_encoding))
Example #34
0
 def _encoded(cls, obj):
     return pyk.encoded(portable_repr(obj).strip(""""'"""), "utf-8")
Example #35
0
 def __lt__ (self, other) :
     return self.encoded < pyk.encoded (other)
Example #36
0
 def __call__ (self, text, mail_opts = (), rcpt_opts = None) :
     email = pyk.email_message_from_bytes (pyk.encoded (text, self.charset))
     self.send_message (email, mail_opts = mail_opts, rcpt_opts = rcpt_opts)
Example #37
0
 def send (self, from_addr, to_addrs, msg, mail_opts = (), rcpt_opts = None) :
     with self.connection () as server :
         msg_x = pyk.encoded (msg, self.charset)
         server.sendmail (from_addr, to_addrs, msg_x, mail_opts, rcpt_opts)
Example #38
0
File: Error.py Project: Tapyr/tapyr
 def __le__ (self, other) :
     return self.encoded <= pyk.encoded (other)
Example #39
0
 def __le__ (self, other) :
     return self.encoded <= pyk.encoded (other)
Example #40
0
def unpickle_time (pv) :
    return datetime.time (pyk.encoded (pv, "latin1"))
Example #41
0
 def encoded (self) :
     return pyk.encoded (self.as_text)
Example #42
0
 def _new (self, mt, st, b) :
     msg = pyk.email_message_from_bytes (pyk.encoded (b, "latin-1"))
     return self.MIME (msg, _subtype = st)
Example #43
0
File: Error.py Project: Tapyr/tapyr
 def encoded (self) :
     return pyk.encoded (self.as_text)
def unpickle_time(pv):
    return datetime.time(pyk.encoded(pv, "latin1"))
Example #45
0
File: Error.py Project: Tapyr/tapyr
 def __eq__ (self, other) :
     return self.encoded == pyk.encoded (other)
Example #46
0
 def _new(self, mt, st, b):
     msg = pyk.email_message_from_bytes(pyk.encoded(b, "latin-1"))
     return self.MIME(msg, _subtype=st)
Example #47
0
def safe_str_cmp(lhs, rhs):
    l = pyk.encoded(lhs, "iso-8859-1")
    r = pyk.encoded(rhs, "iso-8859-1")
    return _wz_safe_str_cmp(l, r)
Example #48
0
File: Error.py Project: Tapyr/tapyr
 def __lt__ (self, other) :
     return self.encoded < pyk.encoded (other)