Skip to content

Commit f0059a4

Browse files
committed
Fix #1: use the same api interface to save files as other pyexcel plugins. Module level save_as and save_to_memory becomes deprecated. Please note: this requires pyexcel v0.1.6, which at the time of commit is not released to pypi. please use pip install git+https://github.com/chfw/pyexcel.git to get it
1 parent 416add4 commit f0059a4

File tree

5 files changed

+250
-40
lines changed

5 files changed

+250
-40
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ python:
1111
- pypy
1212
install:
1313
- pip install -r requirements.txt
14+
- pip install git+https://github.com/chfw/pyexcel-io.git
1415
- pip install git+https://github.com/chfw/pyexcel.git
1516
- pip install -r tests/requirements.txt --use-mirrors
1617
script:

README.rst

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ pyexcel-text
88
.. image:: https://coveralls.io/repos/chfw/pyexcel-text/badge.png?branch=master
99
:target: https://coveralls.io/r/chfw/pyexcel-text?branch=master
1010

11-
.. image:: https://pypip.in/d/pyexcel-text/badge.png
12-
:target: https://pypi.python.org/pypi/pyexcel-text
13-
14-
.. image:: https://pypip.in/py_versions/pyexcel-text/badge.png
15-
:target: https://pypi.python.org/pypi/pyexcel-text
16-
17-
.. image:: https://pypip.in/implementation/pyexcel-text/badge.png
18-
:target: https://pypi.python.org/pypi/pyexcel-text
19-
20-
.. image:: http://img.shields.io/gittip/chfw.svg
21-
:target: https://gratipay.com/chfw/
22-
2311
It is a plugin to `pyexcel <https://github.com/chfw/pyexcel>`__ and extends its capbility to present and write data in text fromats mainly through `tabulate`:
2412

2513
* "plain"
@@ -61,6 +49,18 @@ Usage
6149
1 2 3
6250
4 5 6
6351
7 8 9
52+
>>> text.TABLEFMT = "grid"
53+
>>> sheet
54+
Sheet Name: pyexcel
55+
+------------+------------+------------+
56+
| Column 1 | Column 2 | Column 3 |
57+
+============+============+============+
58+
| 1 | 2 | 3 |
59+
+------------+------------+------------+
60+
| 4 | 5 | 6 |
61+
+------------+------------+------------+
62+
| 7 | 8 | 9 |
63+
+------------+------------+------------+
6464
>>> multiple_sheets = {
6565
... 'Sheet 1':
6666
... [
@@ -82,34 +82,49 @@ Usage
8282
... ]
8383
... }
8484
>>> book = pe.Book(multiple_sheets)
85-
>>> text.TABLEFMT = "rst"
86-
>>> text.save_as(book, "myfile.rst")
87-
>>> myfile = open("myfile.rst")
85+
>>> text.TABLEFMT = "mediawiki"
86+
>>> book.save_as("myfile.mediawiki")
87+
>>> myfile = open("myfile.mediawiki")
8888
>>> print(myfile.read())
8989
Sheet Name: Sheet 1
90-
= = =
91-
1 2 3
92-
4 5 6
93-
7 8 9
94-
= = =
90+
{| class="wikitable" style="text-align: left;"
91+
|+ <!-- caption -->
92+
|-
93+
| align="right"| 1 || align="right"| 2 || align="right"| 3
94+
|-
95+
| align="right"| 4 || align="right"| 5 || align="right"| 6
96+
|-
97+
| align="right"| 7 || align="right"| 8 || align="right"| 9
98+
|}
9599
Sheet Name: Sheet 2
96-
=== === ===
97-
X Y Z
98-
1.0 2.0 3.0
99-
4.0 5.0 6.0
100-
=== === ===
100+
{| class="wikitable" style="text-align: left;"
101+
|+ <!-- caption -->
102+
|-
103+
| X || Y || Z
104+
|-
105+
| 1.0 || 2.0 || 3.0
106+
|-
107+
| 4.0 || 5.0 || 6.0
108+
|}
101109
Sheet Name: Sheet 3
102-
=== === ===
103-
O P Q
104-
3.0 2.0 1.0
105-
4.0 3.0 2.0
106-
=== === ===
110+
{| class="wikitable" style="text-align: left;"
111+
|+ <!-- caption -->
112+
|-
113+
| O || P || Q
114+
|-
115+
| 3.0 || 2.0 || 1.0
116+
|-
117+
| 4.0 || 3.0 || 2.0
118+
|}
119+
<BLANKLINE>
120+
107121

108122
.. testcode::
109123
:hide:
110124

125+
>>> myfile.close()
111126
>>> import os
112-
>>> os.unlink("myfile.rst")
127+
>>> os.unlink("myfile.mediawiki")
113128

