From 881a967e313047dd7cfc954624d24e3bd99a3069 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Mon, 29 Dec 2014 21:28:16 -0800 Subject: [PATCH] Make import accept '-' as an alias for 'stdin' --- cache.py | 15 ++++++--------- commands/import_cmd.py | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/cache.py b/cache.py index 96dfd31d..4868b419 100644 --- a/cache.py +++ b/cache.py @@ -576,14 +576,12 @@ def processItemLine(matches): ###################################################################### -def processPricesFile(tdenv, db, pricesPath, defaultZero=False): +def processPricesFile(tdenv, db, pricesPath, pricesFh=None, defaultZero=False): tdenv.DEBUG0("Processing Prices file '{}'", pricesPath) - assert isinstance(pricesPath, Path) - - with pricesPath.open('rU') as pricesFile: + with pricesFh or pricesPath.open('rU') as pricesFh: warnings, items, buys, sells = processPrices( - tdenv, pricesFile, db, defaultZero + tdenv, pricesFh, db, defaultZero ) if items: @@ -883,16 +881,14 @@ def regeneratePricesFile(tdb, tdenv): ###################################################################### -def importDataFromFile(tdb, tdenv, path, reset=False): +def importDataFromFile(tdb, tdenv, path, pricesFh=None, reset=False): """ Import price data from a file on a per-station basis, that is when a new station is encountered, delete any existing records for that station in the database. """ - assert isinstance(path, Path) - - if not path.exists(): + if not pricesFh and not path.exists(): raise TradeException("No such file: {}".format( str(path) )) @@ -905,6 +901,7 @@ def importDataFromFile(tdb, tdenv, path, reset=False): processPricesFile(tdenv, db=tdb.getDB(), pricesPath=path, + pricesFh=pricesFh, ) # If everything worked, we may need to re-build the prices file. diff --git a/commands/import_cmd.py b/commands/import_cmd.py index 71c7af6a..cc745b10 100644 --- a/commands/import_cmd.py +++ b/commands/import_cmd.py @@ -123,17 +123,23 @@ def run(results, cmdenv, tdb): cmdenv.filename = filename # check the file exists. - filePath = Path(cmdenv.filename) - if not filePath.is_file(): - raise CommandLineError("File not found: {}".format( - str(filePath) - )) + if cmdenv.filename != "-": + fh = None + filePath = Path(cmdenv.filename) + if not filePath.is_file(): + raise CommandLineError("File not found: {}".format( + str(filePath) + )) + else: + filePath = "stdin" + fh = sys.stdin if cmdenv.plug: if not plugin.finish(): cache.regeneratePricesFile() return None - cache.importDataFromFile(tdb, cmdenv, filePath) + cache.importDataFromFile(tdb, cmdenv, filePath, pricesFh=fh) + return None