summaryrefslogtreecommitdiffstats
path: root/backend.py
blob: d41a2c8c8066e2e70617cb7d22ed332d27caa594 (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
import os
import modules.kirbybase
from util import confdir,raw_input_with_default
import config

class _Backend(object):
    """ A class that communicates with the datastore """
    def __init__(self):
        self.table = os.path.join(confdir, "images.tbl")
        self.store = modules.kirbybase.KirbyBase()
        self.__collection = ""
        self.__new_collection = []

        self.load()

    def create_store_if_needed(self):
        if not os.path.exists(self.table):
            return self.store.create(self.table, ["filename:String"])
        return True

    def add(self, filename):
        if filename in self.__collection:
            self.__collection.remove(filename)

        self.__new_collection.append(filename)

    def check(self, filename):        
        collected = filename in self.__collection
        downloaded = filename in self.__new_collection

        if not downloaded:
            self.add(filename)
        
        if collected or downloaded:
            return True

        return False

    def save(self, dump = False):
        if dump:
            self.__new_collection.extend(self.__collection)

        if os.path.exists(self.table):
            os.remove(self.table)

        self.create_store_if_needed()
        for f in self.__new_collection:
            self.store.insert(self.table, [f])

    def load(self):
        if os.path.exists(self.table):
            collection = self.store.select(self.table, ['recno'], ['*'], ['filename'], returnType="report")
            if collection != '':
                self.__collection = collection.split()

_backend = None
def Backend():
    global _backend

    if _backend == None:
        _backend = _Backend()
    return _backend

if __name__ == "__main__":
    backend = Backend()
    config._optioncreator = raw_input_with_default 
    cfg = config.Configuration()
    
    for f in os.listdir(cfg.get_archive_location()):
        backend.add(f)