def regeneratePricesFile(tdb, tdenv): tdenv.DEBUG0("Regenerating .prices file") with tdb.pricesPath.open("w", encoding="utf-8") as pricesFile: prices.dumpPrices(tdb.dbFilename, prices.Element.full, file=pricesFile, debug=tdenv.debug) # Update the DB file so we don't regenerate it. os.utime(tdb.dbFilename)
def regeneratePricesFile(tdb, tdenv): tdenv.DEBUG0("Regenerating .prices file") with tdb.pricesPath.open("w", encoding='utf-8') as pricesFile: prices.dumpPrices(tdb.dbFilename, prices.Element.full, file=pricesFile, debug=tdenv.debug) # Update the DB file so we don't regenerate it. os.utime(tdb.dbFilename)
def saveCopyOfChanges(cmdenv, dbFilename, stationID): if "APPEND_PRICES" in os.environ: mode = "a" else: mode = "w" dumpPath = pathlib.Path("updated.prices") with dumpPath.open(mode, encoding='utf-8') as dumpFile: # Remember the filename so we know we need to delete it. prices.dumpPrices( dbFilename, prices.Element.full | prices.Element.blanks, file=dumpFile, stationID=stationID, defaultZero=False, debug=cmdenv.debug, ) if not cmdenv.quiet: print("- Copy of changes saved as '{}'".format(str(dumpPath)))
def saveCopyOfChanges(cmdenv, dbFilename, stationID): if "APPEND_PRICES" in os.environ: mode = "a" else: mode = "w" dumpPath = pathlib.Path("updated.prices") with dumpPath.open(mode, encoding='utf-8') as dumpFile: # Remember the filename so we know we need to delete it. prices.dumpPrices(dbFilename, prices.Element.full | prices.Element.blanks, file=dumpFile, stationID=stationID, defaultZero=False, debug=cmdenv.debug, ) if not cmdenv.quiet: print("- Copy of changes saved as '{}'".format( str(dumpPath) ))
def editUpdate(tdb, cmdenv, stationID): """ Dump the price data for a specific station to a file and launch the user's text editor to let them make changes to the file. If the user makes changes, re-load the file, update the database and regenerate the master .prices file. """ cmdenv.DEBUG0("'update' mode with editor. editor:{} station:{}", cmdenv.editor, cmdenv.origin) editor, editorArgs = cmdenv.editor, [] if cmdenv.editing == 'sublime': cmdenv.DEBUG0("Sublime mode") editor = editor or \ getEditorPaths(cmdenv, "sublime", "SUBLIME_EDITOR", ["Sublime Text 3", "Sublime Text 2"], "sublime_text.exe", "subl" ) editorArgs += [ "--wait" ] elif cmdenv.editing == 'npp': cmdenv.DEBUG0("Notepad++ mode") editor = editor or \ getEditorPaths(cmdenv, "notepad++", "NOTEPADPP_EDITOR", ["Notepad++"], "notepad++.exe", "notepad++" ) if not cmdenv.quiet: print("NOTE: " "You'll need to exit Notepad++ when you are done.") elif cmdenv.editing == "vim": cmdenv.DEBUG0("VI iMproved mode") if not editor: # Hack... Hackity hack, hack, hack. # This has a disadvantage in that: we don't check for just "vim" (no .exe) under Windows vimDirs = [ "Git\\share\\vim\\vim{}".format(vimVer) for vimVer in range(70,75) ] editor = getEditorPaths(cmdenv, "vim", "EDITOR", vimDirs, "vim.exe", "vim" ) elif cmdenv.editing == "notepad": cmdenv.DEBUG0("Notepad mode") editor = editor or "notepad.exe" # herp try: envArgs = os.environ["EDITOR_ARGS"] if envArgs: editorArgs += envArgs.split(' ') except KeyError: pass # Create a temporary text file with a list of the price data. tmpPath = getTemporaryPath(cmdenv) absoluteFilename = None dbFilename = tdb.dbFilename try: elementMask = prices.Element.basic | prices.Element.supply if cmdenv.timestamps: elementMask |= prices.Element.timestamp if cmdenv.all: elementMask |= prices.Element.blanks # Open the file and dump data to it. with tmpPath.open("w", encoding='utf-8') as tmpFile: # Remember the filename so we know we need to delete it. absoluteFilename = str(tmpPath.resolve()) prices.dumpPrices(dbFilename, elementMask, file=tmpFile, stationID=stationID, defaultZero=cmdenv.forceNa, debug=cmdenv.debug ) # Stat the file so we can determine if the user writes to it. # Use the most recent create/modified timestamp. preStat = tmpPath.stat() preStamp = max(preStat.st_mtime, preStat.st_ctime) # Launch the editor editorCommandLine = [ editor ] + editorArgs + [ absoluteFilename ] cmdenv.DEBUG0("Invoking [{}]", ' '.join(editorCommandLine)) try: result = subprocess.call(editorCommandLine) except FileNotFoundError: raise CommandLineError( "Unable to launch requested editor: {}" .format(editorCommandLine) ) if result != 0: raise CommandLineError( "NO DATA IMPORTED: " "Your editor exited with a 'failed' exit code ({})" .format(result) ) # Did they update the file? Some editors destroy the file and rewrite it, # other files just write back to it, and some OSes do weird things with # these timestamps. That's why we have to use both mtime and ctime. postStat = tmpPath.stat() postStamp = max(postStat.st_mtime, postStat.st_ctime) if postStamp == preStamp: import random print("- No changes detected - doing nothing. {}".format( random.choice([ "Brilliant!", "I'll get my coat.", "I ain't seen you.", "You ain't seen me", "... which was nice", "Bingo!", "Scorchio!", "Boutros, boutros, ghali!", "I'm Ed Winchester!", "Suit you, sir! Oh!" ]) )) else: cache.importDataFromFile(tdb, cmdenv, tmpPath) saveCopyOfChanges(cmdenv, dbFilename, stationID) tmpPath.unlink() tmpPath = None except Exception as e: print("ERROR:", e) print() print("*** YOUR UPDATES WILL BE SAVED AS {} ***".format( "prices.last" )) # Save a copy if absoluteFilename and tmpPath: saveTemporaryFile(tmpPath) if "EXCEPTIONS" in os.environ: raise e
def editUpdate(tdb, cmdenv, stationID): """ Dump the price data for a specific station to a file and launch the user's text editor to let them make changes to the file. If the user makes changes, re-load the file, update the database and regenerate the master .prices file. """ cmdenv.DEBUG0("'update' mode with editor. editor:{} station:{}", cmdenv.editor, cmdenv.origin) editor, editorArgs = cmdenv.editor, [] if cmdenv.editing == 'sublime': cmdenv.DEBUG0("Sublime mode") editor = editor or \ getEditorPaths(cmdenv, "sublime", "SUBLIME_EDITOR", ["Sublime Text 3", "Sublime Text 2"], "sublime_text.exe", "subl" ) editorArgs += ["--wait"] elif cmdenv.editing == 'npp': cmdenv.DEBUG0("Notepad++ mode") editor = editor or \ getEditorPaths(cmdenv, "notepad++", "NOTEPADPP_EDITOR", ["Notepad++"], "notepad++.exe", "notepad++" ) if not cmdenv.quiet: print("NOTE: " "You'll need to exit Notepad++ when you are done.") elif cmdenv.editing == "vim": cmdenv.DEBUG0("VI iMproved mode") if not editor: # Hack... Hackity hack, hack, hack. # This has a disadvantage in that: we don't check for just "vim" (no .exe) under Windows vimDirs = [ "Git\\share\\vim\\vim{}".format(vimVer) for vimVer in range(70, 75) ] editor = getEditorPaths(cmdenv, "vim", "EDITOR", vimDirs, "vim.exe", "vim") elif cmdenv.editing == "notepad": cmdenv.DEBUG0("Notepad mode") editor = editor or "notepad.exe" # herp try: envArgs = os.environ["EDITOR_ARGS"] if envArgs: editorArgs += envArgs.split(' ') except KeyError: pass # Create a temporary text file with a list of the price data. tmpPath = getTemporaryPath(cmdenv) absoluteFilename = None dbFilename = tdb.dbFilename try: elementMask = prices.Element.basic | prices.Element.supply if cmdenv.timestamps: elementMask |= prices.Element.timestamp if cmdenv.all: elementMask |= prices.Element.blanks # Open the file and dump data to it. with tmpPath.open("w", encoding='utf-8') as tmpFile: # Remember the filename so we know we need to delete it. absoluteFilename = str(tmpPath.resolve()) prices.dumpPrices(dbFilename, elementMask, file=tmpFile, stationID=stationID, defaultZero=cmdenv.forceNa, debug=cmdenv.debug) # Stat the file so we can determine if the user writes to it. # Use the most recent create/modified timestamp. preStat = tmpPath.stat() preStamp = max(preStat.st_mtime, preStat.st_ctime) # Launch the editor editorCommandLine = [editor] + editorArgs + [absoluteFilename] cmdenv.DEBUG0("Invoking [{}]", ' '.join(editorCommandLine)) try: result = subprocess.call(editorCommandLine) except FileNotFoundError: raise CommandLineError( "Unable to launch requested editor: {}".format( editorCommandLine)) if result != 0: raise CommandLineError( "NO DATA IMPORTED: " "Your editor exited with a 'failed' exit code ({})".format( result)) # Did they update the file? Some editors destroy the file and rewrite it, # other files just write back to it, and some OSes do weird things with # these timestamps. That's why we have to use both mtime and ctime. postStat = tmpPath.stat() postStamp = max(postStat.st_mtime, postStat.st_ctime) if postStamp == preStamp: import random print("- No changes detected - doing nothing. {}".format( random.choice([ "Brilliant!", "I'll get my coat.", "I ain't seen you.", "You ain't seen me", "... which was nice", "Bingo!", "Scorchio!", "Boutros, boutros, ghali!", "I'm Ed Winchester!", "Suit you, sir! Oh!" ]))) else: cache.importDataFromFile(tdb, cmdenv, tmpPath) saveCopyOfChanges(cmdenv, dbFilename, stationID) tmpPath.unlink() tmpPath = None except Exception as e: print("ERROR:", e) print() print("*** YOUR UPDATES WILL BE SAVED AS {} ***".format("prices.last")) # Save a copy if absoluteFilename and tmpPath: saveTemporaryFile(tmpPath) if "EXCEPTIONS" in os.environ: raise e