Skip to content

pyclientutils API Documentation

cmd

Command line entry point

ClientUtilsCmd

Bases: BaseCmd

Command Line Interface

Source code in clientutils/cmd.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class ClientUtilsCmd(BaseCmd):
    """Command Line Interface"""

    def getArgParser(self, description: str, version_msg) -> ArgumentParser:
        """get the argument parser"""
        parser = super().getArgParser(description, version_msg)
        parser.add_argument(
            "--start",
            action="store_true",
            help="start the webserver",
        )
        parser.add_argument(
            "--port",
            dest="port",
            type=int,
            default=9998,
            help="port for the webserver (default: 9998)",
        )
        return parser

    def handle_args(self, args: Namespace) -> bool:
        """Handle the parsed arguments"""
        # Let base class handle standard args (--about, --debug, etc.)
        handled = super().handle_args(args)
        if handled:
            return True

        # Now handle our custom args
        if args.start:
            server = ClientUtilsServer(port=args.port)
            print(f"Starting ClientUtils server on port {args.port}...")
            server.start()
            return True  # Signal we handled it

        # If --start not provided, just exit normally
        return False

getArgParser(description, version_msg)

get the argument parser

Source code in clientutils/cmd.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def getArgParser(self, description: str, version_msg) -> ArgumentParser:
    """get the argument parser"""
    parser = super().getArgParser(description, version_msg)
    parser.add_argument(
        "--start",
        action="store_true",
        help="start the webserver",
    )
    parser.add_argument(
        "--port",
        dest="port",
        type=int,
        default=9998,
        help="port for the webserver (default: 9998)",
    )
    return parser

handle_args(args)

Handle the parsed arguments

Source code in clientutils/cmd.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def handle_args(self, args: Namespace) -> bool:
    """Handle the parsed arguments"""
    # Let base class handle standard args (--about, --debug, etc.)
    handled = super().handle_args(args)
    if handled:
        return True

    # Now handle our custom args
    if args.start:
        server = ClientUtilsServer(port=args.port)
        print(f"Starting ClientUtils server on port {args.port}...")
        server.start()
        return True  # Signal we handled it

    # If --start not provided, just exit normally
    return False

Version

Version information

Source code in clientutils/cmd.py
13
14
15
16
17
18
19
20
class Version:
    """Version information"""

    name = "clientutils"
    version = clientutils.__version__
    description = "MediaWiki Client Utilties"
    doc_url = "https://media.bitplan.com/index.php?title=CPSA-A-Analysis"
    updated = "2026-01-28"

main(argv=None)

Main entry point.

Source code in clientutils/cmd.py
61
62
63
64
def main(argv=None):
    """Main entry point."""
    exit_code = ClientUtilsCmd.main(Version(), argv)
    return exit_code

webserver

Simple REST server for serving file icons

ClientUtilsServer

Serves static file icons via HTTP

Source code in clientutils/webserver.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class ClientUtilsServer:
    """Serves static file icons via HTTP"""

    def __init__(self, port=9998):
        self.port = port
        self.app = Flask(__name__)
        self._setup_routes()

    def get_icons_directory(self) -> Path:
        """
        Get the path to the icons directory.

        Returns:
            Path: Absolute path to the icons directory

        Raises:
            FileNotFoundError: If icons directory doesn't exist
        """
        # Try relative to this file
        icons_dir = Path(__file__).parent.parent / "clientutils_examples" / "icons"

        if not icons_dir.exists():
            # Try relative to current working directory
            icons_dir = Path.cwd() / "clientutils_examples" / "icons"

        if not icons_dir.exists():
            raise FileNotFoundError(f"Icons directory not found. Tried: {icons_dir}")

        return icons_dir.resolve()

    def _setup_routes(self):
        """Configure routes for static file serving"""
        # Get the icons directory path
        icons_dir = self.get_icons_directory()

        @self.app.route("/fileicon/<path:filename>")
        def serve_icon(filename):
            """Serve file icons"""
            return send_from_directory(icons_dir, filename)

    def start(self):
        """Start the web server"""
        self.app.run(host="0.0.0.0", port=self.port, debug=False)

get_icons_directory()

Get the path to the icons directory.

Returns:

Name Type Description
Path Path

Absolute path to the icons directory

Raises:

Type Description
FileNotFoundError

If icons directory doesn't exist

Source code in clientutils/webserver.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def get_icons_directory(self) -> Path:
    """
    Get the path to the icons directory.

    Returns:
        Path: Absolute path to the icons directory

    Raises:
        FileNotFoundError: If icons directory doesn't exist
    """
    # Try relative to this file
    icons_dir = Path(__file__).parent.parent / "clientutils_examples" / "icons"

    if not icons_dir.exists():
        # Try relative to current working directory
        icons_dir = Path.cwd() / "clientutils_examples" / "icons"

    if not icons_dir.exists():
        raise FileNotFoundError(f"Icons directory not found. Tried: {icons_dir}")

    return icons_dir.resolve()

start()

Start the web server

Source code in clientutils/webserver.py
50
51
52
def start(self):
    """Start the web server"""
    self.app.run(host="0.0.0.0", port=self.port, debug=False)