You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

85 lines
2.7 KiB

import configparser
import os
import argparse
def readConfig(configFilePath):
config = configparser.ConfigParser()
if os.path.isfile(configFilePath):
config.read(configFilePath)
return config
class ArgParser:
def __init__(self):
self.__flags = {}
self.__parser = argparse.ArgumentParser()
self.__instanceDirArgSet = False
self.__config = None
self.__parsedArgs = {}
def addArg(self, alias,
shortFlag,
longFlag,
help,
type,
default=None,
ignoreDatabaseConfig=False):
self.__flags[alias] = {"longFlag": longFlag,
"hasDefault": False,
"ignoreDatabaseConfig": ignoreDatabaseConfig}
if default != None:
self.__flags[alias]["hasDefault"] = True
self.__parser.add_argument("-%s" % shortFlag,
"--%s" % longFlag,
help=help,
type=type,
default=default)
def addInstanceDirArg(self):
self.__instanceDirArgSet = True
self.addArg(alias="datasetDir", shortFlag="d", longFlag="dataset_dir",
help="the base direcotry of the dataset; if this flag is given the others can be omitted",
type=str, ignoreDatabaseConfig=True)
def parse(self):
self.__parsedArgs = {}
args = vars(self.__parser.parse_args())
if self.__instanceDirArgSet:
self.__config = readConfig(os.path.join(args["dataset_dir"],
"dataset.config"))
self.__parseDatasetConfig()
for alias, flag in self.__flags.items():
self.__parsedArgs[alias] = self.__processFlag(args, flag)
self.__config = None
return self.__parsedArgs
def __parseDatasetConfig(self):
for flag, value in self.__config["STRUCTURE"].items():
self.__parsedArgs[flag] = value
def __processFlag(self, args, flag):
longFlag = flag["longFlag"]
tmpValue = self.__parsedArgs[longFlag] if longFlag in self.__parsedArgs else None
if flag["ignoreDatabaseConfig"] == True:
tmpValue = None
if args[longFlag]:
tmpValue = args[longFlag]
if tmpValue == None:
tmpValue == input("pass arguement %s: " % longFlag)
return tmpValue