import sys, string, glob, re class formatter: def __init__ (self): self.inq = False self.inqq = False self.keepq = False self.words = list () self.comment = False self.line = '' # # An each application (using reduce) a single word is given # quoted words are assembled into self.words # def apply (self, dummy, word): if word == '__CMTEOL__': if self.comment: self.comment = False return (self) if self.comment: return (self) if (not self.inq) and (not self.inqq): if word[0] == "#": # we suppose that whenever a # sign exists it closes the complete line! # which means that we cannot continue a line with a local comment self.comment = True return (self) # we are outside of any quote if word[0] == "'": # we start a new qword word = word[1:] if word[-1:] == "'": self.words.append (word[:-1]) else: self.inq = True self.qw = word return (self) elif word[0] == '"': # we start a new qqword word = word[1:] if word[-1:] == '"': self.words.append (word[:-1]) else: self.inqq = True self.qw = word return (self) elif re.search (r'[=][\']$', word): # we start an option with quote self.inq = True self.qw = word self.keepq = True return (self) elif re.search (r'[=][\']^$', word): # we start an option with quote if word[-1:] == "'": # this option is just terminated self.words.append (word) else: self.inq = True self.qw = word self.keepq = True return (self) elif re.search (r'[=][\"]$', word): # we start an option with quote self.inqq = True self.qw = word self.keepq = True return (self) elif re.search (r'[=][\"]^$', word): # we start an option with quote if word[-1:] == '"': # this option is just terminated self.words.append (word) else: self.inqq = True self.qw = word self.keepq = True return (self) else: self.words.append (word) elif self.inq: if word[-1:] == "'": if self.keepq: self.qw += ' ' + word else: self.qw = self.qw + ' ' + word[:-1] self.words.append (self.qw) self.inq = False self.keepq = False self.qw = '' else: # we continue the quote self.qw += ' ' + word elif self.inqq: if word[-1:] == '"': if self.keepq: self.qw += ' ' + word else: self.qw = self.qw + ' ' + word[:-1] self.words.append (self.qw) self.inqq = False self.keepq = False self.qw = '' else: # we continue the quote self.qw += ' ' + word return (self) def main(): n = len (sys.argv) if n < 2: print "Give the name of the file to convert" sys.exit() file_name = sys.argv[1] f = open (file_name, "r") lines = f.readlines() f.close() n = 0 prefix = '' for line in lines: n = n + 1 line = string.strip (line, ' \t\n') # empty lines if len(line) == 0: continue # full comment lines if line[0] == '#': continue # prepend continued lines line = prefix + line # convert statement separators in patterns line = re.sub (r'[;]([ ]*[\\][ ]*)$', r'__CMTSEPARATOR__\1', line) line = re.sub (r'[;]([ ]*)$', r'__CMTSEPARATOR__\1', line) # continuation lines if re.search(r'[\\][ ]*$', line): prefix = re.sub (r'[\\][ \t]*$', ' __CMTEOL__ ', line) continue # split the line into words words = line.split () # Re-assemble words into CMT elements u = formatter () reduce (u.apply, words, u) # format the full line cmtline = '__CMTWORD__'.join (u.words) #print str(n) + " - " + cmtline print cmtline prefix = '' if __name__ == "__main__": main()