# Path: debian/elevator/usr/share/pyshared/elevator/env.py
# class Environment(dict):
#     """
#     Unix shells like environment class. Implements add,
#     get, load, flush methods. Handles lists of values too.
#     Basically Acts like a basic key/value store.
#     """
#     __metaclass__ = Singleton
# 
#     def __init__(self, env_file='', *args, **kwargs):
#         if env_file:
#             self.load_from_file(env_file=env_file)  # Has to be called last!
# 
#         self.update(kwargs)
#         dict.__init__(self, *args, **kwargs)
# 
#     def load_from_file(self, env_file):
#         """
#         Updates the environment using an ini file containing
#         key/value descriptions.
#         """
#         config = ConfigParser()
# 
#         with open(env_file, 'r') as f:
#             config.readfp(f)
# 
#             for section in config.sections():
#                 self.update({section: items_to_dict(config.items(section))})
# 
#     def reload_from_file(self, env_file=''):
#         self.flush(env_file)
#         self.load(env_file)
# 
#     def load_from_args(self, section, args):
#         """Loads argparse kwargs into environment, as `section`"""
#         if not section in self:
#             self[section] = {}
# 
#         for (arg, value) in args:
#             self[section][arg] = value
# 
#     def flush(self):
#         """
#         Flushes the environment from it's manually
#         set attributes.
#         """
#         for attr in self.attributes:
#             delattr(self, attr)
# 
# Path: debian/elevator/usr/share/pyshared/elevator/constants.py
# OS_ERROR = 5
# 
# DATABASE_ERROR = 6
# 
# Path: debian/elevator/usr/share/pyshared/elevator/utils/snippets.py
# def items_to_dict(items):


import os
import uuid
import logging
import leveldb
import ujson as json
from shutil import rmtree
from threading import Thread, Event
from leveldb import LevelDBError
from .env import Environment
from .constants import OS_ERROR, DATABASE_ERROR
from .utils.snippets import from_bytes_to_mo
from .utils.patterns import enum
from .helpers.internals import failure, success
                self[key] = value


class DatabasesHandler(dict):
    STATUSES = enum('MOUNTED', 'UNMOUNTED')

    def __init__(self, store, dest, *args, **kwargs):
        self.env = Environment()
        self.index = dict().fromkeys('name_to_uid')

        self.index['name_to_uid'] = {}
        self['reverse_name_index'] = {}
        self['paths_index'] = {}
        self.dest = dest
        self.store = store

        self._global_cache_size = None

        self.load()
        self.mount('default')  # Always mount default

    @property
    def global_cache_size(self):
        store_datas = self.extract_store_datas()
        max_caches = [int(db["options"]["block_cache_size"]) for db
                      in store_datas.itervalues()]

        return sum([from_bytes_to_mo(x) for x in max_caches])

    def _disposable_cache(self, new_cache_size):

# Path: debian/elevator/usr/share/pyshared/elevator/env.py
# class Environment(dict):
#     """
#     Unix shells like environment class. Implements add,
#     get, load, flush methods. Handles lists of values too.
#     Basically Acts like a basic key/value store.
#     """
#     __metaclass__ = Singleton
# 
#     def __init__(self, env_file='', *args, **kwargs):
#         if env_file:
#             self.load_from_file(env_file=env_file)  # Has to be called last!
# 
#         self.update(kwargs)
#         dict.__init__(self, *args, **kwargs)
# 
#     def load_from_file(self, env_file):
#         """
#         Updates the environment using an ini file containing
#         key/value descriptions.
#         """
#         config = ConfigParser()
# 
#         with open(env_file, 'r') as f:
#             config.readfp(f)
# 
#             for section in config.sections():
#                 self.update({section: items_to_dict(config.items(section))})
# 
#     def reload_from_file(self, env_file=''):
#         self.flush(env_file)
#         self.load(env_file)
# 
#     def load_from_args(self, section, args):
#         """Loads argparse kwargs into environment, as `section`"""
#         if not section in self:
#             self[section] = {}
# 
#         for (arg, value) in args:
#             self[section][arg] = value
# 
#     def flush(self):
#         """
#         Flushes the environment from it's manually
#         set attributes.
#         """
#         for attr in self.attributes:
#             delattr(self, attr)
# 
# Path: debian/elevator/usr/share/pyshared/elevator/constants.py
# OS_ERROR = 5
# 
# DATABASE_ERROR = 6
# 
# Path: debian/elevator/usr/share/pyshared/elevator/utils/snippets.py
# def items_to_dict(items):


# Path: debian/elevator/usr/share/pyshared/elevator/db.py
import os
import uuid
import logging
import leveldb
import ujson as json
from shutil import rmtree
from threading import Thread, Event
from leveldb import LevelDBError
from .env import Environment
from .constants import OS_ERROR, DATABASE_ERROR
from .utils.snippets import from_bytes_to_mo
from .utils.patterns import enum
from .helpers.internals import failure, success

                self[key] = value


class DatabasesHandler(dict):
    STATUSES = enum('MOUNTED', 'UNMOUNTED')

    def __init__(self, store, dest, *args, **kwargs):
        self.env = Environment()
        self.index = dict().fromkeys('name_to_uid')

        self.index['name_to_uid'] = {}
        self['reverse_name_index'] = {}
        self['paths_index'] = {}
        self.dest = dest
        self.store = store

        self._global_cache_size = None

        self.load()
        self.mount('default')  # Always mount default

    @property
    def global_cache_size(self):
        store_datas = self.extract_store_datas()
        max_caches = [int(db["options"]["block_cache_size"]) for db
                      in store_datas.itervalues()]

        return sum([from_bytes_to_mo(x) for x in max_caches])

    def _disposable_cache(self, new_cache_size):

        next_cache_size = self.global_cache_size + from_bytes_to_mo(new_cache_size)