def activate(self, kwargs): def _activate(app): all_apps = Applications.all().all() for app_model in all_apps: app_model.active = 0 app_model.save() app.active = 1 app.save() if 'name' in kwargs: name = kwargs["name"] try: app = Applications.where('app_name', '=', name).first_or_fail() _activate(app) except ModelNotFound as e: print(colored(e, "red")) sys.exit(colored("Application with name ", "red") \ + colored("'" + name + "'", "yellow") + colored(" is not found", "red")) elif 'id' in kwargs: id = kwargs["id"] try: app = Applications.where('application_identifier', '=', id).first_or_fail() _activate(app) except ModelNotFound as e: print(colored(e, "red")) sys.exit(colored("Application with id ", "red") \ + colored("'" + str(id) + "'", "yellow") + colored(" is not found", "red")) else: print( colored( "Please use --name or --id to choose the application to activate", "yellow"))
def delete(self, kwargs): def _delete(app, kwargs): app_dir = str(app.app_directory) app.delete() if 'clean_up' in kwargs: shutil.rmtree(app_dir) if 'name' in kwargs: name = kwargs["name"] try: app = Applications.where('app_name', '=', name).first_or_fail() _delete(app, kwargs) except ModelNotFound as e: print(colored(e, "red")) sys.exit(colored("Application with name ", "red") \ + colored("'" + name + "'", "yellow") + colored(" is not found", "red")) elif 'id' in kwargs: id = kwargs["id"] try: app = Applications.where('application_identifier', '=', id).first_or_fail() _delete(app, kwargs) except ModelNotFound as e: print(colored(e, "red")) sys.exit(colored("Application with id ", "red") \ + colored("'" + str(id) + "'", "yellow") + colored(" is not found", "red")) else: print( colored( "Please use --name or --id to choose the application to delete", "yellow"))
def _activate(app): all_apps = Applications.all().all() for app_model in all_apps: app_model.active = 0 app_model.save() app.active = 1 app.save()
def list(self, kwargs): def decompose(app_model): return { "app_name": app_model.app_name, # "description": app_model.description, "developer": app_model.developer, "version": app_model.version, "application_identifier": app_model.application_identifier, "created_at": app_model.created_at.to_datetime_string(), # "updated_at": app_model.updated_at.to_datetime_string(), "active": app_model.active } apps = Applications.all().all() rows = [decompose(x) for x in apps] headings = [ "app_name", "developer", "version", "application_identifier", "created_at", "active" ] table = PrettyTable(headings) for row in rows: table.add_row(list(row.values())) print(table)
def set_active(self): try: self.active_app = Applications.where('active', '=', 1).first_or_fail() except ModelNotFound as e: sys.exit( colored("No active application found.", "red") + colored(" Please active a application to proceed", "yellow")) self.active_app = Applications.where('active', '=', 1).first_or_fail() self.active_app_dict = self.active_app.attributes_to_dict() app_directory = Path( self.active_app_dict["app_directory"]) / "app/Controllers" root_directory = Path(self.active_app_dict["app_directory"]) sys.path.append(str(root_directory)) self.all_files = [] for root, subdirs, files in os.walk(app_directory): if '__pycache__' not in root: temp_root = Path(root) self.all_files = self.all_files + [ temp_root / file for file in files if file != '__init__.py' ]
def register_application(self, app_data): new_app = Applications() new_app.app_name = app_data["app_name"] new_app.description = app_data["description"] new_app.developer = app_data["developer"] new_app.version = app_data["version"] new_app.app_directory = app_data["app_directory"] new_app.application_identifier = app_data["application_identifier"] new_app.application_type = 1 new_app.save()
def list_applications(self): return Applications.all()
def describe(self, kwargs): def _describe(app): app_data = app.attributes_to_dict() print( colored( pyfiglet.figlet_format("--- " + app_data["app_name"] + " ---"), "green")) print( colored("APPLICATION NAME:", "green") + " " + colored(app_data["app_name"], "blue") + "") print( colored("APPLICATION DESCRIPTION:", "green") + " " + colored(app_data["description"], "blue") + "") print( colored("APPLICATION DEVELOPER:", "green") + " " + colored(app_data["developer"], "blue") + "") print( colored("APPLICATION SOURCE DIRECTORY:", "green") + " " + colored(app_data["app_directory"], "blue") + "") print( colored("APPLICATION IDENTIFIER:", "green") + " " + colored(app_data["application_identifier"], "blue") + "") print( colored("APPLICATION VERSION:", "green") + " " + colored(app_data["version"], "blue") + "") print( colored("APPLICATION TYPE:", "green") + " " + colored(app_data["application_type"], "blue") + "") print( colored("APPLICATION CREATION DATE:", "green") + " " + colored(app_data["created_at"], "blue") + "") print( colored("APPLICATION ACTIVE STATUS:", "green") + " " + colored(app_data["active"], "blue") + "\n") print( colored( pyfiglet.figlet_format("*** " + app_data["app_name"] + " ***"), "green")) if 'name' in kwargs: name = kwargs["name"] try: app = Applications.where('app_name', '=', name).first_or_fail() _describe(app) except ModelNotFound as e: print(colored(e, "red")) sys.exit(colored("Application with name ", "red") \ + colored("'" + name + "'", "yellow") + colored(" is not found", "red")) elif 'id' in kwargs: id = kwargs["id"] try: app = Applications.where('application_identifier', '=', id).first_or_fail() _describe(app) except ModelNotFound as e: print(colored(e, "red")) sys.exit(colored("Application with id ", "red") \ + colored("'" + str(id) + "'", "yellow") + colored(" is not found", "red")) else: print( colored( "Please use --name or --id to choose the application to describe", "yellow"))
def exists(app_name): apps = [x.app_name.lower() for x in Applications.all().all()] if app_name.lower() in apps: return True return False