Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added argument functionality #14

Merged
merged 6 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 51 additions & 12 deletions jsonmerge_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,58 @@
@author: GRENTOR
"""
import os
import argparse
import sys
import logging
#from tqdm import tqdm
from merge_files import Merge

def get_parser():
"""
gets & parses arguments from command line
"""
parser = argparse.ArgumentParser(
description="Merges different JSON files into a single JSON object in a new file",
add_help=True)
parser.add_argument('-ip',
dest = "input_prefix",
help="Input prefix",
required=True)
parser.add_argument('-op',
dest = "output_prefix",
help="Output prefix",
required=True)
parser.add_argument('-maxFileSize',
dest = "max_file_size",
help="The maximum file size (in bytes) that each merged file should have",
type=int,
required=True
)
parser.add_argument('-dir_path',
dest ="data_dir",
help="Path to directory where all json files are stored",
required=True)
parser.add_argument('-log_level',
dest="log_level",
help="The logging level - defaults to INFO",
metavar = "{'INFO','DEBUG','ERROR'}",
choices = ['INFO', 'DEBUG', 'ERROR'],
default="INFO")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
return parser.parse_args()


logger = logging.getLogger(__name__)
def configure_logger():
def configure_logger(args):
MLJBrackett marked this conversation as resolved.
Show resolved Hide resolved
"""
configures the logger object
"""
logging.basicConfig(filename='output.log', level=logging.INFO)
logging.basicConfig(filename='output.log')
if not logger.handlers:
# Prevent logging from propagating to the root logger
logger.setLevel(args.log_level)
MLJBrackett marked this conversation as resolved.
Show resolved Hide resolved
logger.propagate = 0
log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stream_handler = logging.StreamHandler()
Expand Down Expand Up @@ -50,19 +91,17 @@ def main():
Driver Function
"""
try:
configure_logger()
data_dir = input('Folder Path: ')
if not os.path.exists(data_dir):
args = get_parser()
configure_logger(args)
MLJBrackett marked this conversation as resolved.
Show resolved Hide resolved
if not os.path.exists(args.data_dir):
raise FolderNotFoundError
input_prefix = input('I/P Prefix: ')
if not os.path.isfile('{}{}'.format(path_creator(data_dir, input_prefix), '1.json')):
if not os.path.isfile('{}{}'.format
(path_creator(args.data_dir, args.input_prefix), '1.json')):
raise FileNotFoundError
output_prefix = input('O/P Prefix: ')
max_file_size = int(input('Max File Size: '))

merge = Merge(path_creator(data_dir, input_prefix),
path_creator(data_dir, output_prefix),
max_file_size)
merge = Merge(path_creator(args.data_dir, args.input_prefix),
path_creator(args.data_dir, args.output_prefix),
args.max_file_size)

merge.merge()

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jsonmerge
genson
argparse