Example #1
0
 def __init__(self, libraries, opts, testing=False):
     self.opts = opts
     self.library_broker = LibraryBroker(libraries)
     self.testing = testing
     self.lock = Lock()
     self.sessions = Sessions()
Example #2
0
 def __init__(self, libraries, opts, testing=False):
     self.opts = opts
     self.library_broker = LibraryBroker(libraries)
     self.testing = testing
     self.lock = Lock()
     self.sessions = Sessions()
Example #3
0
class Context(object):

    log = None
    url_for = None
    CATEGORY_CACHE_SIZE = 25
    SEARCH_CACHE_SIZE = 100
    SESSION_COOKIE = 'calibre_session'

    def __init__(self, libraries, opts, testing=False):
        self.opts = opts
        self.library_broker = LibraryBroker(libraries)
        self.testing = testing
        self.lock = Lock()
        self.sessions = Sessions()

    def init_session(self, endpoint, data):
        data.session = self.sessions.get_or_create(key=data.cookies.get(self.SESSION_COOKIE), username=data.username)

    def finalize_session(self, endpoint, data, output):
        data.outcookie[self.SESSION_COOKIE] = data.session.key
        data.outcookie[self.SESSION_COOKIE]['path'] = self.url_for(None)

    def get_library(self, library_id=None):
        return self.library_broker.get(library_id)

    def allowed_book_ids(self, data, db):
        # TODO: Implement this based on data.username for per-user
        # restrictions. Cache result on the data object
        with self.lock:
            ans = data.allowed_book_ids.get(db.server_library_id)
            if ans is None:
                ans = data.allowed_book_ids[db.server_library_id] = db.all_book_ids()
            return ans

    def get_categories(self, data, db, restrict_to_ids=None):
        if restrict_to_ids is None:
            restrict_to_ids = self.allowed_book_ids(data, db)
        with self.lock:
            cache = self.library_broker.category_caches[db.server_library_id]
            old = cache.pop(restrict_to_ids, None)
            if old is None or old[0] <= db.last_modified():
                categories = db.get_categories(book_ids=restrict_to_ids)
                cache[restrict_to_ids] = old = (utcnow(), categories)
                if len(cache) > self.CATEGORY_CACHE_SIZE:
                    cache.popitem(last=False)
            else:
                cache[restrict_to_ids] = old
            return old[1]

    def search(self, data, db, query, restrict_to_ids=None):
        if restrict_to_ids is None:
            restrict_to_ids = self.allowed_book_ids(data, db)
        with self.lock:
            cache = self.library_broker.search_caches[db.server_library_id]
            key = (query, restrict_to_ids)
            old = cache.pop(key, None)
            if old is None or old[0] < db.clear_search_cache_count:
                matches = db.search(query, book_ids=restrict_to_ids)
                cache[key] = old = (db.clear_search_cache_count, matches)
                if len(cache) > self.SEARCH_CACHE_SIZE:
                    cache.popitem(last=False)
            else:
                cache[key] = old
            return old[1]
Example #4
0
class Context(object):

    log = None
    url_for = None
    CATEGORY_CACHE_SIZE = 25
    SEARCH_CACHE_SIZE = 100
    SESSION_COOKIE = 'calibre_session'

    def __init__(self, libraries, opts, testing=False):
        self.opts = opts
        self.library_broker = LibraryBroker(libraries)
        self.testing = testing
        self.lock = Lock()
        self.sessions = Sessions()

    def init_session(self, endpoint, data):
        data.session = self.sessions.get_or_create(key=data.cookies.get(self.SESSION_COOKIE), username=data.username)

    def finalize_session(self, endpoint, data, output):
        data.outcookie[self.SESSION_COOKIE] = data.session.key
        data.outcookie[self.SESSION_COOKIE]['path'] = self.url_for(None)

    def get_library(self, library_id=None):
        return self.library_broker.get(library_id)

    @property
    def library_map(self):
        return self.library_broker.library_map, self.library_broker.default_library

    def allowed_book_ids(self, data, db):
        # TODO: Implement this based on data.username for per-user
        # restrictions. Cache result on the data object
        with self.lock:
            ans = data.allowed_book_ids.get(db.server_library_id)
            if ans is None:
                ans = data.allowed_book_ids[db.server_library_id] = db.all_book_ids()
            return ans

    def get_categories(self, data, db, restrict_to_ids=None):
        if restrict_to_ids is None:
            restrict_to_ids = self.allowed_book_ids(data, db)
        with self.lock:
            cache = self.library_broker.category_caches[db.server_library_id]
            old = cache.pop(restrict_to_ids, None)
            if old is None or old[0] <= db.last_modified():
                categories = db.get_categories(book_ids=restrict_to_ids)
                cache[restrict_to_ids] = old = (utcnow(), categories)
                if len(cache) > self.CATEGORY_CACHE_SIZE:
                    cache.popitem(last=False)
            else:
                cache[restrict_to_ids] = old
            return old[1]

    def search(self, data, db, query, restrict_to_ids=None):
        if restrict_to_ids is None:
            restrict_to_ids = self.allowed_book_ids(data, db)
        with self.lock:
            cache = self.library_broker.search_caches[db.server_library_id]
            key = (query, restrict_to_ids)
            old = cache.pop(key, None)
            if old is None or old[0] < db.clear_search_cache_count:
                matches = db.search(query, book_ids=restrict_to_ids)
                cache[key] = old = (db.clear_search_cache_count, matches)
                if len(cache) > self.SEARCH_CACHE_SIZE:
                    cache.popitem(last=False)
            else:
                cache[key] = old
            return old[1]