def show_upcoming_tasks_for_user(user_id): sql = "SELECT task_description, due_date, due_time, time_created " \ "FROM tasks " \ "JOIN users " \ "ON tasks.user_id = users.user_id " \ "WHERE tasks.user_id = %s AND tasks.due_date > NOW()" db = Database() tasks = db.fetchall_results(sql, (user_id, )) db.close() if len(tasks) == 0: return input( "You don't have any upcoming tasks. Would you like to create one now?\n" "yes\nno\n") else: count = 1 for task in tasks: print("------------------------------------") print("TASK #", count) print("task description:", task[0]) print("due date:", calendar.day_name[task[1].weekday()], task[1]) print("due_time:", task[2]) # print("time until:", ... - datetime.now()) # TODO feature... coming up in next sprint! :) print("task created on:", task[3]) count += 1
def __init__(self, session): if not isinstance(session, AnnotationSession): raise AssertionError( 'Use the DatabaseConstructor to build params for annotation') self.dbHelper = Database(session.dbName) self.session = session
def _insert_new_user_to_db(self, user): sql = "INSERT INTO users(username, password) VALUES(%s, %s)" values = (user.get_username(), user.get_password()) db = Database() db.commit_to_db(sql, values) print("You account is successfully registered, " + user.get_username().title() + "!") return self._get_user_id_from_db(user)
def __init__(self): self.bash_proj_dir = "bash" self.default_dir_name = "wallpaper" self.bash_file_name = "changeWall.sh" self.bat_file_name = "changeWall.bat" self.vbs_file_name = "changeWall.vbs" self.setup_files() self.all_pages = [] self.spaceManager = SpaceManager() self.db = Database().get_instance() self.pictureManager = PictureManager()
def _check_if_account_exists(self, user): # searching for a username-password match sql_username = "******" sql_password = "******" values = (user.get_username(), user.get_password()) db = Database() results_u = db.fetchall_results(sql_username, (values[0], )) results_p = db.fetchall_results(sql_password, (values[1], )) db.close() # bug: ignoring the letter case with this list comprehension match = [(a, b) for (a, b) in results_u for (c, d) in results_p if (a == c) and (b == d)] if len(results_u) == 0: # username doesn't exists return False elif len(match) == 1: # username - password match return True elif len( match ) == 0: # username-password mis-match: password either doesn't exist in db while len(match) == 0: # or it doesn't match the entered username print("Password incorrect. Try again.") user.input_password() pw_value = user.get_password() db = Database() results_p = db.fetchall_results(sql_password, (pw_value, )) match = [(a, b) for (a, b) in results_u for (c, d) in results_p if (a == c) and (b == d)] if len(match) == 1: return True
class Crypto(object): def __init__(self): self.db = Database(config.mongodb_uri, config.database_name) self.db.set_collection("Cryptography") self.secret = self.__get_secret() def __del__(self): try: self.db.client.close() except Exception: pass def __find_secret_from_db(self): query = {"secret_key": {"$exists": 1}} secrets = self.db.find(query) temp = [] for secret in secrets: if len(secret) == 2: temp.append(secret) if len(temp) > 1: raise RuntimeError("There is more than one secret in the DB") elif len(temp) == 1: return temp[0]["secret_key"] else: return None def __gen_secret(self): existing_secret = self.__find_secret_from_db() if existing_secret is not None: return existing_secret secret = Fernet.generate_key() self.db.insert({"secret_key": secret}) return secret def __get_secret(self): existing_secret = self.__find_secret_from_db() if existing_secret is not None: return existing_secret return self.__gen_secret() def encrypt(self, data: str): fernet = Fernet(self.secret) return fernet.encrypt(data.encode()) def decrypt(self, data: str): fernet = Fernet(self.secret) return fernet.decrypt(data).decode()
def create_new_task(self, user_id): sql = "INSERT INTO tasks(user_id, task_description, due_date, due_time) " \ "VALUES(%s, %s, %s, %s)" print( "To save a new task, you will have to enter its description and date and time when the task is due." ) self._task.set_description() self._task.set_due_date() self._task.set_due_time() db = Database() values = (user_id, self._task.get_description(), self._task.get_due_date(), self._task.get_due_time()) db.commit_to_db(sql, values) print("Task successfully saved.\n")
def main(): database = Database(db_session) timetable = Timetable(database) t1 = Thread(target=threaded_fun, args=(timetable, )) t1.start() rest_engine = RESTEngine(database, timetable) rest_engine.run()
class SpaceManager: """ If overall size of all downloaded image in app dir was more that 2G then remove oldest file in order. """ def __init__(self): self.space_limit = 2048 # 2 gigabyte(2048 MB), change it to mange space limit self.db = Database().get_instance() def check_space(self): """reduce space tacked by app limit is 2G if necessary""" overall_size = self.db.get_dir_size() if overall_size > self.space_limit: self.remove_oldest_files(overall_size - self.space_limit) size = Util.get_instance().get_wall_dir_size() self.db.update_dir_size(size) def remove_oldest_files(self, overflow_size): """delete oldest file base on modified time""" paths = sorted(Path(Util.get_instance().project_dir).iterdir(), key=os.path.getmtime) tmp_size = 0 index = 0 for file in paths: print(file) if file.exists() and file.is_file(): tmp_size += (os.path.getsize(Path(file)) / (1024 * 1024)) index = paths.index(file) if tmp_size >= overflow_size: break for i in range(len(paths) - 1): if i <= index: file = paths.__getitem__(i) if file.exists() and file.is_file(): os.remove(Path(file)) else: break def remove_duplicate_image(self, page_list): for page in page_list: if os.path.exists(page.image_local_address): if self.db.is_duplicate_file(page.image_name, page.image_hash): os.remove(page.image_local_address)
class Credential(object): def __init__(self): self.db = Database(config.mongodb_uri, config.database_name) self.db.set_collection("credential") def __del__(self): try: self.db.client.close() except Exception: pass def get_all_credentials(self) -> dict: credentials = self.db.find_all({"_id": 1, "name": 1, "username": 1}) for cred in credentials: # convert ObjectId to string cred["_id"] = str(cred["_id"]) credentials = {"credentials": credentials} return credentials def create_credential(self, data: dict) -> bool: # check if i have all the data i need required_key = ["name", "username", "password"] for key in required_key: if key not in data: raise ValueError("credential need name, username and password") # check if name exist in db exist_data = self.db.find({"name": data["name"]}) if len(exist_data) > 0: raise ValueError("Same name for credential already exist in DB") # encrypt the password crypto_tool = Crypto() data["password"] = crypto_tool.encrypt(data["password"]) return self.db.insert(data) def get_one_credential_with_password(self, id) -> dict: crypto_tool = Crypto() credential = self.db.find_by_id(id) if credential is None: raise ValueError("not able to find credential from db") credential["password"] = crypto_tool.decrypt(credential["password"]) return credential
from db.Database import Database from static.config import credentials WCA_Database = Database(credentials['db'], credentials['host'], credentials['user'], credentials['passwd'], socket=credentials.get('socket', None), port=credentials.get('port', 3306))
def cli(): cfg = APPConfig() Database(cfg.read("database"))
from api.app import StandaloneApplication from api.routes.dataset_key_route import DatasetKeyRoute from api.routes.dataset_route import DatasetRoute import falcon import multiprocessing from api.routes.kpi_definition_route import KpiDefinitionRoute from api.routes.operator_route import OperatorRoute from api.routes.outlier_route import OutlierRoute from config.APPConfig import APPConfig from db.Database import Database # TODO cfg = APPConfig() Database(cfg.read("database")) api = application = falcon.API() api.add_route('/dataset_keys', DatasetKeyRoute()) api.add_route('/datasets', DatasetRoute()) api.add_route('/operators', OperatorRoute()) api.add_route('/kpi_definitions', KpiDefinitionRoute()) api.add_route('/outliers', OutlierRoute()) # TODO Database connection freezes when using code below via cli def number_of_workers(): return (multiprocessing.cpu_count() * 2) + 1
def __init__(self): self.db = Database(config.mongodb_uri, config.database_name) self.db.set_collection("credential")
def __init__(self): self.space_limit = 2048 # 2 gigabyte(2048 MB), change it to mange space limit self.db = Database().get_instance()
def __init__(self): self._dbconn = Database()
from AuthenticationMenu import UserAuthenticated from db.Database import Database db = Database() # new = Authentication() # new.register_user(db) # print(new.get_user_id()) l = UserAuthenticated() l.login_user(db) print(l.get_user_id()) # hey = Authentication() # user_id = hey._get_user_id_from_db(db, values=('kokica', 'kokica')) # print(user_id) # user = User() # l1 = Authentication() # result = l1._check_if_account_exists(db, user) # print(result) ''' # username exists, correct password # "nairobi", "vrlotajnalozinka" user1 = Authentication() account_exists = user1.login_user(cursor) print("Account exists:", account_exists) print("Expected: correct user id") #username doesn't exist, password exists # "denver", "vrlotajnalozinka" user2 = Authentication()
def _get_user_id_from_db(user): sql = "SELECT user_id FROM users WHERE username = %s AND password = %s" values = (user.get_username(), user.get_password()) db = Database() results = db.fetchone_result(sql, values) return results[0]
class TruthAnnotator(object): def __init__(self, session): if not isinstance(session, AnnotationSession): raise AssertionError( 'Use the DatabaseConstructor to build params for annotation') self.dbHelper = Database(session.dbName) self.session = session def onKeyPressed(self, event): if event.key == 'n': print 'Pressed: ' + event.key + ': Skip this eyeball' self.addEyeBall(self.fileName, -1, -1) else: plt.waitforbuttonpress() def onPointSelected(self, event): if event.xdata is None or event.ydata is None: # wasn't a valid click, don't close the window yet! plt.waitforbuttonpress() return print ' x=%d, y=%d, xdata=%f, ydata=%f' % (event.x, event.y, event.xdata, event.ydata) self.addEyeBall(self.fileName, event.xdata, event.ydata) def cycle(self): os.chdir(self.session.filePath) files = [ f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.jpeg') or f.endswith('.jpg') ] for file in files: self.fileName = file.__str__() if FeatureDebug.START_TRUTH_FROM_PREV: if self.dbHelper.eyeBallExists(self.fileName): print 'Skipped filename' continue self.image = cv2.imread(file.__str__(), 1) self.annotated = self.image.copy() print '\nLoading ' + self.fileName fig = plt.figure() plt.imshow(cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)) plt.get_current_fig_manager().window.wm_geometry("+0+0") fig.canvas.mpl_connect('button_press_event', self.onPointSelected) fig.canvas.mpl_connect('key_press_event', self.onKeyPressed) plt.waitforbuttonpress() plt.close(fig) def initMouseCallback(self, fileName): cv2.namedWindow(fileName) cv2.setMouseCallback(fileName, self.onPointSelected) pass def addEyeBall(self, fileName, x, y): eyeball = Eyeball(fileName) eyeball.addPupilTruth(str(int(x)), str(int(y))) eyeball.setTimeStamp() eyeball.setPerson(self.session.person) # first character of file name indicates left or right side camera cameraSide = "left" if fileName[ 0] == "L" else "right" if fileName[0] == "R" else "none" eyeball.setCamera(cameraSide) eyeball.setPrescriptionType(self.session.prescriptionType) self.dbHelper.addEyeball(eyeball) # change this pprint.pprint(eyeball.getDict()) pass
def __init__(self): self.db = Database(config.mongodb_uri, config.database_name) self.db.set_collection("resource")
class Resource(object): def __init__(self): self.db = Database(config.mongodb_uri, config.database_name) self.db.set_collection("resource") def __del__(self): try: self.db.client.close() except Exception: pass def discover_new_node(self, node: dict) -> bool: # check if i have all the data i need required_key = ["address", "cred_id"] for key in required_key: if key not in node: raise ValueError("discover new node require address and credential id") # get credential detail from DB cred_tool = Credential() cred = cred_tool.get_one_credential_with_password(node["cred_id"]) # initiate ssh connection ssh = SSHClient(node["address"], cred["username"], cred["password"]) # check if user have sudo privilege if not ssh.check_sudo_privilege(): raise RuntimeError("User don't have sudo previlege") # check Linux distro distro = ssh.command("cat /etc/os-release") distro = util.convert_to_dict.config_to_dict(distro) try: if distro["ID"] != "centos": raise RuntimeError("Only support CentOS 7") if int(distro["VERSION_ID"]) != 7: raise RuntimeError("Only support CentOS 7") except KeyError as err: logging.exception("cannot verify the linux distro : {}".format(distro)) raise RuntimeError("Only support CentOS 7") # get disk list in dict disk_list = util.inventory.get_disk_list(ssh) # check if address exist in db exist_data = self.db.find({"address": node["address"]}) if len(exist_data) > 0: raise ValueError("node is already discovered") # write node infomation to DB return self.db.insert({ "address" : node["address"], "cred_id" : node["cred_id"], "distro" : distro["ID"], "version" : int(distro["VERSION_ID"]), "name" : distro["PRETTY_NAME"] if "PRETTY_NAME" in distro else distro["ID"] + " " + distro["VERSION_ID"], "disks": disk_list }) def get_all_nodes(self): nodes = self.db.find_all() for node in nodes: # convert ObjectId to string node["_id"] = str(node["_id"]) nodes = {"nodes" : nodes} return nodes def delete_one_node(self, node_id): # check if i have all the data i need required_key = ["id"] for key in required_key: if key not in node_id: raise ValueError("delete node requires id") #TODO # Need to add check if node is in use for cluster. if yes do not delete self.db.delete(node_id["id"])
def _get_all_usernames_from_db(): sql = "SELECT username FROM users" db = Database() list_of_tuples = db.query_db(sql, ) return ["".join(i) for i in list_of_tuples]
class BuildMySql: def __init__(self): self._dbconn = Database() def create_database(self, name): sql = "CREATE DATABASE %s" self._dbconn.commit_to_db(sql, (name, )) self._dbconn.close() def create_table_users(self): sql = """CREATE TABLE users( user_id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(50) NOT NULL )""" self._dbconn.commit_to_db(sql) self._dbconn.close() def drop_table(self, name): sql = "DROP TABLE %s" self._dbconn.commit_to_db(sql, (name, )) self._dbconn.close() def truncate_table(self, name): sql = "TRUNCATE TABLE %s" self._dbconn.commit_to_db(sql, (name, )) self._dbconn.close()
def __init__(self): self.db = Database(config.mongodb_uri, config.database_name) self.db.set_collection("Cryptography") self.secret = self.__get_secret()
import json from db.Database import Database from model.Cars import Cars from model.Rentals import Rentals data = Database() # List of Objects rentals = Rentals.get_all(data) # cars = Cars.get_all(data) file_path = './data/output.json' def generate_output(file_path): rentals_output = {"rentals": []} for i in rentals: rentals_output["rentals"].append(i.get_final_output(data)) with open(file_path, 'w') as json_file: json.dump(rentals_output, json_file, indent=4) generate_output(file_path)
class Main: def __init__(self): self.bash_proj_dir = "bash" self.default_dir_name = "wallpaper" self.bash_file_name = "changeWall.sh" self.bat_file_name = "changeWall.bat" self.vbs_file_name = "changeWall.vbs" self.setup_files() self.all_pages = [] self.spaceManager = SpaceManager() self.db = Database().get_instance() self.pictureManager = PictureManager() def main(self): try: image_downloaded_count = self.download_new_wall() if image_downloaded_count > 0: self.spaceManager.remove_duplicate_image(self.all_pages) self.spaceManager.check_space() self.db.update_db(self.all_pages) image_address = self.pictureManager.choose_wallpaper( image_downloaded_count, self.all_pages) self.set_wallpaper(image_address) except Exception as e: pass def download_new_wall(self): image_downloaded_count = 0 old_date = self.db.get_date() today_date = datetime.date(datetime.now()) if old_date != str("today_date"): self.all_pages = [cls() for cls in Page.__subclasses__()] for page in self.all_pages: is_successful = page.fetch_image() if is_successful: image_downloaded_count += 1 return image_downloaded_count def set_wallpaper(self, image_address): if image_address is None or not os.path.exists(image_address): return try: os_platform = sys.platform script_path_dir = Util.get_instance().get_project_root( ) + os.sep + self.bash_proj_dir if os_platform.__contains__("linux"): linux_bash_path = script_path_dir + os.sep + self.bash_file_name subprocess.check_call( [str(linux_bash_path), str(image_address)]) elif os_platform.__contains__("win32"): win_bat_path = script_path_dir + os.sep + self.bat_file_name vbs_path = script_path_dir + os.sep + self.vbs_file_name subprocess.run( [str(vbs_path), str(win_bat_path), str(image_address)], shell=True) except Exception as e: print(e) pass def setup_files(self): home = expanduser("~") project_dir = home + os.sep + self.default_dir_name if not os.path.exists(project_dir): os.makedirs(project_dir) Util.get_instance().set_project_dir(project_dir)