|
| 1 | +"""Houses the main program file""" |
| 2 | + |
| 3 | +from pycaptioncompiler import Subtitles |
| 4 | +from pcslogger import Logger, Level |
| 5 | +from sys import argv |
| 6 | +import argparse |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +LOG_LEVEL = Level.DEBUG |
| 10 | +LOG_PATH = Path(argv[0]).with_name("pycaptioncompiler.log") |
| 11 | +AppLogger = None |
| 12 | + |
| 13 | +def main(): |
| 14 | + parser = argparse.ArgumentParser( |
| 15 | + "Caption Compiler", |
| 16 | + description="Caption compiler program remade in python, for Source Engine games." |
| 17 | + ) |
| 18 | + |
| 19 | + parser.add_argument( |
| 20 | + "-v", "--verbose", |
| 21 | + help="Be verbose, log more information.", |
| 22 | + action="store_true", |
| 23 | + dest="verbose", |
| 24 | + ) |
| 25 | + |
| 26 | + parser.add_argument( |
| 27 | + "-o", "--output", |
| 28 | + help="Output directory. Not required, if not specified, output file will be in the same location as the source file.", |
| 29 | + dest="outpath", |
| 30 | + required=False, |
| 31 | + default=None |
| 32 | + ) |
| 33 | + |
| 34 | + parser.add_argument( |
| 35 | + "source_file", |
| 36 | + help="The source file housing given subtitles." |
| 37 | + ) |
| 38 | + |
| 39 | + result = parser.parse_args(argv[1:]) |
| 40 | + |
| 41 | + global AppLogger |
| 42 | + AppLogger = Logger.RegisterMainApplication("Caption Compiler", result.verbose, LOG_LEVEL, True) |
| 43 | + |
| 44 | + Logger.EnableFileLogging(LOG_PATH) |
| 45 | + |
| 46 | + AppLogger.Info("Caption Compiler, Welcome!") |
| 47 | + AppLogger.Info(f"Verbose: {result.verbose}") |
| 48 | + AppLogger.VInfo(f"Logging to: {LOG_PATH}") |
| 49 | + |
| 50 | + INFILE = Path(result.source_file).absolute() |
| 51 | + if not INFILE.is_file(): |
| 52 | + AppLogger.Error(f"Specified file ({INFILE}) does not exist!") |
| 53 | + raise FileNotFoundError |
| 54 | + |
| 55 | + |
| 56 | + OUTPATH = None |
| 57 | + if result.outpath: |
| 58 | + OUTPATH = Path(result.outpath).resolve() |
| 59 | + if not OUTPATH.is_dir(): |
| 60 | + AppLogger.Warning(f"Specified output directory ({OUTPATH}) does not exist. Creating...") |
| 61 | + OUTPATH.mkdir(parents=True) |
| 62 | + |
| 63 | + OUTPATH = OUTPATH / INFILE.with_suffix(".dat").name |
| 64 | + |
| 65 | + else: |
| 66 | + OUTPATH = INFILE.with_suffix(".dat") |
| 67 | + |
| 68 | + |
| 69 | + AppLogger.Info(f"INFILE: {INFILE}") |
| 70 | + AppLogger.Info(f"OUTFILE: {OUTPATH}") |
| 71 | + |
| 72 | + AppLogger.Info("Starting compile...") |
| 73 | + |
| 74 | + Subtitles_ = Subtitles.from_path(INFILE) |
| 75 | + |
| 76 | + fbytes = Subtitles_.serialize() |
| 77 | + |
| 78 | + AppLogger.Info("Finished. Saving results...") |
| 79 | + AppLogger.VInfo(f"Writing to file: {OUTPATH}") |
| 80 | + |
| 81 | + with open(OUTPATH, "wb") as f: |
| 82 | + f.write(fbytes) |
| 83 | + |
| 84 | + AppLogger.Info("Done.") |
| 85 | + |
| 86 | +main() |
0 commit comments