Skip to content

Commit

Permalink
add argparser:
Browse files Browse the repository at this point in the history
-  improve schema file (re-export it)
- parse arguments to choose subcommand and other arguments
  • Loading branch information
Totto16 committed Aug 17, 2023
1 parent 4a064d5 commit 9e1316a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 27 deletions.
15 changes: 9 additions & 6 deletions schema/content_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@
"description": "The parent folders of this scanned file / folder"
},
"type": {
"type": "string",
"enum": [
"file",
"folder"
],
"$ref": "#/$defs/ScannedFileType",
"title": "file type",
"description": "The type of the file: folder or file"
},
Expand All @@ -84,6 +80,13 @@
],
"additionalProperties": false
},
"ScannedFileType": {
"type": "string",
"enum": [
"file",
"folder"
]
},
"Stats": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -257,4 +260,4 @@
}
},
"$schema": "http://json-schema.org/draft/2020-12/schema#"
}
}
93 changes: 75 additions & 18 deletions src/entry.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#!/usr/bin/env python3


import argparse
import atexit
import re as regex
import sys
from logging import DEBUG, Logger
from logging import Logger
from pathlib import Path
from typing import Optional, Self
from typing import Literal, Optional, Self, cast

from classifier import Classifier, Language
from content.base_class import Content # noqa: TCH002
from content.general import NameParser, Summary
from content.scanner import PartialLanguageScanner
from helper.log import get_logger, setup_custom_logger
from helper.log import LogLevel, get_logger, setup_custom_logger
from helper.timestamp import parse_int_safely
from main import AllContent, generate_json_schema, parse_contents
from typing_extensions import override
Expand Down Expand Up @@ -117,26 +118,82 @@ def main() -> None:
get_logger().info(final)


SubCommand = Literal["run", "schema"]


class ParsedArgNamespace:
level: LogLevel
subcommand: SubCommand


class RunCommandParsedArgNamespace(ParsedArgNamespace):
subcommand: Literal["run"]


class SchemaCommandParsedArgNamespace(ParsedArgNamespace):
subcommand: Literal["schema"]
schema_file: str


AllParsedNameSpaces = RunCommandParsedArgNamespace | SchemaCommandParsedArgNamespace

if __name__ == "__main__":
# TODO use argparse to get loglevel and action
logger: Logger = setup_custom_logger(DEBUG)
parser = argparse.ArgumentParser(
prog="video-language-detection",
description="Detect video languages",
)

loglevel_choices: list[LogLevel] = [
LogLevel.CRITICAL,
LogLevel.ERROR,
LogLevel.WARNING,
LogLevel.INFO,
LogLevel.DEBUG,
LogLevel.NOTSET,
]
loglevel_default: LogLevel = LogLevel.DEBUG
parser.add_argument(
"-l",
"--level",
choices=loglevel_choices,
default=loglevel_default,
dest="level",
)

subparsers = parser.add_subparsers(required=False)
parser.set_defaults(subcommand="run")

run_parser = subparsers.add_parser("run")
run_parser.set_defaults(subcommand="run")

schema_parser = subparsers.add_parser("schema")
schema_parser.set_defaults(subcommand="schema")
schema_parser.add_argument(
"-s",
"--schema",
dest="schema_file",
default="schema/content_list.json",
)

args = cast(AllParsedNameSpaces, parser.parse_args())
logger: Logger = setup_custom_logger(args.level)
try:
if len(sys.argv) > 1:
match sys.argv[1]:
case "schema":
generate_json_schema(
Path("schema/content_list.json"),
list[AllContent],
)
sys.exit(0)
case _:
# pass to say, i don't care about other cases
pass
main()
match args.subcommand:
case "schema":
args = cast(SchemaCommandParsedArgNamespace, args)
generate_json_schema(
Path(args.schema_file),
list[AllContent],
)
sys.exit(0)
case "run":
main()

except KeyboardInterrupt:

def exit_handler() -> None:
logger.info("Ctrl + C pressed")
print() # noqa: T201
print("Ctrl + C pressed") # noqa: T201

atexit.register(exit_handler)

Expand Down
37 changes: 34 additions & 3 deletions src/helper/log.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
from logging import DEBUG, Formatter, Logger, StreamHandler, getLogger
import logging
from enum import Enum
from logging import Formatter, Logger, StreamHandler, getLogger
from typing import Self

__GLOBAL__LOGGER__NAME = "__global__logger__"


class LogLevel(Enum):
CRITICAL = "CRITICAL"
ERROR = "ERROR"
WARNING = "WARNING"
INFO = "INFO"
DEBUG = "DEBUG"
NOTSET = "NOTSET"

@property
def underlying(self: Self) -> int:
match self:
case LogLevel.CRITICAL:
return logging.CRITICAL
case LogLevel.ERROR:
return logging.ERROR
case LogLevel.WARNING:
return logging.WARNING
case LogLevel.INFO:
return logging.INFO
case LogLevel.DEBUG:
return logging.DEBUG
case LogLevel.NOTSET:
return logging.NOTSET
case _:
msg = "UNREACHABLE!"
raise RuntimeError(msg)


def get_logger() -> Logger:
return getLogger(__GLOBAL__LOGGER__NAME)


def setup_custom_logger(level: int = DEBUG) -> Logger:
def setup_custom_logger(level: LogLevel = LogLevel.DEBUG) -> Logger:
formatter = Formatter(fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s")

handler = StreamHandler()
handler.setFormatter(formatter)

logger = get_logger()
logger.setLevel(level)
logger.setLevel(level.underlying)
logger.addHandler(handler)
return logger

0 comments on commit 9e1316a

Please sign in to comment.