def add_args(parser): """Add my arguments to the given argparse parser.""" parser.add_argument( "brewery", help = _("The name of the brewery for the bottle"), ) parser.add_argument( "beerName", help = _("The name of the beer"), ) parser.add_argument( "desc", default = None, help = _("Description or additional notes about beer"), nargs = "?", ) parser.add_argument( "year", default = utils.get_year(), help = _("The year or edition of the current beer"), nargs = "?" )
def check(db, filePath, quiet): """ Check to see if entries in a file already exist in the database. If quiet mode, returns the number found entries. Otherwise, prints out all the duplicates (and the count) args: db - Pointer to database filePath - Path to the file. This will be passed to dbconnect() """ __log__.info(_("Checking entries from file: " + filePath)) lines = [] # Get connection to the file dataFile = dbconnect('text') dataFile.connect(filePath) # Yup, the lines in that file lines = dataFile.get(True) # Did we find a duplicate entry duplicateCount = 0 # Iterate through each line of the file # Get values, and then do the check for those values for val in lines: length = len(val) # Required values brewery = val[0] beerName = val[1] # Optional Values desc = None year = None # XXX Is there a better way to do this? if len(val) >= 4: desc = val[2] year = val[3] elif len(val) >= 3: desc = val[2] else: year = utils.get_year() if isDuplicateLine(db, brewery, beerName, year): duplicateCount += 1 __log__.info("Duplicate found: {} {} ({})".format(brewery, beerName, year)) # Just print the duplicate count if in quiet mode if quiet: print(duplicateCount) else: __log__.info("Duplicate Count: {}".format(duplicateCount)) # Done here, return if any duplicate lines were found return duplicateCount > 0
def add_by_file(db, filePath): """ Runs add entry on each line of file. Keep brewery and beer name in quotes. args: db - pointer to the database filePath - the path to the file that is parsed """ __log__.info(_("Adding entries from a file")) lines = [] lineCount = 1 dataFile = dbconnect('text') dataFile.connect(filePath) lines = dataFile.get(True) # Note that the splitting is done before now. So the lines should be in valid form for val in lines: length = len(val) # Required values brewery = val[0] beerName = val[1] # Optional Values desc = None year = None if len(val) >= 4: desc = val[2] year = val[3] elif len(val) >= 3: desc = val[2] else: year = utils.get_year() __log__.info(_("At line: {}, Adding {} {}, with desc '{}' and year {}").format(lineCount, brewery, beerName, desc, year)) db.insert(brewery, beerName, desc, year) dataFile.close()