Skip to content

Commit

Permalink
fix broken test in test_list.py (strip ansi links)
Browse files Browse the repository at this point in the history
  • Loading branch information
perrette committed Jun 27, 2023
1 parent df77639 commit 22b8d29
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
15 changes: 1 addition & 14 deletions papers/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from papers.config import bcolors, Config, search_config, CONFIG_FILE, CONFIG_FILE_LOCAL, DATA_DIR, CONFIG_FILE_LEGACY, BACKUP_DIR
from papers.duplicate import list_duplicates, list_uniques, edit_entries
from papers.bib import Biblio, FUZZY_RATIO, DEFAULT_SIMILARITY, entry_filecheck, backupfile as backupfile_func, isvalidkey
from papers.utils import move, checksum
from papers.utils import move, checksum, ansi_link as link
from papers import __version__


Expand Down Expand Up @@ -806,19 +806,6 @@ def parse_keywords(e):
print(format_entries(entries))


def link(uri, label=None):
"""https://stackoverflow.com/a/71309268/2192272
"""
if label is None:
label = uri
parameters = ''

# OSC 8 ; params ; URI ST <name> OSC 8 ;; ST
escape_mask = '\033]8;{};{}\033\\{}\033]8;;\033\\'

return escape_mask.format(parameters, uri, label)


def statuscmd(parser, o, config):
print(config.status(check_files=not o.no_check_files, verbose=o.verbose))

Expand Down
28 changes: 28 additions & 0 deletions papers/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import shutil
import hashlib

Expand Down Expand Up @@ -27,6 +28,33 @@ def strip_colors(s):
return s


def ansi_link(uri, label=None):
"""https://stackoverflow.com/a/71309268/2192272
"""
if label is None:
label = uri
parameters = ''

# OSC 8 ; params ; URI ST <name> OSC 8 ;; ST
escape_mask = '\033]8;{};{}\033\\{}\033]8;;\033\\'

return escape_mask.format(parameters, uri, label)


ANSI_LINK_RE = re.compile(r'(?P<ansi_sequence>\033]8;(?P<parameter>.*?);(?P<uri>.*?)\033\\(?P<label>.*?)\033]8;;\033\\)')

def strip_ansi_link(s):
for m in ANSI_LINK_RE.findall(s):
s = s.replace(m[0], m[3])
return s


def strip_all(s):
s = strip_colors(s)
s = strip_ansi_link(s)
return s


def check_filesdir(folder):
folder_size = 0
file_count = 0
Expand Down
10 changes: 5 additions & 5 deletions tests/test_list.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bibtexparser
from tests.common import LocalInstallTest, Biblio
from papers.utils import strip_colors
from papers.utils import strip_all

bibtex = """@article{Perrette_2011,
author = {M. Perrette and A. Yool and G. D. Quartly and E. E. Popova},
Expand Down Expand Up @@ -28,13 +28,13 @@ class FormattingTest(ListTest):

def test_format(self):
out = self.papers(f'list -l', sp_cmd='check_output')
self.assertEqual(strip_colors(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean)")
self.assertEqual(strip_all(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean)")

out = self.papers(f'list --key-only', sp_cmd='check_output')
self.assertEqual(out, "Perrette_2011")

out = self.papers(f'list -f month doi', sp_cmd='check_output')
self.assertEqual(strip_colors(out), "Perrette_2011: feb 10.5194/bg-8-515-2011")
self.assertEqual(strip_all(out), "Perrette_2011: feb 10.5194/bg-8-515-2011")


class SearchTest(ListTest):
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_add_tag(self):
self.assertEqual(out, "")

self.papers(f'list --author perrette --add-tag newtag -1')
# self.assertEqual(strip_colors(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean | newtag)")
# self.assertEqual(strip_all(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean | newtag)")

out = self.papers(f'list --tag newtag -1', sp_cmd='check_output')
self.assertEqual(strip_colors(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean | newtag)")
self.assertEqual(strip_all(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean | newtag)")

0 comments on commit 22b8d29

Please sign in to comment.