better output options

This commit is contained in:
brent s 2018-04-15 23:02:55 -04:00
parent 6d04f262db
commit 652d616471

View File

@ -7,6 +7,7 @@
# stdlib # stdlib
import argparse import argparse
import datetime import datetime
import os
import re import re
from urllib.request import urlopen, urlparse from urllib.request import urlopen, urlparse
# pypi/pip # pypi/pip
@ -161,12 +162,32 @@ class KeyStats(object):
#indent = 4, #indent = 4,
default = str)) default = str))
elif self.output == 'yaml': elif self.output == 'yaml':
has_yaml = False
if 'YAML_MOD' in os.environ.keys():
_mod = os.environ['YAML_MOD']
try: try:
import pyaml import importlib
print(pyaml.dump(self.stats)) yaml = importlib.import_module(_mod)
has_yaml = True
except (ImportError, ModuleNotFoundError):
raise RuntimeError(('Module "{0}" is not ' +
'installed').format(_mod))
else:
try:
import yaml
has_yaml = True
except ImportError: except ImportError:
raise RuntimeError(('You must have PyYAML installed to use ' + pass
'YAML formatting')) try:
import pyaml as yaml
has_yaml = True
except ImportError:
pass
if not has_yaml:
raise RuntimeError(('You must have the PyYAML or pyaml ' +
'module installed to use YAML ' +
'formatting'))
print(yaml.dump(self.stats))
elif self.output == 'py': elif self.output == 'py':
import pprint import pprint
pprint.pprint(self.stats) pprint.pprint(self.stats)
@ -199,7 +220,11 @@ def parseArgs():
action = 'store_const', action = 'store_const',
const = 'yaml', const = 'yaml',
help = ('Output the data in YAML format (requires ' + help = ('Output the data in YAML format (requires ' +
'PyYAML)')) 'PyYAML or pyaml module). You can prefer which ' +
'one by setting an environment variable, ' +
'YAML_MOD, to "yaml" or "pyaml" (for PyYAML or ' +
'pyaml respectively); otherwise preference ' +
'will be PyYAML > pyaml'))
fmt.add_argument('-p', '--python', fmt.add_argument('-p', '--python',
default = 'py', default = 'py',
dest = 'output', dest = 'output',