def onJoin(self, details): middleware = CachingMiddleware(JSONStorage) db = TinyDB(os.path.join(os.path.dirname(__file__), 'data', 'user_data.json'), storage=middleware) auth_config = {'salt': 'fruitfly', 'iterations': 5000, 'keylen': 32} session_user_map = {} chars = string.ascii_letters + string.digits + "@!^_()%[]{}" pwlen = 12 self.log.info('auth started') def auth(realm, authid, details): q = Query() results = db.search(q.username == authid) if not results: raise ApplicationError( "User does not exist", "could not authenticate session - no such user {}".format( authid)) auth_details = results[0]['auth_details'] auth_details['authid'] = results[0]['user_details']['fname'] if results[0]['user_details']['lname']: auth_details['authid'] += (" " + results[0]['user_details']['lname']) if not auth_details['authid']: del auth_details['authid'] session_user_map[details['session']] = { 'username': authid, 'details': details } return auth_details yield self.register(auth, six.u('ffbo.auth_server.auth')) self.log.info("registered ffbo.auth_server.auth") def get_user(session): if session in session_user_map: return session_user_map[session] return {} yield self.register(get_user, six.u('ffbo.auth_server.get_user')) self.log.info("registered ffbo.auth_server.get_user") def generate_password(): rnd = random.SystemRandom() return "".join([rnd.choice(chars) for _ in range(pwlen)]) def register_user(user_details): if user_exists(user_details['username']): return {"error": "User already exists. Please try again"} try: username = user_details['username'] pw = generate_password() salted_pw = ath.derive_key(pw, auth_config['salt'], auth_config['iterations'], auth_config['keylen']) db_rec = { 'username': username, 'user_details': user_details, 'auth_details': copy.deepcopy(auth_config) } db_rec['auth_details']['secret'] = salted_pw db_rec['auth_details']['role'] = u'user' del db_rec['user_details']['username'] #print "Registered user ", db_rec db.insert(db_rec) except Exception as e: print e return {"error": "Unexpected error occured. Please try again"} print "User added to database" send_email(user_details, pw, username) return { "success": "Successfuly registered. Please check your email for your password." } yield self.register(register_user, six.u('ffbo.auth_server.register_user')) self.log.info("registered ffbo.auth_server.register_user") def send_email(user_details, pw, username): title = "Thank you for registering at NeuroNLP" text = "Hi {fname},\n\n" text += "Here are your login details for NeuroNLP.\n\n" text += "Username: {username}\nPassword: {pw}\n\n" text += "If you have any suggestions or feedback, we would love to hear it!" text += " Please use the feedback button on the top left button of the website to write to us.\n\n" text += "Thank you,\nFruit Fly Brain Observatory" text = text.format(username=username, fname=user_details['fname'], pw=pw) msg = MIMEText(text) msg['Subject'] = title msg['From'] = 'NeuroNLP(Fruit Fly Brain Observatory) <*****@*****.**>' msg['To'] = user_details['email'] sender = msg['From'] try: s = smtplib.SMTP(host='localhost', port=465) except Exception as e: print e print "Failed to start SMTP server on localhost" try: # Use a valid smtp server, otherwise the email notification won't be sent out s.sendmail('*****@*****.**', [user_details['email']], msg.as_string()) middleware.flush() print "Email sent to " + user_details[ 'email'] + " for " + user_details[ 'fname'] + ' ' + user_details['lname'] except Exception as e: print e print "Failed to send out email" def user_exists(username): q = Query() results = db.search(q.username == username) if not results: return False return True yield self.register(user_exists, six.u('ffbo.auth_server.user_exists')) self.log.info("registered ffbo.auth_server.user_exists") def get_auth_config(): return auth_config yield self.register(get_auth_config, six.u('ffbo.auth_server.get_auth_config')) self.log.info("registered ffbo.auth_server.get_auth_config") def change_password(): pass def send_new_password(): pass
def setup_nested(): global storage _storage = ConcurrencyMiddleware(CachingMiddleware(MemoryStorage)) storage = _storage() # Initialize MemoryStorage
def __init__(self, fileLocation: str, tableName: str = '_default'): super().__init__() self.tdb = TinyDB(fileLocation, storage=CachingMiddleware(JSONStorage)) self.tbl = self.tdb.table(tableName, cache_size=30)
b'dubbo': [b'^unsupported command'], b'elasticsearch': [b'cluster_name.*elasticsearch'], b'rabbitmq': [b'^amqp\x00\x00\t\x01'], b'zookeeper': [b'^zookeeper version: '] } from tinydb import TinyDB, where from tinydb.storages import JSONStorage from tinydb.middlewares import CachingMiddleware from collections import namedtuple import os Port = namedtuple("Port", ["name", "port", "protocol", "description"]) __BASE_PATH__ = os.path.dirname(os.path.abspath(__file__)) __DATABASE_PATH__ = os.path.join('Auxiliary', 'ports.json') __DB__ = TinyDB(__DATABASE_PATH__, storage=CachingMiddleware(JSONStorage)) # pathname = os.path.dirname(os.path.abspath(__file__)) # sys.path.insert(0,pathname) # sys.path.insert(0,os.path.abspath(os.path.join(pathname,'..'))) def GetPortInfo(port, like=False): """ 判断端口服务,传入参数为 字符串类型的数字 返回服务名称 'http',没有则返回 '检测失效' """ where_field = "port" if port.isdigit() else "name" if like: ports = __DB__.search(where(where_field).search(port))
def test_access_storage(): assert isinstance(TinyDB(storage=MemoryStorage).storage, MemoryStorage) assert isinstance( TinyDB(storage=CachingMiddleware(MemoryStorage)).storage, CachingMiddleware)
def test_caching_read(): db = TinyDB(storage=CachingMiddleware(MemoryStorage)) assert db.all() == []
def __init__(self): self.db = TinyDB("db.json", storage=CachingMiddleware(JSONStorage)) self.threads = self.db.table("threads")
from tinydb import TinyDB, where, Query from tinydb.storages import JSONStorage from tinydb.middlewares import CachingMiddleware import json import os import io if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-s', '--source', dest='raw_json', default='data/raw_dict.json') parser.add_argument('-d', '--dest', dest='tinydb_json', default='data/user_data.json') args = parser.parse_args() db = TinyDB(args.tinydb_json, storage=CachingMiddleware(JSONStorage)) f = open(args.raw_json) with f as data_file: raw_data = json.load(data_file) f.close() db.insert_multiple(raw_data) db.close()
def tester(table, MAT, algorithm=['geormsd', 'wgt', '500'], number=None, testing=False, go=1): from collections import Counter from tinydb import TinyDB, Query from pprint import pprint import defaults as defs import numpy as np from tinydb.storages import JSONStorage from tinydb.middlewares import CachingMiddleware MATs = TinyDB('./dbs/MATS.json', storage=CachingMiddleware(JSONStorage)) core = TinyDB('./dbs/Core.json', storage=CachingMiddleware(JSONStorage)) tab = MATs.table(table) cand = tab.all() tested = tab.get(Query()['Trust name'] == MAT) algorithm2 = [] for a, b, c, d in zip(algorithm[:-3:4], algorithm[1:-2:4], algorithm[2:-1:4], algorithm[3::4]): algorithm2 += [a + b, c, d] algorithm = algorithm2 try: assert all(x in ['wgt', 'is', 'isnot'] or x.endswith('gets') for x in algorithm[1:-1:3]) except AssertionError: raise try: assert 'wgt' in algorithm[1:-1:3] except AssertionError: raise crits = list(zip(algorithm[:-2:3], algorithm[1:-1:3], algorithm[2::3])) cand = list( [x for x in cand if x['Trust name'] not in (tested['Trust name'], '')]) for x in cand: x['sims'] = 0 for key, method, value in crits: try: if method == 'is': if value is 'same': value = tested[key] cand[:] = [x for x in cand if str(x[key]) == str(value)] elif method == 'isnot': if value is 'same': value = tested[key] cand[:] = [x for x in cand if str(x[key]) != str(value)] elif method.endswith('notgets'): method = method[:-7] if method is 'same': method = tested[key] for x in cand: try: if str(x[key]) != str(method): x['sims'] += float(value) except Exception as e: print(x, flush=True) pprint(cand) raise e elif method.endswith('gets'): method = method[:-4] if method is 'same': method = tested[key] for x in cand: try: if str(x[key]) == str(method): x['sims'] += float(value) except Exception as e: print(x, flush=True) pprint(cand) raise e elif method == 'wgt': donecand = [] errorcount = 0 if not any(key in x.keys() for x in cand): print('{} has not been calculated. Purge and recompile, \ then try again if you want to use this variable.'.format(key)) continue elif key not in tested.keys(): print( '{} has not been calculated for the tested MAT. Purge and recompile, \ then try again if you want to use this variable.'.format(key), flush=True) continue for x in cand: try: x['sims'] += (abs( (float(tested[key]) - float(x[key])) / float(tested[key]))) * float(value) * 100 except ZeroDivisionError: pass except KeyError as e: x['sims'] += float(value) errorcount += 1 donecand.append(dict(x)) if errorcount > 0 and go == 0: print('Weight exception: \'{}\' not found ({}/{})'.format( key, errorcount, len(cand)), flush=True) cand = donecand[:] except KeyError: print(x, tested, key, flush=True) raise MATs.close() for x in cand: x['sims'] *= 1000 simsmax = max([x['sims'] for x in cand]) simsmin = min([x['sims'] for x in cand]) counterpack = list(cand) for x in counterpack: x['sims'] = simsmax - x['sims'] counter = Counter({x['Trust name']: x['sims'] for x in counterpack}) finlist = [(a, int((b / (simsmax + 1)) * 100)) for a, b in counter.most_common()] if testing: lastthing = finlist else: dict1 = {x: [] for x in defs.ProgressScoreHeaders} dict2 = {x: [] for x in defs.ProgressScoreHeaders} for x in finlist[:number]: for ID in next(item for item in counterpack if item['Trust name'] == x[0])['IDs']: school = core.get(doc_id=ID) for y, z in school.items(): if y in defs.ProgressScoreHeaders: dict1[y].append(float(z)) for ID in tested['IDs']: school = core.get(doc_id=ID) for y, z in school.items(): if y in defs.ProgressScoreHeaders: dict2[y].append(float(z)) resultavg = {} subjectavg = {} for x, y in dict1.items(): if len(y) > 0: resultavg['Average ' + x] = round(np.average(np.array(y)), 2) else: resultavg['Average ' + x] = 'NaN' for x, y in dict2.items(): if len(y) > 0: subjectavg['Subject ' + x] = round(np.average(np.array(y)), 2) else: subjectavg['Subject ' + x] = 'NaN' lastthing = (subjectavg, resultavg) return (['{} (Score: {}%)'.format(a, b) for a, b in finlist[:number]], lastthing, MAT) """for x in cand:
from tinydb import TinyDB, Query from tinydb.middlewares import CachingMiddleware from iptocc.json_storage_read_only import JSONStorageReadOnly __author__ = "Ronie Martinez" __copyright__ = "Copyright 2017, Ronie Martinez" __credits__ = ["Ronie Martinez"] __license__ = "MIT" __version__ = "1.0.2" __maintainer__ = "Ronie Martinez" __email__ = "*****@*****.**" __status__ = "Production" dir_path = os.path.dirname(os.path.realpath(__file__)) caching_middleware = CachingMiddleware(JSONStorageReadOnly) database = TinyDB(os.path.join(dir_path, 'rir_statistics_exchange.json'), storage=caching_middleware) query = Query() lock = threading.Lock() logger = logging.getLogger(__name__) @lru_cache(maxsize=100000) def ipv4_get_country_code(ip_address): with lock: for record in database.search(query.type == 'ipv4'): start_address = ipaddress.IPv4Address(record.get('start')) if start_address <= ip_address < start_address + record.get('value'): country_code = record.get('country_code') if six.PY2:
def _opendb(self): self.middleware = CachingMiddleware(JSONStorage) self.middleware.WRITE_CACHE_SIZE = 4096 self.db = TinyDB(self.conn_str, storage=self.middleware, default_table=self.default_table)
def get_db(db_file: str): assert (isinstance(db_file, str)) return TinyDB(db_file, storage=CachingMiddleware(JSONStorage))
from sure_tosca.models.base_model_ import Model from sure_tosca.models.node_template import NodeTemplateModel as NodeTemplateModel from sure_tosca.models.node_template_map import NodeTemplateMapModel from sure_tosca.models.tosca_template import ToscaTemplateModel as ToscaTemplateModel from sure_tosca.service import tosca_helper # db = TinyDB(storage=CachingMiddleware(MemoryStorage)) db_dir_path = tempfile.gettempdir() tosca_templates_db_file_path = os.path.join(db_dir_path, "tosca_templates.json") tosca_templates_db = TinyDB(tosca_templates_db_file_path) # tosca_templates_db = TinyDB(storage=CachingMiddleware(MemoryStorage)) node_template_db = TinyDB(storage=CachingMiddleware(MemoryStorage)) dsl_definitions_db = TinyDB(storage=CachingMiddleware(MemoryStorage)) relationship_template_db = TinyDB(storage=CachingMiddleware(MemoryStorage)) interface_types_db = TinyDB(storage=CachingMiddleware(MemoryStorage)) logger = logging.getLogger(__name__) if not getattr(logger, 'handler_set', None): logger.setLevel(logging.INFO) h = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') h.setFormatter(formatter) logger.addHandler(h) logger.handler_set = True root_key = 'root_key'