Skip to content

Commit

Permalink
Merge pull request #41 from alingse/add-test-1
Browse files Browse the repository at this point in the history
add test
  • Loading branch information
alingse authored Mar 10, 2020
2 parents 4872114 + 848ed7e commit d05f6e0
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 87 deletions.
11 changes: 4 additions & 7 deletions jsoncsv/dumptool.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# author@alingse
# 2015.10.09

import unicodecsv as csv
import json

import unicodecsv as csv
import xlwt


class Dump(object):

def __init__(self, fin, fout, **kwargs):
self.fin = fin
self.fout = fout
Expand All @@ -33,7 +33,6 @@ def dump(self):


class ReadHeadersMixin(object):

@staticmethod
def load_headers(fin, read_row=None, sort_type=None):
headers = set()
Expand All @@ -58,14 +57,14 @@ def load_headers(fin, read_row=None, sort_type=None):


class DumpExcel(Dump, ReadHeadersMixin):

def initialize(self, **kwargs):
super(DumpExcel, self).initialize(**kwargs)
self._read_row = kwargs.get('read_row')
self._sort_type = kwargs.get('sort_type')

def prepare(self):
headers, datas = self.load_headers(self.fin, self._read_row, self._sort_type)
headers, datas = self.load_headers(self.fin, self._read_row,
self._sort_type)
self._headers = headers
self._datas = datas

Expand All @@ -87,7 +86,6 @@ def dump_file(self):


class DumpCSV(DumpExcel):

def initialize(self, **kwargs):
super(DumpCSV, self).initialize(**kwargs)
self.csv_writer = None
Expand All @@ -110,7 +108,6 @@ def patch_obj(self, obj):


class DumpXLS(DumpExcel):

def initialize(self, **kwargs):
super(DumpXLS, self).initialize(**kwargs)

Expand Down
7 changes: 2 additions & 5 deletions jsoncsv/jsontool.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# coding=utf-8
# author@alingse
# 2016.05.27
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import absolute_import, unicode_literals

import json
from copy import deepcopy
from itertools import groupby
from operator import itemgetter

from jsoncsv import PY2
from jsoncsv.utils import encode_safe_key
from jsoncsv.utils import decode_safe_key

from jsoncsv.utils import decode_safe_key, encode_safe_key

