# 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
        """Removes a database from store file"""
        store_datas = self.extract_store_datas()
        store_datas.pop(db_name)
        json.dump(store_datas, open(self.store, 'w'))

    def mount(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.UNMOUNTED:
            db_path = self[db_uid]['path']
            connector = self._get_db_connector(db_path)

            if connector is None:
                return failure(DATABASE_ERROR, "Database %s could not be mounted" % db_path)

            self[db_uid]['status'] = self.STATUSES.MOUNTED
            self[db_uid]['connector'] = leveldb.LevelDB(db_path)
        else:
            return failure(DATABASE_ERROR, "Database %r already mounted" % db_name)

        return 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:

# 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

        """Removes a database from store file"""
        store_datas = self.extract_store_datas()
        store_datas.pop(db_name)
        json.dump(store_datas, open(self.store, 'w'))

    def mount(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.UNMOUNTED:
            db_path = self[db_uid]['path']
            connector = self._get_db_connector(db_path)

            if connector is None:
                return failure(DATABASE_ERROR, "Database %s could not be mounted" % db_path)

            self[db_uid]['status'] = self.STATUSES.MOUNTED
            self[db_uid]['connector'] = leveldb.LevelDB(db_path)
        else:
            return failure(DATABASE_ERROR, "Database %r already mounted" % db_name)

        return 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)