summaryrefslogtreecommitdiff
path: root/lesana/command.py
blob: 2beb88b7f5bdb6263392e8c427c0097166a91a3c (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
import logging
import os
import subprocess

import gadona
from . import Collection, Entry


class New(gadona.Command):
    name = 'new'
    arguments = [
        (['--collection', '-c'], dict(
            help='The collection to work on (default .)'
            )),
        ]

    def main(self):
        collection = Collection(self.settings.collection)
        new_entry = Entry(collection)
        collection.save_entries([new_entry])
        filepath = os.path.join(
            collection.basedir,
            'items',
            new_entry.fname
            )
        try:
            subprocess.call(['sensible-editor', filepath])
        except FileNotFoundError as e:
            logging.warning(
                "Could not open new file with editor: {}".format(str(e))
                )
        else:
            collection.update_cache([filepath])
        print(new_entry.fname)


class Index(gadona.Command):
    name = 'index'
    arguments = [
        (['--collection', '-c'], dict(
            help='The collection to work on (default .)'
            )),
        ]

    def main(self):
        collection = Collection(self.settings.collection)
        indexed = collection.update_cache()
        print("Found and indexed {} entries".format(indexed))