Skip to content

Commit

Permalink
Made it more obvious that warnings are warnings during import
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Dec 26, 2014
1 parent 1ad582d commit 9e7d483
Showing 1 changed file with 55 additions and 37 deletions.
92 changes: 55 additions & 37 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ class BuildCacheBaseException(TradeException):
def __init__(self, fromFile, lineNo, error=None):
self.fileName = fromFile.name
self.lineNo = lineNo
self.category = "ERROR"
self.error = error or "UNKNOWN ERROR"

def __str__(self):
return "{}:{} {}".format(self.fileName, self.lineNo, self.error)
return "{}:{} {} {}".format(
self.fileName, self.lineNo,
self.category,
self.error,
)


class UnknownStationError(BuildCacheBaseException):
Expand Down Expand Up @@ -310,6 +315,18 @@ def processPrices(tdenv, priceFile, db, defaultZero):
DELETED = corrections.DELETED
items, buys, sells = [], [], []

warnings = 0

def ignoreOrWarn(error):
nonlocal warnings
if not tdenv.ignoreUnknown:
raise error
if not tdenv.quiet:
error.category = "WARNING"
print(error)
warnings += 1


def changeStation(matches):
nonlocal categoryID, facility, stationID
nonlocal processedStations, processedItems
Expand Down Expand Up @@ -361,12 +378,10 @@ def changeStation(matches):
stationID = -1

if stationID < 0 :
ex = UnknownStationError(priceFile, lineNo, facility)
if not tdenv.ignoreUnknown:
raise ex
stationID = DELETED
if not tdenv.quiet:
print(ex)
ignoreOrWarn(
UnknownStationError(priceFile, lineNo, facility)
)
return

# Check for duplicates
Expand Down Expand Up @@ -411,12 +426,10 @@ def changeCategory(matches):
categoryID = categoriesByName[categoryName]
tdenv.DEBUG1("Renamed: {}", categoryName)
except KeyError:
ex = UnknownCategoryError(priceFile, lineNo, categoryName)
if not tdenv.ignoreUnknown:
raise ex
if not tdenv.quiet:
print(ex)
categoryID = DELETED
ignoreOrWarn(
UnknownCategoryError(priceFile, lineNo, categoryName)
)
return


Expand All @@ -440,11 +453,9 @@ def processItemLine(matches):
itemID = itemByName[itemName]
tdenv.DEBUG1("Renamed {} -> {}", oldName, itemName)
except KeyError:
ex = UnknownItemError(priceFile, lineNo, itemName)
if not tdenv.ignoreUnknown:
raise ex
if not tdenv.quiet:
print(ex)
ignoreOrWarn(
UnknownItemError(priceFile, lineNo, itemName)
)
return

# Check for duplicate items within the station.
Expand Down Expand Up @@ -560,7 +571,7 @@ def processItemLine(matches):

processItemLine(matches)

return items, buys, sells
return warnings, items, buys, sells


######################################################################
Expand All @@ -571,26 +582,33 @@ def processPricesFile(tdenv, db, pricesPath, defaultZero=False):
assert isinstance(pricesPath, Path)

with pricesPath.open('rU') as pricesFile:
items, buys, sells = processPrices(tdenv, pricesFile, db, defaultZero)
if items:
db.executemany("""
INSERT INTO StationItem
(station_id, item_id, modified)
VALUES (?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", items)
if sells:
db.executemany("""
INSERT INTO StationSelling
(station_id, item_id, price, units, level, modified)
VALUES (?, ?, ?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", sells)
if buys:
db.executemany("""
INSERT INTO StationBuying
(station_id, item_id, price, units, level, modified)
VALUES (?, ?, ?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", buys)
db.commit()
warnings, items, buys, sells = processPrices(
tdenv, pricesFile, db, defaultZero
)

if items:
db.executemany("""
INSERT INTO StationItem
(station_id, item_id, modified)
VALUES (?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", items)
if sells:
db.executemany("""
INSERT INTO StationSelling
(station_id, item_id, price, units, level, modified)
VALUES (?, ?, ?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", sells)
if buys:
db.executemany("""
INSERT INTO StationBuying
(station_id, item_id, price, units, level, modified)
VALUES (?, ?, ?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", buys)

db.commit()

if warnings and not tdenv.quiet:
print("Import completed despite warnings")


######################################################################
Expand Down

0 comments on commit 9e7d483

Please sign in to comment.