4grab/backend.py
ryuslash 0323ddbed8 interrupt, save
On keyboard interrupt in the regular flow of downloading the collected images of this session and of last session are saved so as not to destroy the archive accidentally this way.
If an item is added to the new collection, it is removed from the old one.
If a save is being dumped (uncleanly saved because of KB interrupt), the old collection is appended to the new one.
Upon loading the returned string is split to enable removing of the old items.
2010-04-27 21:06:41 +02:00

70 lines
1.9 KiB
Python

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)