summaryrefslogtreecommitdiff
path: root/lesana/command.py
blob: 2915d305f64209c6bbc63cd2d80bc8f654cb813b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import logging
import os
import subprocess
import sys

import guacamole
import jinja2
from . import Collection, Entry


def edit_file_in_external_editor(filepath):
    # First we try to use $EDITOR
    try:
        editor = os.environ['EDITOR']
        subprocess.call([editor, filepath])
    except FileNotFoundError as e:
        if editor in str(e):
            logging.info(
                'Could not open file {} with $EDITOR (currently {})'.format(
                    filepath,
                    editor
                    )
                )
        else:
            logging.warning("Could not open file {}".format(filepath))
            return False
    else:
        return True
    # then we try to use sensible-editor (which should be available on
    # debian and derivatives)
    try:
        subprocess.call(['sensible-editor', filepath])
    except FileNotFoundError as e:
        if 'sensible-editor' in e.strerror:
            logging.debug(
                "Could not open file {} with editor: sensible-editor".format(
                    filepath
                    )
                )
        else:
            logging.warning("Could not open file {}".format(filepath))
            return False
    else:
        return True
    # and finally we fallback to vi, because ed is the standard editor,
    # but that would be way too cruel, and vi is also in posix
    try:
        subprocess.call(['vi', filepath])
    except FileNotFoundError as e:
        if 'vi' in e.strerror:
            logging.warning(
                "Could not open file {} with any known editor".format(filepath)
                )
            return False
        else:
            logging.warning("Could not open file {}".format(filepath))
            return False
    else:
        return True


class New(guacamole.Command):
    arguments = [
        (['--collection', '-c'], dict(
            help='The collection to work on (default .)'
            )),
        (['--no-git'], dict(
            help="Don't add the new entry to git",
            action="store_false",
            dest='git'
            )),
        ]

    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)
        new_entry = Entry(collection)
        collection.save_entries([new_entry])
        filepath = os.path.join(
            collection.itemdir,
            new_entry.fname
            )
        if edit_file_in_external_editor(filepath):
            collection.update_cache([filepath])
            if ctx.args.git:
                collection.git_add_files([filepath])
        print(new_entry)


class Edit(guacamole.Command):
    arguments = [
        (['--collection', '-c'], dict(
            help='The collection to work on (default .)'
            )),
        (['--no-git'], dict(
            help="Don't add the new entry to git",
            action="store_false",
            dest='git'
            )),
        (['uid'], dict(
            help='uid of an entry to edit',
            )),
        ]

    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)
        entry = collection.entry_from_uid(ctx.args.uid)
        if entry is None:
            return "No such entry: {}".format(ctx.args.uid)
        filepath = os.path.join(
            collection.itemdir,
            entry.fname
            )
        if edit_file_in_external_editor(filepath):
            collection.update_cache([filepath])
            if ctx.args.git:
                collection.git_add_files([filepath])
        print(entry)


class Index(guacamole.Command):
    arguments = [
        ]

    def register_arguments(self, parser):
        parser.add_argument(
            '--collection', '-c',
            help='The collection to work on (default .)',
            )
        parser.add_argument(
            'files',
            help='List of files to index (default: everything)',
            default=None,
            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)
        else:
            files = None
        indexed = collection.update_cache(fnames=files)
        print("Found and indexed {} entries".format(indexed))


class Search(guacamole.Command):
    arguments = [
        ]

    def register_arguments(self, parser):
        parser.add_argument(
            '--collection', '-c',
            help='The collection to work on (default .)'
            )
        parser.add_argument(
            '--template', '-t',
            help='Am',
            ),
        parser.add_argument(
            '--offset',
            type=int,
            ),
        parser.add_argument(
            '--pagesize',
            type=int,
            ),
        parser.add_argument(
            '--all',
            action='store_true',
            help='Return all available results'
            )
        parser.add_argument(
            'query',
            help='Xapian query to search in the collection',
            nargs='+'
            ),

    def invoked(self, ctx):
        # TODO: implement "searching" for everything
        if ctx.args.offset:
            logging.warning(
                "offset exposes an internal knob and MAY BE" +
                " REMOVED from a future release of lesana"
                )
        if ctx.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 == ['*']:
            results = collection.get_all_documents()
        else:
            collection.start_search(' '.join(ctx.args.query))
            if ctx.args.all:
                results = collection.get_all_search_results()
            else:
                results = collection.get_search_results(
                    offset,
                    pagesize)
        if ctx.args.template:
            env = jinja2.Environment(
                loader=jinja2.FileSystemLoader(
                    searchpath='.',
                    followlinks=True,
                    ),
                # TODO: add autoescaping settings
                )
            try:
                template = env.get_template(ctx.args.template)
            except jinja2.exceptions.TemplateNotFound as e:
                logging.error("Could not find template: {}".format(e))
                sys.exit(1)
            else:
                print(template.render(entries=results))
        else:
            for entry in results:
                print(entry)


class Init(guacamole.Command):
    arguments = [
        (['--collection', '-c'], dict(
            help='The directory to work on (default .)',
            default='.'
            )),
        (['--no-git'], dict(
            help='Skip setting up git in this directory',
            action="store_false",
            dest='git'
            )),
        ]

    def register_arguments(self, parser):
        for arg in self.arguments:
            parser.add_argument(*arg[0], **arg[1])

    def invoked(self, ctx):
        Collection.init(
            ctx.args.collection,
            git_enabled=ctx.args.git,
            edit_file=edit_file_in_external_editor
            )


class Remove(guacamole.Command):
    def register_arguments(self, parser):
        parser.add_argument(
            '--collection', '-c',
            help='The collection to work on (default .)',
            )
        parser.add_argument(
            'entries',
            help='List of entries to remove',
            nargs='+',
            )

    def invoked(self, ctx):
        collection = Collection(ctx.args.collection)
        collection.remove_entries(uids=ctx.args.entries)