diff --git a/requirements/requirements.txt b/requirements/requirements.txt index cd5d3b9..c332afd 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -1,4 +1,4 @@ click -DataProperty>=0.7.2 +DataProperty>=0.8.1 path.py -SimpleSQLite>=0.4.2 +SimpleSQLite>=0.4.6 diff --git a/setup.py b/setup.py index 39b4fee..13df3b1 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,3 @@ -from __future__ import with_statement import os.path import setuptools import sys diff --git a/sqlitebiter/__init__.py b/sqlitebiter/__init__.py index ab1bdd5..f3737e0 100644 --- a/sqlitebiter/__init__.py +++ b/sqlitebiter/__init__.py @@ -1 +1 @@ -VERSION = "0.1.4" +VERSION = "0.1.5" diff --git a/sqlitebiter/_counter.py b/sqlitebiter/_counter.py new file mode 100644 index 0000000..0fd2595 --- /dev/null +++ b/sqlitebiter/_counter.py @@ -0,0 +1,29 @@ +# encoding: utf-8 + +""" +.. codeauthor:: Tsuyoshi Hombashi +""" + +from __future__ import absolute_import + + +class ResultCounter(object): + + def __init__(self): + self.__success_count = 0 + self.__fail_count = 0 + + def inc_success(self): + self.__success_count += 1 + + def inc_fail(self): + self.__fail_count += 1 + + def get_return_code(self): + if self.__success_count > 0: + return 0 + + if self.__fail_count > 0: + return 1 + + return 2 diff --git a/sqlitebiter/sqlitebiter.py b/sqlitebiter/sqlitebiter.py index cb1f615..cf76c86 100644 --- a/sqlitebiter/sqlitebiter.py +++ b/sqlitebiter/sqlitebiter.py @@ -1,15 +1,14 @@ #!/usr/bin/env python # encoding: utf-8 - """ .. codeauthor:: Tsuyoshi Hombashi """ - from __future__ import absolute_import import collections import re +import sys import click import dataproperty @@ -18,6 +17,8 @@ from simplesqlite.loader import ValidationError from simplesqlite.loader import InvalidDataError +from ._counter import ResultCounter + CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) @@ -81,8 +82,8 @@ def file(files, output_path): """ con = create_database(output_path) + result_counter = ResultCounter() - convert_count = 0 for file_path in files: if not path.Path(file_path).isfile(): continue @@ -96,12 +97,17 @@ def file(files, output_path): for tabledata in loader.load(): click.echo("convert '{:s}' to '{:s}' table".format( file_path, tabledata.table_name)) - con.create_table_from_tabledata(tabledata) - convert_count += 1 - except (ValueError, ValidationError, InvalidDataError): - continue - return 0 if convert_count == 0 else 1 + try: + con.create_table_from_tabledata(tabledata) + result_counter.inc_success() + except (ValueError, IOError): + result_counter.inc_fail() + continue + except (ValidationError, InvalidDataError): + result_counter.inc_fail() + + sys.exit(result_counter.get_return_code()) @cmd.command() @@ -121,17 +127,26 @@ def gs(credentials, title, output_path): """ con = create_database(output_path) + result_counter = ResultCounter() loader = simplesqlite.loader.GoogleSheetsTableLoader() loader.source = credentials loader.title = title - for tabledata in loader.load(): - click.echo("convert '{:s}' to '{:s}' table".format( - title, tabledata.table_name)) - con.create_table_from_tabledata(tabledata) + try: + for tabledata in loader.load(): + click.echo("convert '{:s}' to '{:s}' table".format( + title, tabledata.table_name)) + + try: + con.create_table_from_tabledata(tabledata) + result_counter.inc_success() + except (ValidationError, InvalidDataError): + result_counter.inc_fail() + except (ValidationError, InvalidDataError): + result_counter.inc_fail() - return 0 + sys.exit(result_counter.get_return_code()) if __name__ == '__main__': diff --git a/test/test_counter.py b/test/test_counter.py new file mode 100644 index 0000000..a33a9aa --- /dev/null +++ b/test/test_counter.py @@ -0,0 +1,29 @@ +# encoding: utf-8 + +""" +.. codeauthor:: Tsuyoshi Hombashi +""" + + +import pytest +from sqlitebiter._counter import ResultCounter + + +class Test_ResultCounter(object): + + @pytest.mark.parametrize(["success", "fail", "expected"], [ + [0, 0, 2], + [1, 0, 0], + [1, 1, 0], + [0, 1, 1], + ]) + def test_normal(self, success, fail, expected): + result_counter = ResultCounter() + + for _i in range(success): + result_counter.inc_success() + + for _i in range(fail): + result_counter.inc_fail() + + assert result_counter.get_return_code() == expected