2023-02-17 07:02:21 +01:00
|
|
|
# vim: set fileencoding=utf-8
|
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import yaml
|
2023-03-05 03:58:51 +01:00
|
|
|
from os import path
|
2023-03-15 21:03:22 +01:00
|
|
|
import logging
|
|
|
|
import coloredlogs
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
from typing import List, Optional
|
|
|
|
|
2023-03-12 07:00:51 +01:00
|
|
|
from core import CoreConfig, Utils
|
2023-02-17 07:02:21 +01:00
|
|
|
|
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:
|
2023-02-17 07:02:21 +01:00
|
|
|
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
|
|
|
|
2023-02-17 07:02:21 +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}")
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
return ret
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
if __name__ == "__main__":
|
2023-03-09 17:38:58 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Import Game Information")
|
2023-02-17 07:02:21 +01:00
|
|
|
parser.add_argument(
|
2023-03-09 17:38:58 +01:00
|
|
|
"--series",
|
|
|
|
action="store",
|
2023-02-17 07:02:21 +01:00
|
|
|
type=str,
|
|
|
|
required=True,
|
2023-03-09 17:38:58 +01:00
|
|
|
help="The game series we are importing.",
|
2023-02-17 07:02:21 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-03-09 17:38:58 +01:00
|
|
|
"--version",
|
|
|
|
dest="version",
|
|
|
|
action="store",
|
2023-02-17 07:02:21 +01:00
|
|
|
type=int,
|
|
|
|
required=True,
|
2023-03-09 17:38:58 +01:00
|
|
|
help="The game version we are importing.",
|
2023-02-17 07:02:21 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-03-09 17:38:58 +01:00
|
|
|
"--binfolder",
|
|
|
|
dest="bin",
|
|
|
|
action="store",
|
2023-02-17 07:02:21 +01:00
|
|
|
type=str,
|
2023-03-09 17:38:58 +01:00
|
|
|
help="Folder containing A000 base data",
|
2023-02-17 07:02:21 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-03-09 17:38:58 +01:00
|
|
|
"--optfolder",
|
|
|
|
dest="opt",
|
|
|
|
action="store",
|
2023-02-17 07:02:21 +01:00
|
|
|
type=str,
|
2023-03-09 17:38:58 +01:00
|
|
|
help="Folder containing Option data folders",
|
2023-02-17 07:02:21 +01:00
|
|
|
)
|
|
|
|
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()
|
2023-03-05 03:58:51 +01:00
|
|
|
if path.exists(f"{args.config}/core.yaml"):
|
|
|
|
config.update(yaml.safe_load(open(f"{args.config}/core.yaml")))
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
|
|
|
|
|
|
|
logger.addHandler(fileHandler)
|
|
|
|
logger.addHandler(consoleHandler)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-24 20:10:41 +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-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
if args.series 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)
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
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
|
|
|
|
2023-02-17 07:02:21 +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():
|
|
|
|
if args.series in mod.game_codes:
|
2023-03-15 21:03:22 +01:00
|
|
|
handler = mod.reader(config, args.version,
|
|
|
|
bin_arg, opt_arg, args.extra)
|
2023-02-17 07:02:21 +01:00
|
|
|
handler.read()
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
logger.info("Done")
|