Re: OperatingSystem.py
Tim Hemel <[email protected]>
| Newsgroups | gmane.comp.tools.aap.devel |
|---|---|
| Message-ID | <[email protected]> |
On Sun, Aug 31, 2003 at 11:18:18PM +0200, Bram Moolenaar wrote:
>
> A proposal for refactoring is welcome. Aap has gotten to the point that
> most functionality is present and we can overview what's there, so that
> some cleaning up can be done,
Well, here is one. It is a first attempt at getting the os.name code smell
out. It is far from perfect, but it could clean things up a little bit.
By the way, be sure to check the Python cookbook, it has got a lot of scripts,
including the win32 peculiarities.
Prepare for more...
---OperatingSystem.py----------------------------------------------
import os
# suggestions:
#
# for shorten_name, use relative path script:
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/208993
#
# for dir_contents, use os.listdir instead of glob
#
# Util.py, line 1075: /usr/local/share/aap should be changed to whatever is
# used at install time.
class OperatingSystem:
def quoteShell(self,str):
"""Util.py line 422"""
pass
def system(self,cmd):
"""Util.py line 835"""
# default implementation
# system() on MS-Windows can handle only one command at a time and
# must not end in a NL. Do the same on other non-Unix systems for
# now.
# TODO: system() isn't available on the Mac
# TODO: system() always returns zero for Windows 9x
res = 0
for line in string.split(newcmd, '\n'):
if line: # skip empty lines
res = os.system(line)
if res:
break
return res
def getOsName(self):
"""Work.py, line 81"""
return os.name
def isCaseSensitive(self):
"""Commands.py, line 455"""
return 1
def isSuperUser(self):
"""Commands.py, line 2932"""
return 1
def canUseTee(self):
"""this should be named differently, perhaps isNotInteractive?
Used in:
CopyMove.py, line 91; Remote.py, line 175; Remote.py line 195
"""
# but perhaps it is better to abstract the code that is used after
# this...
return 0
def getExtList(self):
"""RecPython.py, line 296"""
return [ '' ]
def getFullFname(self,name):
"""Util.py, line 483"""
return self.foldFname(os.path.abspath(os.path.normpath(name)))
def foldFname(self,name):
"""fname_fold: Util.py, 493"""
return name
def systemAsync(self,cmd):
"""Util.py, line 973"""
pass
class PosixOperatingSystem(OperatingSystem):
def quoteShell(self,str):
# On Unix a mix of double and single quotes works well
# Also escape characters that have a special meaning.
from Dictlist import listitem2str
return listitem2str(str, ' \t&;|$<>', '&;|')
def system(self,cmd):
return os.system(cmd)
def isSuperUser(self):
return os.access("/", os.W_OK)
def systemAsync(self,cmd):
if not os.fork():
n = os.system(cmd)
os._exit(n)
class WindowsOperatingSystem(OperatingSystem):
def quoteShell(self,str):
# On MS-Windows double quotes works well
return double_quote(str)
def getOsName(self):
if os.name == "dos":
return "msdos"
if os.name == "nt":
return "mswin"
return OperatingSystem.getOsName(self)
def isCaseSensitive(self):
return 0
def getExtList(self):
return ['', '.exe', '.com', '.bat', '.cmd' ]
def foldFname(self,name):
return string.replace(string.lower(n),'\\','/')
def systemAsync(self,cmd):
"""TODO: only on nt?"""
shell = self.getShellName()
os.spawnv(os.P_DETACH, shell, ["/c", cmd] )
def getShellName(self):
"""Get the name of the shell to use (either command.com or cmd.exe)."""
if os.environ.has_key("SHELL"):
shell = os.environ["SHELL"]
elif os.environ.has_key("COMSPEC"):
shell = os.environ["COMSPEC"]
else:
from RecPython import program_path
shell = program_path("cmd.exe")
if not shell:
shell = program_path("command.com")
return shell
def createOperatingSystem():
if os.name == "posix":
return PosixOperatingSystem()
elif os.name in ["dos","nt","os2","win32"]:
return WindowsOperatingSystem()
# vim: set sw=4 et sts=4 tw=79 fo+=l:
---end OperatingSystem.py------------------------------------------
> It's very simple: Run "aap" without arguments in the aap/Exec
> directory.
That is indeed so simple that I would have never thought of it.
Tim
--
Zen Microsystems: we're the om in .commmmmmmmm...
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf