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 run(opts): """ Run the operation based on pre-defined opts (from parser) args: opts - Parsed args passed from top level script """ # Create the database connection and connect to it using the proper path db = dbconnect(opts.dbtype) db.connect(opts.db) __log__.debug("About to run CHECK {}".format(opts)) check(db, opts.file, opts.quiet) # All done, now close connection db.close()
def run(opts): """ Run the operation based on pre-defined opts (from parser) args: opts - Parsed args passed from top level script """ # Create the database connection and connect to it using the proper path db = dbconnect(opts.dbtype) db.connect(opts.db) add_by_file(db, opts.file) # All done, now close connection db.close()
def run(opts): """ Run the operation based on pre-defined opts (from parser) args: opts - Parsed args passed from top level script """ connOpts = { "row_factory": utils.dict_factory } db = dbconnect(opts.dbtype) db.connect(opts.db, connectOpts = connOpts) results = db.js(opts.target) # All done, now close connection db.close()
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()