__all__ = [
'convert_json',
Expand Down
130 changes: 55 additions & 75 deletions jsoncsv/main.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,48 @@
# coding=utf-8
import click

from jsoncsv import jsontool
from jsoncsv import dumptool
from jsoncsv import dumptool, jsontool
from jsoncsv.dumptool import dump_excel
from jsoncsv.jsontool import convert_json
from jsoncsv.utils import unit_char


def separator_type(sep):
if len(sep) != 1:
raise click.BadOptionUsage(
option_name='separator',
message='separator can only be a char')
raise click.BadOptionUsage(option_name='separator',
message='separator can only be a char')
if sep == unit_char:
raise click.BadOptionUsage(
option_name='separator',
message='separator can not be `\\` ')
raise click.BadOptionUsage(option_name='separator',
message='separator can not be `\\` ')
return sep


@click.command()
@click.option(
'-A',
'--array',
'json_array',
is_flag=True,
default=False,
help='read input file as json array')
@click.option(
'-s',
'--sep',
'separator',
type=separator_type,
default='.',
help='separator')
@click.option(
'--safe',
is_flag=True,
help='use safe mode')
@click.option(
'-r',
'--restore',
'restore',
is_flag=True,
help='restore expanded json')
@click.option(
'-e',
'--expand',
'expand',
is_flag=True,
help='expand json (default True)')
@click.argument(
'input',
type=click.File('r', encoding='utf-8'),
default='-')
@click.argument(
'output',
type=click.File('w', encoding='utf-8'),
default='-')
@click.option('-A',
'--array',
'json_array',
is_flag=True,
default=False,
help='read input file as json array')
@click.option('-s',
'--sep',
'separator',
type=separator_type,
default='.',
help='separator')
@click.option('--safe', is_flag=True, help='use safe mode')
@click.option('-r',
'--restore',
'restore',
is_flag=True,
help='restore expanded json')
@click.option('-e',
'--expand',
'expand',
is_flag=True,
help='expand json (default True)')
@click.argument('input', type=click.File('r', encoding='utf-8'), default='-')
@click.argument('output', type=click.File('w', encoding='utf-8'), default='-')
def jsoncsv(output, input, expand, restore, safe, separator, json_array):
if expand and restore:
raise click.UsageError('can not choose both, default is `-e`')
Expand All @@ -68,41 +52,37 @@ def jsoncsv(output, input, expand, restore, safe, separator, json_array):
else:
func = jsontool.restore

convert_json(input, output, func, separator=separator, safe=safe, json_array=json_array)
convert_json(input,
output,
func,
separator=separator,
safe=safe,
json_array=json_array)

input.close()
output.close()


@click.command()
@click.option(
'-t',
'--type',
'type_',
type=click.Choice(['csv', 'xls']),
default='csv',
help='choose dump format')
@click.option(
'-r',
'--row',
type=int,
default=None,
help='number of pre-read `row` lines to load `headers`')
@click.option(
'-s',
'--sort',
'sort_',
is_flag=True,
default=False,
help='enable sort the headers keys')
@click.argument(
'input',
type=click.File('r', encoding='utf-8'),
default='-')
@click.argument(
'output',
type=click.File('wb'),
default='-')
@click.option('-t',
'--type',
'type_',
type=click.Choice(['csv', 'xls']),
default='csv',
help='choose dump format')
@click.option('-r',
'--row',
type=int,
default=None,
help='number of pre-read `row` lines to load `headers`')
@click.option('-s',
'--sort',
'sort_',
is_flag=True,
default=False,
help='enable sort the headers keys')
@click.argument('input', type=click.File('r', encoding='utf-8'), default='-')
@click.argument('output', type=click.File('wb'), default='-')
def mkexcel(output, input, sort_, row, type_):
klass = dumptool.DumpCSV
if type_ == "xls":
Expand Down
7 changes: 7 additions & 0 deletions tests/test_jsoncsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ def test_jsoncsv_with_error_args(self):
result = runner.invoke(jsoncsv, args=args)
assert result.exit_code != 0

def test_jsoncsv_with_error_sep_args(self):
runner = CliRunner()
args = ['-s', '\\', '-e', 'fixture/files/raw.0.json',
'fixture/files/tmp.expand.0.json']
result = runner.invoke(jsoncsv, args=args)
assert result.exit_code != 0

def test_jsoncsv_with_error_args_expand_and_restore(self):
runner = CliRunner()
args = ['-r', '-e', 'fixture/files/raw.0.json',
Expand Down
31 changes: 31 additions & 0 deletions tests/test_mkexcel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# coding=utf-8
# author@alingse
# 2020.03.10

import unittest
from click.testing import CliRunner

from jsoncsv.main import mkexcel


class Testmkexcel(unittest.TestCase):
def test_mkexcel_csv(self):
runner = CliRunner()
args = ['fixture/files/expand.0.json',
'fixture/files/tmp.expand.0.csv']
result = runner.invoke(mkexcel, args=args)
assert result.exit_code == 0

def test_mkexcel_xls(self):
runner = CliRunner()
args = ['-t', 'xls', 'fixture/files/expand.0.json',
'fixture/files/tmp.expand.0.xls']
result = runner.invoke(mkexcel, args=args)
assert result.exit_code == 0

def test_mkexcel_with_error(self):
runner = CliRunner()
args = ['-t', 'xlsx', 'fixture/files/expand.0.json',
'fixture/files/tmp.expand.0.xls']
result = runner.invoke(mkexcel, args=args)
assert result.exit_code == 2

0 comments on commit d05f6e0

Please # to comment.