python-adfs

changeset 86:71640463533b tip

Committed pending refactoring work. Added a has_key method to the base ADFSmap class.
author David Boddie <david@boddie.org.uk>
date Sat Apr 13 18:40:45 2013 +0200
parents bab9a6eaf74c
children
files ADFSlib.py
diffstat 1 files changed, 122 insertions(+), 113 deletions(-) [+]
line diff
     1.1 --- a/ADFSlib.py	Fri Oct 14 00:01:08 2011 +0200
     1.2 +++ b/ADFSlib.py	Sat Apr 13 18:40:45 2013 +0200
     1.3 @@ -114,6 +114,120 @@
     1.4                  new = new + i
     1.5          
     1.6          return new
     1.7 +    
     1.8 +    def _plural(self, msg, values, words):
     1.9 +    
    1.10 +        """Returns a message which takes into account the plural form of
    1.11 +        words in the original message, assuming that the appropriate
    1.12 +        form for negative numbers of items is the same as that for
    1.13 +        more than one item.
    1.14 +        
    1.15 +        values is a list of numeric values referenced in the message.
    1.16 +        words is a list of sequences of words to substitute into the
    1.17 +        message. This takes the form
    1.18 +        
    1.19 +            [ word_to_use_for_zero_items,
    1.20 +              word_to_use_for_one_item,
    1.21 +              word_to_use_for_two_or_more_items ]
    1.22 +        
    1.23 +        The length of the values and words lists must be equal.
    1.24 +        """
    1.25 +        
    1.26 +        substitutions = []
    1.27 +        
    1.28 +        for i in range(0, len(values)):
    1.29 +        
    1.30 +            n = values[i]
    1.31 +            
    1.32 +            # Each number must be mapped to a value in the range [0, 2].
    1.33 +            if n > 1: n = 2
    1.34 +            elif n < 0: n = 2
    1.35 +            
    1.36 +            substitutions.append(values[i])
    1.37 +            substitutions.append(words[i][n])
    1.38 +        
    1.39 +        return msg % tuple(substitutions)
    1.40 +    
    1.41 +    def _create_directory(self, path, name = None):
    1.42 +    
    1.43 +        elements = []
    1.44 +        
    1.45 +        while not os.path.exists(path) and path != "":
    1.46 +        
    1.47 +            path, file = os.path.split(path)
    1.48 +            
    1.49 +            elements.insert(0, file)
    1.50 +        
    1.51 +        if path != "":
    1.52 +        
    1.53 +            elements.insert(0, path)
    1.54 +        
    1.55 +        if name is not None:
    1.56 +        
    1.57 +            elements.append(name)
    1.58 +        
    1.59 +        # Remove any empty list elements or those containing a $ character.
    1.60 +        elements = filter(lambda x: x != '' and x != "$", elements)
    1.61 +        
    1.62 +        try:
    1.63 +        
    1.64 +            built = ""
    1.65 +            
    1.66 +            for element in elements:
    1.67 +            
    1.68 +                built = os.path.join(built, element)
    1.69 +                
    1.70 +                if not os.path.exists(built):
    1.71 +                
    1.72 +                    # This element of the directory does not exist.
    1.73 +                    # Create a directory here.
    1.74 +                    os.mkdir(built)
    1.75 +                    print 'Created directory:', built
    1.76 +                
    1.77 +                elif not os.path.isdir(built):
    1.78 +                
    1.79 +                    # This element of the directory already exists
    1.80 +                    # but is not a directory.
    1.81 +                    print 'A file exists which prevents a ' + \
    1.82 +                        'directory from being created: %s' % built
    1.83 +                    
    1.84 +                    return ""
    1.85 +        
    1.86 +        except OSError:
    1.87 +        
    1.88 +            print 'Directory could not be created: %s' % \
    1.89 +                string.join(elements, os.sep)
    1.90 +            
    1.91 +            return ""
    1.92 +        
    1.93 +        # Success
    1.94 +        return built
    1.95 +    
    1.96 +    def _convert_name(self, old_name, convert_dict):
    1.97 +    
    1.98 +        # Use the conversion dictionary to convert any forbidden
    1.99 +        # characters to accepted local substitutes.
   1.100 +        name = ""
   1.101 +        
   1.102 +        for c in old_name:
   1.103 +        
   1.104 +            if c in convert_dict.keys():
   1.105 +            
   1.106 +                name = name + convert_dict[c]
   1.107 +            
   1.108 +            else:
   1.109 +            
   1.110 +                name = name + c
   1.111 +        
   1.112 +        if self.verify and old_name != name:
   1.113 +        
   1.114 +            self.verify_log.append(
   1.115 +                ( WARNING,
   1.116 +                  "Changed %s to %s" % (old_name, name) )
   1.117 +                )
   1.118 +        
   1.119 +        return name
   1.120 +
   1.121  
   1.122  class ADFS_exception(Exception):
   1.123  
   1.124 @@ -198,6 +312,10 @@
   1.125      
   1.126          return self.disc_map[index]
   1.127      
   1.128 +    def has_key(self, key):
   1.129 +    
   1.130 +        return self.disc_map.has_key(key)
   1.131 +    
   1.132      def _read_freespace(self):
   1.133      
   1.134          # Currently unused
   1.135 @@ -236,6 +354,7 @@
   1.136      
   1.137          checksum1 = self._read_unsigned_byte(self.sectors[base+self.sector_size-1])
   1.138  
   1.139 +
   1.140  class ADFSnewMap(ADFSmap):
   1.141  
   1.142      dir_markers = ('Hugo', 'Nick')
   1.143 @@ -689,6 +808,7 @@
   1.144      
   1.145          return ((addr - begin) * self.sector_size)
   1.146  
   1.147 +
   1.148  class ADFSbigNewMap(ADFSnewMap):
   1.149  
   1.150      dir_markers = ('Nick',)
   1.151 @@ -712,10 +832,12 @@
   1.152          
   1.153          return ((addr - begin) - (upper * 0xc8)) * 0x200
   1.154  
   1.155 +
   1.156  class ADFSoldMap(ADFSmap):
   1.157  
   1.158      pass
   1.159  
   1.160 +
   1.161  class ADFSdisc(Utilities):
   1.162  
   1.163      """disc = ADFSdisc(file_handle, verify = 0)
   1.164 @@ -1396,31 +1518,6 @@
   1.165              
   1.166                  self.print_catalogue(obj.files, path + "." + name, filetypes)
   1.167      
   1.168 -    def _convert_name(self, old_name, convert_dict):
   1.169 -    
   1.170 -        # Use the conversion dictionary to convert any forbidden
   1.171 -        # characters to accepted local substitutes.
   1.172 -        name = ""
   1.173 -        
   1.174 -        for c in old_name:
   1.175 -        
   1.176 -            if c in convert_dict.keys():
   1.177 -            
   1.178 -                name = name + convert_dict[c]
   1.179 -            
   1.180 -            else:
   1.181 -            
   1.182 -                name = name + c
   1.183 -        
   1.184 -        if self.verify and old_name != name:
   1.185 -        
   1.186 -            self.verify_log.append(
   1.187 -                ( WARNING,
   1.188 -                  "Changed %s to %s" % (old_name, name) )
   1.189 -                )
   1.190 -        
   1.191 -        return name
   1.192 -    
   1.193      def _extract_old_files(self, objects, path, filetypes = 0, separator = ",",
   1.194                             convert_dict = {}, with_time_stamps = False):
   1.195      
   1.196 @@ -1611,94 +1708,6 @@
   1.197                  with_time_stamps
   1.198                  )
   1.199      
   1.200 -    def _create_directory(self, path, name = None):
   1.201 -    
   1.202 -        elements = []
   1.203 -        
   1.204 -        while not os.path.exists(path) and path != "":
   1.205 -        
   1.206 -            path, file = os.path.split(path)
   1.207 -            
   1.208 -            elements.insert(0, file)
   1.209 -        
   1.210 -        if path != "":
   1.211 -        
   1.212 -            elements.insert(0, path)
   1.213 -        
   1.214 -        if name is not None:
   1.215 -        
   1.216 -            elements.append(name)
   1.217 -        
   1.218 -        # Remove any empty list elements or those containing a $ character.
   1.219 -        elements = filter(lambda x: x != '' and x != "$", elements)
   1.220 -        
   1.221 -        try:
   1.222 -        
   1.223 -            built = ""
   1.224 -            
   1.225 -            for element in elements:
   1.226 -            
   1.227 -                built = os.path.join(built, element)
   1.228 -                
   1.229 -                if not os.path.exists(built):
   1.230 -                
   1.231 -                    # This element of the directory does not exist.
   1.232 -                    # Create a directory here.
   1.233 -                    os.mkdir(built)
   1.234 -                    print 'Created directory:', built
   1.235 -                
   1.236 -                elif not os.path.isdir(built):
   1.237 -                
   1.238 -                    # This element of the directory already exists
   1.239 -                    # but is not a directory.
   1.240 -                    print 'A file exists which prevents a ' + \
   1.241 -                        'directory from being created: %s' % built
   1.242 -                    
   1.243 -                    return ""
   1.244 -        
   1.245 -        except OSError:
   1.246 -        
   1.247 -            print 'Directory could not be created: %s' % \
   1.248 -                string.join(elements, os.sep)
   1.249 -            
   1.250 -            return ""
   1.251 -        
   1.252 -        # Success
   1.253 -        return built
   1.254 -    
   1.255 -    def _plural(self, msg, values, words):
   1.256 -    
   1.257 -        """Returns a message which takes into account the plural form of
   1.258 -        words in the original message, assuming that the appropriate
   1.259 -        form for negative numbers of items is the same as that for
   1.260 -        more than one item.
   1.261 -        
   1.262 -        values is a list of numeric values referenced in the message.
   1.263 -        words is a list of sequences of words to substitute into the
   1.264 -        message. This takes the form
   1.265 -        
   1.266 -            [ word_to_use_for_zero_items,
   1.267 -              word_to_use_for_one_item,
   1.268 -              word_to_use_for_two_or_more_items ]
   1.269 -        
   1.270 -        The length of the values and words lists must be equal.
   1.271 -        """
   1.272 -        
   1.273 -        substitutions = []
   1.274 -        
   1.275 -        for i in range(0, len(values)):
   1.276 -        
   1.277 -            n = values[i]
   1.278 -            
   1.279 -            # Each number must be mapped to a value in the range [0, 2].
   1.280 -            if n > 1: n = 2
   1.281 -            elif n < 0: n = 2
   1.282 -            
   1.283 -            substitutions.append(values[i])
   1.284 -            substitutions.append(words[i][n])
   1.285 -        
   1.286 -        return msg % tuple(substitutions)
   1.287 -    
   1.288      def print_log(self, verbose = 0):
   1.289      
   1.290          """Prints the disc verification log. Any purely informational messages