[Tutor] Configuaration files and paths?
Allen Fowler
allen.fowler at yahoo.com
Fri Aug 7 22:19:36 CEST 2009
> >
> > FWIW:
> >
> > When using relative paths I got extra ../../ terms, so I changed
> join_relative() to:
> >
> > def join_relative(base, path):
> > return os.path.normpath(os.path.join(script_dir(base), path))
> >
> >
> > Seems to work...
>
>
> Yeah, good catch ... looks great, and thanks for sharing your mod.
>
Glad I could return the favor. :)
Since then, I took it a bit further for use in my code... I am not finished with it yet, but am curios what you think. (I am not sure named things right or structured it logically....)
###################
import os
class HomePath(object):
"""For finding paths based on a home/install directory.
"""
def __init__(self, home_dir = None, home_file = None):
"""Must be called with either a path to a directory or, as a shortcut, a file in that directory.
"""
if home_file != None:
# Set home based on a path to a file in its directory
self.home = os.path.normpath(self.fix_path(home_file))
elif home_dir != None:
# Set home based on its path
self.home = os.path.normpath(self.get_dir(home_dir))
else:
raise Exception("Must call with either a path to a directory or, as a shortcut, a file in that directory.")
def abs(self, rel_from_home):
"""Return an absolute path when passed a path relative to home.
"""
return self.join_relative(self.home, rel_from_home)
def fix_path(self, base):
return os.path.realpath(os.path.abspath(base))
def get_dir(self, base):
return os.path.dirname(self.fix_path(base))
def join_relative(self, base, path):
return os.path.normpath(self.fix_path(os.path.join(self.home, path)))
#############################
More information about the Tutor
mailing list