# 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

    def umount(self, db_name):
        db_uid = self.index['name_to_uid'][db_name] if db_name in self.index['name_to_uid'] else None

        if self[db_uid]['status'] == self.STATUSES.MOUNTED:
            self[db_uid]['status'] = self.STATUSES.UNMOUNTED
            del self[db_uid]['connector']
            self[db_uid]['connector'] = None
        else:
            return failure(DATABASE_ERROR, "Database %r already unmounted" % db_name)

        return success()

    def add(self, db_name, db_options=None):
        """Adds a db to the DatabasesHandler object, and sync it
        to the store file"""
        db_options = db_options or DatabaseOptions()
        cache_status, ratio = self._disposable_cache(db_options["block_cache_size"])
        if not cache_status:
            return failure(DATABASE_ERROR,
                           "Not enough disposable cache memory "
                           "%d Mo missing" % ratio)

        db_name_is_path = db_name.startswith('.') or ('/' in db_name)
        is_abspath = lambda: not db_name.startswith('.') and ('/' in db_name)

        # Handle case when a db is a path
        if db_name_is_path:
            if not is_abspath():
                return failure(DATABASE_ERROR, "Canno't create database from relative path")

# 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


    def umount(self, db_name):
        db_uid = self.index['name_to_uid'][db_name] if db_name in self.index['name_to_uid'] else None

        if self[db_uid]['status'] == self.STATUSES.MOUNTED:
            self[db_uid]['status'] = self.STATUSES.UNMOUNTED
            del self[db_uid]['connector']
            self[db_uid]['connector'] = None
        else:
            return failure(DATABASE_ERROR, "Database %r already unmounted" % db_name)

        return success()

    def add(self, db_name, db_options=None):
        """Adds a db to the DatabasesHandler object, and sync it
        to the store file"""
        db_options = db_options or DatabaseOptions()
        cache_status, ratio = self._disposable_cache(db_options["block_cache_size"])
        if not cache_status:
            return failure(DATABASE_ERROR,
                           "Not enough disposable cache memory "
                           "%d Mo missing" % ratio)

        db_name_is_path = db_name.startswith('.') or ('/' in db_name)
        is_abspath = lambda: not db_name.startswith('.') and ('/' in db_name)

        # Handle case when a db is a path
        if db_name_is_path:
            if not is_abspath():
                return failure(DATABASE_ERROR, "Canno't create database from relative path")

            try: