def delete(self, key, shared=False): """Remove a key and its data from storage :param key: key to remove :param shared: ``True/False`` wether the key to remove should be in the shared (global) namespace """ namespaced_key = self._namespace_key(key, shared) Storage.get_instance().delete(namespaced_key)
def set(self, key, value, expires=None, shared=False): """Store or update a value by key :param key: the key under which to store the data :param value: the data to store :param expires: optional number of seconds after which the data is expired :param shared: ``True/False`` wether this data should be shared by other plugins. Use with care, because it pollutes the global namespace of the storage. """ namespaced_key = self._namespace_key(key, shared) pickled_value = dill.dumps(value) Storage.get_instance().set(namespaced_key, pickled_value, expires)
def get(self, key, shared=False): """Retrieve data by key :param key: key for the data to retrieve :param shared: ``True/False`` wether to retrieve data from the shared (global) namespace. :return: the data, or ``None`` if the key cannot be found/has expired """ namespaced_key = self._namespace_key(key, shared) value = Storage.get_instance().get(namespaced_key) if value: return dill.loads(value) else: return None
def has(self, key, shared=False): """Check if the key exists in storage Note: this class implements ``__contains__`` so instead of calling ``self.storage.has(...)``, you can also use: ``key in self.storage``. This will check the *namespaced* version of the key, so it's the same as: ``self.storage.has('key', shared=False)`` :param key: key to check :param shared: ``True/False`` wether to check in the shared (global) namespace :return: ``True/False`` wether the key exists. Can only return ``True`` if the key has not expired. """ namespaced_key = self._namespace_key(key, shared) return Storage.get_instance().has(namespaced_key)
def __init__(self, settings=None): announce("Initializing Slack Machine:") with indent(4): puts("Loading settings...") if settings: self._settings = settings found_local_settings = True else: self._settings, found_local_settings = import_settings() fmt = '[%(asctime)s][%(levelname)s] %(name)s %(filename)s:%(funcName)s:%(lineno)d |' \ ' %(message)s' date_fmt = '%Y-%m-%d %H:%M:%S' log_level = self._settings.get('LOGLEVEL', logging.ERROR) logging.basicConfig( level=log_level, format=fmt, datefmt=date_fmt, ) if not found_local_settings: warn( "No local_settings found! Are you sure this is what you want?" ) if 'SLACK_API_TOKEN' not in self._settings: error( "No SLACK_API_TOKEN found in settings! I need that to work..." ) sys.exit(1) self._client = Slack() puts("Initializing storage using backend: {}".format( self._settings['STORAGE_BACKEND'])) self._storage = Storage.get_instance() logger.debug("Storage initialized!") self._plugin_actions = { 'process': {}, 'listen_to': {}, 'respond_to': {}, 'catch_all': {} } self._help = {'human': {}, 'robot': {}} puts("Loading plugins...") self.load_plugins() logger.debug("The following plugin actions were registered: %s", self._plugin_actions) self._dispatcher = EventDispatcher(self._plugin_actions, self._settings)
def get_storage_size(self): """Calculate the total size of the storage :return: the total size of the storage in bytes (integer) """ return Storage.get_instance().size()