/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Lib/fileutil.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Lib/fileutil.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 6 by bh, Tue Aug 28 15:41:52 2001 UTC revision 1152 by frank, Wed Jun 11 11:06:27 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001 by Intevation GmbH  # Copyright (c) 2001, 2002 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 11  Functions to deal with filenames Line 11  Functions to deal with filenames
11    
12  __version__ = "$Revision$"  __version__ = "$Revision$"
13    
14    import os
15  import os.path  import os.path
16    from tempfile import mktemp
17    
18  from string import split, join  from string import split, join
19    
20    from Thuban import _
21    
22  def relative_filename_common(dir, absname, sep):  def relative_filename_common(dir, absname, sep):
23      """Return a filename relative to dir for the absolute file name absname.      """Return a filename relative to dir for the absolute file name absname.
# Line 77  def relative_filename_posix(dir, absname Line 81  def relative_filename_posix(dir, absname
81      # for that to faciliate testing      # for that to faciliate testing
82      import posixpath      import posixpath
83      if not posixpath.isabs(absname):      if not posixpath.isabs(absname):
84          return absname          return absname
85    
86      if not posixpath.isabs(dir):      if not posixpath.isabs(dir):
87          raise TypeError("first argument must be an absolute filename")          raise TypeError(_("first argument must be an absolute filename"))
88    
89      dir = posixpath.normpath(dir)      dir = posixpath.normpath(dir)
90      absname = posixpath.normpath(absname)      absname = posixpath.normpath(absname)
# Line 131  def relative_filename_nt(dir, absname): Line 135  def relative_filename_nt(dir, absname):
135      #print dir_drive, dir_rest      #print dir_drive, dir_rest
136      #print absname_drive, absname_rest      #print absname_drive, absname_rest
137      if not dir_drive or not absname_drive:      if not dir_drive or not absname_drive:
138          raise TypeError("Both parameters must have a drive letter")          raise TypeError(_("Both parameters must have a drive letter"))
139    
140      if not ntpath.isabs(dir_rest):      if not ntpath.isabs(dir_rest):
141          raise TypeError("first argument must be an absolute filename")          raise TypeError(_("first argument must be an absolute filename"))
142    
143      # handle some special cases      # handle some special cases
144      if not ntpath.isabs(absname_rest):      if not ntpath.isabs(absname_rest):
145          return absname          return absname
146    
147      if dir_drive != absname_drive:      if dir_drive != absname_drive:
148          return absname          return absname
# Line 148  def relative_filename_nt(dir, absname): Line 152  def relative_filename_nt(dir, absname):
152      # relative name      # relative name
153      return relative_filename_common(dir_rest, absname_rest, "\\")      return relative_filename_common(dir_rest, absname_rest, "\\")
154    
155    def get_application_dir():
156        """Determine the path to the .thuban directory.
157    
158        Under posix systems use the os.expanduser() method.
159        Under Win32 try to read the "Explorer/Shell Folders/" value "AppData".
160        """
161    
162        if os.name == 'posix':
163            return os.path.expanduser("~/.thuban")
164        elif os.name == 'nt':
165            regkey = 1
166            try:
167                import _winreg as wreg
168            except ImportError:
169                regkey = 0
170    
171            if regkey:
172                try:
173                    key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
174                      "Software\\Microsoft\\Windows\\CurrentVersion\\"\
175                      "Explorer\\Shell Folders")
176                    dir = wreg.QueryValueEx(key, "AppData")[0]
177                    dir = os.path.join(dir, "thuban")
178                except:
179                    regkey = 0
180    
181            if not regkey:
182                # The fallback. This should result in something like the
183                # user directory ...
184                guess = os.path.dirname(
185                                    os.path.dirname(os.path.dirname(mktemp()))
186                                )
187                dir = os.path.join(guess, "thuban")
188            if not os.path.isdir(dir):
189               os.mkdir(dir)
190    
191            return dir
192    
193        else:
194            raise RuntimeError(_("No implementation of get_application_dir"
195                           " available for platform") + os.name)
196    
197  # bind the appriate version of relative_filename for the platform we're  # bind the appropriate version of relative_filename for the platform
198  # currently running on.  # we're currently running on.
199  if os.name == "posix":  if os.name == "posix":
200      relative_filename = relative_filename_posix      relative_filename = relative_filename_posix
201  elif os.name == "nt":  elif os.name == "nt":
202      relative_filename = relative_filename_nt      relative_filename = relative_filename_nt
203  else:  else:
204      raise RuntimeError("No implementation of relative_filename"      raise RuntimeError(_("No implementation of relative_filename"
205                         " available for platform" + os.name)                         " available for platform") + os.name)
206    
207  __test__ = {"relative_filename_posix": relative_filename_posix,  __test__ = {"relative_filename_posix": relative_filename_posix,
208              "relative_filename_nt": relative_filename_nt}              "relative_filename_nt": relative_filename_nt}

Legend:
Removed from v.6  
changed lines
  Added in v.1152

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26