114129

115130
Dependencies

pyexcel_text/__init__.py

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
:license: GPL v3
99
"""
1010
from pyexcel.presentation import STRINGIFICATION
11+
from pyexcel_io import BookWriter, SheetWriterBase, is_string, WRITERS
12+
from pyexcel.deprecated import deprecated
13+
from functools import partial
1114

1215

1316
TABLEFMT="simple"
@@ -37,7 +40,8 @@ def present_nominable_sheet(nmsheet_instance):
3740
ret = "Sheet Name: %s\n" % nmsheet_instance.name
3841
if len(nmsheet_instance.colnames) > 0:
3942
data = nmsheet_instance.to_array()
40-
return ret+tabulate.tabulate(data, headers="firstrow")
43+
return ret+tabulate.tabulate(data, headers="firstrow",
44+
tablefmt=TABLEFMT)
4145
else:
4246
return ret+present_matrix(nmsheet_instance)
4347

@@ -54,14 +58,19 @@ def present_book(book_instance):
5458
ret += "\n"
5559
return ret.strip('\n')
5660

57-
61+
@partial(
62+
deprecated,
63+
message="Deprecated since v0.0.3! Please use pyexcel's save_as, save_book_as or instance method Sheet.save_as or Book.save_as")
5864
def save_as(instance, filename):
5965
"""Save a pyexcel instance as text to a file"""
6066
f = open(filename, "w")
6167
f.write(str(instance))
6268
f.close()
6369

6470

71+
@partial(
72+
deprecated,
73+
message="Deprecated since v0.0.3! Please use pyexcel's save_as, save_book_as or instance method Sheet.save_to_memory or Book.save_to_memory")
6574
def save_to_memory(instance, stream):
6675
"""Save a pyexcel instance as text to a stream"""
6776
stream.write(str(instance))
@@ -74,3 +83,88 @@ def save_to_memory(instance, stream):
7483
STRINGIFICATION[class_name("pyexcel.sheets.sheet.Sheet")] = present_nominable_sheet
7584
STRINGIFICATION[class_name("pyexcel.book.Book")] = present_book
7685

86+
87+
class TextSheetWriter(SheetWriterBase):
88+
def __init__(self, filehandle, file_type, name, **keywords):
89+
self.filehandle = filehandle
90+
self.file_type = file_type
91+
self.keywords = keywords
92+
title = "Sheet Name: %s\n" % name
93+
self.filehandle.write(title)
94+
95+
def set_size(self, size):
96+
pass
97+
98+
def write_array(self, table):
99+
import tabulate
100+
if 'single_sheet_in_book' in self.keywords:
101+
self.keywords.pop('single_sheet_in_book')
102+
self.filehandle.write(tabulate.tabulate(table,
103+
tablefmt=self.file_type,
104+
**self.keywords))
105+
106+
def close(self):
107+
self.filehandle.write('\n')
108+
pass
109+
110+
111+
class TextWriter(BookWriter):
112+
def __init__(self, file, **keywords):
113+
BookWriter.__init__(self, file, **keywords)
114+
if is_string(type(file)):
115+
self.f = open(file, 'w')
116+
else:
117+
self.f = file
118+
119+
def create_sheet(self, name):
120+
return TextSheetWriter(
121+
self.f,
122+
self.file_type,
123+
name,
124+
**self.keywords)
125+
126+
def close(self):
127+
if is_string(type(file)):
128+
self.f.close()
129+
130+
131+
class JsonSheetWriter(TextSheetWriter):
132+
def __init__(self, filehandle, name, **keywords):
133+
self.filehandle = filehandle
134+
self.keywords = keywords
135+
136+
def write_array(self, table):
137+
import json
138+
self.filehandle.write(json.dumps(table))
139+
140+
def close(self):
141+
pass
142+
143+
144+
class JsonWriter(TextWriter):
145+
def __init__(self, file, **keywords):
146+
TextWriter.__init__(self, file, **keywords)
147+
148+
def write(self, sheet_dicts):
149+
import json
150+
self.f.write(json.dumps(sheet_dicts))
151+
152+
def create_sheet(self, name):
153+
return JsonSheetWriter(self.f, name)
154+
155+
def close(self):
156+
TextWriter.close(self)
157+
158+
159+
WRITERS.update({
160+
"simple": TextWriter,
161+
"plain": TextWriter,
162+
"grid": TextWriter,
163+
"pipe": TextWriter,
164+
"orgtbl": TextWriter,
165+
"rst": TextWriter,
166+
"mediawiki": TextWriter,
167+
"latex": TextWriter,
168+
"latex_booktabs": TextWriter,
169+
"json": JsonWriter
170+
})

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
setup(
1919
name='pyexcel-text',
2020
author="C. W.",
21-
version='0.0.2',
21+
version='0.0.3',
2222
author_email="[email protected]",
2323
url="https://github.com/chfw/pyexcel-text",
2424
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
2525
include_package_data=True,
2626
install_requires=[
27-
'pyexcel>=0.0.9',
27+
'pyexcel>=0.1.6',
2828
'tabulate'
2929
],
3030
description="It is a plugin to pyexcel and provides the capbility to present and write data in text fromats",

tests/test_io.py

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class TestIO:
1212
def setUp(self):
13-
self.testfile = "testfile.txt"
13+
self.testfile = "testfile.simple"
1414
text.TABLEFMT = "simple"
1515
def test_normal_usage(self):
1616
content = [
@@ -30,8 +30,109 @@ def test_normal_usage(self):
3030
4 588 6
3131
7 8 999
3232
- --- ---""").strip('\n')
33-
print(written_content+"x")
34-
print(content+"x")
33+
assert written_content == content
34+
35+
def test_new_normal_usage(self):
36+
content = [
37+
[1, 2, 3],
38+
[4, 588, 6],
39+
[7, 8, 999]
40+
]
41+
pe.save_as(array=content, dest_file_name=self.testfile)
42+
f = open(self.testfile, "r")
43+
written_content = f.read()
44+
f.close()
45+
content = dedent("""
46+
Sheet Name: pyexcel_sheet1
47+
- --- ---
48+
1 2 3
49+
4 588 6
50+
7 8 999
51+
- --- ---""").strip('\n')
52+
assert written_content.strip('\n') == content
53+
54+
def tearDown(self):
55+
if os.path.exists(self.testfile):
56+
os.unlink(self.testfile)
57+
58+
class TestRst:
59+
def setUp(self):
60+
self.testfile = "testfile.rst"
61+
62+
def test_new_normal_usage(self):
63+
content = [
64+
[1, 2, 3],
65+
[4, 588, 6],
66+
[7, 8, 999]
67+
]
68+
pe.save_as(array=content, dest_file_name=self.testfile)
69+
f = open(self.testfile, "r")
70+
written_content = f.read()
71+
f.close()
72+
content = dedent("""
73+
Sheet Name: pyexcel_sheet1
74+
= === ===
75+
1 2 3
76+
4 588 6
77+
7 8 999
78+
= === ===""").strip('\n')
79+
assert written_content.strip('\n') == content
80+
81+
def test_dict(self):
82+
adict = {
83+
'sheet 1': [[1,2],[3,4]],
84+
'sheet 2': [[5,6],[7,8]]
85+
}
86+
pe.save_book_as(bookdict=adict, dest_file_name=self.testfile)
87+
f = open(self.testfile, "r")
88+
written_content = f.read()
89+
f.close()
90+
content = dedent("""
91+
Sheet Name: sheet 1
92+
= =
93+
1 2
94+
3 4
95+
= =
96+
Sheet Name: sheet 2
97+
= =
98+
5 6
99+
7 8
100+
= =""").strip('\n')
101+
assert written_content.strip('\n') == content
102+
103+
def tearDown(self):
104+
if os.path.exists(self.testfile):
105+
os.unlink(self.testfile)
106+
107+
class TestJSON:
108+
def setUp(self):
109+
self.testfile = "testfile.json"
110+
111+
def test_new_normal_usage(self):
112+
content = [
113+
[1, 2, 3],
114+
[4, 588, 6],
115+
[7, 8, 999]
116+
]
117+
pe.save_as(array=content, dest_file_name=self.testfile)
118+
f = open(self.testfile, "r")
119+
written_content = f.read()
120+
f.close()
121+
content = dedent("""
122+
[[1, 2, 3], [4, 588, 6], [7, 8, 999]]""").strip('\n')
123+
assert written_content == content
124+
125+
def test_dict(self):
126+
adict = {
127+
'sheet 1': [[1,2],[3,4]],
128+
'sheet 2': [[5,6],[7,8]]
129+
}
130+
pe.save_book_as(bookdict=adict, dest_file_name=self.testfile)
131+
f = open(self.testfile, "r")
132+
written_content = f.read()
133+
f.close()
134+
content = dedent("""
135+
{"sheet 1": [[1, 2], [3, 4]], "sheet 2": [[5, 6], [7, 8]]}""").strip('\n')
35136
assert written_content == content
36137

37138
def tearDown(self):
@@ -59,5 +160,4 @@ def test_normal_usage(self):
59160
4 588 6
60161
7 8 999
61162
- --- ---""").strip('\n')
62-
print(written_content)
63-
assert written_content == content
163+
assert written_content == content

0 commit comments

Comments
 (0)