Skip to content

Commit

Permalink
fix: expoter[json, msgpack]
Browse files Browse the repository at this point in the history
  • Loading branch information
cupen committed Mar 26, 2024
1 parent 969513f commit 734499c
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 102 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ python:=$(python_venv)/bin/python

init:
python -m venv $(python_venv)
$(python) -m pip install -r requirements-dev.txt


reinit:
Expand Down Expand Up @@ -37,3 +38,8 @@ publish-test: build
publish: build
twine upload dist/*


.PHONY: run-exmaple
run-example:
$(python) -m pip install -e .
cd example && make build
6 changes: 3 additions & 3 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
root_dir:=$(abspath $(CURDIR)/..)
build_dir:=$(root_path)/example/build
excel2xx_bin:=python3 -m excel2xx
excel2xx_bin_debug:=EXCEL2XX_DEBUG=1 python3 -m excel2xx
build_dir:=$(root_dir)/example/build
excel2xx_bin:=$(root_dir)/.venv/bin/excel2xx
excel2xx_bin_debug:=EXCEL2XX_DEBUG=1 $(excel2xx_bin)


build: clean
Expand Down
4 changes: 2 additions & 2 deletions example/test.lua.mako
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local t = {
% for sheet in excel:
${sheet.name} = {
% for name, sheet in excel.items():
${name} = {
% for row in sheet:
{
% for key in row:
Expand Down
2 changes: 1 addition & 1 deletion excel2xx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import excel2xx.exporter as exporter


VERSION = "0.11.2"
VERSION = "0.11.3"
__author__ = "cupen"
__email__ = "xcupen@gmail.com"

Expand Down
2 changes: 1 addition & 1 deletion excel2xx/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def generate(src: list, context={}, note=NOTE, encoding="utf-8", newline="\n"):
print(f"\ttemplate files: {len(fpaths)}")

for fpath in fpaths:
fpath_new = fpath.replace(".mako", "")
print(f"\t{fpath} -> ", end="")
if not fpath.endswith(".mako"):
print(console.Colors.yellow(fpath_new))
Expand All @@ -39,7 +40,6 @@ def generate(src: list, context={}, note=NOTE, encoding="utf-8", newline="\n"):
ctx["__dir__"] = os.path.dirname(fpath)

tmpl = load_template(fpath)
fpath_new = fpath.replace(".mako", "")
try:
text = tmpl.render(**ctx)
with open(fpath_new, "w", encoding=encoding, newline=newline) as fp:
Expand Down
3 changes: 3 additions & 0 deletions excel2xx/console/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# coding: utf-8
import colorama

colorama.init(autoreset=True)
from colorama import Fore, Back, Style
from .colors import Colors

Expand Down
41 changes: 10 additions & 31 deletions excel2xx/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mako.template import Template
from io import open
from datetime import datetime, date
from excel2xx.exporter import auto


def _defaultSerialize(obj):
Expand All @@ -16,54 +17,32 @@ def _defaultSerialize(obj):


def toJson(excel, output, encoding="utf-8"):
_dict = OrderedDict()
for sheet in excel:
_dict[sheet.name] = list(sheet)

if sys.version_info[0] == 2:
with open(output, mode="wb") as f:
json.dump(
_dict,
f,
ensure_ascii=False,
encoding=encoding,
default=_defaultSerialize,
)
return

with open(output, mode="w", encoding=encoding) as f:
json.dump(_dict, f, ensure_ascii=False, indent=4, default=_defaultSerialize)
data = auto.export(excel)
with open(output, "w", encoding=encoding) as fp:
json.dump(data, fp, ensure_ascii=False, indent=4, default=_defaultSerialize)
pass


def toMako(excel, output, template, encoding="utf-8"):
data = auto.export(excel)
with open(output, mode="w", encoding=encoding) as ouputfile:
text = ""
with open(template, mode="r", encoding=encoding) as f:
text = Template(f.read()).render(excel=excel, format=pformat)
text = Template(f.read()).render(excel=data, format=pformat)
ouputfile.write(text)
pass


def toMsgPack(excel, output, encoding="utf-8"):
import msgpack

_dict = OrderedDict()
for sheet in excel:
_dict[sheet.name] = list(sheet)

data = auto.export(excel)
with open(output, mode="wb") as f:
f.write(msgpack.packb(_dict, default=_defaultSerialize))
f.write(msgpack.packb(data, default=_defaultSerialize))
pass


def toCSV(excel, output, encoding="utf-8"):
import msgpack

_dict = OrderedDict()
for sheet in excel:
_dict[sheet.name] = list(sheet)

with open(output, mode="wb") as f:
f.write(msgpack.packb(_dict, default=_defaultSerialize))
data = exporter.auto(excel)
raise NotImplementedError("toCSV")
pass
44 changes: 29 additions & 15 deletions excel2xx/exporter/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def export(ex, ver=1):
if str.startswith(sheet.name, "#"):
continue
name, _type = parse_sheet_name(sheet.name)
if not name:
raise Exception(f"invalid sheet {sheet.name}")
if _type == "list":
d[name] = sheet.toList()
elif _type == "map":
Expand All @@ -18,40 +20,52 @@ def export(ex, ver=1):
return d


def parse_sheet_name(name) -> (str, str):
def parse_sheet_name(name: str) -> tuple[str, str]:
"""
>>> _parse_sheet_name("name") -> ("name", "list")
>>> _parse_sheet_name("name")
('name', 'list')
>>> _parse_sheet_name("name(list)")
('name', 'list')
>>> _parse_sheet_name("name123(map)")
('name123', 'map')
>>> _parse_sheet_name("name123((map)")
Traceback (most recent call last):
Exception: duplicated symbol '('
>>> _parse_sheet_name("sheet-map(map)")
('sheet-map', 'map')
"""
name = name.strip()
if "=" not in name:
return name, "list"
arr = list(map(lambda x: x.strip(), name.split("=")))
if len(arr) <= 1:
return "", ""
name = arr[1]
if "=" in name:
arr = list(map(lambda x: x.strip(), name.split("=")))
name = arr[1]
return _parse_sheet_name(name)


def _parse_sheet_name(name) -> (str, str):
def _parse_sheet_name(name) -> tuple[str, str]:
realName = ""
realType = ""
state = 0 # 0-init, 1-type, 2-end
state = 0 # 0-start, 1-name, 2-type, 3-end
for c in name:
if c == "(":
if state != 0:
if state != 1:
raise Exception(f"duplicated symbol '('")
state = 1
state = 2
elif c == ")":
if state != 1:
if state != 2:
raise Exception(f" ')' must behind of '('")
if len(realType) <= 0:
raise Exception(f" '()' must be one of list, map, kv")
state = 2
state = 3
break # end
else:
if not str.isalnum(c) and c not in ["-", "_"]:
raise Exception(f"invalid char '{c}'")
if state == 0:
realName += c
realName = c
state = 1
elif state == 1:
realName += c
elif state == 2:
realType += c
else:
raise Exception(f"BUG!")
Expand Down
26 changes: 17 additions & 9 deletions excel2xx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
Options:
-h --help show this help message and exit.
-v --version show version and exit.
--version show version and exit.
-o --output=FILE output to file.
-r --row-number=ROW_NUMBER first row. [default: 2]
--name-row=NAME_ROW name row number. [default: 1]
--type-row=TYPE_ROW type row number. [default: 2]
--desc-row=DESC_ROW desc row number. [default: 3]
--data-row=DATA_ROW data row number. [default: 4]
-v --verbose show debug infomation.
-vv --verbose2 show more debug infomation.
"""
from __future__ import unicode_literals, print_function
import os
import traceback
from docopt import docopt
Expand All @@ -38,8 +40,13 @@ def main(args):
print("Unexist file:" + excelFile)
return 1

excel = Excel(excelFile, fieldMeta=FieldMeta())

meta = FieldMeta(
name=int(args["--name-row"]) - 1,
type=int(args["--type-row"]) - 1,
desc=int(args["--desc-row"]) - 1,
data=int(args["--data-row"]) - 1,
)
excel = Excel(excelFile, fieldMeta=meta)
if args["json"]:
export.toJson(excel, args["--output"] or "%(excelFile)s.json" % locals())
elif args["msgpack"]:
Expand All @@ -51,12 +58,11 @@ def main(args):
else:
print("Invalid subcmd.")
return 2

return 0


def main_docopt():
args = docopt(__doc__)
def main_docopt(argv=None):
args = docopt(__doc__, argv)
if args["--verbose2"]:
print(args)
try:
Expand All @@ -69,4 +75,6 @@ def main_docopt():


if __name__ == "__main__":
exit(main_docopt())
import sys

exit(main_docopt(sys.argv))
18 changes: 1 addition & 17 deletions excel2xx/validator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def run(data, data_dir: str, validator_name="_validator"):
def list_files(_dir: str):
import bisect

rs = []
rs = [] # type: list[str]
for base, dirs, files in os.walk(_dir):
for fname in files:
if not fname.endswith(".py"):
Expand All @@ -51,19 +51,3 @@ def list_files(_dir: str):
pass
pass
return rs


def green(text):
return Fore.LIGHTGREEN_EX + str(text) + Fore.RESET


def red(text):
return Fore.LIGHTRED_EX + str(text) + Fore.RESET


def blue(text):
return Fore.LIGHTBLUE_EX + str(text) + Fore.RESET


def yellow(text):
return Fore.LIGHTYELLOW_EX + str(text) + Fore.RESET
24 changes: 14 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@

root_dir = os.path.dirname(__file__)

fpath = os.path.join(root_dir, "README.md")
readme = open(fpath, "r", encoding="utf-8").read()
_open = lambda fname: open(os.path.join(root_dir, fname), "r", encoding="utf-8")
with _open("README.md") as fp:
readme = fp.read()
pass

with _open("requirements.txt") as fp:
deps = map(str.strip, fp.readlines())
deps = filter(lambda line: bool(line), deps)
deps = list(deps)
pass

print(deps)


setup(
name="excel2xx",
version="0.11.2",
version="0.11.3",
packages=find_packages(),
url="https://github.com/cupen/excel2xx",
license="WTFPL",
Expand All @@ -19,13 +29,7 @@
description="Extract data from excel file, and export to json, msgpack, or any code(mako template).",
long_description=readme,
long_description_content_type="text/markdown",
install_requires=[
"xlrd == 1.2.*",
"docopt >= 0.6.0",
"mako == 1.2.*",
"msgpack-python >= 0.4.8",
"colorama >= 0.4.6",
],
install_requires=deps,
entry_points={
"console_scripts": [
"excel2xx=excel2xx.main:main_docopt",
Expand Down
Loading

0 comments on commit 734499c

Please sign in to comment.