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

Source Code for Module licensechecker.commentinfo

  1  """ 
  2  Defines classes to handle the description of comments in various programming languages. Both single line comments and block comments are handled. 
  3  """ 
  4  import logging 
  5  import os 
  6  import re 
  7   
8 -class CommentInfo:
9 """ 10 Class storing some information about comment types in various programming languages. 11 12 - A type name (C, C++, XML, Java or whatever), 13 - A list of single-line comment delimiters. 14 - A list of block comment delimiters (block opening and closing delimiters). 15 """ 16
17 - def __init__(self,typename='Unknown',linecomments=(),blockcomments=()) :
18 """ 19 Builds an instance of CommentInfo. 20 21 @param typename: a string representing the language this comment info is related to, 22 @param linecomments: a tuple of strings denoting the way single line comments are represented in a given progamming language, 23 @param blockcomments: a tuple of tuples denoting how block comments are formatted. Each first-level tuple contains a two-element second-level tuple, where the first element is the string representing opening block comments, and the second element represents closing block comments. 24 25 As an example, a Java CommentInfo could be built as follows : 26 27 >>> ci=CommentInfo('Java',('//',)(('/*','*/'),)) 28 29 30 31 """ 32 self.typename=typename 33 self.linecomments=[] 34 for linecomment in linecomments : 35 self.linecomments.append(linecomment) 36 self.blockcomments=[] 37 for blockcomment in blockcomments : 38 self.blockcomments.append((blockcomment[0],blockcomment[1]))
39 40
41 - def getTypeName(self) :
42 """ 43 Returns the type name of the current instance. 44 """ 45 return self.typename
46
47 - def getLineComments(self) :
48 """ 49 Returns the tuple of line comments of the current instance. 50 """ 51 return self.linecomments
52
53 - def getBlockComments(self) :
54 """ 55 Returns the tuple of tuples of block comments of the current instance. 56 """ 57 return self.blockcomments
58
59 -class CommentChecker :
60 61 """ 62 Class containing comment information about all known programming languages. 63 64 @note: this class may be given a more meaningful name if it turns out that the actual check for the license conformity is performed elsewhere. 65 """ 66 67 __logger=logging.getLogger('lichk') 68
69 - def __init__(self,filename= os.path.join(os.path.dirname(__file__),'data/commentinfo.txt')):
70 """ 71 Builds a new instance of CommentChecker from a text file containing comment information. 72 73 The text file must be formatted as follows: 74 - blank lines or lines starting with C{#} are ignored, 75 - other lines must have 2, 3 or 4 whitespace separated fields : 76 - the first field is a string with the name of the programming language (case sensitive), 77 - for lines with 2 fields, the second field is the delimiter for single-line comments, 78 - for lines with 3 fields, the second (resp. third field) is the opening (resp. closing) delimiter for comment blocks, 79 - for lines with 4 fields, the three first fields are the same as in a 3-field line, the 4th field is the delimiter for single-line comments. 80 81 Examples:: 82 83 # The following line declares Shell-type comments (single-line comments) 84 shell # 85 # The following line declares Java-type comments (both block and single-line) 86 Java /* */ // 87 88 89 @param filename: (optional) the name of the file with the comment information. The default value looks for a file named C{commentinfo.txt} in this module's C{data} subdirectory. 90 91 @note: there may be multiple lines related to a single programming language in the same file. Each line adds new delimiters those already encountered. Example:: 92 93 # First pair of delimiters for Pascal comment blocks. 94 Pascal (* *) 95 # Second pair of delimiters for Pascal comment blocks. 96 Pascal { } 97 98 """ 99 self.commentinfodict={} 100 try : 101 fd=open(filename,'r') 102 for line in fd : 103 line=line.strip() 104 # Skip empty and comment lines. 105 if len(line)>0 and not line.startswith('#') : 106 fields=re.split('\s+',line) 107 nfields=len(fields) 108 if nfields >= 2 or nfields <= 4 : 109 typename=fields[0] 110 linecomments=[] 111 blockcomments=[] 112 ci=None 113 # Check if we already have comment info for this programming language. 114 if self.commentinfodict.has_key(typename) : 115 ci=self.commentinfodict[typename] 116 linecomments=ci.getLineComments() 117 blockcomments=ci.getBlockComments() 118 # Read the line comment delimiter field of lines with two fields. 119 if nfields == 2 : 120 linecomments.append(fields[1]) 121 # Read the block comment delimiter fields of lines with 3 or 4 fields. 122 if nfields == 3 or nfields == 4 : 123 blockcomment=(fields[1],fields[2]) 124 blockcomments.append(blockcomment) 125 # Read the line comment delimiter field of lines with 4 fields. 126 if nfields == 4 : 127 linecomments.append(fields[3]) 128 # Add this new programming language and it's comment info to the dictionary of known programming languages. 129 if ci == None : 130 ci=CommentInfo(typename,linecomments,blockcomments) 131 self.commentinfodict[typename]=ci 132 else : 133 self.__logger.warn("Incorrect number of columns (%d) in line : %s" % (nfields,filename)) 134 fd.close() 135 except IOError : 136 print "Unable to open file containing file type information : %s" % filename 137 raise
138 139
140 - def getCommentInfo(self,typename):
141 """ 142 Return comment info about a given programming language. 143 144 @param typename: the name of the programming language (string) 145 146 @returns: an instance of CommentInfo if the typename is known (or None). 147 @rtype: CommentInfo 148 """ 149 ci=None 150 try: 151 ci=self.commentinfodict[typename] 152 except KeyError: 153 pass 154 return ci
155
156 - def getAllCommentInfo(self):
157 """ 158 Return comment info about all known programming languages. 159 160 @returns: all known CommentInfo instances in the form of a dictionary whose keys are the names of the programming languages, and whose values are CommentInfo instances. 161 @rtype: {string:CommentInfo,} 162 """ 163 return self.commentinfodict
164 165
166 - def getAllTypeNames(self):
167 """ 168 Return the names of all known programming languages. 169 170 @returns: an array of strings containing the names of all known programming languages. 171 @rtype: [string,] 172 """ 173 return self.commentinfodict.keys()
174