5: #line 88 "source_drivers.ipk"
6:
7:
8: import string
9: class source:
10: __class_protocols__ = ['file','source']
11: def __init__(self, encoding, **kwds):
12: self.lines_read = 0
13: self.mode = 'r'
14: for k in kwds.keys():
15: self.__dict__[k]=kwds[k]
16: self.closed = 1
17: self.set_encoding(encoding)
18:
19: def set_encoding(self, encoding):
20: self.encoding_name = encoding
21: self.encoding_routine = None
22: if encoding != 'utf8':
23: encoding_module = 'interscript.encoding.'+encoding
24: conversion_routine = encoding + '_to_utf8'
25: try:
26: exec 'import '+encoding_module
27: exec 'self.encoding_routine = '+encoding_module+'.'+conversion_routine
28: except:
29: print 'UNABLE TO LOAD ENCODING "'+encoding+'", reading as UTF8'
30:
31: def get_encoding(self): return self.encoding_name
32:
33: def get_source_name(self):
34: return self.name
35:
36: def get_lines_read(self):
37: return self.lines_read
38:
39: def raw_readlines(self):
40: if self.closed: raise eof
41: lines = []
42: try:
43: while 1: lines.append(self.raw_readline())
44: except eof:
45: pass
46: return lines
47:
48: def readline(self, sizehint=None):
49: return self.raw_readline()+'\n'
50:
51: def read(self, sizehint=None):
52: return string.join(self.raw_readlines())+'\n'
53:
54: def readlines(self, sizehint=None):
55: x = []
56: for line in self.raw_readlines():
57: x.append(line+'\n')
58: return x
59:
60: def isatty(self):
61: return 0
62:
63: def close(self):
64: if not self.closed:
65: self._close()
66: self.closed = 1
67:
68: def _close(self): pass
69: def flush(self): pass
70: def __del__(self):
71: self.close()
72:
73: def raw_readline(self):
74: data = self._raw_readline()
75: if self.encoding_routine:
76: data = self.encoding_routine(data)
77: return data
78:
79: class file_source(source):
80: def __init__(self, encoding, *args, **kwds):
81: apply(source.__init__, (self,encoding)+args, kwds)
82:
83: def _close(self):
84: self.file.close()
85:
86: def _raw_readline(self):
87: if self.closed: raise eof
88: line = self.file.readline()
89: if len(line)==0: raise eof
90: self.lines_read = self.lines_read + 1
91: return string.rstrip(line)
92:
93: def get_filename(self):
94: return self.name
95: