Skip to content

Commit 71fc59a

Browse files
committed
🚜 rename all python files. p for parser, r for renderer, both are pyexcel plugin interfaces. for everyone's information, pyexcel-io had these: r for reader, w for writer.
1 parent 5c6fd9d commit 71fc59a

File tree

8 files changed

+115
-115
lines changed

8 files changed

+115
-115
lines changed

pyexcel_text/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
PyexcelPluginChain(__name__).add_a_renderer(
14-
relative_plugin_class_path='_text.Tabulater',
14+
relative_plugin_class_path='textr.Tabulater',
1515
file_types=[
1616
'html',
1717
'simple',
@@ -26,16 +26,16 @@
2626
],
2727
stream_type='string'
2828
).add_a_renderer(
29-
relative_plugin_class_path='_json.Jsonifier',
29+
relative_plugin_class_path='jsonr.Jsonifier',
3030
file_types=['json'],
3131
stream_type='string'
3232
).add_a_parser(
33-
relative_plugin_class_path='jsonr.JsonParser',
33+
relative_plugin_class_path='jsonp.JsonParser',
3434
file_types=[
3535
'json'
3636
]
3737
).add_a_parser(
38-
relative_plugin_class_path='ndjsonr.NDJsonParser',
38+
relative_plugin_class_path='ndjsonp.NDJsonParser',
3939
file_types=[
4040
'ndjson'
4141
]

pyexcel_text/_json.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

pyexcel_text/jsonp.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
pyexcel_text.jsonr
3+
~~~~~~~~~~~~~~~~~~~
4+
5+
Render json input
6+
7+
:copyright: (c) 2014-2016 by C. W.
8+
:license: New BSD
9+
"""
10+
import pyexcel.constants as constants
11+
from pyexcel.parser import AbstractParser
12+
from pyexcel.plugins.sources.pydata.common import (
13+
ArrayReader, RecordsReader, DictReader)
14+
from pyexcel.plugins.sources.pydata.bookdict import BookDictSource
15+
import json
16+
17+
18+
class JsonParser(AbstractParser):
19+
def parse_file(self, file_name, **keywords):
20+
with open(file_name, 'r') as f:
21+
content = json.load(f)
22+
return as_a_dict_of_2_dimensional_array(content, **keywords)
23+
24+
def parse_file_stream(self, file_stream, **keywords):
25+
content = json.load(file_stream)
26+
return as_a_dict_of_2_dimensional_array(content, **keywords)
27+
28+
def parse_file_content(self, file_content, **keywords):
29+
content = json.loads(file_content)
30+
return as_a_dict_of_2_dimensional_array(content, **keywords)
31+
32+
33+
def as_a_dict_of_2_dimensional_array(
34+
content, sheet_name=constants.DEFAULT_NAME,
35+
**keywords):
36+
if isinstance(content, list):
37+
if isinstance(content[0], list):
38+
array_reader = ArrayReader(content, **keywords)
39+
return {sheet_name: array_reader.to_array()}
40+
elif isinstance(content[0], dict):
41+
records_reader = RecordsReader(content, **keywords)
42+
return {sheet_name: records_reader.to_array()}
43+
else:
44+
raise ValueError("Unknow file format")
45+
elif isinstance(content, dict):
46+
try:
47+
keys = list(content.keys())
48+
first_item = content.get(keys[0])[0]
49+
except Exception as e:
50+
print(e)
51+
raise
52+
if isinstance(first_item, list):
53+
bookdict = BookDictSource(content, **keywords)
54+
return bookdict.get_data()
55+
else:
56+
dict_reader = DictReader(content, **keywords)
57+
return {sheet_name: dict_reader.to_array()}

pyexcel_text/jsonr.py

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
11
"""
2-
pyexcel_text.jsonr
2+
pyexcel_text.json
33
~~~~~~~~~~~~~~~~~~~
44
5-
Render json input
5+
Provide json output
66
77
:copyright: (c) 2014-2016 by C. W.
88
:license: New BSD
99
"""
10-
import pyexcel.constants as constants
11-
from pyexcel.parser import AbstractParser
12-
from pyexcel.plugins.sources.pydata.common import (
13-
ArrayReader, RecordsReader, DictReader)
14-
from pyexcel.plugins.sources.pydata.bookdict import BookDictSource
1510
import json
16-
17-
18-
class JsonParser(AbstractParser):
19-
def parse_file(self, file_name, **keywords):
20-
with open(file_name, 'r') as f:
21-
content = json.load(f)
22-
return as_a_dict_of_2_dimensional_array(content, **keywords)
23-
24-
def parse_file_stream(self, file_stream, **keywords):
25-
content = json.load(file_stream)
26-
return as_a_dict_of_2_dimensional_array(content, **keywords)
27-
28-
def parse_file_content(self, file_content, **keywords):
29-
content = json.loads(file_content)
30-
return as_a_dict_of_2_dimensional_array(content, **keywords)
31-
32-
33-
def as_a_dict_of_2_dimensional_array(
34-
content, sheet_name=constants.DEFAULT_NAME,
35-
**keywords):
36-
if isinstance(content, list):
37-
if isinstance(content[0], list):
38-
array_reader = ArrayReader(content, **keywords)
39-
return {sheet_name: array_reader.to_array()}
40-
elif isinstance(content[0], dict):
41-
records_reader = RecordsReader(content, **keywords)
42-
return {sheet_name: records_reader.to_array()}
43-
else:
44-
raise ValueError("Unknow file format")
45-
elif isinstance(content, dict):
46-
try:
47-
keys = list(content.keys())
48-
first_item = content.get(keys[0])[0]
49-
except Exception as e:
50-
print(e)
51-
raise
52-
if isinstance(first_item, list):
53-
bookdict = BookDictSource(content, **keywords)
54-
return bookdict.get_data()
55-
else:
56-
dict_reader = DictReader(content, **keywords)
57-
return {sheet_name: dict_reader.to_array()}
11+
import datetime
12+
from pyexcel.renderer import Renderer
13+
14+
15+
class Jsonifier(Renderer):
16+
17+
def render_sheet(self, sheet):
18+
content = jsonify(sheet, self._file_type, self._write_title)
19+
self._stream.write(content)
20+
21+
def render_book(self, book):
22+
content = jsonify_book(book, self._file_type)
23+
self._stream.write(content)
24+
25+
26+
def jsonify(sheet, file_type, write_title):
27+
content = ""
28+
table = sheet.to_array()
29+
if hasattr(sheet, 'rownames'):
30+
colnames = sheet.colnames
31+
rownames = sheet.rownames
32+
# In the following, row[0] is the name of each row
33+
if colnames and rownames:
34+
table = dict((row[0], dict(zip(colnames, row[1:])))
35+
for row in table[1:])
36+
elif colnames:
37+
table = sheet.to_records()
38+
elif rownames:
39+
table = sheet.to_records()
40+
else:
41+
table = list(table)
42+
if write_title:
43+
content = {sheet.name: table}
44+
else:
45+
content = table
46+
return json.dumps(content, sort_keys=True, default=_serializer)
47+
48+
49+
def jsonify_book(book, file_type):
50+
return json.dumps(book.to_dict(), sort_keys=True,
51+
default=_serializer)
52+
53+
54+
def _serializer(obj):
55+
if isinstance(obj, datetime.datetime):
56+
return obj.strftime('%Y-%m-%d %H:%M:%S.%f')
57+
elif isinstance(obj, datetime.date):
58+
return obj.strftime('%Y-%m-%d')
59+
return str(obj)
File renamed without changes.
File renamed without changes.

tests/test_jsonr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from nose.tools import eq_
33
from pyexcel._compact import StringIO
4-
from pyexcel_text.jsonr import JsonParser
4+
from pyexcel_text.jsonp import JsonParser
55

66

77
class TestStructure:

tests/test_ndjsonr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22
from nose.tools import eq_, raises
33
from pyexcel._compact import StringIO
4-
from pyexcel_text.ndjsonr import NDJsonParser as JsonParser
5-
from pyexcel_text.ndjsonr import ARRAY, RECORDS, DICT, AUTO_DETECT
4+
from pyexcel_text.ndjsonp import NDJsonParser as JsonParser
5+
from pyexcel_text.ndjsonp import ARRAY, RECORDS, DICT, AUTO_DETECT
66

77

88
class TestStructure:

0 commit comments

Comments
 (0)