Skip to content

Commit

Permalink
Merge pull request #10 from thombashi/develop
Browse files Browse the repository at this point in the history
Fix exit code
  • Loading branch information
thombashi authored Aug 11, 2016
2 parents edd6129 + babac51 commit f44bdfb
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 17 deletions.
4 changes: 2 additions & 2 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
click
DataProperty>=0.7.2
DataProperty>=0.8.1
path.py
SimpleSQLite>=0.4.2
SimpleSQLite>=0.4.6
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import with_statement
import os.path
import setuptools
import sys
Expand Down
2 changes: 1 addition & 1 deletion sqlitebiter/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.1.4"
VERSION = "0.1.5"
29 changes: 29 additions & 0 deletions sqlitebiter/_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <[email protected]>
"""

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
41 changes: 28 additions & 13 deletions sqlitebiter/sqlitebiter.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env python
# encoding: utf-8


"""
.. codeauthor:: Tsuyoshi Hombashi <[email protected]>
"""


from __future__ import absolute_import
import collections
import re
import sys

import click
import dataproperty
Expand All @@ -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"])

Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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__':
Expand Down
29 changes: 29 additions & 0 deletions test/test_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <[email protected]>
"""


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

0 comments on commit f44bdfb

Please sign in to comment.