Skip to content

Commit

Permalink
updated python script
Browse files Browse the repository at this point in the history
  • Loading branch information
octopoulos committed Jan 6, 2021
1 parent c804240 commit 4e04e4d
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 38 deletions.
3 changes: 3 additions & 0 deletions script/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding: utf-8
# @author octopoulo <[email protected]>
# @version 2021-01-05

"""
Init
Expand All @@ -12,4 +14,5 @@
'download_json',
'inspector',
'sync',
'util',
]
31 changes: 16 additions & 15 deletions script/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf-8
# @author octopoulo <[email protected]>
# @version 2020-09-07
# @version 2021-01-05

"""
Main
Expand All @@ -10,39 +10,40 @@
import os
from time import time

from commoner import create_group
from download_json import download_json
from sync import Sync
from sync import add_arguments_sync, main_sync
from util import add_arguments_util, main_util


def main():
"""Main
"""
parser = ArgumentParser(description='Sync', prog='python __main__.py')
add = parser.add_argument
parser = ArgumentParser(description='TCEC', prog='python __main__.py')

add('--clean', action='store_true', help='delete all .gz files')
add('--console-debug', nargs='?', default='INFO', help='console debug level',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])
add('--debug', nargs='?', default=0, const=1, type=int, help="keep debug code from the javascript")
add = create_group(parser, 'tcec')
add('--download', action='store_true', help='download JSON files')
add('--host', nargs='?', default='/', help='host, ex: /seriv/')
add('--inspector', nargs='?', default=0, const=1, type=int, help='run the inspector')
add('--no-process', nargs='?', default=0, const=1, type=int, help="don't process the images")
add('--zip', action='store_true', help='create .gz files')

add_arguments_util(parser)
add_arguments_sync(parser)

# configure args
args = parser.parse_args()
args_dict = vars(args)
args_set = set(item for item, value in args_dict.items() if value)

if args.download:
download_json()
# utils
if args_set & {'inspector'}:
main_util(parser)
elif args.inspector:
from inspector import Inspect
inspector = Inspect()
inspector.go()
else:
sync = Sync(**args_dict)
sync.synchronise()
# sync
elif args.sync:
main_sync(parser)


if __name__ == '__main__':
Expand Down
30 changes: 29 additions & 1 deletion script/commoner.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
# coding: utf-8
# @author octopoulo <[email protected]>
# @version 2020-04-13
# @version 2021-01-05

"""
Common functions
"""

from argparse import ArgumentParser
import errno
import logging
import os


def create_group(parser: ArgumentParser, group: str) -> callable:
group = parser.add_argument_group(group)
return group.add_argument


def default_int(value: int or str, default: int=None, origin: str=None, log: bool=False) -> int or None:
"""Convert a value to an int, on exception, return a default value
"""
if isinstance(value, int):
return value
if value is None:
return default

try:
value = int(float(value))
except (TypeError, ValueError):
if log:
logging.warning({
'status': 'default_int__cannot_convert', 'value': value, 'default': default, 'origin': origin})
value = default

return value


def makedirs_safe(folder: str) -> bool:
"""Create a folder recursively + handle errors
:return: True if the folder has been created or existed already, False otherwise
Expand All @@ -28,6 +53,9 @@ def makedirs_safe(folder: str) -> bool:
return False


pinfo = print


def read_text_safe(filename: str, locked: bool=False, want_bytes: bool=False) -> str or bytes or None:
"""Read the content of a file and convert it to utf-8
"""
Expand Down
Loading

0 comments on commit 4e04e4d

Please sign in to comment.