Skip to content

Commit

Permalink
Clear cached currencies on update and after APP_KEY is set
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Nov 3, 2017
1 parent 384fc3a commit 395208d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
Binary file not shown.
1 change: 1 addition & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
CRYPTO_COMPARE_BASE_URL = 'https://min-api.cryptocompare.com/data/price?fsym={}&tsyms={}'
SYMBOLS_PER_REQUEST = 20
USER_AGENT = 'Alfred Convert/{}'.format(os.getenv('alfred_workflow_version'))
NOKEY_FILENAME = 'nokey'

# ----------------------------------------------------------------------
# Unit definition files
Expand Down
36 changes: 36 additions & 0 deletions src/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from workflow import Workflow3, ICON_WARNING, ICON_INFO
from workflow.background import run_in_background, is_running
from workflow.update import Version
from config import (
bootstrap,
DEFAULT_UNIT_DEFINITIONS,
Expand All @@ -32,6 +33,8 @@
DEFAULT_SETTINGS,
HELP_URL,
ICON_UPDATE,
NOKEY_FILENAME,
OPENX_APP_KEY,
THOUSANDS_SEPARATOR,
UPDATE_SETTINGS,
)
Expand All @@ -44,6 +47,38 @@
# Q = ureg.Quantity


def handle_update(wf):
"""Clear cache on update.
Delete cached data if last-run version used a different format,
or if user has just added the ``APP_KEY`` for fiat currency
exchange rates.
"""
nokey = wf.cachefile(NOKEY_FILENAME)
clear = False

# Clear cache if previous version was old
lv = wf.last_version_run
log.debug('version=%s, last_version=%s', wf.version, lv)
if wf.version > Version('3.0') and lv < Version('3.1'):
log.debug('clearing cache: saved data is incompatible')
clear = True

if OPENX_APP_KEY:
if os.path.exists(nokey):
os.unlink(nokey)
log.debug('clearing cache: APP_KEY was set')
clear = True
else:
if not os.path.exists(nokey):
open(nokey, 'wb').write('')

if clear:
wf.cache_data(CURRENCY_CACHE_NAME, None)
log.debug('cleared old cached currencies')


class NoToUnits(Exception):
"""Raised if there are no to units (or defaults)."""

Expand Down Expand Up @@ -390,6 +425,7 @@ def main(wf):
query = wf.args[0] # .lower()
log.debug('query : %s', query)

handle_update(wf)
# Create data files if necessary
bootstrap(wf)

Expand Down
7 changes: 4 additions & 3 deletions src/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def load_openx_rates(symbols):
wanted = set(symbols)
if not OPENX_APP_KEY:
log.warning(
'Not fetching fiat currency exchange rates: '
'not fetching fiat currency exchange rates: '
'APP_KEY for openexchangerates.org not set. '
'Please sign up for a free account here: '
'https://openexchangerates.org/signup/free'
Expand Down Expand Up @@ -147,7 +147,7 @@ def load_active_currencies():
with open(user_currencies) as fp:
for line in fp:
line = line.strip()
if line.startswith('#'):
if not line or line.startswith('#'):
continue

symbols.add(line.upper())
Expand Down Expand Up @@ -201,7 +201,8 @@ def main(wf):
start_time = time.time()
bootstrap(wf)

log.info('fetching exchange rates from Yahoo! and CryptoCompare.com ...')
log.info('fetching exchange rates from OpenExchangeRates.org and '
'CryptoCompare.com ...')

rates = wf.cached_data(CURRENCY_CACHE_NAME,
fetch_exchange_rates,
Expand Down
2 changes: 1 addition & 1 deletion src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ UPDATE_INTERVAL is the number of minutes between exchange rate updates.</string>
<string>APP_KEY</string>
</array>
<key>version</key>
<string>3.2</string>
<string>3.2.1</string>
<key>webaddress</key>
<string></string>
</dict>
Expand Down

0 comments on commit 395208d

Please sign in to comment.