[Pythonmac-SIG] How do you use macfs.FInfo?

Richard Gordon maccgi@bellsouth.net
Mon, 11 Oct 1999 10:23:36 -0400


--============_-1272470664==_ma============
Content-Type: text/plain; charset="us-ascii" ; format="flowed"

At 11:54 +0200 10/11/1999, Jack Jansen wrote:
>That works, but that doesn't use a FInfo object. It is the old method of
>setting creator/type, and while I would love to depracate it in favor of using
>FInfo it is used too often by too many people (including me:-)

Well, I got past this stuff by butchering FixCreator.py from the 
distribution, but now I've got a slight mystery that I think has to 
do with recursive functions. When a search folder is selected, any 
subfolders within it are processed as well and I suppose that this 
would go on as deeply as they may be nested. That's fine, but I'm 
having trouble keeping a cumulative total of the files that have been 
analyzed to display in a dialog at the end of everything, e.g.:

# import various things

# set some variables for file types- we're converting gifs, etc, to 
PictureViewer ("ogle")

def walktree(name, change):
	if os.path.isfile(name):
		fs = macfs.FSSpec(name)
		cur_cr, cur_tp = fs.GetCreatorType()
		if (cur_cr != NEW) and (cur_tp == GIF or JPEG or TIFF):
			fs.SetCreatorType(NEW, cur_tp)
			macostools.touched(fs)
	elif os.path.isdir(name):
		files = os.listdir(name)
		global lcounter
		lcounter = 0
		for f in files:
			walktree(os.path.join(name, f), change)
			lcounter = lcounter + 1

def run(change):
	fss, ok = macfs.GetDirectory('Folder to search:')
	if not ok:
		sys.exit(0)
	start = time.time()
	walktree(fss.as_pathname(), change)
	stop = time.time()
	runtime = stop - start
	if runtime <= 1:
		theString = 'Finished analyzing %d files in 1 second. 
Pretty cool.' % runtime
	else:
		theString = 'Finished analyzing %d files in %d 
seconds. Pretty cool.' % (lcounter, runtime) # forgive the wrapping 
in Eudora
	EasyDialogs.Message(theString)


if __name__ == '__main__':
	run(1)

# EOF

Because walktree keeps calling itself every time a new folder is 
encountered, the lcounter variable keeps being reset to 0, so the 
final dialog just says how many files happen to have been in the last 
folder processed. I've tried to park the current count in another 
variable, but if it's at the top level, it isn't seen for some reason 
and if it's set up as a global within walktree and initialized to 0, 
it keeps going back to 0 even tho I try to accumulate in it with 
something like totcounter = totcounter + lcounter.

So, my question is where do you hide a cumulative counter when a 
function is recursive? Or do I need to take this whole thing apart 
and split walktree into 2 functions so it's no longer recursive in 
nature? Thanks.


Richard Gordon
--------------------
Gordon Consulting & Design
Database Design/Scripting Languages
mailto:richard@richardgordon.net
http://www.richardgordon.net
770.971.6887 (voice)
770.216.1829 (fax)
--============_-1272470664==_ma============
Content-Type: text/enriched; charset="us-ascii"

At 11:54 +0200 10/11/1999, Jack Jansen wrote:

<excerpt>That works, but that doesn't use a FInfo object. It is the old
method of 

setting creator/type, and while I would love to depracate it in favor
of using 

FInfo it is used too often by too many people (including me:-)

</excerpt>

Well, I got past this stuff by butchering FixCreator.py from the
distribution, but now I've got a slight mystery that I think has to do
with recursive functions. When a search folder is selected, any
subfolders within it are processed as well and I suppose that this
would go on as deeply as they may be nested. That's fine, but I'm
having trouble keeping a cumulative total of the files that have been
analyzed to display in a dialog at the end of everything, e.g.:


<bold><fixed><smaller># import various things


</smaller></fixed></bold><fixed><smaller># set some variables for file
types- we're converting gifs, etc, to PictureViewer ("ogle")


<bold>def
</bold><color><param>BFFF,0000,0000</param>walktree</color>(name,
change):

	<bold>if</bold> os.path.isfile(name):

		fs = macfs.FSSpec(name)

		cur_cr, cur_tp = fs.GetCreatorType()

		<bold>if</bold> (cur_cr != NEW) <bold>and</bold> (cur_tp == GIF
<bold>or </bold>JPEG <bold>or</bold> TIFF):

			fs.SetCreatorType(NEW, cur_tp)

			macostools.touched(fs)

	<bold>elif</bold> os.path.isdir(name):

		files = os.listdir(name)

		<bold>global </bold>lcounter

		lcounter = 0

		for f <bold>in</bold> files:

			walktree(os.path.join(name, f), change)

			lcounter = lcounter + 1


<bold>def</bold><color><param>BFFF,0000,0000</param>
run</color>(change):

	fss, ok =
macfs.GetDirectory(<color><param>0000,7FFF,0000</param>'Folder to
search:'</color>)

	<bold>if not</bold> ok:

		sys.exit(0)

	start = time.time()

	walktree(fss.as_pathname(), change)

	stop = time.time()

	runtime = stop - start

	<bold>if</bold> runtime <<= 1:

		theString = <color><param>0000,7FFF,0000</param>'Finished analyzing
%d files in 1 second. Pretty cool.'</color> % runtime

	<bold>else</bold>:

		theString = <color><param>0000,7FFF,0000</param>'Finished analyzing
%d files in %d seconds. Pretty cool.'</color> % (lcounter, runtime) #
forgive the wrapping in Eudora

	EasyDialogs.Message(theString)


	

<bold>if</bold> __name__ ==
<color><param>0000,7FFF,0000</param>'__main__'</color>:

	run(1)


# EOF


</smaller></fixed>Because walktree keeps calling itself every time a
new folder is encountered, the lcounter variable keeps being reset to
0, so the final dialog just says how many files happen to have been in
the last folder processed. I've tried to park the current count in
another variable, but if it's at the top level, it isn't seen for some
reason and if it's set up as a global within walktree and initialized
to 0, it keeps going back to 0 even tho I try to accumulate in it with
something like totcounter = totcounter + lcounter.


So, my question is where do you hide a cumulative counter when a
function is recursive? Or do I need to take this whole thing apart and
split walktree into 2 functions so it's no longer recursive in nature?
Thanks.



Richard Gordon

--------------------

Gordon Consulting & Design

Database Design/Scripting Languages

mailto:richard@richardgordon.net

http://www.richardgordon.net

770.971.6887 (voice)

770.216.1829 (fax)

--============_-1272470664==_ma============--