Skip to content

Commit

Permalink
Allow multiple output formats (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
twaugh committed Oct 5, 2015
1 parent 1f9a885 commit a73930e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
14 changes: 8 additions & 6 deletions journal_brief/cli/main.py
Expand Up @@ -78,9 +78,9 @@ def get_args(self, args):
help='enable debugging')
parser.add_argument('--dry-run', action='store_true', default=False,
help='do not update cursor bookmark file')
parser.add_argument('-o', '--output', metavar='FORMAT',
help='output format for journal entries',
choices=list_formatters())
help = ('output format for journal entries, '
'comma-separated list from {0}'.format(list_formatters()))
parser.add_argument('-o', '--output', metavar='FORMAT', help=help)

cmds = parser.add_subparsers(dest='cmd')
debrief = cmds.add_parser('debrief', help='construct exclusions list')
Expand Down Expand Up @@ -128,8 +128,7 @@ def run(self):
log_level = int(PRIORITY_MAP[priority])
log.debug("priority=%r from args/config", log_level)

output = self.config.get('output', 'short')
formatter = get_formatter(output)
outputs = self.config.get('output', 'short').split(',')
reader = SelectiveReader(this_boot=self.args.b,
log_level=log_level,
inclusions=self.config.get('inclusions'))
Expand All @@ -144,7 +143,10 @@ def run(self):
elif self.args.cmd == 'stats':
self.show_stats(entries, exclusions)
else:
formatter.format(jfilter, output_stream=sys.stdout)
for output in outputs:
formatter = get_formatter(output)
formatter.format(jfilter, output_stream=sys.stdout)
## TODO: But now all the entries have been read!


def run():
Expand Down
15 changes: 11 additions & 4 deletions journal_brief/config.py
Expand Up @@ -180,10 +180,17 @@ def validate_output(self):
return

formatters = list_formatters()
if self['output'] not in formatters:
yield SemanticError('invalid output format, must be in %s' %
formatters, 'output',
{'output': self['output']})
output = self['output']
if isinstance(output, list):
outputs = output
else:
outputs = [output]

for output in outputs:
if output not in formatters:
yield SemanticError('invalid output format, must be in %s' %
formatters, output,
{'output': self['output']})

def validate_priority(self, valid_prios):
if 'priority' not in self:
Expand Down
49 changes: 27 additions & 22 deletions tests/test_config.py
Expand Up @@ -43,13 +43,12 @@ def test_validation_bad_yaml(self, badyaml):
cfp.write(badyaml.strip())
cfp.flush()
with pytest.raises(ConfigError):
cfg = Config(config_file=cfp.name)

# Test the exception can be represented as a string
try:
cfg = Config(config_file=cfp.name)
except ConfigError as ex:
str(ex)
try:
cfg = Config(config_file=cfp.name)
except ConfigError as ex:
# Test the exception can be represented as a string
str(ex)
raise

@pytest.mark.parametrize('badconfig', [
"- not a map",
Expand All @@ -58,7 +57,6 @@ def test_validation_bad_yaml(self, badyaml):
"debug: [1]",
"debug: debug",
"output: none",
"output: [json]",
"priority: -1",
"priority: [0, 1, 2, error, 2]",
Expand All @@ -74,13 +72,12 @@ def test_validation_bad(self, badconfig):
cfp.write(badconfig.strip())
cfp.flush()
with pytest.raises(ConfigError):
cfg = Config(config_file=cfp.name)

# Test the exception can be represented as a string
try:
cfg = Config(config_file=cfp.name)
except ConfigError as ex:
str(ex)
try:
cfg = Config(config_file=cfp.name)
except ConfigError as ex:
# Test the exception can be represented as a string
str(ex)
raise

@pytest.mark.parametrize('badconfig', [
"{key}: 1",
Expand Down Expand Up @@ -142,10 +139,18 @@ def test_validation_bad_regex(self, badconfig):
cfp.write(badconfig.strip())
cfp.flush()
with pytest.raises(ConfigError):
cfg = Config(config_file=cfp.name)

# Test the exception can be represented as a string
try:
cfg = Config(config_file=cfp.name)
except ConfigError as ex:
str(ex)
try:
cfg = Config(config_file=cfp.name)
except ConfigError as ex:
# Test the exception can be represented as a string
str(ex)
raise

@pytest.mark.parametrize('goodconfig', [
"output: [json-pretty, config]",
])
def test_validation_good(self, goodconfig):
with NamedTemporaryFile(mode='wt') as cfp:
cfp.write(goodconfig.strip())
cfp.flush()
cfg = Config(config_file=cfp.name)

0 comments on commit a73930e

Please sign in to comment.