def auth(data): existing = Database.check_user(data['user']) if existing == []: Database.add_user(data['user'], data['PIN']) verify = Database.verify_user(data['user'], data['PIN']) if verify != []: return "Success" return "Authentication Failure"
def load(self, path): """ Load the solute library from the given path """ Database.load(self, path, local_context={'SoluteData': SoluteData}, global_context={})
def send(): try: login = auth(request.form) if login == "Success": Database.add_message(request.form['message'], request.form['user'], request.form['channel']) return login except Exception as e: return str(e), 403
def check(): try: login = auth(request.form) if login == "Success": if Database.check_channel(request.form['channel']) == []: return "Invalid channel request" messages = Database.fetch_messages(request.form['channel']) if len(messages) > int(request.form['num']): return "Yes." return "No." except Exception as e: return str(e), 403
def get(): try: login = auth(request.form) if login == "Success": if Database.check_channel(request.form['channel']) == []: return "Invalid channel request" list = {} for message in Database.fetch_messages(request.form['channel']): current = {'author': message[2], 'message': message[1]} list[message[0]] = current return list except Exception as e: return str(e), 403
def clean(): trace_up("Clean database") db = Database.connect_mysqldb() dirty = True while dirty: dirty = False db.query("SHOW TABLES") result = db.store_result() while True: row = result.fetch_row() if row == None:break if len(row)==0:break dirty = True for i in row: table_name = i[0] trace("Remove table: "+table_name) try: db.query("DROP TABLE `"+table_name+"`") except _mysql_exceptions.IntegrityError: trace("fail, retry later") db.commit() db.close() trace_down("done") trace_up("Clean filesystem") if os.path.isdir(core_config.DATA_FOLDER): data_folder=os.listdir(core_config.DATA_FOLDER) for i in data_folder: shutil.rmtree(core_config.DATA_FOLDER+"/"+i) trace_down("done")
def test_add_torrent(self): cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) user.User.add_user_account(session=session, user_id="uuuu0", password="******") self.assertEqual(1,HiFile._database.add_torrent(session,"uuuu0","0123456789012345678901234567890123456789","name",123)) self.assertEqual(2,HiFile._database.add_torrent(session,"uuuu0","0123456789012345678901234567890123456789","name",123)) self.assertEqual(3,HiFile._database.add_torrent(session,"uuuu0","0123456789012345678901234567890123456789","name",123)) self.assertEqual(HiFile._database.list_user_torrent(session,"uuuu0"),[ \ { \ "torrent_id":3, \ "user_id":"uuuu0", \ "info_hash_bin":"0123456789012345678901234567890123456789", \ "name":"name", \ "size":123, \ }, \ { \ "torrent_id":2, \ "user_id":"uuuu0", \ "info_hash_bin":"0123456789012345678901234567890123456789", \ "name":"name", \ "size":123, \ }, \ { \ "torrent_id":1, \ "user_id":"uuuu0", \ "info_hash_bin":"0123456789012345678901234567890123456789", \ "name":"name", \ "size":123, \ }, \ ]) self.assertEqual(HiFile._database.list_user_torrent(session,"uuuu1"),[])
def test_owner(self): cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) self.assertTrue(User.check_user_account_exist(session,_testcommon.OWNER_USERNAME)) self.assertTrue(User.check_user_account_password(session,_testcommon.OWNER_USERNAME,_testcommon.OWNER_PASSWORD)) self.assertTrue(UserGroup.get_group(session,_testcommon.OWNER_USERNAME),["owner"]) self.assertTrue(Permission.get_user_permission(session, _testcommon.OWNER_USERNAME, "admin"))
def test_check_user_password(self): Session = sessionmaker(bind=Database.create_sqlalchemy_engine()) session = Session() User.add_user_account(session=session, user_id="apple", password="******") self.assertTrue(User.check_user_account_password(session=session, user_id="apple", password="******")) self.assertFalse(User.check_user_account_password(session=session, user_id="apple", password="******")) self.assertFalse(User.check_user_account_password(session=session, user_id="xxx", password="******")) session.close()
def test_get_name(self): cleanup = Cleanup() session = Database.create_sqlalchemy_session() cleanup.push(session.close) self.assertEqual(None, Group.get_name(session,"asdf")) Group.add(session,"asdf","qwer") self.assertEqual("qwer", Group.get_name(session,"asdf"))
def test_remove_user(self): cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) self.assertFalse(User.check_user_account_exist(session=session, user_id="apple")) User.add_user_account(session=session, user_id="apple", password="******") self.assertTrue(User.check_user_account_exist(session=session, user_id="apple")) User.remove_user_account(session=session, user_id="apple") self.assertFalse(User.check_user_account_exist(session=session, user_id="apple"))
def build(install_config): if install_config != None: cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) Group.add(session, "owner", "Owner") GroupPermission.set(session, "owner", "admin", 99999999, True) User.add_user_account(session, install_config.OWNER_USERNAME, install_config.OWNER_PASSWORD) UserGroup.join(session, install_config.OWNER_USERNAME, "owner") session.commit()
def test_session(self): cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) c = session.query(TrackerManager.XBT_FILES).count(); self.assertEqual(c, 0) session.add(TrackerManager.XBT_FILES(info_hash="01234567890123456789",mtime=0,ctime=0)) session.flush() c = session.query(TrackerManager.XBT_FILES).count(); self.assertEqual(c, 1)
def command_guest_change_user_account_password(txt_user_id, txt_old_password, txt_new_password): cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) if not User.check_user_account_password(session, txt_user_id, txt_old_password): return hs_plugin.fail(reason="auth") User.change_password(session, txt_user_id, txt_new_password) session.commit() cleanup.clean_all(); return hs_plugin.ok()
def test_api(self): cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) c = session.query(TrackerManager.XBT_FILES).count(); self.assertEqual(c, 0) TrackerManager.add_hash(session, "01234567890123456789") c = session.query(TrackerManager.XBT_FILES).count(); self.assertEqual(c, 1)
def test_rename(self): cleanup = Cleanup() session = Database.create_sqlalchemy_session() cleanup.push(session.close) Group.add(session,"asdf","qwer") self.assertEqual("qwer", Group.get_name(session,"asdf")) self.assertNotEqual("zxcv", Group.get_name(session,"asdf")) self.assertTrue(Group.rename(session,"asdf","zxcv")) self.assertEqual("zxcv", Group.get_name(session,"asdf")) self.assertNotEqual("qwer", Group.get_name(session,"asdf")) self.assertFalse(Group.rename(session,"xxx","xxx"))
def command_guest_generate_user_login_token(txt_user_id, txt_password): ''' Generate admin token ''' cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) if not User.check_user_account_password(session, txt_user_id, txt_password): return hs_plugin.fail(reason="auth") cleanup.clean_all(); token = UserLoginToken.generate_user_login_token(txt_user_id) return hs_plugin.ok(value=token)
def command_user_remove_user_account(txtf_user_token, txt_user_id): cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) if not Permission.get_user_permission(session, txtf_user_token, "admin"): return hs_plugin.fail(reason="permission") if not User.check_user_account_exist(session, txt_user_id): return hs_plugin.fail(reason="user_id") User.remove_user_account(session, txt_user_id) session.commit() cleanup.clean_all(); return hs_plugin.ok()
def command_user_create_user_account(txtf_user_token,txt_user_id, txt_password): cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) if not Permission.get_user_permission(session, txtf_user_token, "admin"): return hs_plugin.fail(reason="permission") if User.check_user_account_exist(session, txt_user_id): return hs_plugin.fail(reason=FAIL_REASON_USER_EXIST) User.add_user_account(session, txt_user_id, txt_password) session.commit() cleanup.clean_all(); return hs_plugin.ok()
def command_human_create_user_account(txtf_turing_turing,env_ip,txt_user_id, txt_password): cleanup = Cleanup() session = Database.create_sqlalchemy_session_push(cleanup) if(User.check_user_account_exist(session, txt_user_id)): return hs_plugin.fail(reason=FAIL_REASON_USER_EXIST) User.add_user_account(session, txt_user_id, txt_password) session.commit() cleanup.clean_all(); token = UserLoginToken.generate_user_login_token(txt_user_id) return hs_plugin.ok({"user_login_token":token})
def test_unset(self): cleanup = Cleanup() session = Database.create_sqlalchemy_session() cleanup.push(session.close) self.assertEqual(GroupPermission.get(session,"g0","p0"),None) Group.add(session,"g0","G-0") session.flush() self.assertEqual("G-0", Group.get_name(session,"g0")) self.assertEqual(GroupPermission.get(session,"g0","p0"),None) GroupPermission.set(session,"g0","p0",10,True) session.flush() self.assertEqual(GroupPermission.get(session,"g0","p0"),{GroupPermission.KEY_ORDER:10,GroupPermission.KEY_ENABLE:True}) GroupPermission.unset(session,"g0","p0") session.flush() self.assertEqual(GroupPermission.get(session,"g0","p0"),None) pass
def test_get_user_permission(self): cleanup = Cleanup() session = Database.create_sqlalchemy_session() cleanup.push(session.close) User.add_user_account(session, "u0", "up0") Group.add(session, "g0", "g0") UserGroup.join(session, "u0", "g0") self.assertEqual(Permission.get_user_permission(session, "u0", "p0"), False) GroupPermission.set(session, "g0", "p0", 10, True) self.assertEqual(Permission.get_user_permission(session, "u0", "p0"), True) GroupPermission.set(session, "g0", "p0", 20, False) self.assertEqual(Permission.get_user_permission(session, "u0", "p0"), False) Group.add(session, "g1", "g1") UserGroup.join(session, "u0", "g1") GroupPermission.set(session, "g1", "p0", 30, True) self.assertEqual(Permission.get_user_permission(session, "u0", "p0"), True) self.assertEqual(Permission.get_user_permission(session, "u1", "p0"), False) self.assertEqual(Permission.get_user_permission(session, "u0", "p1"), False)
def check(message): sites = Database.all(()) ct = f'Отслеживаю сайты\n{sites}' bot.send_message(message.chat.id, ct) # # @bot.message_handler(commands=['site']) # def send_welcome(message): # bot.send_message(message.chat.id, f'Слежу за сайтами: {page}') # # # def tg(message): # ms = bot.send_message(message, message.text) # return ms # bot.polling(none_stop=True, interval=0)
def build(install_config): trace_up("Create data folder") try: os.makedirs(core_config.DATA_FOLDER) except OSError: pass trace_down("done") trace_up("Set data folder permission") os.chmod(core_config.DATA_FOLDER,0777) trace_down("done") trace_up("Build database") me=os.path.abspath(__file__) core_path=os.path.dirname(os.path.dirname(me)) for i in os.listdir(core_path): if(os.path.isfile(core_path+"/"+i+"/_database.py")): trace("Module found: "+i) __import__(name=i,fromlist=["_database"]) engine = Database.create_sqlalchemy_engine() DBB.metadata.create_all(engine) trace_down("done")
# Rudimentary admin program... will be improved from base import Database def help(): print("Welcome to the HURBIM management system, please select an option: ") print("1) Create a channel.\n\ 2) Delete a channel. \n\ 3) List current channels. \n\ 4) Ban a user. \n\ 5) List server metadata \n\ Run 'h' for this menu at any time") help() while True: choice = input("> ") if choice == 'h': help() elif choice == '1': channel = input("Name your new channel (leave blank to cancel): ") Database.add_channel(channel) elif choice == '2': channel = input( "Which channel should be deleted (leave blank to cancel): ") Database.delete_channel(channel) elif choice == '3': list = Database.get_channels() for current in list: print(current)
def load(self, path): """ Load the solvent library from the given path """ Database.load(self, path, local_context={'SolventData': SolventData}, global_context={})
def cursor_open(self): """ Overridden to apply timeout """ c, s = Database.cursor_open(self) if self.timeout > 0: s.execute("SET SESSION max_execution_time=%d" % self.timeout) return c, s
print() def print_support(self): '''Print support of frequent itemsets''' itemsets = list(self.support.keys()) itemsets.sort(key=lambda item: self.support[item], reverse=True) for item in itemsets: print(f"{list(item)} supp = {self.support[item]:.2f}") print() if __name__ == "__main__": # initialize database object with list of fields db = Database(['A', 'B', 'C', 'D', 'E', 'F', 'G']) # add transactions to database db.add_transaction(Transaction(0, ['A', 'D', 'F'])) db.add_transaction(Transaction(1, ['A', 'C', 'D', 'E'])) db.add_transaction(Transaction(2, ['B', 'D'])) db.add_transaction(Transaction(3, ['B', 'C', 'D'])) db.add_transaction(Transaction(4, ['B', 'C'])) db.add_transaction(Transaction(5, ['A', 'B', 'D'])) db.add_transaction(Transaction(6, ['B', 'D', 'E'])) db.add_transaction(Transaction(7, ['B', 'C', 'E', 'G'])) db.add_transaction(Transaction(8, ['C', 'D', 'F'])) db.add_transaction(Transaction(9, ['A', 'B', 'D'])) # print database db.print_as_boolean()
def itemset_count(self): '''Calc itemsets count for all levels''' return sum([len(item) for item in self.itemsets]) def print_support(self): '''Print support for frequent itemsets''' for key in self.support: print(list(key), f"{self.support[key]:.2f}") print() if __name__ == "__main__": # Init database and field list db = Database(['A', 'B', 'C', 'D', 'E', 'F']) # Create transactions t1 = Transaction(0, frozenset(['A', 'B', 'C'])) t2 = Transaction(1, frozenset(['A', 'C'])) t3 = Transaction(2, frozenset(['A', 'D'])) t4 = Transaction(3, frozenset(['B', 'E', 'F'])) # Add transactions to database db.add_transaction(t1) db.add_transaction(t2) db.add_transaction(t3) db.add_transaction(t4) # Output database db.print_as_set()
import json from base import Database database = Database() def on_message_come(client, msg): if database.connect(): deviceName = msg.deviceName data = msg.data time = float(msg.time) database.write(deviceName, data, time) print("Data saved successfully")
@app.route('/check', methods=['POST']) def check(): try: login = auth(request.form) if login == "Success": if Database.check_channel(request.form['channel']) == []: return "Invalid channel request" messages = Database.fetch_messages(request.form['channel']) if len(messages) > int(request.form['num']): return "Yes." return "No." except Exception as e: return str(e), 403 @app.route('/send', methods=['POST']) def send(): try: login = auth(request.form) if login == "Success": Database.add_message(request.form['message'], request.form['user'], request.form['channel']) return login except Exception as e: return str(e), 403 if __name__ == '__main__': app.run(debug=True) Database()
from base import Database import requests from big_base import Database database = Database(host='ec2-34-192-173-173.compute-1.amazonaws.com', database='df0b31na5sq5g1', user='******', password='******') class Datachange: result = database.execute_query('select * from site') def check(self): count = len(Datachange.result) for num in count: pg = Datachange.result[num][0] print(pg) # rq = requests.get(pg) # status = rq.status_code # if status == 200: # message = f'{pg} гуд' # else: # message = f'Страница {pg} упала!!! Код ошибки {status}' # print(message) Datachange.check(()) print(len(Datachange.result)) print(Datachange.result[0][0])
def cursor_open(self): """ Overridden to apply timeout """ c, s = Database.cursor_open(self) if self.timeout > 0: s.execute("SET statement_timeout=%d" % self.timeout) return c, s
def __init__(self, label="", name="", shortDesc="", longDesc=""): Database.__init__(self, label=label, name=name, shortDesc=shortDesc, longDesc=longDesc)
def test_command_user_upload_torrent(self): cleanup = Cleanup() user._hisocial.command_human_create_user_account("", "", "uuuu0", "pppp0") torrent_file=open("res/test0.torrent","rb") cleanup.push(torrent_file.close) ret=HiFile._hisocial.command_user_upload_torrent("uuuu0", torrent_file) cleanup.clean() self.check_ok(ret) session = Database.create_sqlalchemy_session_push(cleanup) self.assertTrue(filecmp.cmp("res/test0.torrent",HiFile.TorrentStorage._torrentid_to_path(0x1))) self.assertEqual(HiFile._database.list_user_torrent(session,"uuuu0"), [ \ { \ "torrent_id":1, \ "user_id":"uuuu0", \ "info_hash_bin":"2034385a2621c53a490f34c5893a860664741da4", \ "name":"Super Eurobeat Vol. 220 - Anniversary Hits",\ "size":365751495 \ } \ ]) r=session.query(HiFile.TrackerManager.XBT_FILES).all() self.assertEqual(len(r),1) self.assertEqual(binascii.b2a_hex(r[0].info_hash),"2034385a2621c53a490f34c5893a860664741da4") r=session.query(HiFile._database.Torrent).all() self.assertEqual(len(r),1) self.assertEqual(r[0].torrent_id,1) self.assertEqual(r[0].user_id,"uuuu0") self.assertEqual(r[0].name,"Super Eurobeat Vol. 220 - Anniversary Hits") self.assertEqual(r[0].size,365751495) self.assertEqual(binascii.b2a_hex(r[0].info_hash_bin),"2034385a2621c53a490f34c5893a860664741da4") cleanup.clean() ret=HiFile._hisocial.command_user_list_user_torrent("uuuu0", "uuuu0") self.assertEqual(len(ret),2) self.check_ok(ret) ret_torrent_list=ret["torrent_list"] self.assertEqual(len(ret_torrent_list),1) self.assertEqual(len(ret_torrent_list[0]),4) self.assertEqual(ret_torrent_list[0]["torrent_id"],1) self.assertEqual(ret_torrent_list[0]["name"],"Super Eurobeat Vol. 220 - Anniversary Hits") self.assertEqual(ret_torrent_list[0]["size"],365751495) self.assertNotEquals(ret_torrent_list[0]["torrent_token"],None) torrent_file=open("res/test2.torrent","rb") ret=HiFile._hisocial.command_user_upload_torrent("uuuu0", torrent_file) self.assertEqual(ret,{"result":"ok","torrent_id":2}) torrent_file.close() session = Database.create_sqlalchemy_session_push(cleanup) self.assertTrue(filecmp.cmp("res/test2.torrent",HiFile.TorrentStorage._torrentid_to_path(0x2))) self.assertEqual(HiFile._database.list_user_torrent(session,"uuuu0"),[ \ { \ "torrent_id":2, \ "user_id":"uuuu0", \ "info_hash_bin":"39eaf2230aa0bcf9148f84f6efe0c64cc1ab80c1", \ "name":"魔法少女小圓",\ "size":616839726 \ }, \ { \ "torrent_id":1, \ "user_id":"uuuu0", \ "info_hash_bin":"2034385a2621c53a490f34c5893a860664741da4", \ "name":"Super Eurobeat Vol. 220 - Anniversary Hits",\ "size":365751495 \ }, \ ]) r=session.query(HiFile.TrackerManager.XBT_FILES).count() self.assertEqual(r,2) r=session.query(HiFile._database.Torrent).count() self.assertEqual(r,2) r=session.query(HiFile._database.Torrent).filter(HiFile._database.Torrent.info_hash_bin==binascii.a2b_hex("39eaf2230aa0bcf9148f84f6efe0c64cc1ab80c1")).all() self.assertEqual(len(r),1) self.assertEqual(r[0].torrent_id,2) self.assertEqual(r[0].user_id,"uuuu0") self.assertEqual(r[0].name,"魔法少女小圓") self.assertEqual(r[0].size,616839726) self.assertEqual(binascii.b2a_hex(r[0].info_hash_bin),"39eaf2230aa0bcf9148f84f6efe0c64cc1ab80c1") cleanup.clean_all() torrent_file=open("res/test3.torrent","rb") ret=HiFile._hisocial.command_user_upload_torrent("uuuu0", torrent_file) self.assertEqual(ret,{"result":"ok","torrent_id":3}) torrent_file.close() session = Database.create_sqlalchemy_session_push(cleanup) r=session.query(HiFile._database.Torrent).filter(HiFile._database.Torrent.info_hash_bin==binascii.a2b_hex("da5142099da45138ebbab05a40664c98ac2c0496")).all() self.assertEqual(len(r),1) self.assertEqual(r[0].torrent_id,3) self.assertEqual(r[0].user_id,"uuuu0") self.assertEqual(r[0].name,"[CASO&SumiSora][Puella_Magi_Madoka_Magica][BDRIP][GB_BIG5]") self.assertEqual(r[0].size,20185116425) self.assertEqual(binascii.b2a_hex(r[0].info_hash_bin),"da5142099da45138ebbab05a40664c98ac2c0496") cleanup.clean_all()
def __init__(self, label='', name='', shortDesc='', longDesc=''): Database.__init__(self, label=label, name=name, shortDesc=shortDesc, longDesc=longDesc)
def load(self, path): """ Load the solute library from the given path """ Database.load(self, path, local_context={"SoluteData": SoluteData}, global_context={})