pokede pkm: Make the base64 encoding default more sane

This commit is contained in:
Petr Viktorin 2012-10-14 18:17:22 +02:00
parent 3f513653e1
commit e33d25e3df

View file

@ -284,7 +284,6 @@ def command_pkm(*args):
Options: Options:
--gen=NUM, -g Generation to use (4 or 5) --gen=NUM, -g Generation to use (4 or 5)
--crypt, -c Use encrypted binary format.
--format=FORMAT, -f FORMAT --format=FORMAT, -f FORMAT
Select the human-readable format to use. Select the human-readable format to use.
FORMAT can be: FORMAT can be:
@ -292,9 +291,11 @@ def command_pkm(*args):
yaml: use YAML. Needs the PyYAML library yaml: use YAML. Needs the PyYAML library
installed. installed.
python: use Python literal syntax python: use Python literal syntax
--crypt, -c Use encrypted binary format.
--base64, -b Use Base64 encoding for the binary format. --base64, -b Use Base64 encoding for the binary format.
By default, on for 'encode' but off for 'decode' --binary, -B Output raw binary data. This is the default,
--binary, -B Output raw binary data, do not use Base64 but you need to specify -B explicitly if you're
dumping binary data to a terminal.
If no files are given, reads from standard input. If no files are given, reads from standard input.
""").encode(sys.getdefaultencoding(), 'replace') """).encode(sys.getdefaultencoding(), 'replace')
@ -303,8 +304,7 @@ def command_pkm(*args):
parser.add_option('-g', '--gen', default=5, type=int) parser.add_option('-g', '--gen', default=5, type=int)
parser.add_option('-c', '--crypt', action='store_true') parser.add_option('-c', '--crypt', action='store_true')
parser.add_option('-f', '--format', default='json') parser.add_option('-f', '--format', default='json')
parser.add_option('-b', '--base64', action='store_true', parser.add_option('-b', '--base64', action='store_true', default=None)
default=(mode == 'encode'))
parser.add_option('-B', '--no-base64', action='store_false', dest='base64') parser.add_option('-B', '--no-base64', action='store_false', dest='base64')
options, files = parser.parse_args(list(args[1:])) options, files = parser.parse_args(list(args[1:]))
@ -324,6 +324,18 @@ def command_pkm(*args):
UnicodeLoader.add_constructor(u'tag:yaml.org,2002:str', UnicodeLoader.add_constructor(u'tag:yaml.org,2002:str',
construct_yaml_str) construct_yaml_str)
if options.format not in ('yaml', 'json', 'python'):
raise parser.error('Bad "format"')
if mode == 'encode' and options.base64 is None:
try:
isatty = sys.stdout.isatty
except AttributeError:
pass
else:
if isatty():
parser.error('Refusing to dump binary data to terminal. '
'Please use -B to override, or -b for base64.')
if not files: if not files:
# Use sys.stdin in place of name, handle specially later # Use sys.stdin in place of name, handle specially later
@ -342,8 +354,6 @@ def command_pkm(*args):
dict_ = json.loads(content) dict_ = json.loads(content)
elif options.format == 'python': elif options.format == 'python':
dict_ = ast.literal_eval(content) dict_ = ast.literal_eval(content)
else:
raise parser.error('Bad "format"')
struct = cls(session=session, dict_=dict_) struct = cls(session=session, dict_=dict_)
if options.crypt: if options.crypt:
data = struct.as_encrypted data = struct.as_encrypted
@ -365,8 +375,6 @@ def command_pkm(*args):
print json.dumps(dict_), print json.dumps(dict_),
elif options.format == 'python': elif options.format == 'python':
pprint.pprint(dict_) pprint.pprint(dict_)
else:
raise parser.error('Bad "format"')
def command_help(): def command_help():