Skip to content

Commit

Permalink
option_parserを廃止し、Clickを利用するように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
mypaceshun committed Sep 28, 2020
1 parent 659a8b2 commit be3a83c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 44 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ test: ${VENV}
.PHONY: build
build:
rm -rf dist build *.egg-info
${MAKE} init
${ACTIVATE} && python setup.py bdist_wheel sdist --format=gztar,zip
${ACTIVATE} && twine check dist/*

.PHONY: upload
upload:
${MAKE} build
${ACTIVATE} && twine upload --repository ${TARGET} dist/*.tar.gz dist/*.whl
${ACTIVATE} && twine upload --repository ${TARGET} dist/*.tar.gz dist/*.whl dist/*.zip


.PHONY: clean
Expand Down
59 changes: 16 additions & 43 deletions akerun_sum/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*- vim:shiftwidth=4:expandtab:

import click
import math
import calendar
import datetime
Expand All @@ -15,45 +16,6 @@
KEYS = {'date': '日時', 'user': 'ユーザー名', 'lock': 'アクション'}


def option_parser():
usage = 'Usage: python {} \
-i <input filename> -o <output filename> \
-d <period(yyyymm)> [-f formatnumber]'\
.format(__file__)
arguments = sys.argv
if len(arguments) != 7 and len(arguments) != 9:
print(usage)
sys.exit()

index_number = [1, 3, 5]
if len(arguments) == 9:
index_number = [1, 3, 5, 7]

# default
format_num = 0
for index in index_number:
option = arguments[index]
if option == '-i':
input_filename = arguments[index + 1]
elif option == '-o':
output_filename = arguments[index + 1]
elif option == '-d':
period = arguments[index + 1]
elif option == '-f':
format_num = int(arguments[index + 1])
if format_num not in [0, 1]:
print('This format number is not exist.')
sys.exit()
else:
print(usage)
sys.exit()

return {'input_filename': input_filename,
'output_filename': output_filename,
'period': period,
'format_num': format_num}


def input_data(filename):
lookup = ('utf_8', 'euc_jp', 'euc_jis_2004', 'euc_jisx0213',
'shift_jis', 'shift_jis_2004', 'shift_jisx0213',
Expand Down Expand Up @@ -87,7 +49,8 @@ def data_shaping(data_list, period):
except:
pass
# data mining
period_start = datetime.datetime.strptime(period, '%Y%m')
period_start = period
period_str = period.strftime('%Y%m')
day_range = calendar.monthrange(period_start.year, period_start.month)[1]
period_end = period_start + datetime.timedelta(days=day_range)

Expand All @@ -105,7 +68,7 @@ def data_shaping(data_list, period):
user_list.append(data[KEYS['user']])
shaped_data.append({
'name': data[KEYS['user']], 'timecard_data': [],
'writed_days': [], 'period': period})
'writed_days': [], 'period': period_str})

# data reconstruction
for data in mining_data:
Expand Down Expand Up @@ -278,8 +241,18 @@ def output_data1(filename, encode, shaped_data):
writer.writerow(['', '', '', '', '', ''])


def main():
commandline_vars = option_parser()
@click.command()
@click.option('-i', '--input-filename', required=True, type=click.Path(exists=True))
@click.option('-o', '--output-filename', required=True, type=click.Path())
@click.option('-d', '--period', required=True, type=click.DateTime(['%Y%m']))
@click.option('-f', '--format', default='0', show_default=True, type=click.Choice(['0', '1']))
def main(input_filename, output_filename, period, format):
commandline_vars = {
'input_filename': input_filename,
'output_filename': output_filename,
'period': period,
'format_num': int(format),
}
data_list, encode = input_data(commandline_vars['input_filename'])
shaped_data = data_shaping(data_list, commandline_vars['period'])
if commandline_vars['format_num'] == 0:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
click
twine
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ classfiers =
packages = akerun_sum
zip_safe = false
python_require = '>=3.5'
install_requires =
click>=7.1

[options.entry_points]
console_scripts =
Expand Down

0 comments on commit be3a83c

Please sign in to comment.