crafty-4/app/classes/shared/command.py

137 lines
4.6 KiB
Python
Raw Normal View History

import sys
import cmd
import time
import threading
import logging
2022-05-16 19:39:10 +00:00
import getpass
from app.classes.shared.console import Console
from app.classes.shared.import3 import Import3
2022-05-26 13:28:54 +00:00
from app.classes.shared.helpers import Helpers
from app.classes.shared.tasks import TasksManager
from app.classes.shared.migration import MigrationManager
from app.classes.shared.main_controller import Controller
logger = logging.getLogger(__name__)
2022-03-08 04:40:44 +00:00
class MainPrompt(cmd.Cmd):
2022-06-05 20:02:52 +00:00
def __init__(
self, helper, tasks_manager, migration_manager, main_controller, import3
):
super().__init__()
2022-05-26 13:28:54 +00:00
self.helper: Helpers = helper
self.tasks_manager: TasksManager = tasks_manager
self.migration_manager: MigrationManager = migration_manager
self.controller: Controller = main_controller
2022-06-05 20:02:52 +00:00
self.import3: Import3 = import3
2022-05-16 19:39:10 +00:00
# overrides the default Prompt
self.prompt = ""
2022-05-20 23:04:03 +00:00
def emptyline(self):
pass
def do_exit(self, _line):
2021-12-09 23:35:00 +00:00
self.tasks_manager._main_graceful_exit()
self.universal_exit()
2021-08-18 15:11:53 +00:00
def do_migrations(self, line):
if line == "up":
2021-08-18 15:11:53 +00:00
self.migration_manager.up()
elif line == "down":
2021-08-18 15:11:53 +00:00
self.migration_manager.down()
elif line == "done":
Console.info(self.migration_manager.done)
elif line == "todo":
Console.info(self.migration_manager.todo)
elif line == "diff":
Console.info(self.migration_manager.diff)
elif line == "info":
Console.info(f"Done: {self.migration_manager.done}")
Console.info(f"FS: {self.migration_manager.todo}")
Console.info(f"Todo: {self.migration_manager.diff}")
elif line.startswith("add "):
migration_name = line[len("add ") :]
2021-08-18 15:11:53 +00:00
self.migration_manager.create(migration_name, False)
else:
Console.info("Unknown migration command")
2022-01-15 00:23:50 +00:00
2022-05-16 19:39:10 +00:00
def do_set_passwd(self, line):
try:
username = line
2022-05-18 21:37:07 +00:00
# If no user is found it returns None
2022-05-16 19:39:10 +00:00
user_id = self.controller.users.get_id_by_name(username)
2022-05-18 21:37:07 +00:00
if not username:
Console.error("You must enter a username. Ex: `set_passwd admin'")
return False
if not user_id:
Console.error(
f"No user found by the name of {username} this is case sensitive"
)
2022-05-18 21:37:07 +00:00
return False
2022-05-17 20:01:37 +00:00
except:
2022-05-16 19:39:10 +00:00
Console.error(f"User: {line} Not Found")
return False
new_pass = getpass.getpass(prompt=f"NEW password for: {username} > ")
new_pass_conf = getpass.getpass(prompt="Re-enter your password: > ")
if new_pass != new_pass_conf:
Console.error("Passwords do not match. Please try again.")
return False
if len(new_pass) > 512:
2022-05-18 21:22:39 +00:00
Console.warning("Passwords must be greater than 6char long and under 512")
2022-05-16 19:39:10 +00:00
return False
if len(new_pass) < 6:
2022-05-18 21:22:39 +00:00
Console.warning("Passwords must be greater than 6char long and under 512")
2022-05-16 19:39:10 +00:00
return False
self.controller.users.update_user(user_id, {"password": new_pass})
2022-01-15 00:23:50 +00:00
@staticmethod
def do_threads(_line):
for thread in threading.enumerate():
2022-01-15 00:23:50 +00:00
if sys.version_info >= (3, 8):
print(
f"Name: {thread.name}\tIdentifier: "
f"{thread.ident}\tTID/PID: {thread.native_id}"
)
2022-01-15 00:23:50 +00:00
else:
print(f"Name: {thread.name}\tIdentifier: {thread.ident}")
2022-05-20 22:53:17 +00:00
def print_prompt(self):
self.stdout.write(self.prompt)
self.stdout.flush()
2022-03-19 03:59:10 +00:00
def do_import3(self, _line):
2022-06-05 20:02:52 +00:00
self.import3.start_import()
def universal_exit(self):
logger.info("Stopping all server daemons / threads")
Console.info(
"Stopping all server daemons / threads - This may take a few seconds"
)
self.helper.websocket_helper.disconnect_all()
Console.info("Waiting for main thread to stop")
while True:
if self.tasks_manager.get_main_thread_run_status():
sys.exit(0)
time.sleep(1)
def help_exit(self):
Console.help("Stops the server if running, Exits the program")
2022-01-15 00:23:50 +00:00
def help_migrations(self):
Console.help("Only for advanced users. Use with caution")
2022-03-20 17:14:51 +00:00
def help_import3(self):
Console.help("Import users and servers from Crafty 3")
def help_set_passwd(self):
Console.help("Set a user's password. Example: set_passwd admin")
def help_threads(self):
Console.help("Get all of the Python threads used by Crafty")