add docstrings
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 52s
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 52s
This commit is contained in:
parent
2c32f833bc
commit
f045434cfb
@ -27,6 +27,7 @@ from rsbbs.models import User
|
|||||||
# Main UI console class
|
# Main UI console class
|
||||||
|
|
||||||
class Console():
|
class Console():
|
||||||
|
"""Class for user input and output."""
|
||||||
|
|
||||||
def __init__(self, config: Config, controller: Controller, user: User):
|
def __init__(self, config: Config, controller: Controller, user: User):
|
||||||
self.config = config
|
self.config = config
|
||||||
@ -43,52 +44,72 @@ class Console():
|
|||||||
#
|
#
|
||||||
|
|
||||||
def disconnect(self) -> None:
|
def disconnect(self) -> None:
|
||||||
|
"""End the program, disconnecting the user
|
||||||
|
"""
|
||||||
logging.info("caller disconnected")
|
logging.info("caller disconnected")
|
||||||
exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
def read_enter(self, prompt) -> None:
|
def read_enter(self, prompt: str) -> None:
|
||||||
"""Wait for the user to press enter.
|
"""Wait for the user to press enter.
|
||||||
|
|
||||||
|
:param prompt: an optional prompt to show the user
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if prompt:
|
if prompt:
|
||||||
self.write_output(prompt)
|
self.write_output(prompt)
|
||||||
sys.stdin.readline().strip()
|
sys.stdin.readline().strip()
|
||||||
|
|
||||||
def read_line(self, prompt) -> str:
|
def read_line(self, prompt: str) -> str:
|
||||||
"""Read a single line of input, with an optional prompt,
|
"""Read a single line of input, with an optional prompt,
|
||||||
until we get something.
|
until we get something.
|
||||||
|
|
||||||
|
:param prompt: an optional prompt to show the user
|
||||||
|
|
||||||
"""
|
"""
|
||||||
output = None
|
valid_input = None
|
||||||
while not output:
|
while not valid_input:
|
||||||
if prompt:
|
if prompt:
|
||||||
self.write_output(prompt)
|
self.write_output(prompt)
|
||||||
input = sys.stdin.readline().strip()
|
input_line = sys.stdin.readline().strip()
|
||||||
if input != "":
|
if input_line != "":
|
||||||
output = input
|
valid_input = input_line
|
||||||
return output
|
return valid_input
|
||||||
|
|
||||||
def read_multiline(self, prompt) -> str:
|
def read_multiline(self, prompt: str) -> str:
|
||||||
"""Read multiple lines of input, with an optional prompt,
|
"""Read multiple lines of input, with an optional prompt,
|
||||||
until the user enters '/ex' by itself on a line.
|
until the user enters '/ex' by itself on a line.
|
||||||
|
|
||||||
|
:param prompt: an optional prompt to show the user
|
||||||
|
|
||||||
"""
|
"""
|
||||||
output = []
|
input_lines = []
|
||||||
if prompt:
|
if prompt:
|
||||||
self.write_output(prompt)
|
self.write_output(prompt)
|
||||||
while True:
|
while True:
|
||||||
line = sys.stdin.readline()
|
input_line = sys.stdin.readline()
|
||||||
if line.lower().strip() == "/ex":
|
if input_line.lower().strip() != "/ex":
|
||||||
break
|
break
|
||||||
else:
|
input_lines.append(input_line)
|
||||||
output.append(line)
|
return ''.join(input_lines)
|
||||||
return ''.join(output)
|
|
||||||
|
|
||||||
def write_output(self, output) -> None:
|
def write_output(self, output: str) -> None:
|
||||||
"""Write something to stdout."""
|
"""Write something to stdout.
|
||||||
|
|
||||||
|
:param output: the string to write to stdout
|
||||||
|
|
||||||
|
"""
|
||||||
sys.stdout.write(output + '\r\n')
|
sys.stdout.write(output + '\r\n')
|
||||||
|
|
||||||
def print_configuration(self) -> None:
|
def print_configuration(self) -> None:
|
||||||
|
"""Print the current running configuration.
|
||||||
|
|
||||||
|
"""
|
||||||
self.write_output(repr(self.config))
|
self.write_output(repr(self.config))
|
||||||
|
|
||||||
def print_greeting(self) -> None:
|
def print_greeting(self) -> None:
|
||||||
|
"""Show a greeting to welcome the user upon connection.
|
||||||
|
|
||||||
|
"""
|
||||||
# Show greeting
|
# Show greeting
|
||||||
greeting = []
|
greeting = []
|
||||||
greeting.append(f"[RSBBS-{rsbbs.__version__}] listening on "
|
greeting.append(f"[RSBBS-{rsbbs.__version__}] listening on "
|
||||||
@ -105,8 +126,12 @@ class Console():
|
|||||||
|
|
||||||
self.write_output('\r\n'.join(greeting))
|
self.write_output('\r\n'.join(greeting))
|
||||||
|
|
||||||
def print_message(self, message):
|
def print_message(self, message: str):
|
||||||
"""Print an individual message."""
|
"""Print an individual message.
|
||||||
|
|
||||||
|
:param message: the message to print
|
||||||
|
|
||||||
|
"""
|
||||||
# Format the big ol' date and time string
|
# Format the big ol' date and time string
|
||||||
datetime = message.Message.datetime.strftime(
|
datetime = message.Message.datetime.strftime(
|
||||||
'%A, %B %-d, %Y at %-H:%M %p UTC')
|
'%A, %B %-d, %Y at %-H:%M %p UTC')
|
||||||
@ -120,8 +145,12 @@ class Console():
|
|||||||
self.write_output("")
|
self.write_output("")
|
||||||
self.write_output(f"{message.Message.message}")
|
self.write_output(f"{message.Message.message}")
|
||||||
|
|
||||||
def print_message_list(self, messages) -> None:
|
def print_message_list(self, messages: list) -> None:
|
||||||
"""Print a list of messages."""
|
"""Print a list of messages.
|
||||||
|
|
||||||
|
:param messages: a list of Message objects to print
|
||||||
|
|
||||||
|
"""
|
||||||
# Print the column headers
|
# Print the column headers
|
||||||
self.write_output(f"{'MSG#': <{5}} "
|
self.write_output(f"{'MSG#': <{5}} "
|
||||||
f"{'TO': <{9}} "
|
f"{'TO': <{9}} "
|
||||||
@ -142,11 +171,13 @@ class Console():
|
|||||||
#
|
#
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
"""Main UI loop.
|
||||||
|
|
||||||
|
"""
|
||||||
# If asked to show the config, show the config;
|
# If asked to show the config, show the config;
|
||||||
if self.config.args.show_config:
|
if self.config.args.show_config:
|
||||||
self.print_configuration()
|
self.print_configuration()
|
||||||
exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# Show greeting
|
# Show greeting
|
||||||
self.print_greeting()
|
self.print_greeting()
|
||||||
@ -155,15 +186,14 @@ class Console():
|
|||||||
self.write_output(self.config.command_prompt)
|
self.write_output(self.config.command_prompt)
|
||||||
|
|
||||||
# Parse the BBS interactive commands for the rest of time
|
# Parse the BBS interactive commands for the rest of time
|
||||||
for line in sys.stdin:
|
for input_line in sys.stdin:
|
||||||
try:
|
try:
|
||||||
args = self.parser.parse_args(line.split())
|
args = self.parser.parse_args(input_line.split())
|
||||||
args.func(args)
|
args.func(args)
|
||||||
except Exception:
|
except Exception:
|
||||||
if self.config.debug:
|
if self.config.debug:
|
||||||
raise
|
raise
|
||||||
else:
|
pass
|
||||||
pass
|
|
||||||
|
|
||||||
# Show our prompt to the calling user again
|
# Show our prompt to the calling user again
|
||||||
self.write_output(self.config.command_prompt)
|
self.write_output(self.config.command_prompt)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user