[PythonCE] Current Directory emulation

Telion telionce@yahoo.com
Wed, 2 Oct 2002 09:48:39 -0700 (PDT)


Here is how to emulate Current Directory
so that some scripts would run without modification on CE.

If you include following code at the beginning of site.py, 
os.getcwd(), os.chdir() will be emulated.
Also, it wrap os.listdir().

Same thing can be applied to other os functions like;
mkdir, rename, chmod, etc..etc...

What do you think of this idea?
Any suggestion and/or improved code?


### os.getcwd, os.chdir emulation + os.listdir fix ###
import os
os.cwd = "\\"
def __getcwd(): return os.cwd
os.getcwd =  __getcwd
def __fullpath(p): 
	if not p or type(p) != str:
		raise OSError, "Invalid path"
	if not p[0] in "/\\":
		p = os.path.join(os.cwd,p)
	p = p.replace('/','\\')
	pl = p.split('\\')
	#print pl
	i =0
	while 1:
		if i >= len(pl):
			break
		#print i, pl[i]
		if pl[i] =='..':
			try:
				pl.pop(i)
				pl.pop(i-1)
			except:
				raise OSError, "Invalid path"
			i -= 1
			if i < 0:
				raise OSError, "Invalid path"
		elif not pl[i]:
			pl.pop(i)
		else:
			i += 1
	return "\\"+"\\".join(pl)

def __chdir(p):
	fullpath = __fullpath(p)
	if os.path.isdir(fullpath):
		os.cwd = fullpath
	else:
		raise OSError, "Invalid path"

os.chdir = __chdir

# os.listdir may fails when "\\storage card\\../" is the given path
# It does not fail when "\\wndows\\..\\" or "/Program files/../"
# Probably something related with CF or memory card.
# So, give the aboslute path, instead.
os.oldlistdir = os.listdir

def __listdirfix(p): 
	if p != '':
		p = __pathfix(p)
	#print p
	if p == "\\":
		p = ''
	return os.oldlistdir(p)

os.listdir = __listdirfix


#### END OF CE FIX ###


=====
Telion
- telionce@yahoo.com -
http://pages.ccapcable.com

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com