From 53f116abd38f47ef9f51d2fd0e65515e89c9b9be Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sun, 11 Aug 2019 11:25:44 +0200 Subject: Stop using guacamole (no longer in Debian) --- README.rst | 6 +- lesana/command.py | 160 ++++++++++++++++++++++--------------------------- scripts/lesana | 36 ++++++++--- scripts/tellico2lesana | 27 ++++----- setup.py | 1 - 5 files changed, 110 insertions(+), 120 deletions(-) diff --git a/README.rst b/README.rst index fe67833..2dac769 100644 --- a/README.rst +++ b/README.rst @@ -30,7 +30,6 @@ lesana expects to run on a POSIX-like system and requires the following dependencies: * python3 -* `guacamole `_ * xapian_ * `ruamel.yaml `_ * `jinja2 `_ @@ -39,8 +38,7 @@ dependencies: Under debian (and derivatives), the packages to install are:: - apt install python3-guacamole python3-jinja2 python3-ruamel.yaml \ - python3-xapian python3-git + apt install python3-jinja2 python3-ruamel.yaml python3-xapian python3-git (some of those are only available on stretch+ because earlier versions lacked python3 support.) @@ -52,7 +50,7 @@ use ``setup.py`` you will also need setuptools (e.g. from the License ------- -Copyright (C) 2016-2017 Elena Grandi +Copyright (C) 2016-2019 Elena Grandi This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/lesana/command.py b/lesana/command.py index 7f70597..7f683c8 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -3,7 +3,6 @@ import os import subprocess import sys -import guacamole import jinja2 from . import Collection, Entry @@ -59,7 +58,15 @@ def edit_file_in_external_editor(filepath): return True -class New(guacamole.Command): +class Command(): + help = '' + + def _main(self, args): + self.args = args + self.main() + + +class New(Command): arguments = [ (['--collection', '-c'], dict( help='The collection to work on (default .)' @@ -71,12 +78,8 @@ class New(guacamole.Command): )), ] - def register_arguments(self, parser): - for arg in self.arguments: - parser.add_argument(*arg[0], **arg[1]) - - def invoked(self, ctx): - collection = Collection(ctx.args.collection) + def main(self): + collection = Collection(self.args.collection) new_entry = Entry(collection) collection.save_entries([new_entry]) filepath = os.path.join( @@ -85,12 +88,12 @@ class New(guacamole.Command): ) if edit_file_in_external_editor(filepath): collection.update_cache([filepath]) - if ctx.args.git: + if self.args.git: collection.git_add_files([filepath]) print(new_entry) -class Edit(guacamole.Command): +class Edit(Command): arguments = [ (['--collection', '-c'], dict( help='The collection to work on (default .)' @@ -105,18 +108,14 @@ class Edit(guacamole.Command): )), ] - def register_arguments(self, parser): - for arg in self.arguments: - parser.add_argument(*arg[0], **arg[1]) - - def invoked(self, ctx): - collection = Collection(ctx.args.collection) - entries = collection.entries_from_short_uid(ctx.args.uid) + def main(self): + collection = Collection(self.args.collection) + entries = collection.entries_from_short_uid(self.args.uid) if len(entries) > 1: - return "{} is not an unique uid".format(ctx.args.uid) + return "{} is not an unique uid".format(self.args.uid) if not entries: return "Could not find an entry with uid starting with: {}".format( - ctx.args.uid + self.args.uid ) entry = entries[0] filepath = os.path.join( @@ -125,95 +124,83 @@ class Edit(guacamole.Command): ) if edit_file_in_external_editor(filepath): collection.update_cache([filepath]) - if ctx.args.git: + if self.args.git: collection.git_add_files([filepath]) print(entry) -class Index(guacamole.Command): +class Index(Command): arguments = [ - ] - - def register_arguments(self, parser): - parser.add_argument( - '--collection', '-c', - help='The collection to work on (default .)', - ) - parser.add_argument( - 'files', + (['--collection', '-c'], dict( + help='The collection to work on (default .)' + )), + (['files'], dict( help='List of files to index (default: everything)', default=None, - nargs='*' - ) + nargs='*', + )), + ] - def invoked(self, ctx): - collection = Collection(ctx.args.collection) - if ctx.args.files: - files = (os.path.basename(f) for f in ctx.args.files) + def main(self): + collection = Collection(self.args.collection) + if self.args.files: + files = (os.path.basename(f) for f in self.args.files) else: files = None indexed = collection.update_cache(fnames=files) print("Found and indexed {} entries".format(indexed)) -class Search(guacamole.Command): +class Search(Command): arguments = [ - ] - - def register_arguments(self, parser): - parser.add_argument( - '--collection', '-c', + (['--collection', '-c'], dict( help='The collection to work on (default .)' - ) - parser.add_argument( - '--template', '-t', - help='Am', - ), - parser.add_argument( - '--offset', + )), + (['--template', '-t'], dict( + help='Template to use when displaying results', + )), + (['--offset'], dict( type=int, - ), - parser.add_argument( - '--pagesize', + )), + (['--pagesize'], dict( type=int, - ), - parser.add_argument( - '--all', + )), + (['--all'], dict( action='store_true', help='Return all available results' - ) - parser.add_argument( - 'query', + )), + (['query'], dict( help='Xapian query to search in the collection', nargs='+' - ), + )), + ] - def invoked(self, ctx): + def main(self): # TODO: implement "searching" for everything - if ctx.args.offset: + if self.args.offset: logging.warning( "offset exposes an internal knob and MAY BE" + " REMOVED from a future release of lesana" ) - if ctx.args.pagesize: + if self.args.pagesize: logging.warning( "pagesize exposes an internal knob and MAY BE" + " REMOVED from a future release of lesana" ) - offset = ctx.args.offset or 0 - pagesize = ctx.args.pagesize or 12 - collection = Collection(ctx.args.collection) - if ctx.args.query == ['*']: + offset = self.args.offset or 0 + pagesize = self.args.pagesize or 12 + collection = Collection(self.args.collection) + if self.args.query == ['*']: results = collection.get_all_documents() else: - collection.start_search(' '.join(ctx.args.query)) - if ctx.args.all: + collection.start_search(' '.join(self.args.query)) + if self.args.all: results = collection.get_all_search_results() else: results = collection.get_search_results( offset, pagesize) - if ctx.args.template: + if self.args.template: env = jinja2.Environment( loader=jinja2.FileSystemLoader( searchpath='.', @@ -222,7 +209,7 @@ class Search(guacamole.Command): # TODO: add autoescaping settings ) try: - template = env.get_template(ctx.args.template) + template = env.get_template(self.args.template) except jinja2.exceptions.TemplateNotFound as e: logging.error("Could not find template: {}".format(e)) sys.exit(1) @@ -236,7 +223,7 @@ class Search(guacamole.Command): )) -class Init(guacamole.Command): +class Init(Command): arguments = [ (['--collection', '-c'], dict( help='The directory to work on (default .)', @@ -249,30 +236,25 @@ class Init(guacamole.Command): )), ] - def register_arguments(self, parser): - for arg in self.arguments: - parser.add_argument(*arg[0], **arg[1]) - - def invoked(self, ctx): + def main(self): Collection.init( - ctx.args.collection, - git_enabled=ctx.args.git, + self.args.collection, + git_enabled=self.args.git, edit_file=edit_file_in_external_editor ) -class Remove(guacamole.Command): - def register_arguments(self, parser): - parser.add_argument( - '--collection', '-c', +class Remove(Command): + arguments = [ + (['--collection', '-c'], dict( help='The collection to work on (default .)', - ) - parser.add_argument( - 'entries', + )), + (['entries'], dict( help='List of entries to remove', nargs='+', - ) + )), + ] - def invoked(self, ctx): - collection = Collection(ctx.args.collection) - collection.remove_entries(uids=ctx.args.entries) + def main(self): + collection = Collection(self.args.collection) + collection.remove_entries(uids=self.args.entries) diff --git a/scripts/lesana b/scripts/lesana index 02403dc..713f8a7 100755 --- a/scripts/lesana +++ b/scripts/lesana @@ -1,21 +1,39 @@ #!/usr/bin/env python3 -import guacamole +import argparse + from lesana.command import New, Edit, Index, Search, Init, Remove -class Lesana(guacamole.Command): +class Lesana(): """ Manage collections """ - sub_commands = ( - ('new', New), - ('edit', Edit), - ('index', Index), - ('search', Search), - ('init', Init), - ('rm', Remove), + commands = ( + ('new', New()), + ('edit', Edit()), + ('index', Index()), + ('search', Search()), + ('init', Init()), + ('rm', Remove()), ) + def _main(self): + self.parser.print_help() + + def main(self): + self.parser = argparse.ArgumentParser() + self.parser.set_defaults(func=self._main) + self.subparsers = self.parser.add_subparsers() + for name, sub in self.commands: + s_parser = self.subparsers.add_parser(name, help=sub.help) + for arg in sub.arguments: + s_parser.add_argument(*arg[0], **arg[1]) + s_parser.set_defaults(func=sub._main) + self.args = self.parser.parse_args() + self.args.func(self.args) + + + if __name__ == '__main__': Lesana().main() diff --git a/scripts/tellico2lesana b/scripts/tellico2lesana index 6f9cc5a..0d16ad4 100755 --- a/scripts/tellico2lesana +++ b/scripts/tellico2lesana @@ -6,8 +6,6 @@ import zipfile import lesana -import guacamole - NS = {'tellico': 'http://periapsis.org/tellico/'} @@ -28,21 +26,19 @@ F_TYPE_MAP = { } -class T2L(guacamole.Command): +class T2L(lesana.command.Command): """ Manage collections """ - - def register_arguments(self, parser): - parser.add_argument( - '-c', '--collection', + arguments = [ + (['-c', '--collection'], dict( help='Name of the new lesana collection', default=None, - ) - parser.add_argument( - 'file', + )), + (['file'], dict( help='Tellico file to convert to lesana.', - ) + )), + ] def read_field_data(self, xfield): if xfield.tag in self.date_fields: @@ -69,9 +65,8 @@ class T2L(guacamole.Command): data = xfield.text return data - - def invoked(self, ctx): - with zipfile.ZipFile(ctx.args.file, 'r') as zp: + def main(self): + with zipfile.ZipFile(self.args.file, 'r') as zp: tree = ElementTree.parse(zp.open('tellico.xml')) # open collection xml_collection = tree.getroot().find('tellico:collection', NS) @@ -109,7 +104,7 @@ class T2L(guacamole.Command): field['list'] = l_type fields.append(field) # Create a collection with the settings we have loaded - directory = ctx.args.collection or ctx.args.file.replace( + directory = self.args.collection or self.args.file.replace( '.tc', '.lesana' ) @@ -137,7 +132,5 @@ class T2L(guacamole.Command): self.collection.update_cache() - - if __name__ == '__main__': T2L().main() diff --git a/setup.py b/setup.py index b90ae02..afed290 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,6 @@ setup( test_suite='tests', install_requires=[ - 'guacamole', # 'xapian >= 1.4', 'ruamel.yaml', 'jinja2', -- cgit v1.2.3