:hammer: make format using isort and black
chfw
5 years ago
8 | 8 | # this line has to be place above all else |
9 | 9 | # because of dynamic import |
10 | 10 | from pyexcel_io.plugins import IOPluginInfoChain |
11 | from pyexcel_io.io import get_data as read_data, isstream, store_data as write_data | |
11 | from pyexcel_io.io import ( | |
12 | get_data as read_data, | |
13 | isstream, | |
14 | store_data as write_data, | |
15 | ) | |
12 | 16 | |
13 | __FILE_TYPE__ = 'ods' | |
17 | __FILE_TYPE__ = "ods" | |
14 | 18 | IOPluginInfoChain(__name__).add_a_reader( |
15 | relative_plugin_class_path='odsr.ODSBook', | |
19 | relative_plugin_class_path="odsr.ODSBook", | |
16 | 20 | file_types=[__FILE_TYPE__], |
17 | stream_type='binary' | |
21 | stream_type="binary", | |
18 | 22 | ).add_a_writer( |
19 | relative_plugin_class_path='odsw.ODSWriter', | |
23 | relative_plugin_class_path="odsw.ODSWriter", | |
20 | 24 | file_types=[__FILE_TYPE__], |
21 | stream_type='binary' | |
25 | stream_type="binary", | |
22 | 26 | ) |
23 | 27 | |
24 | 28 |
20 | 20 | # See the License for the specific language governing permissions and |
21 | 21 | # limitations under the License. |
22 | 22 | |
23 | import pyexcel_io.service as service | |
24 | from odf.namespaces import OFFICENS | |
25 | from odf.opendocument import load | |
26 | from odf.table import Table, TableCell, TableRow | |
27 | ||
23 | 28 | # Thanks to grt for the fixes |
24 | 29 | from odf.teletype import extractText |
25 | from odf.table import TableRow, TableCell, Table | |
26 | 30 | from odf.text import P |
27 | from odf.namespaces import OFFICENS | |
28 | from odf.opendocument import load | |
29 | ||
31 | from pyexcel_io._compact import OrderedDict | |
30 | 32 | from pyexcel_io.book import BookReader |
31 | 33 | from pyexcel_io.sheet import SheetReader |
32 | from pyexcel_io._compact import OrderedDict | |
33 | import pyexcel_io.service as service | |
34 | 34 | |
35 | 35 | |
36 | 36 | class ODSSheet(SheetReader): |
37 | 37 | """native ods sheet""" |
38 | ||
38 | 39 | def __init__(self, sheet, auto_detect_int=True, **keywords): |
39 | 40 | SheetReader.__init__(self, sheet, **keywords) |
40 | 41 | self.__auto_detect_int = auto_detect_int |
69 | 70 | value = cell.getAttrNS(OFFICENS, value_token) |
70 | 71 | currency = cell.getAttrNS(OFFICENS, cell_type) |
71 | 72 | if currency: |
72 | ret = value + ' ' + currency | |
73 | ret = value + " " + currency | |
73 | 74 | else: |
74 | 75 | ret = value |
75 | 76 | else: |
76 | 77 | if cell_type in service.VALUE_CONVERTERS: |
77 | 78 | value = cell.getAttrNS(OFFICENS, value_token) |
78 | 79 | n_value = service.VALUE_CONVERTERS[cell_type](value) |
79 | if cell_type == 'float' and self.__auto_detect_int: | |
80 | if cell_type == "float" and self.__auto_detect_int: | |
80 | 81 | if service.has_no_digits_in_float(n_value): |
81 | 82 | n_value = int(n_value) |
82 | 83 | ret = n_value |
91 | 92 | # for each text node |
92 | 93 | for paragraph in paragraphs: |
93 | 94 | name_space, tag = paragraph.parentNode.qname |
94 | if tag != str('annotation'): | |
95 | if tag != str("annotation"): | |
95 | 96 | data = extractText(paragraph) |
96 | 97 | text_content.append(data) |
97 | return '\n'.join(text_content) | |
98 | return "\n".join(text_content) | |
98 | 99 | |
99 | 100 | |
100 | 101 | class ODSBook(BookReader): |
101 | 102 | """read ods book""" |
103 | ||
102 | 104 | def open(self, file_name, **keywords): |
103 | 105 | """open ods file""" |
104 | 106 | BookReader.open(self, file_name, **keywords) |
112 | 114 | def read_sheet_by_name(self, sheet_name): |
113 | 115 | """read a named sheet""" |
114 | 116 | tables = self._native_book.spreadsheet.getElementsByType(Table) |
115 | rets = [table for table in tables | |
116 | if table.getAttribute('name') == sheet_name] | |
117 | rets = [ | |
118 | table | |
119 | for table in tables | |
120 | if table.getAttribute("name") == sheet_name | |
121 | ] | |
117 | 122 | if len(rets) == 0: |
118 | 123 | raise ValueError("%s cannot be found" % sheet_name) |
119 | 124 | else: |
126 | 131 | if sheet_index < length: |
127 | 132 | return self.read_sheet(tables[sheet_index]) |
128 | 133 | else: |
129 | raise IndexError("Index %d of out bound %d" % ( | |
130 | sheet_index, length)) | |
134 | raise IndexError( | |
135 | "Index %d of out bound %d" % (sheet_index, length) | |
136 | ) | |
131 | 137 | |
132 | 138 | def read_all(self): |
133 | 139 | """read all sheets""" |
8 | 8 | """ |
9 | 9 | import sys |
10 | 10 | |
11 | from odf.table import TableRow, TableCell, Table | |
12 | from odf.text import P | |
11 | import pyexcel_io.service as converter | |
13 | 12 | from odf.namespaces import OFFICENS |
14 | 13 | from odf.opendocument import OpenDocumentSpreadsheet |
15 | ||
14 | from odf.table import Table, TableCell, TableRow | |
15 | from odf.text import P | |
16 | 16 | from pyexcel_io.book import BookWriter |
17 | 17 | from pyexcel_io.sheet import SheetWriter |
18 | ||
19 | import pyexcel_io.service as converter | |
20 | 18 | |
21 | 19 | PY2 = sys.version_info[0] == 2 |
22 | 20 | |
27 | 25 | """ |
28 | 26 | ODS sheet writer |
29 | 27 | """ |
28 | ||
30 | 29 | def set_sheet_name(self, name): |
31 | 30 | """initialize the native table""" |
32 | 31 | self._native_sheet = Table(name=name) |
40 | 39 | cell_to_be_written = TableCell() |
41 | 40 | cell_type = type(cell) |
42 | 41 | cell_odf_type = converter.ODS_WRITE_FORMAT_COVERSION.get( |
43 | cell_type, "string") | |
42 | cell_type, "string" | |
43 | ) | |
44 | 44 | cell_to_be_written.setAttrNS(OFFICENS, "value-type", cell_odf_type) |
45 | 45 | cell_odf_value_token = converter.VALUE_TOKEN.get( |
46 | cell_odf_type, "value") | |
46 | cell_odf_type, "value" | |
47 | ) | |
47 | 48 | converter_func = converter.ODS_VALUE_CONVERTERS.get( |
48 | cell_odf_type, None) | |
49 | cell_odf_type, None | |
50 | ) | |
49 | 51 | if converter_func: |
50 | 52 | cell = converter_func(cell) |
51 | if cell_odf_type != 'string': | |
53 | if cell_odf_type != "string": | |
52 | 54 | cell_to_be_written.setAttrNS(OFFICENS, cell_odf_value_token, cell) |
53 | 55 | cell_to_be_written.addElement(P(text=cell)) |
54 | 56 | else: |
55 | lines = cell.split('\n') | |
57 | lines = cell.split("\n") | |
56 | 58 | for line in lines: |
57 | 59 | cell_to_be_written.addElement(P(text=line)) |
58 | 60 | row.addElement(cell_to_be_written) |
79 | 81 | open document spreadsheet writer |
80 | 82 | |
81 | 83 | """ |
84 | ||
82 | 85 | def __init__(self): |
83 | 86 | BookWriter.__init__(self) |
84 | 87 | self._native_book = OpenDocumentSpreadsheet() |
0 | 0 | #!/usr/bin/env python3 |
1 | 1 | |
2 | import codecs | |
2 | 3 | # Template by pypi-mobans |
3 | 4 | import os |
4 | 5 | import sys |
5 | import codecs | |
6 | 6 | from shutil import rmtree |
7 | 7 | |
8 | from setuptools import Command, setup, find_packages | |
9 | ||
8 | from setuptools import Command, find_packages, setup | |
10 | 9 | |
11 | 10 | NAME = 'pyexcel-ods' |
12 | 11 | AUTHOR = 'C.W.' |
0 | import datetime # noqa | |
0 | 1 | import os # noqa |
2 | ||
1 | 3 | import pyexcel |
2 | import datetime # noqa | |
3 | from nose.tools import raises, eq_ # noqa | |
4 | from nose.tools import eq_, raises # noqa | |
4 | 5 | |
5 | 6 | |
6 | 7 | def create_sample_file1(file): |
7 | data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1.1, 1] | |
8 | data = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1.1, 1] | |
8 | 9 | table = [] |
9 | 10 | table.append(data[:4]) |
10 | 11 | table.append(data[4:8]) |
16 | 17 | """ |
17 | 18 | Abstract functional test for hat writers |
18 | 19 | """ |
20 | ||
19 | 21 | content = { |
20 | 22 | "X": [1, 2, 3, 4, 5], |
21 | 23 | "Y": [6, 7, 8, 9, 10], |
22 | "Z": [11, 12, 13, 14, 15] | |
24 | "Z": [11, 12, 13, 14, 15], | |
23 | 25 | } |
24 | 26 | |
25 | 27 | def test_series_table(self): |
35 | 37 | testfile and testfile2 have to be initialized before |
36 | 38 | it is used for testing |
37 | 39 | """ |
40 | ||
38 | 41 | content = [ |
39 | 42 | [1, 2, 3, 4, 5], |
40 | 43 | [1, 2, 3, 4, 5], |
41 | 44 | [1, 2, 3, 4, 5], |
42 | [1, 2, 3, 4, 5] | |
45 | [1, 2, 3, 4, 5], | |
43 | 46 | ] |
44 | 47 | |
45 | 48 | def _create_a_file(self, file): |
53 | 56 | |
54 | 57 | |
55 | 58 | class PyexcelMultipleSheetBase: |
56 | ||
57 | 59 | def _write_test_file(self, filename): |
58 | 60 | pyexcel.save_book_as(bookdict=self.content, dest_file_name=filename) |
59 | 61 | |
79 | 81 | expected = [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]] |
80 | 82 | assert data == expected |
81 | 83 | data = list(b["Sheet3"].rows()) |
82 | expected = [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]] | |
84 | expected = [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]] | |
83 | 85 | assert data == expected |
84 | 86 | sheet3 = b["Sheet3"] |
85 | 87 | sheet3.name_columns_by_row(0) |
89 | 91 | |
90 | 92 | |
91 | 93 | class ODSCellTypes: |
92 | ||
93 | 94 | def test_formats(self): |
94 | 95 | # date formats |
95 | 96 | date_format = "%d/%m/%Y" |
103 | 104 | eq_(self.data["Sheet1"][1][1].strftime(time_format), "12:12:11") |
104 | 105 | eq_(self.data["Sheet1"][2][1].strftime(time_format), "12:00:00") |
105 | 106 | eq_(self.data["Sheet1"][3][1], 0) |
106 | eq_(self.data["Sheet1"][4][1], datetime.timedelta(hours=27, | |
107 | minutes=17, | |
108 | seconds=54)) | |
107 | eq_( | |
108 | self.data["Sheet1"][4][1], | |
109 | datetime.timedelta(hours=27, minutes=17, seconds=54), | |
110 | ) | |
109 | 111 | eq_(self.data["Sheet1"][5][1], "Other") |
110 | 112 | # boolean |
111 | 113 | eq_(self.data["Sheet1"][0][2], "Boolean") |
116 | 118 | eq_(self.data["Sheet1"][1][3], 11.11) |
117 | 119 | # Currency |
118 | 120 | eq_(self.data["Sheet1"][0][4], "Currency") |
119 | eq_(self.data["Sheet1"][1][4], '1 GBP') | |
120 | eq_(self.data["Sheet1"][2][4], '-10000 GBP') | |
121 | eq_(self.data["Sheet1"][1][4], "1 GBP") | |
122 | eq_(self.data["Sheet1"][2][4], "-10000 GBP") | |
121 | 123 | # Percentage |
122 | 124 | eq_(self.data["Sheet1"][0][5], "Percentage") |
123 | 125 | eq_(self.data["Sheet1"][1][5], 2) |
0 | 0 | #!/usr/bin/python |
1 | 1 | # -*- encoding: utf-8 -*- |
2 | 2 | import os |
3 | ||
3 | 4 | import psutil |
4 | 5 | import pyexcel as pe |
6 | from nose import SkipTest | |
7 | from nose.tools import eq_, raises | |
8 | from pyexcel_io.exceptions import IntegerAccuracyLossError | |
9 | ||
5 | 10 | from pyexcel_ods import get_data, save_data |
6 | from pyexcel_io.exceptions import IntegerAccuracyLossError | |
7 | from nose.tools import raises, eq_ | |
8 | from nose import SkipTest | |
9 | 11 | |
10 | IN_TRAVIS = 'TRAVIS' in os.environ | |
12 | IN_TRAVIS = "TRAVIS" in os.environ | |
11 | 13 | |
12 | 14 | |
13 | 15 | def test_bug_fix_for_issue_1(): |
14 | 16 | data = get_data(get_fixtures("repeated.ods")) |
15 | eq_(data["Sheet1"], [['repeated', 'repeated', 'repeated', 'repeated']]) | |
17 | eq_(data["Sheet1"], [["repeated", "repeated", "repeated", "repeated"]]) | |
16 | 18 | |
17 | 19 | |
18 | 20 | def test_bug_fix_for_issue_2(): |
21 | 23 | data.update({"Sheet 2": [[u"row 1", u"Héllô!", u"HolÁ!"]]}) |
22 | 24 | save_data("your_file.ods", data) |
23 | 25 | new_data = get_data("your_file.ods") |
24 | assert new_data["Sheet 2"] == [[u'row 1', u'H\xe9ll\xf4!', u'Hol\xc1!']] | |
26 | assert new_data["Sheet 2"] == [[u"row 1", u"H\xe9ll\xf4!", u"Hol\xc1!"]] | |
25 | 27 | |
26 | 28 | |
27 | 29 | @raises(Exception) |
28 | 30 | def test_invalid_date(): |
29 | 31 | from pyexcel_ods.ods import date_value |
32 | ||
30 | 33 | value = "2015-08-" |
31 | 34 | date_value(value) |
32 | 35 | |
34 | 37 | @raises(Exception) |
35 | 38 | def test_fake_date_time_10(): |
36 | 39 | from pyexcel_ods.ods import date_value |
40 | ||
37 | 41 | date_value("1234567890") |
38 | 42 | |
39 | 43 | |
40 | 44 | @raises(Exception) |
41 | 45 | def test_fake_date_time_19(): |
42 | 46 | from pyexcel_ods.ods import date_value |
47 | ||
43 | 48 | date_value("1234567890123456789") |
44 | 49 | |
45 | 50 | |
46 | 51 | @raises(Exception) |
47 | 52 | def test_fake_date_time_20(): |
48 | 53 | from pyexcel_ods.ods import date_value |
54 | ||
49 | 55 | date_value("12345678901234567890") |
50 | 56 | |
51 | 57 | |
52 | 58 | def test_issue_13(): |
53 | 59 | test_file = "test_issue_13.ods" |
54 | data = [ | |
55 | [1, 2], | |
56 | [], | |
57 | [], | |
58 | [], | |
59 | [3, 4] | |
60 | ] | |
60 | data = [[1, 2], [], [], [], [3, 4]] | |
61 | 61 | save_data(test_file, {test_file: data}) |
62 | 62 | written_data = get_data(test_file, skip_empty_rows=False) |
63 | 63 | eq_(data, written_data[test_file]) |
67 | 67 | def test_issue_14(): |
68 | 68 | # pyexcel issue 61 |
69 | 69 | test_file = "issue_61.ods" |
70 | data = get_data(get_fixtures(test_file), | |
71 | skip_empty_rows=True) | |
72 | eq_(data['S-LMC'], [[u'aaa'], [0]]) | |
70 | data = get_data(get_fixtures(test_file), skip_empty_rows=True) | |
71 | eq_(data["S-LMC"], [[u"aaa"], [0]]) | |
73 | 72 | |
74 | 73 | |
75 | 74 | def test_issue_6(): |
76 | 75 | test_file = "12_day_as_time.ods" |
77 | data = get_data(get_fixtures(test_file), | |
78 | skip_empty_rows=True) | |
79 | eq_(data['Sheet1'][0][0].days, 12) | |
76 | data = get_data(get_fixtures(test_file), skip_empty_rows=True) | |
77 | eq_(data["Sheet1"][0][0].days, 12) | |
80 | 78 | |
81 | 79 | |
82 | 80 | def test_issue_19(): |
83 | 81 | test_file = "pyexcel_81_ods_19.ods" |
84 | data = get_data(get_fixtures(test_file), | |
85 | skip_empty_rows=True) | |
86 | eq_(data['product.template'][1][1], 'PRODUCT NAME PMP') | |
82 | data = get_data(get_fixtures(test_file), skip_empty_rows=True) | |
83 | eq_(data["product.template"][1][1], "PRODUCT NAME PMP") | |
87 | 84 | |
88 | 85 | |
89 | 86 | def test_issue_83_ods_file_handle(): |
94 | 91 | open_files_l1 = proc.open_files() |
95 | 92 | |
96 | 93 | # start with a csv file |
97 | data = pe.iget_array(file_name=test_file, library='pyexcel-ods') | |
94 | data = pe.iget_array(file_name=test_file, library="pyexcel-ods") | |
98 | 95 | open_files_l2 = proc.open_files() |
99 | 96 | delta = len(open_files_l2) - len(open_files_l1) |
100 | 97 | # cannot catch open file handle |
119 | 116 | test_file = get_fixtures("white_space.ods") |
120 | 117 | data = get_data(test_file) |
121 | 118 | # OrderedDict([(u'Sheet1', [[u'paragraph with tab, space, new line']])]) |
122 | eq_(data['Sheet1'][0][0], 'paragraph with tab(\t), space, \nnew line') | |
119 | eq_(data["Sheet1"][0][0], "paragraph with tab(\t), space, \nnew line") | |
123 | 120 | |
124 | 121 | |
125 | 122 | def test_issue_23(): |
126 | 123 | if not IN_TRAVIS: |
127 | 124 | raise SkipTest() |
128 | pe.get_book(url="https://github.com/pyexcel/pyexcel-ods/raw/master/tests/fixtures/white_space.ods") # noqa: E501 | |
125 | pe.get_book( | |
126 | url="https://github.com/pyexcel/pyexcel-ods/raw/master/tests/fixtures/white_space.ods" | |
127 | ) # noqa: E501 | |
129 | 128 | |
130 | 129 | |
131 | 130 | def test_issue_24(): |
132 | 131 | test_file = get_fixtures("comment-in-cell.ods") |
133 | 132 | data = get_data(test_file) |
134 | eq_(data['Sheet1'], [['test']]) | |
133 | eq_(data["Sheet1"], [["test"]]) | |
135 | 134 | |
136 | 135 | |
137 | 136 | def test_issue_27(): |
138 | 137 | test_file = get_fixtures("issue_27.ods") |
139 | 138 | data = get_data(test_file, skip_empty_rows=True) |
140 | eq_(data['VGPMX'], [['', 'Cost Basis', '0']]) | |
139 | eq_(data["VGPMX"], [["", "Cost Basis", "0"]]) | |
141 | 140 | |
142 | 141 | |
143 | 142 | def test_issue_30(): |
0 | 0 | import os |
1 | 1 | |
2 | from nose.tools import eq_ | |
2 | 3 | from pyexcel_io import get_data, save_data |
3 | from nose.tools import eq_ | |
4 | 4 | |
5 | 5 | |
6 | 6 | class TestFilter: |
12 | 12 | [3, 23, 33], |
13 | 13 | [4, 24, 34], |
14 | 14 | [5, 25, 35], |
15 | [6, 26, 36] | |
15 | [6, 26, 36], | |
16 | 16 | ] |
17 | 17 | save_data(self.test_file, sample) |
18 | 18 | self.sheet_name = "pyexcel_sheet1" |
19 | 19 | |
20 | 20 | def test_filter_row(self): |
21 | filtered_data = get_data(self.test_file, start_row=3, | |
22 | library="pyexcel-ods") | |
21 | filtered_data = get_data( | |
22 | self.test_file, start_row=3, library="pyexcel-ods" | |
23 | ) | |
23 | 24 | expected = [[4, 24, 34], [5, 25, 35], [6, 26, 36]] |
24 | 25 | eq_(filtered_data[self.sheet_name], expected) |
25 | 26 | |
26 | 27 | def test_filter_row_2(self): |
27 | filtered_data = get_data(self.test_file, start_row=3, row_limit=1, | |
28 | library="pyexcel-ods") | |
28 | filtered_data = get_data( | |
29 | self.test_file, start_row=3, row_limit=1, library="pyexcel-ods" | |
30 | ) | |
29 | 31 | expected = [[4, 24, 34]] |
30 | 32 | eq_(filtered_data[self.sheet_name], expected) |
31 | 33 | |
32 | 34 | def test_filter_column(self): |
33 | filtered_data = get_data(self.test_file, start_column=1, | |
34 | library="pyexcel-ods") | |
35 | expected = [[21, 31], [22, 32], [23, 33], | |
36 | [24, 34], [25, 35], [26, 36]] | |
35 | filtered_data = get_data( | |
36 | self.test_file, start_column=1, library="pyexcel-ods" | |
37 | ) | |
38 | expected = [[21, 31], [22, 32], [23, 33], [24, 34], [25, 35], [26, 36]] | |
37 | 39 | eq_(filtered_data[self.sheet_name], expected) |
38 | 40 | |
39 | 41 | def test_filter_column_2(self): |
40 | filtered_data = get_data(self.test_file, | |
41 | start_column=1, column_limit=1, | |
42 | library="pyexcel-ods") | |
42 | filtered_data = get_data( | |
43 | self.test_file, | |
44 | start_column=1, | |
45 | column_limit=1, | |
46 | library="pyexcel-ods", | |
47 | ) | |
43 | 48 | expected = [[21], [22], [23], [24], [25], [26]] |
44 | 49 | eq_(filtered_data[self.sheet_name], expected) |
45 | 50 | |
46 | 51 | def test_filter_both_ways(self): |
47 | filtered_data = get_data(self.test_file, | |
48 | start_column=1, start_row=3, | |
49 | library="pyexcel-ods") | |
52 | filtered_data = get_data( | |
53 | self.test_file, start_column=1, start_row=3, library="pyexcel-ods" | |
54 | ) | |
50 | 55 | expected = [[24, 34], [25, 35], [26, 36]] |
51 | 56 | eq_(filtered_data[self.sheet_name], expected) |
52 | 57 | |
53 | 58 | def test_filter_both_ways_2(self): |
54 | filtered_data = get_data(self.test_file, | |
55 | start_column=1, column_limit=1, | |
56 | start_row=3, row_limit=1, | |
57 | library="pyexcel-ods") | |
59 | filtered_data = get_data( | |
60 | self.test_file, | |
61 | start_column=1, | |
62 | column_limit=1, | |
63 | start_row=3, | |
64 | row_limit=1, | |
65 | library="pyexcel-ods", | |
66 | ) | |
58 | 67 | expected = [[24]] |
59 | 68 | eq_(filtered_data[self.sheet_name], expected) |
60 | 69 |
0 | 0 | import os |
1 | 1 | from textwrap import dedent |
2 | from nose.tools import eq_ | |
3 | 2 | |
4 | 3 | import pyexcel as pe |
4 | from nose.tools import eq_ | |
5 | 5 | |
6 | 6 | |
7 | 7 | class TestAutoDetectInt: |
8 | 8 | def setUp(self): |
9 | 9 | self.content = [[1, 2, 3.1]] |
10 | 10 | self.test_file = "test_auto_detect_init.ods" |
11 | pe.save_as( | |
12 | array=self.content, dest_file_name=self.test_file | |
13 | ) | |
11 | pe.save_as(array=self.content, dest_file_name=self.test_file) | |
14 | 12 | |
15 | 13 | def test_auto_detect_int(self): |
16 | 14 | sheet = pe.get_sheet(file_name=self.test_file, library="pyexcel-ods") |
17 | expected = dedent(""" | |
15 | expected = dedent( | |
16 | """ | |
18 | 17 | pyexcel_sheet1: |
19 | 18 | +---+---+-----+ |
20 | 19 | | 1 | 2 | 3.1 | |
21 | +---+---+-----+""").strip() | |
20 | +---+---+-----+""" | |
21 | ).strip() | |
22 | 22 | eq_(str(sheet), expected) |
23 | 23 | |
24 | 24 | def test_get_book_auto_detect_int(self): |
25 | 25 | book = pe.get_book(file_name=self.test_file, library="pyexcel-ods") |
26 | expected = dedent(""" | |
26 | expected = dedent( | |
27 | """ | |
27 | 28 | pyexcel_sheet1: |
28 | 29 | +---+---+-----+ |
29 | 30 | | 1 | 2 | 3.1 | |
30 | +---+---+-----+""").strip() | |
31 | +---+---+-----+""" | |
32 | ).strip() | |
31 | 33 | eq_(str(book), expected) |
32 | 34 | |
33 | 35 | def test_auto_detect_int_false(self): |
34 | sheet = pe.get_sheet(file_name=self.test_file, auto_detect_int=False, | |
35 | library="pyexcel-ods") | |
36 | expected = dedent(""" | |
36 | sheet = pe.get_sheet( | |
37 | file_name=self.test_file, | |
38 | auto_detect_int=False, | |
39 | library="pyexcel-ods", | |
40 | ) | |
41 | expected = dedent( | |
42 | """ | |
37 | 43 | pyexcel_sheet1: |
38 | 44 | +-----+-----+-----+ |
39 | 45 | | 1.0 | 2.0 | 3.1 | |
40 | +-----+-----+-----+""").strip() | |
46 | +-----+-----+-----+""" | |
47 | ).strip() | |
41 | 48 | eq_(str(sheet), expected) |
42 | 49 | |
43 | 50 | def test_get_book_auto_detect_int_false(self): |
44 | book = pe.get_book(file_name=self.test_file, auto_detect_int=False, | |
45 | library="pyexcel-ods") | |
46 | expected = dedent(""" | |
51 | book = pe.get_book( | |
52 | file_name=self.test_file, | |
53 | auto_detect_int=False, | |
54 | library="pyexcel-ods", | |
55 | ) | |
56 | expected = dedent( | |
57 | """ | |
47 | 58 | pyexcel_sheet1: |
48 | 59 | +-----+-----+-----+ |
49 | 60 | | 1.0 | 2.0 | 3.1 | |
50 | +-----+-----+-----+""").strip() | |
61 | +-----+-----+-----+""" | |
62 | ).strip() | |
51 | 63 | eq_(str(book), expected) |
52 | 64 | |
53 | 65 | def tearDown(self): |
0 | 0 | import os |
1 | ||
1 | 2 | import pyexcel |
2 | 3 | |
3 | 4 | |
4 | 5 | def test_reading_multiline_ods(): |
5 | 6 | testfile = os.path.join("tests", "fixtures", "multilineods.ods") |
6 | 7 | sheet = pyexcel.get_sheet(file_name=testfile) |
7 | assert sheet[0, 0] == '1\n2\n3\n4' | |
8 | assert sheet[1, 0] == 'Line 1\n\nLine 2' | |
8 | assert sheet[0, 0] == "1\n2\n3\n4" | |
9 | assert sheet[1, 0] == "Line 1\n\nLine 2" | |
9 | 10 | |
10 | 11 | |
11 | 12 | def test_writing_multiline_ods(): |
0 | 0 | import os |
1 | 1 | import sys |
2 | ||
2 | 3 | import pyexcel |
3 | 4 | from nose.tools import raises |
5 | ||
4 | 6 | from base import PyexcelMultipleSheetBase |
5 | 7 | |
6 | 8 | if sys.version_info[0] == 2 and sys.version_info[1] < 7: |
41 | 43 | 3,3,3,3 |
42 | 44 | """ |
43 | 45 | self.rows = 3 |
44 | pyexcel.save_book_as(bookdict=self.content, | |
45 | dest_file_name=file) | |
46 | pyexcel.save_book_as(bookdict=self.content, dest_file_name=file) | |
46 | 47 | |
47 | 48 | def setUp(self): |
48 | 49 | self.testfile = "multiple1.ods" |
54 | 55 | def test_load_a_single_sheet(self): |
55 | 56 | b1 = pyexcel.get_book(file_name=self.testfile, sheet_name="Sheet1") |
56 | 57 | assert len(b1.sheet_names()) == 1 |
57 | assert b1['Sheet1'].to_array() == self.content['Sheet1'] | |
58 | assert b1["Sheet1"].to_array() == self.content["Sheet1"] | |
58 | 59 | |
59 | 60 | def test_load_a_single_sheet2(self): |
60 | 61 | b1 = pyexcel.load_book(self.testfile, sheet_index=0) |
61 | 62 | assert len(b1.sheet_names()) == 1 |
62 | assert b1['Sheet1'].to_array() == self.content['Sheet1'] | |
63 | assert b1["Sheet1"].to_array() == self.content["Sheet1"] | |
63 | 64 | |
64 | 65 | @raises(IndexError) |
65 | 66 | def test_load_a_single_sheet3(self): |
228 | 229 | self.testfile = "file_with_an_empty_sheet.ods" |
229 | 230 | |
230 | 231 | def test_reader_with_correct_sheets(self): |
231 | r = pyexcel.BookReader(os.path.join("tests", "fixtures", | |
232 | self.testfile)) | |
232 | r = pyexcel.BookReader( | |
233 | os.path.join("tests", "fixtures", self.testfile) | |
234 | ) | |
233 | 235 | assert r.number_of_sheets() == 3 |
234 | 236 | |
235 | 237 | |
236 | 238 | def _produce_ordered_dict(): |
237 | 239 | data_dict = OrderedDict() |
238 | data_dict.update({ | |
239 | "Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]}) | |
240 | data_dict.update({ | |
241 | "Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]}) | |
242 | data_dict.update({ | |
243 | "Sheet3": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) | |
240 | data_dict.update({"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]}) | |
241 | data_dict.update({"Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]}) | |
242 | data_dict.update( | |
243 | {"Sheet3": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]} | |
244 | ) | |
244 | 245 | return data_dict |
0 | 0 | import os |
1 | ||
2 | from base import ODSCellTypes | |
1 | 3 | from pyexcel_ods.odsr import ODSBook |
2 | 4 | from pyexcel_ods.odsw import ODSWriter |
3 | from base import ODSCellTypes | |
4 | 5 | |
5 | 6 | |
6 | 7 | class TestODSReader(ODSCellTypes): |
7 | 8 | def setUp(self): |
8 | 9 | r = ODSBook() |
9 | r.open(os.path.join("tests", | |
10 | "fixtures", | |
11 | "ods_formats.ods")) | |
10 | r.open(os.path.join("tests", "fixtures", "ods_formats.ods")) | |
12 | 11 | self.data = r.read_all() |
13 | 12 | for key in self.data.keys(): |
14 | 13 | self.data[key] = list(self.data[key]) |
18 | 17 | class TestODSWriter(ODSCellTypes): |
19 | 18 | def setUp(self): |
20 | 19 | r = ODSBook() |
21 | r.open(os.path.join("tests", | |
22 | "fixtures", | |
23 | "ods_formats.ods")) | |
20 | r.open(os.path.join("tests", "fixtures", "ods_formats.ods")) | |
24 | 21 | self.data1 = r.read_all() |
25 | 22 | self.testfile = "odswriter.ods" |
26 | 23 | w = ODSWriter() |
0 | 0 | import os |
1 | ||
1 | 2 | import pyexcel |
2 | 3 | from nose.tools import eq_ |
4 | ||
3 | 5 | from base import create_sample_file1 |
4 | 6 | |
5 | 7 | |
6 | 8 | class TestStringIO: |
7 | ||
8 | 9 | def test_ods_stringio(self): |
9 | 10 | testfile = "cute.ods" |
10 | 11 | create_sample_file1(testfile) |
11 | 12 | with open(testfile, "rb") as f: |
12 | 13 | content = f.read() |
13 | r = pyexcel.get_sheet(file_type="ods", file_content=content, | |
14 | library="pyexcel-ods") | |
15 | result = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1.1, 1] | |
14 | r = pyexcel.get_sheet( | |
15 | file_type="ods", file_content=content, library="pyexcel-ods" | |
16 | ) | |
17 | result = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1.1, 1] | |
16 | 18 | actual = list(r.enumerate()) |
17 | 19 | eq_(result, actual) |
18 | 20 | if os.path.exists(testfile): |
19 | 21 | os.unlink(testfile) |
20 | 22 | |
21 | 23 | def test_ods_output_stringio(self): |
22 | data = [ | |
23 | [1, 2, 3], | |
24 | [4, 5, 6] | |
25 | ] | |
26 | io = pyexcel.save_as(dest_file_type="ods", | |
27 | array=data) | |
28 | r = pyexcel.get_sheet(file_type="ods", file_content=io.getvalue(), | |
29 | library="pyexcel-ods") | |
24 | data = [[1, 2, 3], [4, 5, 6]] | |
25 | io = pyexcel.save_as(dest_file_type="ods", array=data) | |
26 | r = pyexcel.get_sheet( | |
27 | file_type="ods", file_content=io.getvalue(), library="pyexcel-ods" | |
28 | ) | |
30 | 29 | result = [1, 2, 3, 4, 5, 6] |
31 | 30 | actual = list(r.enumerate()) |
32 | 31 | eq_(result, actual) |
0 | 0 | import os |
1 | ||
2 | from base import PyexcelHatWriterBase, PyexcelWriterBase | |
3 | from pyexcel_ods.odsr import ODSBook as Reader | |
1 | 4 | from pyexcel_ods.odsw import ODSWriter as Writer |
2 | from pyexcel_ods.odsr import ODSBook as Reader | |
3 | from base import PyexcelWriterBase, PyexcelHatWriterBase | |
4 | 5 | |
5 | 6 | |
6 | 7 | class TestNativeODSWriter: |
8 | 9 | self.content = { |
9 | 10 | "Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], |
10 | 11 | "Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]], |
11 | "Sheet3": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]] | |
12 | "Sheet3": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]], | |
12 | 13 | } |
13 | 14 | self.testfile = "writer.ods" |
14 | 15 | writer = Writer() |