4grab/backend.py

71 lines
1.9 KiB
Python
Raw Normal View History

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)