adds a *doc add|remove|export|open* command; depricates commands *open* and *attach*; fixed typos in readme.md and uis;
parent
76be98a900
commit
a926c4c654
@ -1,50 +0,0 @@
|
||||
from .. import repo
|
||||
from .. import color
|
||||
from ..uis import get_ui
|
||||
from .. import content
|
||||
|
||||
def parser(subparsers):
|
||||
parser = subparsers.add_parser('attach',
|
||||
help='attach a document to an existing paper')
|
||||
# parser.add_argument('-c', '--copy', action='store_true', default=True,
|
||||
# help="copy document files into library directory (default)")
|
||||
parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True,
|
||||
help="don't copy document files, just create a link.")
|
||||
parser.add_argument('-M', '--move', action='store_true', dest='move', default=False,
|
||||
help="move document instead of of copying (ignored if --link).")
|
||||
parser.add_argument('citekey',
|
||||
help='citekey of the paper')
|
||||
parser.add_argument('document',
|
||||
help='document file')
|
||||
return parser
|
||||
|
||||
|
||||
def command(conf, args):
|
||||
"""
|
||||
:param bibfile: bibtex file (in .bib, .bibml or .yaml format.
|
||||
:param docfile: path (no url yet) to a pdf or ps file
|
||||
"""
|
||||
|
||||
ui = get_ui()
|
||||
|
||||
rp = repo.Repository(conf)
|
||||
paper = rp.pull_paper(args.citekey)
|
||||
|
||||
try:
|
||||
document = args.document
|
||||
rp.push_doc(paper.citekey, document, copy=args.copy)
|
||||
if args.copy:
|
||||
if args.move:
|
||||
content.remove_file(document)
|
||||
# else:
|
||||
# if ui.input_yn('{} has been copied into pubs; should the original be removed?'.format(color.dye_out(document, 'filepath'))):
|
||||
# content.remove_file(document)
|
||||
|
||||
ui.message('{} attached to {}'.format(color.dye_out(document, 'filepath'), color.dye_out(paper.citekey, 'citekey')))
|
||||
|
||||
except ValueError as v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
||||
except IOError as v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
@ -0,0 +1,160 @@
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
from .. import repo
|
||||
from .. import color
|
||||
from ..uis import get_ui
|
||||
from .. import content
|
||||
from ..utils import resolve_citekey, resolve_citekey_list
|
||||
|
||||
# doc --+- add $file $key [[-L|--link] | [-M|--move]] [-f|--force]
|
||||
# +- remove $key [$key [...]] [-f|--force]
|
||||
# +- export $key [$path]
|
||||
# +- open $key [-w|--with $cmd]
|
||||
# supplements attach, open
|
||||
|
||||
def parser(subparsers):
|
||||
doc_parser = subparsers.add_parser('doc', help='manage the document relating to a publication')
|
||||
doc_subparsers = doc_parser.add_subparsers(title='document actions', help='actions to interact with the documents',
|
||||
dest='action')
|
||||
|
||||
add_parser = doc_subparsers.add_parser('add', help='add a document to a publication')
|
||||
add_parser.add_argument('-f', '--force', action='store_true', dest='force', default=False,
|
||||
help='force overwriting an already assigned document')
|
||||
add_parser.add_argument('document', nargs=1, help='document file to assign')
|
||||
add_parser.add_argument('citekey', nargs=1, help='citekey of the publication')
|
||||
add_exclusives = add_parser.add_mutually_exclusive_group()
|
||||
add_exclusives.add_argument('-L', '--link', action='store_false', dest='link', default=False,
|
||||
help='do not copy document files, just create a link')
|
||||
add_exclusives.add_argument('-M', '--move', action='store_true', dest='move', default=False,
|
||||
help='move document instead of of copying (ignored if --link)')
|
||||
|
||||
remove_parser = doc_subparsers.add_parser('remove', help='remove assigned documents from publications')
|
||||
remove_parser.add_argument('citekeys', nargs='+', help='citekeys of the publications')
|
||||
remove_parser.add_argument('-f', '--force', action='store_true', dest='force', default=False,
|
||||
help='force removing assigned documents')
|
||||
|
||||
# favor key+ path over: key
|
||||
export_parser = doc_subparsers.add_parser('export', help='export assigned documents to given path')
|
||||
export_parser.add_argument('citekeys', nargs='+', help='citekeys of the documents to export')
|
||||
export_parser.add_argument('path', nargs=1, help='directory to export the files to')
|
||||
|
||||
open_parser = doc_subparsers.add_parser('open', help='open an assigned document')
|
||||
open_parser.add_argument('citekey', nargs=1, help='citekey of the document to open')
|
||||
open_parser.add_argument('-w', '--with', dest='cmd', help='command to open the file with')
|
||||
|
||||
return doc_parser
|
||||
|
||||
def command(conf, args):
|
||||
|
||||
ui = get_ui()
|
||||
rp = repo.Repository(conf)
|
||||
|
||||
|
||||
# print(args)
|
||||
# ui.exit()
|
||||
|
||||
if args.action == 'add':
|
||||
citekey = resolve_citekey(rp, args.citekey[0], ui=ui, exit_on_fail=True)
|
||||
paper = rp.pull_paper(citekey)
|
||||
|
||||
if paper.docpath is not None and not args.force:
|
||||
msg = ("The publication {} has already the document {} assigned." + os.linesep +
|
||||
"Overwrite? ").format(color.dye_out(paper.citekey, 'citekey'), color.dye_out(paper.docpath, 'filepath'))
|
||||
if not ui.input_yn(question=msg, default='n'):
|
||||
ui.exit(0)
|
||||
else:
|
||||
try:
|
||||
rp.remove_doc(paper.citekey)
|
||||
except (ValueError, IOError) as v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
||||
|
||||
try:
|
||||
document = args.document[0]
|
||||
if args.link:
|
||||
rp.push_doc(paper.citekey, document, copy=False)
|
||||
else:
|
||||
rp.push_doc(paper.citekey, document, copy=True)
|
||||
if not args.link and args.move:
|
||||
content.remove_file(document)
|
||||
|
||||
ui.message('{} added to {}'.format(color.dye_out(document, 'filepath'),
|
||||
color.dye_out(paper.citekey, 'citekey')))
|
||||
except (ValueError, IOError) as v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
||||
|
||||
elif args.action == 'remove':
|
||||
|
||||
for key in resolve_citekey_list(rp, args.citekeys, ui=ui, exit_on_fail=True):
|
||||
paper = rp.pull_paper(key)
|
||||
|
||||
# if there is no document (and the user cares) -> inform + continue
|
||||
if paper.docpath is None and not args.force:
|
||||
ui.message('Publication {} has no assigned document. Not removed.'.format(
|
||||
color.dye_out(paper.citekey, 'citekey')))
|
||||
continue
|
||||
|
||||
if not args.force:
|
||||
msg = 'Do you really want to remove {} from {} ?'.format(color.dye_out(paper.docpath, 'filepath'),
|
||||
color.dye_out(paper.citekey, 'citekey'))
|
||||
if not ui.input_yn(question=msg, default='n'):
|
||||
continue
|
||||
|
||||
try:
|
||||
rp.remove_doc(paper.citekey)
|
||||
except (ValueError, IOError) as v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
||||
|
||||
elif args.action == 'export':
|
||||
|
||||
if os.path.isdir(args.path[0]):
|
||||
path = args.path[0]
|
||||
if not path.endswith('/'):
|
||||
path += '/'
|
||||
else:
|
||||
ui.error('{} is not a directory.'.format(
|
||||
color.dye_err(args.path[0], 'filepath')))
|
||||
ui.exit(1)
|
||||
|
||||
for key in resolve_citekey_list(rp, args.citekeys, ui=ui, exit_on_fail=True):
|
||||
try:
|
||||
paper = rp.pull_paper(key)
|
||||
doc = paper.docpath
|
||||
|
||||
if doc is None:
|
||||
ui.message('Publication {} has no document assigned.'.format(
|
||||
color.dye_out(paper.citekey, 'citekey')))
|
||||
else:
|
||||
real_doc_path = rp.pull_docpath(key)
|
||||
dest_path = path + os.path.basename(real_doc_path)
|
||||
content.copy_content(real_doc_path, dest_path)
|
||||
except (ValueError, IOError) as e:
|
||||
ui.error(e.message)
|
||||
|
||||
elif args.action == 'open':
|
||||
with_command = args.cmd
|
||||
citekey = resolve_citekey(rp, args.citekey[0], ui=ui, exit_on_fail=True)
|
||||
paper = rp.pull_paper(citekey)
|
||||
|
||||
if paper.docpath is None:
|
||||
ui.error('No document associated with the entry {}.'.format(
|
||||
color.dye_err(citekey, 'citekey')))
|
||||
ui.exit()
|
||||
|
||||
if with_command is None:
|
||||
with_command = conf['main']['open_cmd']
|
||||
if with_command is None: # default in conf have not been changed
|
||||
pass # TODO platform specific
|
||||
|
||||
try:
|
||||
docpath = content.system_path(rp.databroker.real_docpath(paper.docpath))
|
||||
cmd = with_command.split()
|
||||
cmd.append(docpath)
|
||||
subprocess.Popen(cmd)
|
||||
ui.message('{} opened.'.format(color.dye_out(docpath, 'filepath')))
|
||||
except OSError:
|
||||
ui.error("Command does not exist: %s." % with_command)
|
||||
ui.exit(127)
|
@ -1,47 +0,0 @@
|
||||
import subprocess
|
||||
|
||||
from .. import repo
|
||||
|
||||
from ..uis import get_ui
|
||||
from .. import color
|
||||
from ..content import system_path
|
||||
from ..utils import resolve_citekey
|
||||
|
||||
def parser(subparsers):
|
||||
parser = subparsers.add_parser('open',
|
||||
help='open the paper in a pdf viewer')
|
||||
parser.add_argument('-w', '--with', dest='with_command', default=None,
|
||||
help='command to use to open the document file')
|
||||
parser.add_argument('citekey',
|
||||
help='citekey of the paper')
|
||||
return parser
|
||||
|
||||
|
||||
def command(conf, args):
|
||||
|
||||
ui = get_ui()
|
||||
with_command = args.with_command
|
||||
|
||||
rp = repo.Repository(conf)
|
||||
citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True)
|
||||
paper = rp.pull_paper(citekey)
|
||||
|
||||
if with_command is None:
|
||||
with_command = conf['main']['open_cmd']
|
||||
if with_command is None: # default in conf have not been changed
|
||||
pass # TODO platform specific
|
||||
|
||||
if paper.docpath is None:
|
||||
ui.error('No document associated with the entry {}.'.format(
|
||||
color.dye_err(citekey, 'citekey')))
|
||||
ui.exit()
|
||||
|
||||
try:
|
||||
docpath = system_path(rp.databroker.real_docpath(paper.docpath))
|
||||
cmd = with_command.split()
|
||||
cmd.append(docpath)
|
||||
subprocess.Popen(cmd)
|
||||
ui.message('{} opened.'.format(color.dye_out(docpath, 'filepath')))
|
||||
except OSError:
|
||||
ui.error("Command does not exist: %s." % with_command)
|
||||
ui.exit(127)
|
Loading…
Reference in new issue