1
0
mirror of synced 2024-12-18 08:15:53 +01:00
artemis/read.py

148 lines
3.9 KiB
Python
Raw Normal View History

2024-01-09 09:07:04 +01:00
#!/usr/bin/env python3
import argparse
import re
import os
import yaml
from os import path
import logging
import coloredlogs
2024-01-09 20:42:17 +01:00
import asyncio
from logging.handlers import TimedRotatingFileHandler
from typing import List, Optional
from core import CoreConfig, Utils
2023-03-09 17:38:58 +01:00
class BaseReader:
def __init__(
self,
config: CoreConfig,
version: int,
bin_dir: Optional[str],
opt_dir: Optional[str],
extra: Optional[str],
) -> None:
self.logger = logging.getLogger("reader")
self.config = config
self.bin_dir = bin_dir
self.opt_dir = opt_dir
self.version = version
self.extra = extra
2023-03-09 17:38:58 +01:00
def get_data_directories(self, directory: str) -> List[str]:
ret: List[str] = []
for root, dirs, files in os.walk(directory):
2023-03-09 17:38:58 +01:00
for dir in dirs:
if re.fullmatch("[A-Z0-9]{4,4}", dir) is not None:
ret.append(f"{root}/{dir}")
return ret
2024-01-09 20:42:17 +01:00
async def read(self) -> None:
pass
2023-03-09 17:38:58 +01:00
if __name__ == "__main__":
2023-03-09 17:38:58 +01:00
parser = argparse.ArgumentParser(description="Import Game Information")
parser.add_argument(
2023-08-30 17:19:13 +02:00
"--game",
2023-03-09 17:38:58 +01:00
action="store",
type=str,
required=True,
2023-08-30 17:19:13 +02:00
help="The game we are importing.",
)
parser.add_argument(
2023-03-09 17:38:58 +01:00
"--version",
dest="version",
action="store",
type=int,
required=True,
2023-03-09 17:38:58 +01:00
help="The game version we are importing.",
)
parser.add_argument(
2023-03-09 17:38:58 +01:00
"--binfolder",
dest="bin",
action="store",
type=str,
2023-03-09 17:38:58 +01:00
help="Folder containing A000 base data",
)
parser.add_argument(
2023-03-09 17:38:58 +01:00
"--optfolder",
dest="opt",
action="store",
type=str,
2023-03-09 17:38:58 +01:00
help="Folder containing Option data folders",
)
parser.add_argument(
"--config",
type=str,
default="config",
help="Folder containing the core configuration for importing to DB. Defaults to 'config'.",
)
parser.add_argument(
"--extra",
type=str,
help="Any extra data that a reader might require.",
)
# Parse args, validate invariants.
args = parser.parse_args()
config = CoreConfig()
if path.exists(f"{args.config}/core.yaml"):
config.update(yaml.safe_load(open(f"{args.config}/core.yaml")))
log_fmt_str = "[%(asctime)s] Reader | %(levelname)s | %(message)s"
log_fmt = logging.Formatter(log_fmt_str)
logger = logging.getLogger("reader")
2023-03-09 17:38:58 +01:00
fileHandler = TimedRotatingFileHandler(
"{0}/{1}.log".format(config.server.log_dir, "reader"), when="d", backupCount=10
)
fileHandler.setFormatter(log_fmt)
2023-03-09 17:38:58 +01:00
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(log_fmt)
logger.addHandler(fileHandler)
logger.addHandler(consoleHandler)
2023-03-09 17:38:58 +01:00
log_lv = logging.DEBUG if config.server.is_develop else logging.INFO
logger.setLevel(log_lv)
coloredlogs.install(level=log_lv, logger=logger, fmt=log_fmt_str)
2023-08-30 17:19:13 +02:00
if args.game is None or args.version is None:
logger.error("Game or version not specified")
parser.print_help()
2023-03-09 17:38:58 +01:00
exit(1)
if args.bin is None and args.opt is None:
logger.error("Must specify either bin or opt directory")
parser.print_help()
exit(1)
if args.bin is not None and (args.bin.endswith("\\") or args.bin.endswith("/")):
bin_arg = args.bin[:-1]
else:
bin_arg = args.bin
2023-03-09 17:38:58 +01:00
if args.opt is not None and (args.opt.endswith("\\") or args.opt.endswith("/")):
opt_arg = args.opt[:-1]
else:
opt_arg = args.opt
logger.info("Starting importer...")
titles = Utils.get_all_titles()
for dir, mod in titles.items():
2023-08-30 17:19:13 +02:00
if args.game in mod.game_codes:
handler = mod.reader(config, args.version, bin_arg, opt_arg, args.extra)
2024-01-09 20:42:17 +01:00
loop = asyncio.get_event_loop()
loop.run_until_complete(handler.read())
2023-03-09 17:38:58 +01:00
logger.info("Done")