gcmd-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gcmd-dev] Python plugins


From: Piotr Eljasiak
Subject: Re: [gcmd-dev] Python plugins
Date: Tue, 05 Dec 2006 01:30:52 +0100

Yet another python plugin example:

        #! /usr/bin/env python
        
        try:
            import gnomevfs
        except ImportError:
            import gnome.vfs as gnomevfs
        
        import md5
        
        
        def main(main_wnd_xid, active_cwd, inactive_cwd,
        selected_files):
            for uri in selected_files:
                fname = gnomevfs.get_file_info(uri).name
                f = file.open(fname,'rb')
                file_content = f.read()
                    f.close()
                md5sum = md5.new(file_content).hexdigest()
                print md5sum, fname
            return True
        
        
The above snippet code generates md5 sums for selected files and outputs
them to stdout. It should be self explanatory


The 'production' version should resemble the one listed below:

        #! /usr/bin/env python
        
        try:
            import gnomevfs
        except ImportError:
            import gnome.vfs as gnomevfs
        
        import os
        import string
        import md5
        
        
        def main(main_wnd_xid, active_cwd, inactive_cwd,
        selected_files):
            parent_dir = string.split(active_cwd, os.sep)[-1]
            if parent_dir=='':
                parent_dir = 'root'
            f_md5sum = file(parent_dir+'.md5sum','w')
            for uri in selected_files:
                if
        gnomevfs.get_file_info(uri).type==gnomevfs.FILE_TYPE_REGULAR:
                    fname = gnomevfs.get_file_info(uri).name
                    f = file(fname,'rb')
                    file_content = f.read()
                    f.close()
                    md5sum = md5.new(file_content).hexdigest()
                    f_md5sum.write('%s  %s\n' % (md5sum, fname))
            f_md5sum.close()
            return True
        
Now we write to file (named after parent dir) using printf-like
formatting. Additionally uri is checked against file type - we generate
md5 for regular files only.

        os.sep - string used to separate file pathname components
        (here: /)
        string.split - splits string into the list of pathname
        components
        [-1] - stands for the last list element
        
Enjoy,
Piotr





reply via email to

[Prev in Thread] Current Thread [Next in Thread]