/ a2woz / __main__.py
__main__.py
 1  #!/usr/bin/env python3
 2  
 3  # SPDX-FileCopyrightText: 2019 4am
 4  # SPDX-FileCopyrightText: 2019 Peter Ferrie
 5  #
 6  # SPDX-License-Identifier: MIT
 7  
 8  # (c) 2018-9 by 4am
 9  # MIT-licensed
10  
11  import click
12  
13  from . import eddimage, wozardry, a2rimage
14  from .loggers import DefaultLogger, DebugLogger
15  from . import RawConvert, PassportGlobals
16  from .strings import STRINGS, version
17  import argparse
18  import os.path
19  
20  __progname__ = "a2woz"
21  
22  @click.command()
23  @click.help_option()
24  @click.version_option(version=version)
25  @click.option("--debug", "-d", is_flag="True", help="print debugging information while processing")
26  @click.option("--output-dir", "output_dir", type=click.Path(file_okay=False, dir_okay=True), default=None, help="Output directory")
27  @click.option("--output", "-o", "output_file", type=click.Path(), default=None, help="Output path, defaults to the input with the extension replaced with .woz. When multiple input files are specified, --output may not be used.")
28  @click.option("--overwrite/--no-overwrite", "-f/-n", default=False, help="Controls whether to overwrite an output file. Files are not overwritten by default.")
29  @click.argument("input-files", type=click.Path(exists=True), nargs=-1)
30  def main(debug, input_files, output_file, output_dir, overwrite):
31      "Convert a disk image to .woz format with minimal processing"
32      logger = debug and DebugLogger or DefaultLogger
33      
34      if output_file is not None and output_dir is not None:
35          raise SystemExit("--output and --output-dir are mutually exclusive")
36  
37      if len(input_files) == 1:
38          if output_file is None:
39              output_file = os.path.splitext(input_files[0])[0] + ".woz"
40  
41          if not overwrite:
42              if os.path.exists(output_file):
43                  raise SystemExit(f"Use --overwrite to overwrite {output_file}.")
44      elif output_file is not None:
45          raise SystemExit(f"--output is only valid with one input file")
46  
47      if output_dir is not None:
48          os.makedirs(output_dir, exist_ok=True)
49  
50      logger = DebugLogger if debug else DefaultLogger
51      logger(PassportGlobals()).PrintByID("header")
52  
53      for input_file in input_files:
54          base, ext = os.path.splitext(input_file)
55          ext = ext.lower()
56          if ext == ".edd":
57              reader = eddimage.EDDReader
58          elif ext == ".a2r":
59              reader = a2rimage.A2RImage
60          else:
61              raise SystemExit("unrecognized file type")
62  
63          if output_dir:
64              output_file = os.path.join(output_dir, os.path.splitext(os.path.basename(input_file))[0] + ".woz")
65  
66          with open(input_file, "rb") as f:
67              RawConvert(input_file, reader(f), logger, output_file)
68  
69  if __name__ == '__main__':
70      main()