Mon, 25 Mar 2013 03:28:43 +0100
Minimum modifications to start Eric5 with Py2.
2526
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

1  # * coding: utf8 * 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

2  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

3  # Copyright (c) 2013 Tobias Rzepka <tobias.rzepka@gmail.com> 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

4  # 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

5  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

6  """ 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

7  Module implementing the open behavior of Python3 for use with Eric5. 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

8  The from Eric5 used features are emulated only. The not emulated features 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

9  should throw a NotImplementedError exception. 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

10  """ 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

11  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

12  from __future__ import unicode_literals # __IGNORE_WARNING__ 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

13  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

14  import __builtin__ 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

15  import codecs 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

16  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

17  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

18  def open(file, mode='r', buffering=1, encoding=None, errors=None, newline=None, closefd=True): 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

19  return File(file, mode, buffering, encoding, errors, newline, closefd) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

20  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

21  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

22  class File(file): 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

23  def __init__(self, filein, mode='r', buffering=1, encoding=None, errors=None, newline=None, closefd=True): 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

24  self.__encoding = encoding 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

25  self.__newline = newline 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

26  self.__closefd = closefd 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

27  if newline is not None: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

28  if 'r' in mode: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

29  raise NotImplementedError 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

30  else: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

31  mode = mode.replace('t', 'b') 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

32  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

33  if closefd == False: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

34  raise NotImplementedError 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

35  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

36  if errors is None: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

37  self.__errors = 'strict' 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

38  else: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

39  self.__errors = errors 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

40  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

41  file.__init__(self, filein, mode, buffering) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

42  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

43  def read(self, n=1): 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

44  txt = super(File, self).read(n) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

45  if self.__encoding is None: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

46  return txt 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

47  else: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

48  return codecs.decode(txt, self.__encoding) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

49  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

50  def readline(self, limit=1): 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

51  txt = super(File, self).readline(limit) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

52  if self.__encoding is None: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

53  return txt 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

54  else: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

55  return codecs.decode(txt, self.__encoding) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

56  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

57  def readlines(self, hint=1): 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

58  if self.__encoding is None: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

59  return super(File, self).readlines(hint) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

60  else: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

61  return [codecs.decode(txt, self.__encoding) for txt in super(File, self).readlines(hint)] 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

62  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

63  def write(self, txt): 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

64  if self.__encoding is not None: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

65  txt = codecs.encode(txt, self.__encoding, self.__errors) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

66  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

67  if self.__newline in ['\r\n', '\r']: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

68  txt = txt.replace('\n', self.__newline) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

69  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

70  super(File, self).write(txt) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

71  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

72  def next(self): 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

73  txt = super(File, self).next() 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

74  if self.__encoding is None: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

75  return txt 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

76  else: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

77  return codecs.decode(txt, self.__encoding) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

78  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

79  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

80  __builtin__.open = open 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

81  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

82  if __name__ == '__main__': 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

83  fp = open('compatibility_fixes.py', encoding='latin1') 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

84  print(fp.read()) 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

85  fp.close() 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

86  
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

87  with open('compatibility_fixes.py', encoding='UTF8') as fp: 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

88  rlines = fp.readlines() 
a91cba8291b9
Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

89  print(rlines[1]) 