Improves interface of PrintUI
`print_out()` is replaced by `message()`, that redirect the output to the stdout controlled by the PrintUI instance. Other than that, can be used the same way as `print()`.
This commit is contained in:
parent
17b420f102
commit
4b3b2a23bd
@ -135,7 +135,7 @@ def command(args):
|
||||
# elif ui.input_yn('{} has been copied into pubs; should the original be removed?'.format(color.dye_out(docfile, 'bold'))):
|
||||
# content.remove_file(docfile)
|
||||
|
||||
ui.print_out('added to pubs:\n{}'.format(pretty.paper_oneliner(p)))
|
||||
ui.message('added to pubs:\n{}'.format(pretty.paper_oneliner(p)))
|
||||
except ValueError as v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
||||
|
@ -41,7 +41,7 @@ def command(args):
|
||||
# if ui.input_yn('{} has been copied into pubs; should the original be removed?'.format(color.dye_out(document, 'bold'))):
|
||||
# content.remove_file(document)
|
||||
|
||||
ui.print_out('{} attached to {}'.format(color.dye_out(document, 'bold'), color.dye_out(paper.citekey, color.citekey)))
|
||||
ui.message('{} attached to {}'.format(color.dye_out(document, 'bold'), color.dye_out(paper.citekey, color.citekey)))
|
||||
|
||||
except ValueError as v:
|
||||
ui.error(v.message)
|
||||
|
@ -36,4 +36,4 @@ def command(args):
|
||||
bib[p.citekey] = p.bibdata
|
||||
exporter = endecoder.EnDecoder()
|
||||
bibdata_raw = exporter.encode_bibdata(bib)
|
||||
ui.print_out(bibdata_raw)
|
||||
ui.message(bibdata_raw)
|
||||
|
@ -80,7 +80,7 @@ def command(args):
|
||||
ui.error('could not load entry for citekey {}.'.format(k))
|
||||
else:
|
||||
rp.push_paper(p)
|
||||
ui.print_out('{} imported'.format(color.dye_out(p.citekey, color.citekey)))
|
||||
ui.message('{} imported'.format(color.dye_out(p.citekey, color.citekey)))
|
||||
docfile = bibstruct.extract_docfile(p.bibdata)
|
||||
if docfile is None:
|
||||
ui.warning("no file for {}.".format(p.citekey))
|
||||
|
@ -37,7 +37,7 @@ def command(args):
|
||||
color.dye_err(pubsdir, color.filepath)))
|
||||
ui.exit()
|
||||
|
||||
ui.print_out('Initializing pubs in {}'.format(color.dye_out(pubsdir, color.filepath)))
|
||||
ui.message('Initializing pubs in {}'.format(color.dye_out(pubsdir, color.filepath)))
|
||||
|
||||
config().pubsdir = pubsdir
|
||||
config().docsdir = docsdir
|
||||
|
@ -49,7 +49,7 @@ def command(args):
|
||||
else:
|
||||
papers = sorted(papers, key=date_added)
|
||||
if len(papers) > 0:
|
||||
ui.print_out('\n'.join(
|
||||
ui.message('\n'.join(
|
||||
pretty.paper_oneliner(p, citekey_only=args.citekeys)
|
||||
for p in papers))
|
||||
|
||||
|
@ -39,7 +39,7 @@ def command(args):
|
||||
cmd = with_command.split()
|
||||
cmd.append(docpath)
|
||||
subprocess.Popen(cmd)
|
||||
ui.print_out('{} opened.'.format(color.dye(docpath, color.filepath)))
|
||||
ui.message('{} opened.'.format(color.dye(docpath, color.filepath)))
|
||||
except OSError:
|
||||
ui.error("Command does not exist: %s." % with_command)
|
||||
ui.exit(127)
|
||||
|
@ -27,7 +27,7 @@ def command(args):
|
||||
if force or sure:
|
||||
for c in args.citekeys:
|
||||
rp.remove_paper(c)
|
||||
ui.print_out('The paper(s) [{}] were removed'.format(', '.join([color.dye_out(c, color.citekey) for c in args.citekeys])))
|
||||
ui.message('The paper(s) [{}] were removed'.format(', '.join([color.dye_out(c, color.citekey) for c in args.citekeys])))
|
||||
# FIXME: print should check that removal proceeded well.
|
||||
else:
|
||||
ui.print_out('The paper(s) [{}] were *not* removed'.format(', '.join([color.dye_out(c, color.citekey) for c in args.citekeys])))
|
||||
ui.message('The paper(s) [{}] were *not* removed'.format(', '.join([color.dye_out(c, color.citekey) for c in args.citekeys])))
|
||||
|
@ -83,12 +83,12 @@ def command(args):
|
||||
rp = Repository(config())
|
||||
|
||||
if citekeyOrTag is None:
|
||||
ui.print_out(color.dye_out(' '.join(sorted(rp.get_tags())), color.tag))
|
||||
ui.message(color.dye_out(' '.join(sorted(rp.get_tags())), color.tag))
|
||||
else:
|
||||
if rp.databroker.exists(citekeyOrTag):
|
||||
p = rp.pull_paper(citekeyOrTag)
|
||||
if tags == []:
|
||||
ui.print_out(color.dye_out(' '.join(sorted(p.tags)),
|
||||
ui.message(color.dye_out(' '.join(sorted(p.tags)),
|
||||
color.tag))
|
||||
else:
|
||||
add_tags, remove_tags = _tag_groups(_parse_tags(tags))
|
||||
@ -108,5 +108,5 @@ def command(args):
|
||||
len(p.tags.intersection(excluded)) == 0):
|
||||
papers_list.append(p)
|
||||
|
||||
ui.print_out('\n'.join(pretty.paper_oneliner(p)
|
||||
ui.message('\n'.join(pretty.paper_oneliner(p)
|
||||
for p in papers_list))
|
||||
|
@ -19,10 +19,10 @@ def command(args):
|
||||
repo_version = int(config().version)
|
||||
|
||||
if repo_version == code_version:
|
||||
ui.print_out('Your pubs repository is up-to-date.')
|
||||
ui.message('Your pubs repository is up-to-date.')
|
||||
sys.exit(0)
|
||||
elif repo_version > code_version:
|
||||
ui.print_out('Your repository was generated with an newer version of pubs.\n'
|
||||
ui.message('Your repository was generated with an newer version of pubs.\n'
|
||||
'You should not use pubs until you install the newest version.')
|
||||
sys.exit(0)
|
||||
else:
|
||||
|
@ -102,7 +102,7 @@ def check_content(path):
|
||||
|
||||
def _get_byte_url_content(path, ui=None):
|
||||
if ui is not None:
|
||||
ui.print_out(u'dowloading {}'.format(path))
|
||||
ui.message(u'dowloading {}'.format(path))
|
||||
response = urlopen(path)
|
||||
return response.read()
|
||||
|
||||
|
@ -48,7 +48,7 @@ def _update_check(config, ui):
|
||||
'to bypass this error)')
|
||||
sys.exit()
|
||||
elif repo_version < code_version:
|
||||
ui.print_out(
|
||||
ui.message(
|
||||
'warning: your repository version (v{})'.format(repo_version)
|
||||
+ 'must be updated to version {}.\n'.format(code_version)
|
||||
+ "run 'pubs update'.")
|
||||
|
34
pubs/uis.py
34
pubs/uis.py
@ -55,25 +55,17 @@ class PrintUI(object):
|
||||
self._stderr = codecs.getwriter(self.encoding)(_get_raw_stderr(),
|
||||
errors='replace')
|
||||
|
||||
def print_out(self, *strings, **kwargs):
|
||||
"""Like print, but rather than raising an error when a character
|
||||
is not in the terminal's encoding's character set, just silently
|
||||
replaces it.
|
||||
"""
|
||||
print(' '.join(strings), file=self._stdout, **kwargs)
|
||||
def message(self, *messages, **kwargs):
|
||||
kwargs['file'] = self._stdout
|
||||
print(*messages, **kwargs)
|
||||
|
||||
def print_err(self, *strings, **kwargs):
|
||||
"""Like print, but rather than raising an error when a character
|
||||
is not in the terminal's encoding's character set, just silently
|
||||
replaces it.
|
||||
"""
|
||||
print(' '.join(strings), file=self._stderr, **kwargs)
|
||||
def warning(self, message, **kwargs):
|
||||
kwargs['file'] = self._stderr
|
||||
print('{}: {}'.format(color.dye_err('warning', 'yellow'), message), **kwargs)
|
||||
|
||||
def warning(self, message):
|
||||
self.print_err("%s: %s" % (color.dye_err('warning', 'yellow'), message))
|
||||
|
||||
def error(self, message):
|
||||
self.print_err('{}: {}'.format(color.dye_err('error', 'red'), message))
|
||||
def error(self, message, **kwargs):
|
||||
kwargs['file'] = self._stderr
|
||||
print('{}: {}'.format(color.dye_err('error', 'red'), message), **kwargs)
|
||||
|
||||
def exit(self, error_code=1):
|
||||
sys.exit(error_code)
|
||||
@ -119,7 +111,7 @@ class InputUI(PrintUI):
|
||||
option_str = '/'.join(["{}{}".format(color.dye_out(c, 'bold'), s[1:])
|
||||
for c, s in zip(displayed_chars, options)])
|
||||
|
||||
self.print_out('{} {}: '.format(question, option_str), end='')
|
||||
self.message('{} {}: '.format(question, option_str), end='')
|
||||
while True:
|
||||
answer = self.input()
|
||||
if answer is None or answer == '':
|
||||
@ -133,7 +125,7 @@ class InputUI(PrintUI):
|
||||
return option_chars.index(answer.lower())
|
||||
except ValueError:
|
||||
pass
|
||||
self.print_out('Incorrect option.', option_str)
|
||||
self.message('Incorrect option.', option_str)
|
||||
|
||||
|
||||
def input_choice(self, options, option_chars, default=None, question=''):
|
||||
@ -155,7 +147,7 @@ class InputUI(PrintUI):
|
||||
for i, s in enumerate(option_chars)]
|
||||
option_str = ', '.join(["[%s]%s" % (color.dye_out(c, 'cyan'), o)
|
||||
for c, o in zip(displayed_chars, options)])
|
||||
self.print_out(question, option_str)
|
||||
self.message(question, option_str)
|
||||
while True:
|
||||
answer = self.input()
|
||||
if answer is None or answer == '':
|
||||
@ -166,7 +158,7 @@ class InputUI(PrintUI):
|
||||
return option_chars.index(answer.lower())
|
||||
except ValueError:
|
||||
pass
|
||||
self.print_out('Incorrect option.', option_str)
|
||||
self.message('Incorrect option.', option_str)
|
||||
|
||||
def input_yn(self, question='', default='y'):
|
||||
d = 0 if default in (True, 'y', 'yes') else 1
|
||||
|
@ -24,7 +24,7 @@ def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True):
|
||||
citekey))
|
||||
for c in citekeys:
|
||||
p = repo.pull_paper(c)
|
||||
ui.print_out(u' {}'.format(pretty.paper_oneliner(p)))
|
||||
ui.message(u' {}'.format(pretty.paper_oneliner(p)))
|
||||
if exit_on_fail:
|
||||
ui.exit()
|
||||
return citekey
|
||||
|
@ -17,7 +17,7 @@ from pubs.commands import init_cmd, import_cmd
|
||||
|
||||
|
||||
# makes the tests very noisy
|
||||
PRINT_OUTPUT=False
|
||||
messagePUT=False
|
||||
CAPTURE_OUTPUT=True
|
||||
|
||||
|
||||
@ -102,7 +102,7 @@ class CommandTestCase(unittest.TestCase):
|
||||
except fake_env.FakeInput.UnexpectedInput:
|
||||
self.fail('Unexpected input asked by command: {}.'.format(
|
||||
actual_cmd))
|
||||
if PRINT_OUTPUT:
|
||||
if messagePUT:
|
||||
print(outs)
|
||||
return outs
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user