Package licensechecker :: Module fileinfo
[hide private]
[frames] | no frames]

Source Code for Module licensechecker.fileinfo

  1  """ 
  2  Defines classes handling the association of file types (mostly programming languages but any type can be described) and file extensions, and allowing the lookup of either from the other. 
  3  """ 
  4   
  5  import os 
  6  import re 
  7   
8 -class FileInfo:
9 """ 10 Class used to match file extensions and file types (mostly programming languages though). 11 """ 12
13 - def __init__(self,typename='Unknown',extensions=()) :
14 """ 15 Build an new instance of FileInfo. 16 17 @param typename: a string denoting the file type (ex.: 'C', 'Pascal', 'Java') 18 @param extensions: a tuple of strings describing the file extensions associated with the file type. The strings must not begin with a dot. 19 """ 20 self.typename=typename 21 self.extensions=[] 22 for extension in extensions : 23 self.extensions.append(extension)
24
25 - def getTypeName(self) :
26 """ 27 Returns the typename of this FileInfo. 28 29 @returns: the typename of this FileInfo. 30 @rtype: string 31 """ 32 return self.typename
33
34 - def getExtensions(self) :
35 """ 36 Returns the extensions of this FileInfo. 37 38 @returns: a list of extensions associated to this FileInfo. 39 @rtype: [string,] 40 """ 41 return self.extensions
42
43 -class FileChecker :
44 """ 45 Class holding FileInfo instances related to a set of programming languages and file extensions. 46 """ 47
48 - def __init__(self,filename= os.path.join(os.path.dirname(__file__),'data','fileinfo.txt')):
49 """ 50 Builds a new instance of FileChecker from a text file containing descriptions of programming languages and file extensions. 51 52 The textfile must be formatted as follows: 53 - blank lines or lines starting with C{#} are ignored, 54 - other lines are split in whitespace separated fields: 55 - the first field contains an indentifier (case sensitive) for the programming language, 56 - the remaining fields each contain a file extension related to the programming language. 57 58 Example:: 59 60 # Enumeration of file extensions for C++ 61 C++ .cc .C .cxx .cpp .h .hxx 62 63 64 @param filename: a string with the filename containing the association between programming languages and file extensions. If no argument is given, the instance is built from the contents of the C{fileinfo.txt} file located in the module's C{data} subdirectory. 65 66 """ 67 self.filetypedict={} 68 self.fileextdict={} 69 try : 70 fd=open(filename,'r') 71 for line in fd : 72 line=line.strip() 73 # Skip empty and comment lines. 74 if len(line)>0 and not line.startswith('#') : 75 fields=re.split('\s+',line,1) 76 typename=fields[0] 77 extensionstring=fields[1] 78 extensions=re.split('\s+',extensionstring) 79 fi=FileInfo(typename,extensions) 80 self.filetypedict[typename]=fi 81 for extension in extensions: 82 self.fileextdict[extension]=fi 83 fd.close() 84 except IOError : 85 print "Unable to open file containing file type information : %s" % filename 86 raise
87
88 - def getExtensions(self,filetype=None):
89 """ 90 Returns the list of extensions for a given programming language, or all known file extensions. 91 92 @param filetype: the programming language for which the file extensions will be returned (or None if all extensions are to be returned). 93 94 @returns: a list with all the resulting file extensions. 95 @rtype: [string,] 96 """ 97 extensions=None 98 if filetype == None : 99 extensions=self.fileextdict.keys() 100 else: 101 try : 102 fi=self.filetypedict[filetype] 103 extensions=fi.getExtensions() 104 except KeyError: 105 pass 106 return extensions
107
108 - def getTypeName(self,extension):
109 """ 110 Returns the type name (programming language) for a given file extension. 111 112 @param extension: a string containing the file extension (without the leading dot). An exception is raised if the extension is unknown. 113 114 @returns: the type (programming language) corresponding to the extension. 115 @rtype: string 116 """ 117 typename=None 118 try: 119 fi=self.fileextdict[extension] 120 typename=fi.getTypeName() 121 except KeyError: 122 pass 123 return typename
124
125 - def getFileInfo(self,filename):
126 """ 127 Returns an instance of FileInfo given a filename. 128 129 @param filename: a string containing a filename. 130 131 @returns: an instance of FileInfo looked up from the extension of the given filename. An exception occurs if no mathing FileInfo is found. 132 @rtype: FileInfo 133 """ 134 fields=re.split('\.',filename) 135 extension=fields[-1] 136 fi=None 137 try: 138 fi=self.fileextdict[extension] 139 except KeyError: 140 pass 141 return fi
142