Compare commits

..

2 Commits

Author SHA1 Message Date
c02f713ad2 sort bbs commands alphabetically in help 2023-04-30 11:36:52 -05:00
9c0dd4d581 add basic stats command 2023-04-30 11:36:39 -05:00
2 changed files with 87 additions and 0 deletions

View File

@ -29,6 +29,25 @@ class BBSArgumentParser(argparse.ArgumentParser):
pass
class SortedHelpFormatter(argparse.HelpFormatter):
def _iter_indented_subactions(self, action):
try:
get_subactions = action._get_subactions
except AttributeError:
pass
else:
self._indent()
if isinstance(action, argparse._SubParsersAction):
for subaction in sorted(
get_subactions(),
key=lambda x: x.dest):
yield subaction
else:
for subaction in get_subactions():
yield subaction
self._dedent()
class Parser(BBSArgumentParser):
def __init__(self):
@ -43,12 +62,15 @@ class Parser(BBSArgumentParser):
return getattr(self.parser, attr)
def _init_parser(self):
self.sort_actions = True
# Root parser for BBS commands
self.parser = BBSArgumentParser(
description='BBS Main Menu',
prog='',
add_help=False,
usage=argparse.SUPPRESS,
formatter_class=SortedHelpFormatter,
)
# We will create a subparser for each individual command

View File

@ -0,0 +1,65 @@
#!/usr/bin/env python
#
# Really Simple BBS - a really simple BBS for ax.25 packet radio.
# Copyright (C) 2023 John Burwell <john@atatdotdot.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
import sqlalchemy
import sqlalchemy.orm
import subprocess
from rsbbs import __version__
from rsbbs import Console, Parser
from rsbbs.models import Message
class Plugin():
def __init__(self, api: Console) -> None:
self.api = api
self.init_parser(api.parser)
logging.info(f"Plugin {__name__} loaded")
def init_parser(self, parser: Parser) -> None:
subparser = parser.subparsers.add_parser(
name='stats',
aliases=['st'],
help='Report some statistics about this BBS.')
subparser.set_defaults(func=self.run)
def get_message_count(self) -> int:
with self.api.controller.session() as session:
try:
count = session.execute(sqlalchemy.select(
sqlalchemy.func.count(Message.id))).scalar_one()
return int(count)
except Exception as e:
logging.error(e)
def get_uptime(self) -> str:
result = subprocess.run(
['uptime'], capture_output=True, text=True)
return result.stdout
def run(self, args) -> None:
"""Show some stats."""
response = []
response.append(f"[RSBBS-{__version__}] listening on "
f"{self.api.config.callsign} ")
response.append(f"Messages: {self.get_message_count()}")
response.append(f"Uptime: {self.get_uptime()}")
self.api.write_output('\r\n'.join(response))
logging.info(f"report stats")