diff options
author | 2015-01-08 13:51:52 -0800 | |
---|---|---|
committer | 2015-01-08 22:07:29 -0800 | |
commit | c4dfd5ac664f6afec52a77ab911c2130e028e402 (patch) | |
tree | a137d1d423b5a913150ac0492020506d6b1a49a7 | |
parent | Revert "gkeys/base.py: Wrap all output with _unicode_decode()" (diff) | |
download | gentoo-keys-c4dfd5ac664f6afec52a77ab911c2130e028e402.tar.gz gentoo-keys-c4dfd5ac664f6afec52a77ab911c2130e028e402.tar.bz2 gentoo-keys-c4dfd5ac664f6afec52a77ab911c2130e028e402.zip |
gkeys: Fix seed file loading, saving unicode issues
Fix unicode cli args issues.
-rw-r--r-- | gkeys/gkeys/actions.py | 36 | ||||
-rw-r--r-- | gkeys/gkeys/seed.py | 28 | ||||
-rw-r--r-- | gkeys/gkeys/seedhandler.py | 6 |
3 files changed, 45 insertions, 25 deletions
diff --git a/gkeys/gkeys/actions.py b/gkeys/gkeys/actions.py index 9d57870..59929cd 100644 --- a/gkeys/gkeys/actions.py +++ b/gkeys/gkeys/actions.py @@ -241,15 +241,15 @@ class Actions(object): if self.config.options['print_results']: if print_key: print() - print("Nick.....:", key.nick) - print("Name.....:", key.name) - print("Keydir...:", key.keydir) + print("Nick.....: %s" % key.nick) + print("Name.....: %s" % key.name) + print("Keydir...: %s" % key.keydir) c = 0 for line in result.split('\n'): if c == 0: - print("Gpg info.:", line) + print("Gpg info.: %s" % line) else: - print(" ", line) + print(" %s" % line) c += 1 self.logger.debug("data output:\n" + str(result)) return (True, result) @@ -289,10 +289,12 @@ class Actions(object): self.logger.debug("ACTIONS: installkey; result.failed = " + str(result.failed)) if self.config.options['print_results']: + msg = "key desired: %(name)s, key added: %(key)s, succeeded:" +\ + " %(success)s, fingerprint: %(fpr)s" for result in results[key.name]: - print("key desired:", key.name, ", key added:", - result.username, ", succeeded:", - not result.failed, ", fingerprint:", result.fingerprint) + print(msg % ({'name': key.name, 'key': result.username, + 'success': not result.failed, + 'fpr': result.fingerprint})) self.logger.debug("stderr_out: " + str(result.stderr_out)) if result.failed: failed.append(key) @@ -488,7 +490,7 @@ class Actions(object): return (False, ["Please provide a nickname or -n *"]) handler = SeedHandler(self.logger, self.config) kwargs = handler.build_gkeydict(args) - self.logger.debug("ACTIONS: addkey; kwargs: %s" % str(kwargs)) + self.logger.debug("ACTIONS: removekey; kwargs: %s" % str(kwargs)) success, installed_keys = self.installed(args)[1] for gkey in installed_keys: if kwargs['nick'] not in gkey.nick: @@ -539,17 +541,21 @@ class Actions(object): self.logger.debug("ACTIONS: importkey; adding key: %s", gkey.name) results[gkey.name] = self.gpg.add_key(gkey) if self.config.options['print_results']: + msg = "key desired: %(name)s, key added: %(key)s, " + \ + "succeeded: %(success)s, fingerprint: %(fpr)s" for result in results[gkey.name]: - print("key desired:", gkey.name, ", key added:", - result.username, ", succeeded:", - not result.failed, ", fingerprint:", result.fingerprint) + print(msg % ({'name': gkey.name, 'key': result.username, + 'success': not result.failed, + 'fpr': result.fingerprint})) self.logger.debug("stderr_out: " + str(result.stderr_out)) if result.failed: - self.logger.debug("ACTIONS: importkey; result.failed = " + str(result.failed)) + self.logger.debug("ACTIONS: importkey; result.failed = " + + str(result.failed)) failed.append(gkey) if not results[gkey.name][0].failed: - print("Importing: ", gkey.name) - self.logger.debug("ACTIONS: importkey; importing key: %s", gkey.name) + print("Importing: %s" % gkey.name) + self.logger.debug("ACTIONS: importkey; importing key: %s" + % gkey.name) _keyring = os.path.join(catdir, args.keyring + '.gpg') self.gpg.add_to_keyring(gkey, catdir, _keyring) if failed and self.output: diff --git a/gkeys/gkeys/seed.py b/gkeys/gkeys/seed.py index 40bdd46..2f6e72a 100644 --- a/gkeys/gkeys/seed.py +++ b/gkeys/gkeys/seed.py @@ -16,8 +16,10 @@ with gentoo-keys specific convienience functions. ''' +import codecs import json import os +import sys from gkeys.exception import UpdateDbError from gkeys.log import logger @@ -25,6 +27,14 @@ from gkeys.gkey import GKEY from gkeys.fileops import ensure_dirs +if sys.version_info[0] >= 3: + def decoder(text, enc='utf_8'): + return text +else: + def decoder(text, enc='utf_8'): + return codecs.decode(text, enc) + + class Seeds(object): '''Handles all seed key file operations''' @@ -51,7 +61,7 @@ class Seeds(object): seedlines = None self.seeds = {} try: - with open(self.filename, "r+") as seedfile: + with codecs.open(self.filename, "r+", encoding='utf_8') as seedfile: seedlines = json.load(seedfile) except IOError as err: self.logger.debug("Seed: load; IOError occurred while loading file") @@ -94,7 +104,7 @@ class Seeds(object): fatal=True) os.umask(int(self.config.get_key("permissions", "files"),0)) try: - with open(self.filename, 'w') as seedfile: + with codecs.open(self.filename, 'w', encoding='utf_8') as seedfile: seedfile.write(self._seeds2json(self.seeds)) seedfile.write("\n") except IOError as err: @@ -156,7 +166,10 @@ class Seeds(object): break result = res else: - result = {dev: gkey for dev, gkey in list(result.items()) if kwargs[key].lower() in getattr(gkey, key).lower()} + result = {dev: gkey for dev, gkey in list(result.items()) + if kwargs[key].lower() + in getattr(gkey, key).lower() + } return sorted(result.values()) @@ -193,10 +206,10 @@ class Seeds(object): if self._list_search(value, val, exact): results.append(seed) elif exact: - if value in val: + if decoder(value) in val: results.append(seed) else: - if value.lower() in val.lower(): + if decoder(value).lower() in val.lower(): results.append(seed) return results @@ -209,11 +222,12 @@ class Seeds(object): found.append(self._list_search(f, values, exact)) return True in found for val in values: + val = val if exact: - if find in val: + if decoder(find) in val: return True else: - if find.lower() in val.lower(): + if decoder(find).lower() in val.lower(): return True return False diff --git a/gkeys/gkeys/seedhandler.py b/gkeys/gkeys/seedhandler.py index 5f15222..adf9507 100644 --- a/gkeys/gkeys/seedhandler.py +++ b/gkeys/gkeys/seedhandler.py @@ -16,7 +16,7 @@ from json import load from gkeys.exception import UpdateDbError from gkeys.gkey import GKEY -from gkeys.seed import Seeds +from gkeys.seed import Seeds, decoder from gkeys.fileops import ensure_dirs @@ -51,10 +51,10 @@ class SeedHandler(object): for attr in GKEY._fields: try: value = getattr(args, attr) - if attr == 'name' and value: + if attr == 'name' and isinstance(value, list): value = " ".join(value) if value is not None: - keyinfo[attr] = value + keyinfo[attr] = decoder(value) except AttributeError: pass return keyinfo |