diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index 2db215f..9c2ea28 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -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) diff --git a/pubs/commands/attach_cmd.py b/pubs/commands/attach_cmd.py index cc04769..d0ea5bf 100644 --- a/pubs/commands/attach_cmd.py +++ b/pubs/commands/attach_cmd.py @@ -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) diff --git a/pubs/commands/export_cmd.py b/pubs/commands/export_cmd.py index bac2af3..0e87296 100644 --- a/pubs/commands/export_cmd.py +++ b/pubs/commands/export_cmd.py @@ -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) diff --git a/pubs/commands/import_cmd.py b/pubs/commands/import_cmd.py index ad889ea..c34de89 100644 --- a/pubs/commands/import_cmd.py +++ b/pubs/commands/import_cmd.py @@ -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)) diff --git a/pubs/commands/init_cmd.py b/pubs/commands/init_cmd.py index 52cfded..416f535 100644 --- a/pubs/commands/init_cmd.py +++ b/pubs/commands/init_cmd.py @@ -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 diff --git a/pubs/commands/list_cmd.py b/pubs/commands/list_cmd.py index dc0a8f7..5b648aa 100644 --- a/pubs/commands/list_cmd.py +++ b/pubs/commands/list_cmd.py @@ -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)) diff --git a/pubs/commands/open_cmd.py b/pubs/commands/open_cmd.py index 5a852c6..7f47a4a 100644 --- a/pubs/commands/open_cmd.py +++ b/pubs/commands/open_cmd.py @@ -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) diff --git a/pubs/commands/remove_cmd.py b/pubs/commands/remove_cmd.py index 141261b..a4245c2 100644 --- a/pubs/commands/remove_cmd.py +++ b/pubs/commands/remove_cmd.py @@ -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]))) diff --git a/pubs/commands/tag_cmd.py b/pubs/commands/tag_cmd.py index 27e605e..34485b8 100644 --- a/pubs/commands/tag_cmd.py +++ b/pubs/commands/tag_cmd.py @@ -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)) diff --git a/pubs/commands/update_cmd.py b/pubs/commands/update_cmd.py index 90e8495..bc1d899 100644 --- a/pubs/commands/update_cmd.py +++ b/pubs/commands/update_cmd.py @@ -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: diff --git a/pubs/content.py b/pubs/content.py index f441d3c..f280a58 100644 --- a/pubs/content.py +++ b/pubs/content.py @@ -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() diff --git a/pubs/pubs_cmd.py b/pubs/pubs_cmd.py index 5d9cf83..2cae25a 100644 --- a/pubs/pubs_cmd.py +++ b/pubs/pubs_cmd.py @@ -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'.") diff --git a/pubs/uis.py b/pubs/uis.py index 1a1d1c8..c019105 100644 --- a/pubs/uis.py +++ b/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 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 message(self, *messages, **kwargs): + kwargs['file'] = self._stdout + print(*messages, **kwargs) - def warning(self, message): - self.print_err("%s: %s" % (color.dye_err('warning', 'yellow'), message)) + def warning(self, message, **kwargs): + kwargs['file'] = self._stderr + print('{}: {}'.format(color.dye_err('warning', 'yellow'), message), **kwargs) - 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 diff --git a/pubs/utils.py b/pubs/utils.py index 73facca..8ca1a2a 100644 --- a/pubs/utils.py +++ b/pubs/utils.py @@ -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 diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 2949b2f..5464d21 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -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