Skip to content

Commit

Permalink
Make import accept '-' as an alias for 'stdin'
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Dec 30, 2014
1 parent 8a21ed0 commit 881a967
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
15 changes: 6 additions & 9 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
))
Expand All @@ -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.
Expand Down
18 changes: 12 additions & 6 deletions commands/import_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 881a967

Please sign in to comment.