def get_journals(): """Get journals.""" key = request.values.get('key') if not key: return jsonify({}) datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config['WEKO_WORKFLOW_OAPOLICY_SEARCH'].format( keyword=key) if datastore.redis.exists(cache_key): data = datastore.get(cache_key) multiple_result = json.loads(data.decode('utf-8'), object_pairs_hook=OrderedDict) else: multiple_result = search_romeo_jtitles(key, 'starts') if key else {} try: datastore.put( cache_key, json.dumps(multiple_result).encode('utf-8'), ttl_secs=int( current_app.config['WEKO_WORKFLOW_OAPOLICY_CACHE_TTL'])) except Exception: pass return jsonify(multiple_result)
def shib_sp_login(): """The request from shibboleth sp. :return: confirm page when relation is empty """ try: if not current_app.config['SHIB_ACCOUNTS_LOGIN_ENABLED']: return url_for_security('login') shib_session_id = request.form.get('SHIB_ATTR_SESSION_ID', None) if shib_session_id is None or len(shib_session_id) == 0: return url_for_security('login') shib_attr, error = parse_attributes() if error: return url_for_security('login') datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) ttl_sec = int(current_app.config['SHIB_ACCOUNTS_LOGIN_CACHE_TTL']) datastore.put(config.SHIB_CACHE_PREFIX + shib_session_id, bytes(json.dumps(shib_attr), encoding='utf-8'), ttl_secs=ttl_sec) shib_user = ShibUser(shib_attr) rst = shib_user.get_relation_info() """ check the relation of shibboleth user with weko account""" next_url = 'weko_accounts.shib_auto_login' if rst is None: """relation is not existed, cache shibboleth info to redis.""" next_url = 'weko_accounts.shib_login' query_string = { 'SHIB_ATTR_SESSION_ID': shib_session_id, '_method': 'GET' } return url_for(next_url, **query_string) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return url_for_security('login')
def iframe_items_index(pid_value=0): try: if pid_value == 0: return redirect(url_for('.iframe_index')) if request.method == 'GET': return render_template( 'weko_items_ui/iframe/item_index.html', pid_value=pid_value) if request.headers['Content-Type'] != 'application/json': flash(_('invalide request'), 'error') return render_template( 'weko_items_ui/iframe/item_index.html') data = request.get_json() sessionstore = RedisStore(redis.StrictRedis.from_url( 'redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) if request.method == 'PUT': """update index of item info.""" item_str = sessionstore.get('item_index_{}'.format(pid_value)) sessionstore.delete('item_index_{}'.format(pid_value)) current_app.logger.debug(item_str) item = json.loads(item_str) item['index'] = data current_app.logger.debug(item) elif request.method == 'POST': """update item data info.""" current_app.logger.debug(data) sessionstore.put('item_index_{}'.format(pid_value), json.dumps(data), ttl_secs=300) return jsonify(data) except: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def reset_redis_cache(cache_key, value): """Delete and then reset a cache value to Redis.""" try: datastore = RedisStore(redis.StrictRedis.from_url( current_app.config['CACHE_REDIS_URL'])) datastore.delete(cache_key) datastore.put(cache_key, value.encode('utf-8')) except Exception as e: current_app.logger.error('Could not reset redis value', e) raise
def iframe_items_index(pid_value=0): """Iframe items index.""" try: if pid_value == 0: return redirect(url_for('.iframe_index')) record = WekoRecord.get_record_by_pid(pid_value) action = 'private' if record.get('publish_status', '1') == '1' \ else 'publish' if request.method == 'GET': return render_template( 'weko_items_ui/iframe/item_index.html', render_widgets=True, pid_value=pid_value, action=action, activity=session['itemlogin_activity'], item=session['itemlogin_item'], steps=session['itemlogin_steps'], action_id=session['itemlogin_action_id'], cur_step=session['itemlogin_cur_step'], record=session['itemlogin_record'], histories=session['itemlogin_histories'], res_check=session['itemlogin_res_check'], pid=session['itemlogin_pid'], community_id=session['itemlogin_community_id']) if request.headers['Content-Type'] != 'application/json': flash(_('Invalid Request'), 'error') return render_template('weko_items_ui/iframe/item_index.html', render_widgets=True) data = request.get_json() sessionstore = RedisStore( redis.StrictRedis.from_url('redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) if request.method == 'PUT': """update index of item info.""" item_str = sessionstore.get('item_index_{}'.format(pid_value)) sessionstore.delete('item_index_{}'.format(pid_value)) current_app.logger.debug(item_str) item = json.loads(item_str) item['index'] = data current_app.logger.debug(item) elif request.method == 'POST': """update item data info.""" sessionstore.put('item_index_{}'.format(pid_value), json.dumps(data), ttl_secs=300) return jsonify(data) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def items_index(pid_value='0'): """Items index.""" try: if pid_value == '0' or pid_value == 0: return redirect(url_for('.index')) record = WekoRecord.get_record_by_pid(pid_value) action = 'private' if record.get('publish_status', '1') == '1' \ else 'publish' from weko_theme.utils import get_design_layout # Get the design for widget rendering page, render_widgets = get_design_layout( current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) if request.method == 'GET': return render_template( current_app.config['WEKO_ITEMS_UI_INDEX_TEMPLATE'], page=page, render_widgets=render_widgets, pid_value=pid_value, action=action) if request.headers['Content-Type'] != 'application/json': flash(_('Invalid request'), 'error') return render_template( current_app.config['WEKO_ITEMS_UI_INDEX_TEMPLATE'], page=page, render_widgets=render_widgets) data = request.get_json() sessionstore = RedisStore( redis.StrictRedis.from_url('redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) if request.method == 'PUT': """update index of item info.""" item_str = sessionstore.get('item_index_{}'.format(pid_value)) sessionstore.delete('item_index_{}'.format(pid_value)) current_app.logger.debug(item_str) item = json.loads(item_str) item['index'] = data current_app.logger.debug(item) elif request.method == 'POST': """update item data info.""" sessionstore.put('item_index_{}'.format(pid_value), json.dumps(data), ttl_secs=300) return jsonify(data) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def shib_sp_login(): """The request from shibboleth sp. :return: confirm page when relation is empty """ _shib_enable = current_app.config['WEKO_ACCOUNTS_SHIB_LOGIN_ENABLED'] _shib_username_config = current_app.config[ 'WEKO_ACCOUNTS_SHIB_ALLOW_USERNAME_INST_EPPN'] try: shib_session_id = request.form.get('SHIB_ATTR_SESSION_ID', None) if not shib_session_id and not _shib_enable: flash(_("Missing SHIB_ATTR_SESSION_ID!"), category='error') return redirect(url_for_security('login')) shib_attr, error = parse_attributes() # Check SHIB_ATTR_EPPN and SHIB_ATTR_USER_NAME: if error or not (shib_attr.get('shib_eppn', None) or _shib_username_config and shib_attr.get('shib_user_name')): flash(_("Missing SHIB_ATTRs!"), category='error') return _redirect_method() datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) ttl_sec = int(current_app.config['WEKO_ACCOUNTS_SHIB_LOGIN_CACHE_TTL']) datastore.put(current_app.config['WEKO_ACCOUNTS_SHIB_CACHE_PREFIX'] + shib_session_id, bytes(json.dumps(shib_attr), encoding='utf-8'), ttl_secs=ttl_sec) shib_user = ShibUser(shib_attr) # Check the relation of shibboleth user with weko account. rst = shib_user.get_relation_info() next_url = 'weko_accounts.shib_auto_login' if not rst: # Relation is not existed, cache shibboleth info to redis. next_url = 'weko_accounts.shib_login' query_string = { 'SHIB_ATTR_SESSION_ID': shib_session_id, '_method': 'GET' } return url_for(next_url, **query_string) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return _redirect_method()
def cache_schema(schema_name, delete=False): """ Cache the schema to Redis. :param schema_name: :return: """ def get_schema(): try: rec = WekoSchema.get_record_by_name(schema_name) if rec: dstore = dict() dstore['root_name'] = rec.get('root_name') dstore['target_namespace'] = rec.get('target_namespace') dstore['schema_location'] = rec.get('schema_location') dstore['namespaces'] = rec.model.namespaces.copy() dstore['schema'] = json.loads(rec.model.xsd, object_pairs_hook=OrderedDict) rec.model.namespaces.clear() del rec return dstore except BaseException: return None try: # schema cached on Redis by schema name datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config['WEKO_SCHEMA_CACHE_PREFIX'].format( schema_name=schema_name) data_str = datastore.get(cache_key) data = json.loads(data_str.decode('utf-8'), object_pairs_hook=OrderedDict) if delete: datastore.delete(cache_key) except BaseException: try: schema = get_schema() if schema: datastore.put(cache_key, json.dumps(schema)) except BaseException: return get_schema() else: return schema return data
def put(self, **kwargs): """""" try: data = request.get_json() pid = kwargs.get('pid_value').value # item metadata cached on Redis by pid import redis from simplekv.memory.redisstore import RedisStore datastore = RedisStore(redis.StrictRedis.from_url( current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config[ 'WEKO_DEPOSIT_ITEMS_CACHE_PREFIX'].format(pid_value=pid) ttl_sec = int(current_app.config['WEKO_DEPOSIT_ITEMS_CACHE_TTL']) datastore.put(cache_key, json.dumps(data), ttl_secs=ttl_sec) except BaseException: abort(400, "Failed to register item") return jsonify({'status': 'success'})
def iframe_save_model(): """Renders an item register view. :return: The rendered template. """ try: data = request.get_json() activity_session = session['activity_info'] activity_id = activity_session.get('activity_id', None) if activity_id: sessionstore = RedisStore( redis.StrictRedis.from_url('redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) sessionstore.put('activity_item_' + activity_id, json.dumps(data).encode('utf-8'), ttl_secs=60 * 60 * 24 * 7) except Exception as ex: current_app.logger.exception(str(ex)) return jsonify(code=1, msg='Model save error') return jsonify(code=0, msg='Model save success')
def index(self): cache_key = current_app.config['WEKO_ADMIN_CACHE_PREFIX'].\ format(name='display_stats') current_display_setting = True # Default datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) if datastore.redis.exists(cache_key): curr_display_setting = datastore.get(cache_key).decode('utf-8') current_display_setting = True if curr_display_setting == 'True' \ else False if request.method == 'POST': display_setting = request.form.get('record_stats_radio', 'True') datastore.delete(cache_key) datastore.put(cache_key, display_setting.encode('utf-8')) flash(_('Successfully Changed Settings.')) return redirect(url_for('statssettings.index')) return self.render( current_app.config["WEKO_ADMIN_STATS_SETTINGS_TEMPLATE"], display_stats=current_display_setting)
def open(db_name, backend='filesystem', **kwargs): path = os.path.join(os.path.expanduser('~'), '.experimentdb', db_name) if backend == 'filesystem': print("Using filesystem backend") if not os.path.exists(path): try: os.makedirs(path) except Exception as e: print(e) pass return DataBase(FilesystemStore(path)) if backend == 'redis': print("Using redis backend") # find or create a new db index redis_config_store = RedisStore(StrictRedis(db=0, **kwargs)) if db_name in redis_config_store: db_index = int(redis_config_store.get(db_name)) print("Database", db_name, "already exists with index", db_index) else: if 'next_db_index' in redis_config_store: next_db_index = int(redis_config_store.get('next_db_index')) db_index = next_db_index next_db_index += 1 redis_config_store.put('next_db_index', str(next_db_index)) else: db_index = 1 redis_config_store.put('next_db_index', '2') print("New database", db_name, "created with index", db_index) redis_config_store.put(db_name, db_index) return DataBase(RedisStore(StrictRedis(db=db_index, **kwargs)))
def iframe_items_index(pid_value='0'): """Iframe items index.""" try: if pid_value == '0' or pid_value == 0: return redirect(url_for('.iframe_index')) record = WekoRecord.get_record_by_pid(pid_value) action = 'private' if record.get('publish_status', '1') == '1' \ else 'publish' community_id = session.get('itemlogin_community_id') ctx = {'community': None} if community_id: comm = GetCommunity.get_community_by_id(community_id) ctx = {'community': comm} if request.method == 'GET': cur_activity = session['itemlogin_activity'] # If enable auto set index feature # and activity is usage application item type steps = session['itemlogin_steps'] contain_application_endpoint = False for step in steps: if step.get('ActionEndpoint') == 'item_login_application': contain_application_endpoint = True enable_auto_set_index = current_app.config.get( 'WEKO_WORKFLOW_ENABLE_AUTO_SET_INDEX_FOR_ITEM_TYPE') if enable_auto_set_index and contain_application_endpoint: index_id = get_index_id(cur_activity.activity_id) update_index_tree_for_record(pid_value, index_id) return redirect(url_for('weko_workflow.iframe_success')) # Get the design for widget rendering from weko_theme.utils import get_design_layout page, render_widgets = get_design_layout( community_id or current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) return render_template('weko_items_ui/iframe/item_index.html', page=page, render_widgets=render_widgets, pid_value=pid_value, action=action, activity=session['itemlogin_activity'], item=session['itemlogin_item'], steps=session['itemlogin_steps'], action_id=session['itemlogin_action_id'], cur_step=session['itemlogin_cur_step'], record=session['itemlogin_record'], histories=session['itemlogin_histories'], res_check=session['itemlogin_res_check'], pid=session['itemlogin_pid'], community_id=community_id, **ctx) if request.headers['Content-Type'] != 'application/json': flash(_('Invalid Request'), 'error') from weko_theme.utils import get_design_layout page, render_widgets = get_design_layout( current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) return render_template('weko_items_ui/iframe/item_index.html', page=page, render_widgets=render_widgets, community_id=community_id, **ctx) data = request.get_json() sessionstore = RedisStore( redis.StrictRedis.from_url('redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) if request.method == 'PUT': """update index of item info.""" item_str = sessionstore.get('item_index_{}'.format(pid_value)) sessionstore.delete('item_index_{}'.format(pid_value)) item = json.loads(item_str) item['index'] = data elif request.method == 'POST': """update item data info.""" sessionstore.put('item_index_{}'.format(pid_value), json.dumps(data), ttl_secs=300) return jsonify(data) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def next_action(activity_id='0', action_id=0): """Next action.""" work_activity = WorkActivity() history = WorkActivityHistory() post_json = request.get_json() activity = dict(activity_id=activity_id, action_id=action_id, action_version=post_json.get('action_version'), action_status=ActionStatusPolicy.ACTION_DONE, commond=post_json.get('commond')) action = Action().get_action_detail(action_id) action_endpoint = action.action_endpoint if action_endpoint == 'begin_action': return jsonify(code=0, msg=_('success')) if action_endpoint == 'end_action': work_activity.end_activity(activity) return jsonify(code=0, msg=_('success')) if action_endpoint == 'item_login': register_hdl(activity_id) activity_detail = work_activity.get_activity_detail(activity_id) item_id = None recid = None record = None pid_without_ver = None if activity_detail and activity_detail.item_id: item_id = activity_detail.item_id current_pid = PersistentIdentifier.get_by_object(pid_type='recid', object_type='rec', object_uuid=item_id) recid = get_record_identifier(current_pid.pid_value) record = WekoDeposit.get_record(item_id) if record: pid_without_ver = get_record_without_version(current_pid) deposit = WekoDeposit(record, record.model) if post_json.get('temporary_save') == 1 \ and action_endpoint != 'identifier_grant': if 'journal' in post_json: work_activity.create_or_update_action_journal( activity_id=activity_id, action_id=action_id, journal=post_json.get('journal')) else: work_activity.upt_activity_action_comment( activity_id=activity_id, action_id=action_id, comment=post_json.get('commond')) return jsonify(code=0, msg=_('success')) elif post_json.get('journal'): work_activity.create_or_update_action_journal( activity_id=activity_id, action_id=action_id, journal=post_json.get('journal')) if action_endpoint == 'approval' and item_id: item = ItemsMetadata.get_record(id_=item_id) pid_identifier = PersistentIdentifier.get_by_object( pid_type='depid', object_type='rec', object_uuid=item.id) record_class = import_string('weko_deposit.api:WekoRecord') resolver = Resolver(pid_type='recid', object_type='rec', getter=record_class.get_record) _pid, _approval_record = resolver.resolve(pid_identifier.pid_value) action_feedbackmail = work_activity.get_action_feedbackmail( activity_id=activity_id, action_id=ITEM_REGISTRATION_ACTION_ID) if action_feedbackmail: FeedbackMailList.update( item_id=item_id, feedback_maillist=action_feedbackmail.feedback_maillist) if not recid and pid_without_ver: FeedbackMailList.update( item_id=pid_without_ver.object_uuid, feedback_maillist=action_feedbackmail.feedback_maillist) if record: deposit.update_feedback_mail() deposit.update_jpcoar_identifier() # TODO: Make private as default. # UpdateItem.publish(pid, approval_record) if action_endpoint == 'item_link' and record: current_pid = PersistentIdentifier.get_by_object(pid_type='recid', object_type='rec', object_uuid=item_id) if not pid_without_ver: pid_without_ver = get_record_without_version(current_pid) item_link = ItemLink(pid_without_ver.pid_value) relation_data = post_json.get('link_data') if relation_data: errors = item_link.update(relation_data) if errors: return jsonify(code=-1, msg=_(errors)) # save pidstore_identifier to ItemsMetadata identifier_select = post_json.get('identifier_grant') if 'identifier_grant' == action_endpoint and identifier_select: idf_grant_jalc_doi_manual = post_json.get( 'identifier_grant_jalc_doi_suffix') idf_grant_jalc_cr_doi_manual = post_json.get( 'identifier_grant_jalc_cr_doi_suffix') idf_grant_jalc_dc_doi_manual = post_json.get( 'identifier_grant_jalc_dc_doi_suffix') idf_grant_ndl_jalc_doi_manual = post_json.get( 'identifier_grant_ndl_jalc_doi_suffix') # If is action identifier_grant, then save to to database identifier_grant = { 'action_identifier_select': identifier_select, 'action_identifier_jalc_doi': idf_grant_jalc_doi_manual, 'action_identifier_jalc_cr_doi': idf_grant_jalc_cr_doi_manual, 'action_identifier_jalc_dc_doi': idf_grant_jalc_dc_doi_manual, 'action_identifier_ndl_jalc_doi': idf_grant_ndl_jalc_doi_manual } work_activity.create_or_update_action_identifier( activity_id=activity_id, action_id=action_id, identifier=identifier_grant) error_list = item_metadata_validation(item_id, identifier_select) if post_json.get('temporary_save') == 1: return jsonify(code=0, msg=_('success')) if isinstance(error_list, str): return jsonify(code=-1, msg=_(error_list)) sessionstore = RedisStore( redis.StrictRedis.from_url('redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) if error_list: sessionstore.put('updated_json_schema_{}'.format(activity_id), json.dumps(error_list).encode('utf-8'), ttl_secs=300) return previous_action(activity_id=activity_id, action_id=action_id, req=-1) else: if sessionstore.redis.exists( 'updated_json_schema_{}'.format(activity_id)): sessionstore.delete( 'updated_json_schema_{}'.format(activity_id)) if identifier_select != IDENTIFIER_GRANT_SELECT_DICT['NotGrant'] \ and item_id is not None: record_without_version = item_id if record and pid_without_ver and not recid: record_without_version = pid_without_ver.object_uuid saving_doi_pidstore(item_id, record_without_version, post_json, int(identifier_select)) rtn = history.create_activity_history(activity) if not rtn: return jsonify(code=-1, msg=_('error')) # next action work_activity.upt_activity_action_status( activity_id=activity_id, action_id=action_id, action_status=ActionStatusPolicy.ACTION_DONE) work_activity.upt_activity_action_comment(activity_id=activity_id, action_id=action_id, comment='') flow = Flow() next_flow_action = flow.get_next_flow_action( activity_detail.flow_define.flow_id, action_id) if next_flow_action and len(next_flow_action) > 0: next_action_endpoint = next_flow_action[0].action.action_endpoint if 'end_action' == next_action_endpoint: new_activity_id = None if record: deposit.publish() updated_item = UpdateItem() # publish record without version ID when registering newly if recid: # new record attached version ID ver_attaching_record = deposit.newversion(current_pid) new_activity_id = ver_attaching_record.model.id ver_attaching_deposit = WekoDeposit( ver_attaching_record, ver_attaching_record.model) ver_attaching_deposit.publish() record_bucket_id = merge_buckets_by_records( current_pid.object_uuid, ver_attaching_record.model.id, sub_bucket_delete=True) if not record_bucket_id: return jsonify(code=-1, msg=_('error')) # Record without version: Make status Public as default updated_item.publish(record) else: # update to record without version ID when editing new_activity_id = record.model.id if pid_without_ver: record_without_ver = WekoDeposit.get_record( pid_without_ver.object_uuid) deposit_without_ver = WekoDeposit( record_without_ver, record_without_ver.model) deposit_without_ver['path'] = deposit.get('path', []) parent_record = deposit_without_ver.\ merge_data_to_record_without_version(current_pid) deposit_without_ver.publish() set_bucket_default_size(new_activity_id) merge_buckets_by_records(new_activity_id, pid_without_ver.object_uuid) updated_item.publish(parent_record) delete_unregister_buckets(new_activity_id) activity.update( action_id=next_flow_action[0].action_id, action_version=next_flow_action[0].action_version, item_id=new_activity_id, ) work_activity.end_activity(activity) else: next_action_id = next_flow_action[0].action_id work_activity.upt_activity_action( activity_id=activity_id, action_id=next_action_id, action_status=ActionStatusPolicy.ACTION_DOING) # delete session value if session.get('itemlogin_id'): del session['itemlogin_id'] del session['itemlogin_activity'] del session['itemlogin_item'] del session['itemlogin_steps'] del session['itemlogin_action_id'] del session['itemlogin_cur_step'] del session['itemlogin_record'] del session['itemlogin_res_check'] del session['itemlogin_pid'] del session['itemlogin_community_id'] return jsonify(code=0, msg=_('success'))
class DatabaseService(Service): """ Database bindings for leveldb """ def __init__(self, engine): Service.__init__(self, name='database') self.engine = engine self.DB = None self.salt = None self.req_count = 0 self.set_state(Service.INIT) def on_register(self): # TODO: Add authentication support for redis try: redis_instance = redis.StrictRedis( host=os.environ.get('REDIS_URL', 'localhost'), db=self.engine.config['database']['index']) redis_instance.ping() self.DB = RedisStore(redis_instance) except Exception as e: tools.log(e) sys.stderr.write( 'Redis connection cannot be established!\nFalling to SQLAlchemy' ) return False try: self.salt = self.DB.get('salt').decode() if self.salt is None: raise Exception except Exception as e: self.salt = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(5)) self.DB.put('salt', self.salt.encode()) return True @sync def get(self, key): """gets the key in args[0] using the salt""" try: return yaml.load(self.DB.get(self.salt + str(key)).decode()) except Exception as e: return None @sync def put(self, key, value): """ Puts the val in args[1] under the key in args[0] with the salt prepended to the key. """ try: self.DB.put(self.salt + str(key), yaml.dump(value).encode()) return True except Exception as e: return False @sync def exists(self, key): """ Checks if the key in args[0] with the salt prepended is in the database. """ try: return (self.salt + str(key)) in self.DB except KeyError: return False @sync def delete(self, key): """ Removes the entry in the database under the the key in args[0] with the salt prepended. """ try: self.DB.delete(self.salt + str(key)) return True except: return False