Package eoxserver :: Package core :: Package util :: Module filetools
[hide private]
[frames] | no frames]

Source Code for Module eoxserver.core.util.filetools

  1  #------------------------------------------------------------------------------- 
  2  # $Id: filetools.py 1912 2012-08-01 12:51:15Z martin.paces $ 
  3  # 
  4  # Project: EOxServer <http://eoxserver.org> 
  5  # Authors: Stephan Krause <stephan.krause@eox.at> 
  6  #          Stephan Meissl <stephan.meissl@eox.at> 
  7  #          Martin Paces <martin.paces@eox.at> 
  8  # 
  9  #------------------------------------------------------------------------------- 
 10  # Copyright (C) 2011 EOX IT Services GmbH 
 11  # 
 12  # Permission is hereby granted, free of charge, to any person obtaining a copy 
 13  # of this software and associated documentation files (the "Software"), to deal 
 14  # in the Software without restriction, including without limitation the rights 
 15  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 16  # copies of the Software, and to permit persons to whom the Software is  
 17  # furnished to do so, subject to the following conditions: 
 18  # 
 19  # The above copyright notice and this permission notice shall be included in all 
 20  # copies of this Software or works derived from this Software. 
 21  # 
 22  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 23  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 24  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 25  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 26  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 27  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 28  # THE SOFTWARE. 
 29  #------------------------------------------------------------------------------- 
 30   
 31  """ 
 32  This module contains utility functions for file operations. 
 33  """ 
 34   
 35  import sys 
 36  import os 
 37  import os.path 
 38  import tempfile  
 39  from fnmatch import fnmatch 
 40   
 41  from eoxserver.core.exceptions import InternalError 
 42   
43 -def findFiles(dir, pattern):
44 """ 45 This function mimicks the behaviour of the ``find`` shell command. 46 It expects a directory path ``dir`` and a file name pattern 47 ``pattern`` which may contain wildcards as accepted by the 48 :func:`fnmatch.fnmatch` function. It returns a list of paths to 49 matching files in ``dir`` and its subdirectories. 50 51 If ``dir`` does not exist or does not point to a directory or if no 52 matching files are found an empty list is returned. 53 54 Directories and files whose name starts with "." are omitted. 55 """ 56 filenames = [] 57 58 if os.path.exists(dir) and os.path.isdir(dir): 59 for path in os.listdir(dir): 60 if not path.startswith("."): 61 if os.path.isdir(os.path.join(dir, path)): 62 filenames.extend(findFiles(os.path.join(dir, path), pattern)) 63 elif fnmatch(path, pattern): 64 filenames.append(os.path.join(dir, path)) 65 66 return filenames
67
68 -def pathToModuleName(path):
69 """ 70 This function takes a module path ``path`` as argument and returns 71 the corresponding dotted name of the module. 72 """ 73 74 module_name = [os.path.splitext(os.path.basename(path))[0]] 75 tmp_path = os.path.abspath(os.path.dirname(path)) 76 77 search_paths = [dir for dir in sys.path] 78 if '' in search_paths: 79 search_paths.remove('') 80 search_paths.append(os.getcwd()) 81 82 while tmp_path != "/" and tmp_path not in search_paths: 83 tmp_path, name = os.path.split(tmp_path) 84 module_name.append(name) 85 86 if tmp_path == "/": 87 raise InternalError("'%s' not on Python path." % path) 88 else: 89 return ".".join(reversed(module_name))
90 91
92 -class TmpFile :
93 """ temporary file object - ``with - as`` statement friendly """ 94
95 - def __init__( self , suffix , prefix = "" ) :
96 _ , self.__fname = tempfile.mkstemp(suffix,prefix) 97
98 - def __str__( self ) :
99 """Converts class to name of the temporary file.""" 100 return self.__fname
101
102 - def __enter__( self ) :
103 """Begin of ``with - as`` block - returns name of the temporary file.""" 104 return self.__fname
105
106 - def __exit__( self , type, value, traceback) :
107 """End of ``with - as`` block - discards the temporary file.""" 108 os.remove(self.__fname)
109