Changeset 414 for pkpgcounter/trunk/pkpgpdls
- Timestamp:
- 09/15/06 00:47:15 (18 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/plain.py
r412 r414 35 35 """A parser for plain text documents.""" 36 36 def isValid(self) : 37 """Returns True if data is plain text, else False.""" 38 return True 37 """Returns True if data is plain text, else False. 38 39 It's hard to detect a plain text file, so we just 40 read the first line, and if it doesn't end in CR or LF 41 we consider it's not plain text. 42 """ 43 line = self.infile.readline() 44 self.infile.seek(0) 45 if line.endswith("\n") or line.endswith("\r") : 46 self.logdebug("DEBUG: Input file seems to be in the plain text format.") 47 return True 48 else : 49 return False 39 50 40 51 def getJobSize(self) : … … 45 56 linecount = 0 46 57 for line in self.infile : 47 linecount += 1 48 if (linecount > pagesize) \ 49 or (line.find(chr(12)) != -1) : 50 pagecount += 1 51 linecount = 0 52 return pagecount + 1 58 if line.endswith("\n") or line.endswith("\r") : 59 linecount += 1 60 if (linecount > pagesize) \ 61 or (line.find("\f") != -1) : 62 pagecount += 1 63 linecount = 0 64 else : 65 raise pdlparser.PDLParserError, "Unsupported file format. Please send the file to %s" % version.__authoremail__ 66 return pagecount + 1 # NB : empty files are catched in isValid() 53 67 54 def test() :55 """Test function."""56 if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :57 sys.argv.append("-")58 totalsize = 059 for arg in sys.argv[1:] :60 if arg == "-" :61 infile = sys.stdin62 mustclose = 063 else :64 infile = open(arg, "rb")65 mustclose = 166 try :67 parser = Parser(infile, debug=1)68 totalsize += parser.getJobSize()69 except pdlparser.PDLParserError, msg :70 sys.stderr.write("ERROR: %s\n" % msg)71 sys.stderr.flush()72 if mustclose :73 infile.close()74 print "%s" % totalsize75 76 68 if __name__ == "__main__" : 77 test()69 pdlparser.test(Parser)