| OLD | NEW |
| 1 # Copyright (c) 2006,2007,2008 Mitch Garnaat http://garnaat.org/ | 1 # Copyright (c) 2006,2007,2008 Mitch Garnaat http://garnaat.org/ |
| 2 # | 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a | 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the | 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including | 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- | 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit | 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- | 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: | 9 # lowing conditions: |
| 10 # | 10 # |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 @property | 30 @property |
| 31 def file(self): | 31 def file(self): |
| 32 from StringIO import StringIO | 32 from StringIO import StringIO |
| 33 if self._file: | 33 if self._file: |
| 34 f = self._file | 34 f = self._file |
| 35 else: | 35 else: |
| 36 f = StringIO(self.value) | 36 f = StringIO(self.value) |
| 37 return f | 37 return f |
| 38 | 38 |
| 39 def __str__(self): | 39 def __str__(self): |
| 40 return unicode(self).encode('utf-8') |
| 41 |
| 42 def __unicode__(self): |
| 40 if hasattr(self.file, "get_contents_as_string"): | 43 if hasattr(self.file, "get_contents_as_string"): |
| 41 value = self.file.get_contents_as_string() | 44 value = self.file.get_contents_as_string() |
| 42 else: | 45 else: |
| 43 value = self.file.getvalue() | 46 value = self.file.getvalue() |
| 44 try: | 47 if isinstance(value, unicode): |
| 45 return str(value) | 48 return value |
| 46 except: | 49 else: |
| 47 return unicode(value) | 50 return value.decode('utf-8') |
| 51 |
| 48 | 52 |
| 49 def read(self): | 53 def read(self): |
| 50 return self.file.read() | 54 if hasattr(self.file, "get_contents_as_string"): |
| 55 return self.file.get_contents_as_string() |
| 56 else: |
| 57 return self.file.read() |
| 51 | 58 |
| 52 def readline(self): | 59 def readline(self): |
| 53 return self.file.readline() | 60 return self.file.readline() |
| 54 | 61 |
| 55 def next(self): | 62 def next(self): |
| 56 return self.file.next() | 63 return self.file.next() |
| 57 | 64 |
| 58 def __iter__(self): | 65 def __iter__(self): |
| 59 return iter(self.file) | 66 return iter(self.file) |
| 60 | 67 |
| 61 @property | 68 @property |
| 62 def size(self): | 69 def size(self): |
| 63 if self._file: | 70 if self._file: |
| 64 return self._file.size | 71 return self._file.size |
| 65 elif self.value: | 72 elif self.value: |
| 66 return len(self.value) | 73 return len(self.value) |
| 67 else: | 74 else: |
| 68 return 0 | 75 return 0 |
| OLD | NEW |