X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/bbb113f66ef45881f595c8dacbcb492554848878..ad70a954d0c71d5506143d57c63e58aa09d6a13f:/pwsafe diff --git a/pwsafe b/pwsafe index 205df23..40a64ce 100644 --- a/pwsafe +++ b/pwsafe @@ -24,13 +24,13 @@ ### along with Catacomb/Python; if not, write to the Free Software Foundation, ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -###--------------------------------------------------------------------------- +###-------------------------------------------------------------------------- ### Imported modules. from __future__ import with_statement from os import environ -from sys import argv, exit, stdin, stdout, stderr +import sys as SYS; from sys import argv, exit, stdin, stdout, stderr from getopt import getopt, GetoptError from fnmatch import fnmatch import re @@ -41,6 +41,24 @@ from catacomb.pwsafe import * ###-------------------------------------------------------------------------- ### Python version portability. +if SYS.version_info >= (3,): + import io as IO + def hack_stream(stream): + _enc = stream.encoding + _lbuf = stream.line_buffering + _nl = stream.newlines + return IO.TextIOWrapper(stream.detach(), + encoding = _enc, + line_buffering = _lbuf, + newline = _nl, + errors = "surrogateescape") + SYS.stdout = stdout = hack_stream(stdout) + def _text(bin): return bin.decode(errors = "surrogateescape") + def _bin(text): return text.encode(errors = "surrogateescape") +else: + def _text(bin): return bin + def _bin(text): return text + def _excval(): return SYS.exc_info()[1] ###-------------------------------------------------------------------------- @@ -99,7 +117,7 @@ def cmd_changepp(av): def cmd_find(av): if len(av) != 1: return 1 with PW(file) as pw: - try: print(pw[av[0]]) + try: print(_text(pw[_bin(av[0])])) except KeyError: die("Password `%s' not found" % _excval().args[0]) def cmd_store(av): @@ -111,10 +129,10 @@ def cmd_store(av): vpp = C.getpass("Confirm passphrase `%s': " % tag) if pp != vpp: die("passphrases don't match") elif av[1] == '-': - pp = stdin.readline().rstrip('\n') + pp = _bin(stdin.readline().rstrip('\n')) else: - pp = av[1] - pw[av[0]] = pp + pp = _bin(av[1]) + pw[_bin(av[0])] = pp def cmd_copy(av): if len(av) < 1 or len(av) > 2: return 1 @@ -123,7 +141,8 @@ def cmd_copy(av): if len(av) >= 3: pat = av[1] else: pat = None for k in pw_in: - if pat is None or fnmatch(k, pat): pw_out[k] = pw_in[k] + ktext = _text(k) + if pat is None or fnmatch(ktext, pat): pw_out[k] = pw_in[k] def cmd_list(av): if len(av) > 1: return 1 @@ -131,16 +150,17 @@ def cmd_list(av): if len(av) >= 1: pat = av[0] else: pat = None for k in pw: - if pat is None or fnmatch(k, pat): print(k) + ktext = _text(k) + if pat is None or fnmatch(ktext, pat): print(ktext) def cmd_topixie(av): if len(av) > 2: return 1 with PW(file) as pw: pix = C.Pixie() if len(av) == 0: - for tag in pw: pix.set(tag, pw[tag]) + for tag in pw: pix.set(tag, pw[_bin(tag)]) else: - tag = av[0] + tag = _bin(av[0]) if len(av) >= 2: pptag = av[1] else: pptag = av[0] pix.set(pptag, pw[tag]) @@ -148,7 +168,7 @@ def cmd_topixie(av): def cmd_del(av): if len(av) != 1: return 1 with PW(file, writep = True) as pw: - tag = av[0] + tag = _bin(av[0]) try: del pw[tag] except KeyError: die("Password `%s' not found" % _excval().args[0])