python-adfs

changeset 53:f8ff54851fbc

Added support for character translation in filenames and tidied up the getopt support to allow newly introduced options, such as the "-h" help option and the "-c" character translation list.
author David Boddie <david@boddie.org.uk>
date Sun Jul 20 00:25:32 2003 +0200
parents a9449aa046e1
children 3b6e121ae42f
files ADF2INF.py
diffstat 1 files changed, 87 insertions(+), 27 deletions(-) [+]
line diff
     1.1 --- a/ADF2INF.py	Sun Jul 20 00:24:28 2003 +0200
     1.2 +++ b/ADF2INF.py	Sun Jul 20 00:25:32 2003 +0200
     1.3 @@ -3,7 +3,7 @@
     1.4  Name        : ADF2INF.py
     1.5  Author      : David Boddie
     1.6  Created     : Wed 18th October 2000
     1.7 -Updated     : Tue 15th April 2003
     1.8 +Updated     : Sat 19th July 2003
     1.9  Purpose     : Convert ADFS disc images (ADF) to INF files
    1.10  WWW         : http://david.boddie.org.uk/Projects/Python/ADFSlib
    1.11  
    1.12 @@ -31,7 +31,7 @@
    1.13  """
    1.14  
    1.15  
    1.16 -import os, sys
    1.17 +import os, string, sys
    1.18  import ADFSlib
    1.19  
    1.20  try:
    1.21 @@ -45,7 +45,9 @@
    1.22      use_getopt = 1
    1.23  
    1.24  
    1.25 -__version__ = "0.33c (Wed 2nd July 2003)"
    1.26 +__version__ = "0.34c (Sat 19th July 2003)"
    1.27 +
    1.28 +default_convert_dict = {"/": "."}
    1.29  
    1.30  
    1.31  def read_cmdsyntax_input(argv, syntax):
    1.32 @@ -74,12 +76,12 @@
    1.33  
    1.34  def read_getopt_input(argv):
    1.35  
    1.36 -    opts, args = getopt.getopt(argv[1:], "ldts:v")
    1.37 -
    1.38 +    opts, args = getopt.getopt(argv[1:], "ldts:c:vh")
    1.39 +    
    1.40      match = {}
    1.41      
    1.42 -    opt_dict = {"-l": "list", "-d": "directory", "-t": "file-types", "-s": "separator",
    1.43 -                "-v": "verify"}
    1.44 +    opt_dict = {"-l": "list", "-d": "create-directory", "-t": "file-types", "-s": "separator",
    1.45 +                "-v": "verify", "-c": "convert", "-h": "help"}
    1.46      arg_list = ["ADF file", "destination path"]
    1.47      
    1.48      # Read the options specified.
    1.49 @@ -94,14 +96,25 @@
    1.50          
    1.51              return None
    1.52      
    1.53 -    # Read the remaining arguments: there should be two of these.
    1.54 +    # Read the remaining arguments.
    1.55      
    1.56 -    if match.has_key("list") and len(args) != 1:
    1.57 +    if match.has_key("help"):
    1.58      
    1.59          return None
    1.60      
    1.61 -    elif not match.has_key("list") and len(args) != 2:
    1.62 +    elif match.has_key("list") and len(args) != 1:
    1.63      
    1.64 +        # For list operations, there should be one remaining argument.
    1.65 +        return None
    1.66 +    
    1.67 +    elif match.has_key("verify") and len(args) != 1:
    1.68 +    
    1.69 +        # For verify operations, there should be one remaining argument.
    1.70 +        return None
    1.71 +    
    1.72 +    elif len(args) != 2:
    1.73 +    
    1.74 +        # For all other operations, there should be two remaining arguments.
    1.75          return None
    1.76      
    1.77      else:
    1.78 @@ -122,28 +135,28 @@
    1.79      if use_getopt == 0:
    1.80      
    1.81          syntax = """
    1.82 -        \r(
    1.83 -        \r    (-l | --list) [-t | --file-types] <ADF file>
    1.84 -        \r) |
    1.85 -        \r(
    1.86 -        \r    [-d | --create-directory]
    1.87 -        \r    [(-t | --file-types) [(-s separator) | --separator=character]]
    1.88 -        \r    <ADF file> <destination path>
    1.89 -        \r) |
    1.90 -        \r(
    1.91 -        \r    (-v | --verify) <ADF file>
    1.92 -        \r)
    1.93 +        \r( (-l | --list) [-t | --file-types] <ADF file> ) |
    1.94 +        \r
    1.95 +        \r( [-d | --create-directory]
    1.96 +        \r  [ (-t | --file-types) [(-s separator) | --separator=character] ]
    1.97 +        \r  [(-c convert) | --convert=characters]
    1.98 +        \r  <ADF file> <destination path> ) |
    1.99 +        \r
   1.100 +        \r( (-v | --verify) <ADF file> ) |
   1.101 +        \r
   1.102 +        \r(-h | --help)
   1.103          """
   1.104          
   1.105          match = read_cmdsyntax_input(sys.argv, syntax)
   1.106      
   1.107      else:
   1.108      
   1.109 -        syntax = "[-l] [-d] [-t] [-s separator] [-v]" + \
   1.110 +        syntax = "[-l] [-d] [-t] [-s separator] [-v] [-c characters] " + \
   1.111                   "<ADF file> <destination path>"
   1.112          match = read_getopt_input(sys.argv)
   1.113      
   1.114 -    if match == {} or match is None:
   1.115 +    if match == {} or match is None or \
   1.116 +        match.has_key("h") or match.has_key("help"):
   1.117      
   1.118          print "Syntax: ADF2INF.py " + syntax
   1.119          print
   1.120 @@ -168,6 +181,17 @@
   1.121          print "correctly located by this tool, whether due to a corrupted disc image"
   1.122          print "or a bug in the image decoding techniques used."
   1.123          print
   1.124 +        print "The -c flag allows the user to define a conversion dictionary for the"
   1.125 +        print "characters found in ADFS filenames. The format of the string used to"
   1.126 +        print "define this dictionary is a comma separated list of character pairs:"
   1.127 +        print
   1.128 +        print "    <src1><dest1>[,<src2><dest2>]..."
   1.129 +        print
   1.130 +        print "If no conversion dictionary is specified then a default dictionary will"
   1.131 +        print "be used. This is currently defined as"
   1.132 +        print
   1.133 +        print "    %s" % repr(default_convert_dict)
   1.134 +        print
   1.135          sys.exit()
   1.136      
   1.137      
   1.138 @@ -178,6 +202,7 @@
   1.139      filetypes = match.has_key("t") or match.has_key("file-types")
   1.140      use_separator = match.has_key("s") or match.has_key("separator")
   1.141      verify = match.has_key("v") or match.has_key("verify")
   1.142 +    convert = match.has_key("c") or match.has_key("convert")
   1.143      
   1.144      adf_file = match["ADF file"]
   1.145      
   1.146 @@ -207,11 +232,16 @@
   1.147          sys.exit()
   1.148      
   1.149      
   1.150 -    if verify == 0:
   1.151 +    if listing == 0 and verify == 0:
   1.152      
   1.153          # Create an ADFSdisc instance using this file.
   1.154          adfsdisc = ADFSlib.ADFSdisc(adf)
   1.155      
   1.156 +    elif listing != 0:
   1.157 +    
   1.158 +        # Create an ADFSdisc instance using this file.
   1.159 +        adfsdisc = ADFSlib.ADFSdisc(adf, verify = 1)
   1.160 +    
   1.161      else:
   1.162      
   1.163          # Verifying
   1.164 @@ -233,11 +263,15 @@
   1.165          # Print catalogue
   1.166          print 'Contents of', adfsdisc.disc_name,':'
   1.167          print
   1.168 -    
   1.169 +        
   1.170          adfsdisc.print_catalogue(
   1.171              adfsdisc.files, adfsdisc.root_name, filetypes, separator
   1.172              )
   1.173 -    
   1.174 +        
   1.175 +        print
   1.176 +        
   1.177 +        adfsdisc.print_log()
   1.178 +        
   1.179          # Exit
   1.180          sys.exit()
   1.181      
   1.182 @@ -250,8 +284,34 @@
   1.183          # Place the output files on this new path.
   1.184          out_path = os.path.join(out_path, adfsdisc.disc_name)
   1.185      
   1.186 +    # If a list of conversions was specified then create a dictionary to
   1.187 +    # pass to the disc object's extraction method.
   1.188 +    if match.has_key("convert"):
   1.189 +    
   1.190 +        convert_dict = {}
   1.191 +        
   1.192 +        pairs = string.split(match["convert"])
   1.193 +        
   1.194 +        try:
   1.195 +        
   1.196 +            for pair in pairs:
   1.197 +            
   1.198 +                convert_dict[pair[0]] = pair[1]
   1.199 +        
   1.200 +        except IndexError:
   1.201 +        
   1.202 +            print "Insufficient characters in character conversion list."
   1.203 +            sys.exit()
   1.204 +    
   1.205 +    else:
   1.206 +    
   1.207 +        # Use a default conversion dictionary.
   1.208 +        convert_dict = default_convert_dict
   1.209 +    
   1.210      # Extract the files
   1.211 -    adfsdisc.extract_files(out_path, adfsdisc.files, filetypes, separator)
   1.212 +    adfsdisc.extract_files(
   1.213 +        out_path, adfsdisc.files, filetypes, separator, convert_dict
   1.214 +        )
   1.215      
   1.216      # Exit
   1.217      sys.exit()