Jeremy Hylton | 53d527a | 2002-08-05 18:29:45 +0000 | [diff] [blame] | 1 | """Generate the skeleton for cStringIO as an example of framer.""" |
| 2 | |
| 3 | from framer.bases import Module, Type |
| 4 | from framer.member import member |
| 5 | |
| 6 | class cStringIO(Module): |
| 7 | """A simple fast partial StringIO replacement. |
| 8 | |
| 9 | This module provides a simple useful replacement for the StringIO |
| 10 | module that is written in C. It does not provide the full |
| 11 | generality of StringIO, but it provides enough for most |
| 12 | applications and is especially useful in conjunction with the |
| 13 | pickle module. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 14 | |
Jeremy Hylton | 53d527a | 2002-08-05 18:29:45 +0000 | [diff] [blame] | 15 | Usage: |
| 16 | |
| 17 | from cStringIO import StringIO |
| 18 | |
| 19 | an_output_stream = StringIO() |
| 20 | an_output_stream.write(some_stuff) |
| 21 | ... |
| 22 | value = an_output_stream.getvalue() |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 23 | |
Jeremy Hylton | 53d527a | 2002-08-05 18:29:45 +0000 | [diff] [blame] | 24 | an_input_stream = StringIO(a_string) |
| 25 | spam = an_input_stream.readline() |
| 26 | spam = an_input_stream.read(5) |
| 27 | an_input_stream.seek(0) # OK, start over |
| 28 | spam = an_input_stream.read() # and read it all |
| 29 | """ |
| 30 | |
| 31 | __file__ = "cStringIO.c" |
| 32 | |
| 33 | def StringIO(o): |
| 34 | """Return a StringIO-like stream for reading or writing""" |
| 35 | StringIO.pyarg = "|O" |
| 36 | |
| 37 | class InputType(Type): |
| 38 | "Simple type for treating strings as input file streams" |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 39 | |
Jeremy Hylton | 53d527a | 2002-08-05 18:29:45 +0000 | [diff] [blame] | 40 | abbrev = "input" |
| 41 | |
| 42 | struct = """\ |
| 43 | typedef struct { |
| 44 | PyObject_HEAD |
| 45 | char *buf; |
| 46 | int pos; |
| 47 | int size; |
| 48 | PyObject *pbuf; |
| 49 | } InputObject; |
| 50 | """ |
| 51 | |
| 52 | def flush(self): |
| 53 | """Does nothing""" |
| 54 | |
| 55 | def getvalue(self): |
| 56 | """Get the string value. |
| 57 | |
| 58 | If use_pos is specified and is a true value, then the |
| 59 | string returned will include only the text up to the |
| 60 | current file position. |
| 61 | """ |
| 62 | |
| 63 | def isatty(self): |
| 64 | """Always returns False""" |
| 65 | |
| 66 | def read(self, s): |
| 67 | """Return s characters or the rest of the string.""" |
| 68 | read.pyarg = "|i" |
| 69 | |
| 70 | def readline(self): |
| 71 | """Read one line.""" |
| 72 | |
| 73 | def readlines(self, hint): |
| 74 | """Read all lines.""" |
| 75 | readlines.pyarg = "|i" |
| 76 | |
| 77 | def reset(self): |
| 78 | """Reset the file position to the beginning.""" |
| 79 | |
| 80 | def tell(self): |
| 81 | """Get the current position.""" |
| 82 | |
| 83 | def truncate(self, pos): |
| 84 | """Truncate the file at the current position.""" |
| 85 | truncate.pyarg = "|i" |
| 86 | |
| 87 | def seek(self, position, mode=0): |
| 88 | """Set the current position. |
| 89 | |
| 90 | The optional mode argument can be 0 for absolute, 1 for relative, |
| 91 | and 2 for relative to EOF. The default is absolute. |
| 92 | """ |
| 93 | seek.pyarg = "i|i" |
| 94 | |
| 95 | def close(self): |
| 96 | pass |
| 97 | |
| 98 | class OutputType(InputType): |
| 99 | "Simple type for output strings." |
| 100 | |
| 101 | abbrev = "output" |
| 102 | |
| 103 | struct = """\ |
| 104 | typedef struct { |
| 105 | PyObject_HEAD |
| 106 | char *buf; |
| 107 | int pos; |
| 108 | int size; |
| 109 | int softspace; |
| 110 | } OutputObject; |
| 111 | """ |
| 112 | |
| 113 | softspace = member() |
| 114 | |
| 115 | def close(self): |
| 116 | """Explicitly release resources.""" |
| 117 | |
| 118 | def write(self, s): |
| 119 | """Write a string to the file.""" |
| 120 | # XXX Hack: writing None resets the buffer |
| 121 | |
| 122 | def writelines(self, lines): |
| 123 | """Write each string in lines.""" |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 124 | |
Jeremy Hylton | 53d527a | 2002-08-05 18:29:45 +0000 | [diff] [blame] | 125 | |
| 126 | cStringIO.gen() |