Simplified color usage.
This commit is contained in:
parent
3af7590827
commit
f734e2f172
@ -1,29 +1,55 @@
|
|||||||
# display
|
# display
|
||||||
|
|
||||||
bold = '\033[1m'
|
BOLD = '\033[1m'
|
||||||
end = '\033[0m'
|
END = '\033[0m'
|
||||||
|
|
||||||
black = '\033[0;30m'
|
COLORS = {
|
||||||
red = '\033[0;31m'
|
'black' : '\033[0;30m',
|
||||||
green = '\033[0;32m'
|
'red' : '\033[0;31m',
|
||||||
yellow = '\033[0;33m'
|
'green' : '\033[0;32m',
|
||||||
blue = '\033[0;34m'
|
'yellow': '\033[0;33m',
|
||||||
purple = '\033[0;35m'
|
'blue' : '\033[0;34m',
|
||||||
cyan = '\033[0;36m'
|
'purple': '\033[0;35m',
|
||||||
grey = '\033[0;37m'
|
'cyan' : '\033[0;36m',
|
||||||
|
'grey' : '\032[0;37m',
|
||||||
|
}
|
||||||
|
|
||||||
# Bold
|
# Bold
|
||||||
bblack = '\033[1;30m'
|
BCOLORS = {
|
||||||
bred = '\033[1;31m'
|
'black' : '\033[1;30m',
|
||||||
bgreen = '\033[1;32m'
|
'red' : '\033[1;31m',
|
||||||
byellow = '\033[1;33m'
|
'green' : '\033[1;32m',
|
||||||
bblue = '\033[1;34m'
|
'yellow': '\033[1;33m',
|
||||||
bpurple = '\033[1;35m'
|
'blue' : '\033[1;34m',
|
||||||
bcyan = '\033[1;36m'
|
'purple': '\033[1;35m',
|
||||||
bgrey = '\033[1;37m'
|
'cyan' : '\033[1;36m',
|
||||||
|
'grey' : '\033[1;37m',
|
||||||
|
}
|
||||||
|
|
||||||
# application specific
|
# application specific
|
||||||
error = red
|
ALIASES = {
|
||||||
normal = grey
|
'error' : 'red',
|
||||||
citekey = purple
|
'normal' : 'grey',
|
||||||
filepath = cyan
|
'citekey' : 'purple',
|
||||||
|
'filepath': 'cyan',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def colored(s, color=None, bold=False):
|
||||||
|
if color in ALIASES:
|
||||||
|
color = ALIASES[color]
|
||||||
|
try:
|
||||||
|
if bold:
|
||||||
|
color_code = BCOLORS[color]
|
||||||
|
else:
|
||||||
|
color_code = COLORS[color]
|
||||||
|
except KeyError:
|
||||||
|
if bold:
|
||||||
|
color_code = CODE
|
||||||
|
else:
|
||||||
|
color_code = ''
|
||||||
|
if color_code != '':
|
||||||
|
end_code = END
|
||||||
|
else:
|
||||||
|
end_code = ''
|
||||||
|
return color_code + s + end_code
|
||||||
|
@ -4,6 +4,7 @@ import shutil
|
|||||||
|
|
||||||
from .. import repo
|
from .. import repo
|
||||||
from ..paper import Paper, NoDocumentFile
|
from ..paper import Paper, NoDocumentFile
|
||||||
|
from .. import files
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
@ -27,12 +28,12 @@ def command(config, bibpath, copy):
|
|||||||
copy = config.get('papers', 'import-copy')
|
copy = config.get('papers', 'import-copy')
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory()
|
||||||
# Get directory for document
|
# Get directory for document
|
||||||
doc_path = rp.get_document_directory(config)
|
doc_path = files.clean_path(rp.get_document_directory(config))
|
||||||
if not (os.path.exists(doc_path) and os.path.isdir(doc_path)):
|
if not (os.path.exists(doc_path) and os.path.isdir(doc_path)):
|
||||||
print "Document directory %s, does not exist." % doc_path
|
print "Document directory %s, does not exist." % doc_path
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
# Extract papers from bib
|
# Extract papers from bib
|
||||||
papers = Paper.many_from_path(bibpath)
|
papers = Paper.many_from_path(bibpath, fatal=False)
|
||||||
for p in papers:
|
for p in papers:
|
||||||
doc_file = None
|
doc_file = None
|
||||||
try:
|
try:
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from ..repo import Repository
|
from ..repo import Repository
|
||||||
from .. import color
|
from ..color import colored
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
@ -16,12 +16,13 @@ def command(config):
|
|||||||
"""Create a .papers directory"""
|
"""Create a .papers directory"""
|
||||||
papersdir = os.getcwd() + '/.papers'
|
papersdir = os.getcwd() + '/.papers'
|
||||||
if not os.path.exists(papersdir):
|
if not os.path.exists(papersdir):
|
||||||
print('{}initializing papers in {}{}{}'.format(
|
print('Initializing papers in {}'.format(
|
||||||
color.grey, color.cyan, papersdir, color.end))
|
colored(papersdir, 'filepath')))
|
||||||
repo = Repository()
|
repo = Repository()
|
||||||
repo.init(papersdir) # Creates directories
|
repo.init(papersdir) # Creates directories
|
||||||
repo.save() # Saves empty repository description
|
repo.save() # Saves empty repository description
|
||||||
else:
|
else:
|
||||||
print('{}error {} : papers already present in {}{}{}'.format(
|
print(colored('error', 'error') +
|
||||||
color.red, color.grey, color.cyan, papersdir, color.end))
|
' : papers already present in {}.'.format(
|
||||||
|
colored(papersdir, 'filepath')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
@ -2,7 +2,7 @@ import subprocess
|
|||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
from .. import pretty
|
from .. import pretty
|
||||||
from .. import color
|
from ..color import colored
|
||||||
from .. import repo
|
from .. import repo
|
||||||
|
|
||||||
|
|
||||||
@ -17,7 +17,11 @@ def command(config):
|
|||||||
for n in range(rp.size()):
|
for n in range(rp.size()):
|
||||||
paper = rp.paper_from_number(n, fatal=True)
|
paper = rp.paper_from_number(n, fatal=True)
|
||||||
bibdesc = pretty.bib_oneliner(paper.bibentry)
|
bibdesc = pretty.bib_oneliner(paper.bibentry)
|
||||||
articles.append((u'{:3d} {}{}{}{} {}'.format(int(n), color.purple, rp.citekeys[n], color.end, (10 - len(paper.citekey))*' ', bibdesc)).encode('utf-8'))
|
articles.append((u'{num:d}: [{citekey}] {descr}'.format(
|
||||||
|
num=int(n),
|
||||||
|
citekey=colored(rp.citekeys[n], 'purple'),
|
||||||
|
descr=bibdesc,
|
||||||
|
)).encode('utf-8'))
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=True) as tmpf:
|
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=True) as tmpf:
|
||||||
tmpf.write('\n'.join(articles))
|
tmpf.write('\n'.join(articles))
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from .. import color
|
from ..color import colored
|
||||||
from .. import repo
|
from .. import repo
|
||||||
from ..paper import NoDocumentFile
|
from ..paper import NoDocumentFile
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
parser = subparsers.add_parser('open', help='{}open the paper in a pdf viewer{}'.format(color.normal, color.end))
|
parser = subparsers.add_parser('open',
|
||||||
parser.add_argument('citekey', help='{}the paper associated citekey{}'.format(color.normal, color.end))
|
help=colored('open the paper in a pdf viewer', 'normal'))
|
||||||
|
parser.add_argument('citekey',
|
||||||
|
help=colored('the paper associated citekey', 'normal'))
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
@ -19,11 +21,10 @@ def command(config, citekey):
|
|||||||
filepath = paper.get_file_path()
|
filepath = paper.get_file_path()
|
||||||
subprocess.Popen([config.get('papers', 'open-cmd'),
|
subprocess.Popen([config.get('papers', 'open-cmd'),
|
||||||
filepath])
|
filepath])
|
||||||
print('{}{}{} opened.{}'.format(
|
print('{} opened.'.format(colored(filepath, 'filepath')))
|
||||||
color.filepath, filepath, color.normal, color.end))
|
|
||||||
else:
|
else:
|
||||||
raise NoDocumentFile
|
raise NoDocumentFile
|
||||||
except NoDocumentFile:
|
except NoDocumentFile:
|
||||||
print('{}error{}: No document associated to this entry {}{}{}'.format(
|
print('{}: No document associated to this entry {}{}{}'.format(
|
||||||
color.error, color.normal, color.citekey, citekey, color.end))
|
colored('error', 'error'), colored('citekey', 'citekey')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
@ -4,7 +4,7 @@ import tempfile
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
import color
|
from .color import colored
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import pybtex
|
import pybtex
|
||||||
@ -19,8 +19,9 @@ try:
|
|||||||
import pybtex.database.output.bibyaml
|
import pybtex.database.output.bibyaml
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print '{}error{}: you need to install Pybtex; try running \'pip install'
|
print(colored('error', 'error')
|
||||||
'pybtex\' or \'easy_install pybtex\''.format(color.red, color.end)
|
+ ': you need to install Pybtex; try running \'pip install'
|
||||||
|
'pybtex\' or \'easy_install pybtex\'')
|
||||||
|
|
||||||
|
|
||||||
_papersdir = None
|
_papersdir = None
|
||||||
@ -42,17 +43,20 @@ def find_papersdir():
|
|||||||
if _papersdir is None:
|
if _papersdir is None:
|
||||||
curdir = os.path.abspath('')
|
curdir = os.path.abspath('')
|
||||||
while curdir != '':
|
while curdir != '':
|
||||||
if (os.path.exists(curdir + '/.papers')
|
curdir_path = os.path.join(clean_path(curdir), '.papers')
|
||||||
and os.path.isdir(curdir + '/.papers')):
|
if (os.path.exists(curdir_path) and os.path.isdir(curdir_path)):
|
||||||
_papersdir = curdir + '/.papers'
|
_papersdir = curdir + '/.papers'
|
||||||
curdir = ''
|
curdir = ''
|
||||||
if curdir == '/':
|
if curdir == '/':
|
||||||
|
curdir = '~'
|
||||||
|
elif curdir == '~':
|
||||||
curdir = ''
|
curdir = ''
|
||||||
else:
|
else:
|
||||||
curdir = os.path.split(curdir)[0]
|
curdir = os.path.split(curdir)[0]
|
||||||
if _papersdir is None:
|
if _papersdir is None:
|
||||||
print '{}error{} : no papers repo found in this directory or in'
|
print (colored('error', 'error')
|
||||||
'any parent directory.{}'.format(color.red, color.grey, color.end)
|
+ ': no papers repo found in this directory or in '
|
||||||
|
'any parent directory.')
|
||||||
exit(-1)
|
exit(-1)
|
||||||
return _papersdir
|
return _papersdir
|
||||||
|
|
||||||
@ -61,22 +65,22 @@ def name_from_path(fullpdfpath, verbose=False):
|
|||||||
name, ext = os.path.splitext(os.path.split(fullpdfpath)[1])
|
name, ext = os.path.splitext(os.path.split(fullpdfpath)[1])
|
||||||
if verbose:
|
if verbose:
|
||||||
if ext != '.pdf' and ext != '.ps':
|
if ext != '.pdf' and ext != '.ps':
|
||||||
print('{}warning{}: extension {}{}{} not recognized{}'.format(
|
print(colored('warning', 'yellow')
|
||||||
color.yellow, color.grey, color.cyan, ext, color.grey,
|
+ '{: extension {ext} not recognized'.format(
|
||||||
color.end))
|
ext=colored(ext, 'cyan')))
|
||||||
return name, ext
|
return name, ext
|
||||||
|
|
||||||
|
|
||||||
def check_file(filepath):
|
def check_file(filepath):
|
||||||
if not os.path.exists(filepath):
|
if not os.path.exists(filepath):
|
||||||
print '{}error{}: {}{}{} does not exists{}'.format(
|
print(colored('error', 'error') +
|
||||||
color.red, color.grey, color.cyan, filepath, color.grey,
|
': {} does not exists'.format(
|
||||||
color.end)
|
colored(filepath, 'filepath')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
if not os.path.isfile(filepath):
|
if not os.path.isfile(filepath):
|
||||||
print '{}error{}: {}{}{} is not a file{}'.format(
|
print(colored('error', 'error')
|
||||||
color.red, color.grey, color.cyan, filepath, color.grey,
|
+ ': {} is not a file'.format(
|
||||||
color.end)
|
colored(filepath, 'filepath')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
|
|
||||||
@ -87,8 +91,9 @@ def write_yamlfile(filepath, datamap):
|
|||||||
with open(filepath, 'w') as f:
|
with open(filepath, 'w') as f:
|
||||||
yaml.dump(datamap, f)
|
yaml.dump(datamap, f)
|
||||||
except IOError:
|
except IOError:
|
||||||
print '{}error{} : impossible to read file {}{:s}{}'.format(
|
print(colored('error', 'error')
|
||||||
color.red, color.grey, color.cyan, filepath, color.end)
|
+ ': impossible to read file {}'.format(
|
||||||
|
colored(filepath, 'filepath')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
|
|
||||||
@ -98,8 +103,9 @@ def read_yamlfile(filepath):
|
|||||||
with open(filepath, 'r') as f:
|
with open(filepath, 'r') as f:
|
||||||
return yaml.load(f)
|
return yaml.load(f)
|
||||||
except IOError:
|
except IOError:
|
||||||
print '{}error{} : impossible to read file {}{:s}{}'.format(
|
print(colored('error', 'error')
|
||||||
color.red, color.grey, color.cyan, filepath, color.end)
|
+ ': impossible to read file {}'.format(
|
||||||
|
colored(filepath, 'filepath')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
|
|
||||||
@ -137,8 +143,9 @@ def load_externalbibfile(fullbibpath):
|
|||||||
parser = pybtex.database.input.bibyaml.Parser()
|
parser = pybtex.database.input.bibyaml.Parser()
|
||||||
bib_data = parser.parse_file(fullbibpath)
|
bib_data = parser.parse_file(fullbibpath)
|
||||||
else:
|
else:
|
||||||
print '{}error{}: {}{}{} not recognized format for bibliography{}'.format(
|
print(colored('error', 'error')
|
||||||
color.red, color.grey, color.cyan, ext, color.grey, color.end)
|
+ ': {} not recognized format for bibliography'.format(
|
||||||
|
colored(ext, 'cyan')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
return bib_data
|
return bib_data
|
||||||
|
@ -172,7 +172,7 @@ class Paper(object):
|
|||||||
return BASE_META.copy()
|
return BASE_META.copy()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def many_from_path(cls, bibpath):
|
def many_from_path(cls, bibpath, fatal=True):
|
||||||
"""Extract list of papers found in bibliographic files in path.
|
"""Extract list of papers found in bibliographic files in path.
|
||||||
"""
|
"""
|
||||||
bibpath = files.clean_path(bibpath)
|
bibpath = files.clean_path(bibpath)
|
||||||
@ -182,5 +182,15 @@ class Paper(object):
|
|||||||
else:
|
else:
|
||||||
all_files = [bibpath]
|
all_files = [bibpath]
|
||||||
bib_data = [files.load_externalbibfile(f) for f in all_files]
|
bib_data = [files.load_externalbibfile(f) for f in all_files]
|
||||||
return [Paper(bibentry=b.entries[k], citekey=k)
|
if fatal:
|
||||||
for b in bib_data for k in b.entries]
|
return [Paper(bibentry=b.entries[k], citekey=k)
|
||||||
|
for b in bib_data for k in b.entries]
|
||||||
|
else:
|
||||||
|
papers = []
|
||||||
|
for b in bib_data:
|
||||||
|
for k in b.entries:
|
||||||
|
try:
|
||||||
|
papers.append(Paper(bibentry=b.entries[k], citekey=k))
|
||||||
|
except ValueError, e:
|
||||||
|
print "Warning, skipping paper (%s)." % e
|
||||||
|
return papers
|
||||||
|
@ -1,16 +1,27 @@
|
|||||||
# display formatting
|
# display formatting
|
||||||
|
|
||||||
import color
|
from color import colored
|
||||||
|
|
||||||
|
|
||||||
def person_repr(p):
|
def person_repr(p):
|
||||||
return ' '.join(s for s in [' '.join(p.first(abbr = True)),
|
return ' '.join(s for s in [
|
||||||
' '.join(p.middle(abbr = True)),
|
' '.join(p.first(abbr=True)),
|
||||||
' '.join(p.prelast(abbr = False)),
|
' '.join(p.middle(abbr=True)),
|
||||||
' '.join(p.last(abbr = False)),
|
' '.join(p.prelast(abbr=False)),
|
||||||
' '.join(p.lineage(abbr = True))] if s)
|
' '.join(p.last(abbr=False)),
|
||||||
|
' '.join(p.lineage(abbr=True))] if s)
|
||||||
|
|
||||||
|
|
||||||
|
def short_authors(bibentry):
|
||||||
|
authors = [person_repr(p) for p in bibentry.persons['author']]
|
||||||
|
if len(authors) < 3:
|
||||||
|
return ', '.join(authors)
|
||||||
|
else:
|
||||||
|
return authors[0] + (' et al.' if len(authors) > 1 else '')
|
||||||
|
|
||||||
|
|
||||||
def bib_oneliner(bibentry):
|
def bib_oneliner(bibentry):
|
||||||
authors = ', '.join(person_repr(p) for p in bibentry.persons['author'])
|
authors = short_authors(bibentry)
|
||||||
title = bibentry.fields['title']
|
title = bibentry.fields['title']
|
||||||
year = bibentry.fields.get('year', '')
|
year = bibentry.fields.get('year', '')
|
||||||
journal = ''
|
journal = ''
|
||||||
@ -18,14 +29,18 @@ def bib_oneliner(bibentry):
|
|||||||
if bibentry.type == 'inproceedings':
|
if bibentry.type == 'inproceedings':
|
||||||
field = 'booktitle'
|
field = 'booktitle'
|
||||||
journal = bibentry.fields.get(field, '')
|
journal = bibentry.fields.get(field, '')
|
||||||
return u'{}{}{} \"{}{}{}\" {}{}{} {}({}{}{}){}'.format(
|
return u'{authors} \"{title}\" {journal} ({year})'.format(
|
||||||
color.green, authors, color.grey, color.bcyan, title, color.grey,
|
authors=colored(authors, 'green'),
|
||||||
color.yellow, journal, color.end, color.grey, color.end, year,
|
title=title,
|
||||||
color.grey, color.end)
|
journal=colored(journal, 'yellow'),
|
||||||
|
year=year,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def bib_desc(bib_data):
|
def bib_desc(bib_data):
|
||||||
article = bib_data.entries[list(bib_data.entries.keys())[0]]
|
article = bib_data.entries[list(bib_data.entries.keys())[0]]
|
||||||
s = '\n'.join('author: {}'.format(person_repr(p)) for p in article.persons['author'])
|
s = '\n'.join('author: {}'.format(person_repr(p))
|
||||||
|
for p in article.persons['author'])
|
||||||
s += '\n'
|
s += '\n'
|
||||||
s += '\n'.join('{}: {}'.format(k, v) for k, v in article.fields.items())
|
s += '\n'.join('{}: {}'.format(k, v) for k, v in article.fields.items())
|
||||||
return s
|
return s
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import color
|
from .color import colored
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import files
|
import files
|
||||||
@ -25,11 +25,11 @@ class Repository(object):
|
|||||||
citekey = self.citekeys[int(number)]
|
citekey = self.citekeys[int(number)]
|
||||||
paper = self.paper_from_citekey(citekey)
|
paper = self.paper_from_citekey(citekey)
|
||||||
return paper
|
return paper
|
||||||
except KeyError:
|
except (IndexError, ValueError):
|
||||||
if fatal:
|
if fatal:
|
||||||
print('{}error{}: no paper with number {}{}{}'.format(
|
print(colored('error', 'error')
|
||||||
color.error, color.normal, color.citekey, citekey,
|
+ ': no paper with number {}'.format(
|
||||||
color.end))
|
colored(number, 'citekey')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
raise(IOError('file not found'))
|
raise(IOError('file not found'))
|
||||||
|
|
||||||
@ -40,9 +40,9 @@ class Repository(object):
|
|||||||
metapath=self.path_to_paper_file(citekey, 'meta'))
|
metapath=self.path_to_paper_file(citekey, 'meta'))
|
||||||
else:
|
else:
|
||||||
if fatal:
|
if fatal:
|
||||||
print('{}error{}: no paper with citekey {}{}{}'.format(
|
print(colored('error', 'error')
|
||||||
color.error, color.normal, color.citekey, citekey,
|
+ ': no paper with citekey {}'.format(
|
||||||
color.end))
|
colored(citekey, 'citekey')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
raise(IOError('file not found'))
|
raise(IOError('file not found'))
|
||||||
|
|
||||||
@ -54,8 +54,9 @@ class Repository(object):
|
|||||||
return self.paper_from_number(key, fatal=False)
|
return self.paper_from_number(key, fatal=False)
|
||||||
except IOError:
|
except IOError:
|
||||||
if fatal:
|
if fatal:
|
||||||
print('{}error{}: paper with citekey or number {}{}{} not found{}'.format(
|
print(colored('error', 'error')
|
||||||
color.error, color.normal, color.citekey, key, color.normal, color.end))
|
+ (': paper with citekey or number %s not found'
|
||||||
|
% colored(key, 'citekey')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
raise(IOError('file not found'))
|
raise(IOError('file not found'))
|
||||||
|
|
||||||
@ -141,7 +142,7 @@ class Repository(object):
|
|||||||
repo = cls()
|
repo = cls()
|
||||||
if papersdir is None:
|
if papersdir is None:
|
||||||
papersdir = files.find_papersdir()
|
papersdir = files.find_papersdir()
|
||||||
repo.papersdir = papersdir
|
repo.papersdir = files.clean_path(papersdir)
|
||||||
repo.load()
|
repo.load()
|
||||||
return repo
|
return repo
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user