| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
|
| 5 |
# standard library dependencies |
| 6 |
import io |
| 7 |
from petl.compat import next, PY2, text_type |
| 8 |
|
| 9 |
|
| 10 |
# internal dependencies |
| 11 |
from petl.util.base import Table, asdict |
| 12 |
from petl.io.base import getcodec |
| 13 |
from petl.io.sources import read_source_from_arg, write_source_from_arg |
| 14 |
|
| 15 |
|
| 16 |
def fromtext(source=None, encoding=None, errors='strict', strip=None, |
| 17 |
header=('lines',)): |
| 18 |
""" |
| 19 |
Extract a table from lines in the given text file. E.g.:: |
| 20 |
|
| 21 |
>>> import petl as etl |
| 22 |
>>> # setup example file |
| 23 |
... text = 'a,1\\nb,2\\nc,2\\n' |
| 24 |
>>> with open('example.txt', 'w') as f: |
| 25 |
... f.write(text) |
| 26 |
... |
| 27 |
12 |
| 28 |
>>> table1 = etl.fromtext('example.txt') |
| 29 |
>>> table1 |
| 30 |
+-------+ |
| 31 |
| lines | |
| 32 |
+=======+ |
| 33 |
| 'a,1' | |
| 34 |
+-------+ |
| 35 |
| 'b,2' | |
| 36 |
+-------+ |
| 37 |
| 'c,2' | |
| 38 |
+-------+ |
| 39 |
|
| 40 |
>>> # post-process, e.g., with capture() |
| 41 |
... table2 = table1.capture('lines', '(.*),(.*)$', ['foo', 'bar']) |
| 42 |
>>> table2 |
| 43 |
+-----+-----+ |
| 44 |
| foo | bar | |
| 45 |
+=====+=====+ |
| 46 |
| 'a' | '1' | |
| 47 |
+-----+-----+ |
| 48 |
| 'b' | '2' | |
| 49 |
+-----+-----+ |
| 50 |
| 'c' | '2' | |
| 51 |
+-----+-----+ |
| 52 |
|
| 53 |
Note that the strip() function is called on each line, which by default |
| 54 |
will remove leading and trailing whitespace, including the end-of-line |
| 55 |
character - use the `strip` keyword argument to specify alternative |
| 56 |
characters to strip. Set the `strip` argument to `False` to disable this |
| 57 |
behaviour and leave line endings in place. |
| 58 |
|
| 59 |
""" |
| 60 |
|
| 61 |
source = read_source_from_arg(source) |
| 62 |
return TextView(source, header=header, encoding=encoding, |
| 63 |
errors=errors, strip=strip) |
| 64 |
|
| 65 |
|
| 66 |
class TextView(Table): |
| 67 |
|
| 68 |
def __init__(self, source, header=('lines',), encoding=None, |
| 69 |
errors='strict', strip=None): |
| 70 |
self.source = source |
| 71 |
self.header = header |
| 72 |
self.encoding = encoding |
| 73 |
self.errors = errors |
| 74 |
self.strip = strip |
| 75 |
|
| 76 |
def __iter__(self): |
| 77 |
with self.source.open('rb') as buf: |
| 78 |
|
| 79 |
# deal with text encoding |
| 80 |
if PY2: |
| 81 |
codec = getcodec(self.encoding) |
| 82 |
f = codec.streamreader(buf, errors=self.errors) |
| 83 |
else: |
| 84 |
f = io.TextIOWrapper(buf, |
| 85 |
encoding=self.encoding, |
| 86 |
errors=self.errors, |
| 87 |
newline='') |
| 88 |
|
| 89 |
# generate the table |
| 90 |
try: |
| 91 |
if self.header is not None: |
| 92 |
yield tuple(self.header) |
| 93 |
if self.strip is False: |
| 94 |
for line in f: |
| 95 |
yield (line,) |
| 96 |
else: |
| 97 |
for line in f: |
| 98 |
yield (line.strip(self.strip),) |
| 99 |
finally: |
| 100 |
if not PY2: |
| 101 |
f.detach() |
| 102 |
|
| 103 |
|
| 104 |
def totext(table, source=None, encoding=None, errors='strict', template=None, |
| 105 |
prologue=None, epilogue=None): |
| 106 |
""" |
| 107 |
Write the table to a text file. E.g.:: |
| 108 |
|
| 109 |
>>> import petl as etl |
| 110 |
>>> table1 = [['foo', 'bar'], |
| 111 |
... ['a', 1], |
| 112 |
... ['b', 2], |
| 113 |
... ['c', 2]] |
| 114 |
>>> prologue = '''{| class="wikitable" |
| 115 |
... |- |
| 116 |
... ! foo |
| 117 |
... ! bar |
| 118 |
... ''' |
| 119 |
>>> template = '''|- |
| 120 |
... | {foo} |
| 121 |
... | {bar} |
| 122 |
... ''' |
| 123 |
>>> epilogue = '|}' |
| 124 |
>>> etl.totext(table1, 'example.txt', template=template, |
| 125 |
... prologue=prologue, epilogue=epilogue) |
| 126 |
>>> # see what we did |
| 127 |
... print(open('example.txt').read()) |
| 128 |
{| class="wikitable" |
| 129 |
|- |
| 130 |
! foo |
| 131 |
! bar |
| 132 |
|- |
| 133 |
| a |
| 134 |
| 1 |
| 135 |
|- |
| 136 |
| b |
| 137 |
| 2 |
| 138 |
|- |
| 139 |
| c |
| 140 |
| 2 |
| 141 |
|} |
| 142 |
|
| 143 |
The `template` will be used to format each row via |
| 144 |
`str.format <http://docs.python.org/library/stdtypes.html#str.format>`_. |
| 145 |
|
| 146 |
""" |
| 147 |
|
| 148 |
_writetext(table, source=source, mode='wb', encoding=encoding, |
| 149 |
errors=errors, template=template, prologue=prologue, |
| 150 |
epilogue=epilogue) |
| 151 |
|
| 152 |
|
| 153 |
Table.totext = totext |
| 154 |
|
| 155 |
|
| 156 |
def appendtext(table, source=None, encoding=None, errors='strict', template=None, |
| 157 |
prologue=None, epilogue=None): |
| 158 |
""" |
| 159 |
Append the table to a text file. |
| 160 |
|
| 161 |
""" |
| 162 |
|
| 163 |
_writetext(table, source=source, mode='ab', encoding=encoding, |
| 164 |
errors=errors, template=template, prologue=prologue, |
| 165 |
epilogue=epilogue) |
| 166 |
|
| 167 |
|
| 168 |
Table.appendtext = appendtext |
| 169 |
|
| 170 |
|
| 171 |
def _writetext(table, source, mode, encoding, errors, template, prologue, |
| 172 |
epilogue): |
| 173 |
|
| 174 |
# guard conditions |
| 175 |
assert template is not None, 'template is required' |
| 176 |
|
| 177 |
# prepare source |
| 178 |
source = write_source_from_arg(source, mode) |
| 179 |
|
| 180 |
with source.open(mode) as buf: |
| 181 |
|
| 182 |
# deal with text encoding |
| 183 |
if PY2: |
| 184 |
codec = getcodec(encoding) |
| 185 |
f = codec.streamwriter(buf, errors=errors) |
| 186 |
else: |
| 187 |
f = io.TextIOWrapper(buf, |
| 188 |
encoding=encoding, |
| 189 |
errors=errors, |
| 190 |
newline='') |
| 191 |
|
| 192 |
# write the table |
| 193 |
try: |
| 194 |
if prologue is not None: |
| 195 |
f.write(prologue) |
| 196 |
it = iter(table) |
| 197 |
try: |
| 198 |
hdr = next(it) |
| 199 |
except StopIteration: |
| 200 |
hdr = [] |
| 201 |
flds = list(map(text_type, hdr)) |
| 202 |
for row in it: |
| 203 |
rec = asdict(flds, row) |
| 204 |
s = template.format(**rec) |
| 205 |
f.write(s) |
| 206 |
if epilogue is not None: |
| 207 |
f.write(epilogue) |
| 208 |
f.flush() |
| 209 |
|
| 210 |
finally: |
| 211 |
if not PY2: |
| 212 |
f.detach() |
| 213 |
|
| 214 |
|
| 215 |
def teetext(table, source=None, encoding=None, errors='strict', template=None, |
| 216 |
prologue=None, epilogue=None): |
| 217 |
""" |
| 218 |
Return a table that writes rows to a text file as they are iterated over. |
| 219 |
|
| 220 |
""" |
| 221 |
|
| 222 |
assert template is not None, 'template is required' |
| 223 |
return TeeTextView(table, source=source, encoding=encoding, errors=errors, |
| 224 |
template=template, prologue=prologue, epilogue=epilogue) |
| 225 |
|
| 226 |
|
| 227 |
Table.teetext = teetext |
| 228 |
|
| 229 |
|
| 230 |
class TeeTextView(Table): |
| 231 |
|
| 232 |
def __init__(self, table, source=None, encoding=None, errors='strict', |
| 233 |
template=None, prologue=None, epilogue=None): |
| 234 |
self.table = table |
| 235 |
self.source = source |
| 236 |
self.encoding = encoding |
| 237 |
self.errors = errors |
| 238 |
self.template = template |
| 239 |
self.prologue = prologue |
| 240 |
self.epilogue = epilogue |
| 241 |
|
| 242 |
def __iter__(self): |
| 243 |
return _iterteetext(self.table, self.source, self.encoding, |
| 244 |
self.errors, self.template, self.prologue, |
| 245 |
self.epilogue) |
| 246 |
|
| 247 |
|
| 248 |
def _iterteetext(table, source, encoding, errors, template, prologue, epilogue): |
| 249 |
|
| 250 |
# guard conditions |
| 251 |
assert template is not None, 'template is required' |
| 252 |
|
| 253 |
# prepare source |
| 254 |
source = write_source_from_arg(source) |
| 255 |
|
| 256 |
with source.open('wb') as buf: |
| 257 |
|
| 258 |
# deal with text encoding |
| 259 |
if PY2: |
| 260 |
codec = getcodec(encoding) |
| 261 |
f = codec.streamwriter(buf, errors=errors) |
| 262 |
else: |
| 263 |
f = io.TextIOWrapper(buf, |
| 264 |
encoding=encoding, |
| 265 |
errors=errors) |
| 266 |
|
| 267 |
# write the data |
| 268 |
try: |
| 269 |
if prologue is not None: |
| 270 |
f.write(prologue) |
| 271 |
it = iter(table) |
| 272 |
try: |
| 273 |
hdr = next(it) |
| 274 |
except StopIteration: |
| 275 |
return |
| 276 |
yield tuple(hdr) |
| 277 |
flds = list(map(text_type, hdr)) |
| 278 |
for row in it: |
| 279 |
rec = asdict(flds, row) |
| 280 |
s = template.format(**rec) |
| 281 |
f.write(s) |
| 282 |
yield row |
| 283 |
if epilogue is not None: |
| 284 |
f.write(epilogue) |
| 285 |
f.flush() |
| 286 |
|
| 287 |
finally: |
| 288 |
if not PY2: |
| 289 |
f.detach() |