You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.0 KiB

#!/usr/bin/env python
import sys, os
import datetime
import shutil
import tempfile
import textwrap
from subprocess import call
import webbrowser
import urllib
try:
import pybtex
except ImportError:
print '{}error{}: you need to install Pybtex; try running \'pip install pybtex\'.'.format(red, end)
# display
bold = '\033[1m'
end = '\033[0m'
black = '\033[0;30m'
red = '\033[0;31m'
green = '\033[0;32m'
yellow = '\033[0;33m'
blue = '\033[0;34m'
purple = '\033[0;35m'
cyan = '\033[0;36m'
grey = '\033[0;37m'
# Bold
bblack = '\033[1;30m'
bred = '\033[1;31m'
bgreen = '\033[1;32m'
byellow = '\033[1;33m'
bblue = '\033[1;34m'
bpurple = '\033[1;35m'
bcyan = '\033[1;36m'
bgrey = '\033[1;37m'
# utility functions
papersdir = None
def find_papersdir():
global papersdir
curdir = os.path.abspath(os.getcwd())
while curdir != '':
if os.path.exists(curdir + '/.papers') and os.path.isdir(curdir + '/.papers'):
papersdir = curdir + '/.papers'
curdir = ''
if curdir == '/':
curdir = ''
else:
curdir = os.path.split(curdir)[0]
if papersdir is None:
print '{}error{} : no papers repo found in this directory or in any parent directory.{}'.format(red, grey, end)
exit(0)
# commands
def init_cmd():
"""Create a .papers directory"""
# create dir
papersdir = os.getcwd() + '/.papers'
if not os.path.exists(papersdir):
print '{}initializing papers in {}{}{}'.format(grey, cyan, papersdir, end)
os.makedirs(papersdir)
else:
print '{}error{} : papers already present in {}{}{}'.format(red, grey, cyan, papersdir, end)
def install_cmd():
"""Install command on the system"""
print '{}file to install : {}{}{}'.format(grey, cyan, __file__, end)
default = '/usr/local/bin'
print "{}folder to install the papers command [{}{:s}{}] : {}".format(grey, cyan, default, grey, end),
sys.stdout.flush()
path = raw_input()
if path == '':
path = default
if not os.path.exists(path):
print "{}error{}: {}{:s}{} does not exist - installation aborted".format(red, end, cyan, path, end)
else:
if os.path.exists(path+'/papers'):
if os.path.samefile(path+'/papers', __file__):
return
shutil.copy(__file__, path)
def websearch_cmd(search_string):
url = 'https://scholar.google.fr/scholar?hl=fr&q={}&lr='.format(urllib.quote_plus(search_string))
webbrowser.open(url)
# argument parsing (old school)
cmds = {'init': init_cmd,
'install': install_cmd,
'websearch': websearch_cmd,
}
error_msg = "{}banana {}banana {}banana{}".format(purple, yellow, cyan, end)
if len(sys.argv) == 1:
print error_msg
else:
cmd = sys.argv[1]
if cmd in cmds and cmd not in ['init', 'install', 'websearch']:
find_papersdir()
try:
args = sys.argv[2:]
cmds[cmd](*args)
except KeyError, TypeError:
print error_msg