[Pythonmac-SIG] Fixfiletypes via IC (first draft)

Benjamin Schollnick junkster@rochester.rr.com
Mon, 15 Apr 2002 18:20:39 -0400


Folks,

    This is not the finished version, but it's a start....

    This absolutely requires v2.21, due to the IC issues in
    v2.2 (& lower?).

    First of all, I dislike the -666 issue (if the extension/filetype
    is not registered in the Internet Control panel, it dies with a
    fatal exception.... unless caught with a try/except).

    And second, It blindly tries to change every file, even if it's
    already set correctly.

    I'm going to change this to use another more similar scheme.
    Check the existing mapping vs. IC, and only change if needed.

    But this is the first attempt.

    If anyone has any suggestions to streamline this, please let me know.

    BTW-> Adding the progress bar was rather annoying, any suggestions on
        simplifying it, without using GLOBALs?  (I dislike globals, and
        even this method stinks too much like a global...)

            - Benjamin

#
# Fixfiletypes - Set mac filetypes to something sensible,
#  recursively down a directory tree.
#
# It will only touch extensions it feels pretty sure about.
# This script is useful after copying files from unix.
#
# Jack Jansen, CWI, 1995.
#
import os
import ic
import MacOS
import macfs
import sys
import macostools
import string
import EasyDialogs


class        GUI_Features :
    def        __init__(self):
        self.max_file_count        = 0
        self.current_file_count    = 0
        self.Progress_Bar        = None
        
    def        set_max_files (self, count ):
        self.max_file_count        = count
        
    def        return_max_files (self):
        return self.max_file_count
        
    def        increment_file_count (self, increment = 1 ):
        self.current_file_count = self.current_file_count + increment
        
    def        return_incremental ( self ):
        return self.current_file_count
        
    def    init_progress_bar ( self, text ):
        self.Progress_Bar = EasyDialogs.ProgressBar ( text,
self.max_file_count )
    
    def    change_pb_text  ( self, text ):
        self.Progress_Bar.label ( text )
        
    def    refresh_progress_bar (self):
        self.Progress_Bar.set ( self.current_file_count,
max=self.max_file_count)

class    tree_file_count:
    def    __init__ ( self ):
        self.file_count = 0

    def    walktree_count (self, args, dirname, names):
        self.file_count = self.file_count + len(names)

    def    directory_to_count (self, pathname ):
        os.path.walk ( pathname, self.walktree_count, None)

    def    total_files ( self ):
        return self.file_count
        
    def    reset_to_zero ( self ):
        self.file_count = 0
        
        
    

def walktree(name, change, g_app, total, IC):

    #
    #    Walk file by file through the listings of each sub directory
    #
    if os.path.isfile(name) and not os.path.isdir (name):
        try:
            #
            #    Skip .DS_Store files
            #
            if name.endswith (".DS_Store"):
                return
    
            #
            #    Automatic IC mapper....  (Last touched isn't really needed,
            #    settypecreator automatically does it)
            #
            IC.settypecreator ( name )
            fs = macfs.FSSpec(name)
            macostools.touched (fs)
            
        except MacOS.Error:
            #
            #    This file type is not defined in the Internet Control
Panel, skip.
            #
            print "%s is not registered in Internet Control Panel" % name
                   
    elif os.path.isdir(name):
    
#        print '->', name
        files = os.listdir(name)
    
        for f in files:
            g_app.increment_file_count ()
            g_app.change_pb_text  ( "\t%s of %s" % (
g_app.return_incremental (), total.total_files () ) )
            g_app.refresh_progress_bar ( )

            walktree(os.path.join(name, f), change, g_app, total, IC )
        
        
                   
def run( change ):
    
    fss, ok = macfs.GetDirectory('Folder to search:')
    if not ok:
        sys.exit(0)

    
    GUI             = GUI_Features()
    total_count     = tree_file_count ()
    total_count.directory_to_count ( fss.as_pathname() )
    Internet_config = ic.IC ()
    
    GUI.init_progress_bar ( "Files to Process" )
    GUI.set_max_files ( total_count.total_files() )
    
    walktree(fss.as_pathname(), change, GUI, total_count, Internet_config)
    
if __name__ == '__main__':
    run(1)