def init_pdappend_file() -> pdappend.Args: load_dotenv(".pdappend") config = pdappend.Config( sheet_name=utils._or( os.getenv("SHEET_NAME"), pdappend.DEFAULT_CONFIG.sheet_name ), header_row=utils._or( os.getenv("HEADER_ROW"), pdappend.DEFAULT_CONFIG.header_row ), excel_header_row=utils._or( os.getenv("EXCEL_HEADER_ROW"), pdappend.DEFAULT_CONFIG.excel_header_row ), csv_header_row=utils._or( os.getenv("CSV_HEADER_ROW"), pdappend.DEFAULT_CONFIG.csv_header_row ), save_as=utils._or(os.getenv("SAVE_AS"), pdappend.DEFAULT_CONFIG.save_as), show=utils._or( utils.str_to_bool(os.getenv("SHOW")), pdappend.DEFAULT_CONFIG.show ), ) args = pdappend.Args(targets=DEFAULT_TARGETS, flags=config) return args
def main(): root = Tk() root.withdraw() # TODO: from pdappend.pdappend.FILE_TYPES files = filedialog.askopenfilenames( initialdir=os.getcwd(), filetypes=[(".xlsx .xls .csv", ".xlsx .xls .csv")] ) args = pdappend.Args( targets=pdappend.Targets(values=files), flags=pdappend.DEFAULT_CONFIG ) cli.main(args)
def merge_args(args0: pdappend.Args, args1: pdappend.Args) -> pdappend.Args: """ Override default values from args1 -> args0 and return pdappend.Args to use. :args0: pdappend.Args to override :args1: pdappend.Args to override args0 with Returns pdappend.Args """ if args0 == DEFAULT_ARGS and args1 != DEFAULT_ARGS: return args1 if args0 != DEFAULT_ARGS and args1 == DEFAULT_ARGS: return args0 targets = merge_targets(targets0=args0.targets, targets1=args1.targets) flags = merge_flags(flags0=args0.flags, flags1=args1.flags) return pdappend.Args(targets, flags)
def init_cli() -> pdappend.Args: """ Return pdappend.Args using prioritized commands and secondary .pdappend """ pdappend_file_args = init_pdappend_file() parsed_args = init_argparser().parse_args() command_args = pdappend.Args( targets=pdappend.Targets(values=parsed_args.targets), flags=pdappend.Config( sheet_name=parsed_args.sheet_name, header_row=parsed_args.header_row, excel_header_row=parsed_args.excel_header_row, csv_header_row=parsed_args.csv_header_row, save_as=parsed_args.save_as, show=parsed_args.show, ), ) args = merge_args(args0=pdappend_file_args, args1=command_args) return args
import os import sys import logging from dotenv import load_dotenv from argparse import ArgumentParser from typing import List, Optional, Union from pdappend import pdappend, utils DEFAULT_TARGETS = pdappend.Targets(values=".") DEFAULT_ARGS = pdappend.Args(targets=DEFAULT_TARGETS, flags=pdappend.DEFAULT_CONFIG) def init_logging() -> None: """cli.py module logging init function""" logging.basicConfig( format="%(asctime)s %(levelname)-8s %(message)s", level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S", ) def merge_targets( targets0: pdappend.Targets, targets1: pdappend.Targets ) -> pdappend.Targets: """ Override default values from targets1 -> targets0 and return pdappend.Targets to use.