Skip to content

Commit

Permalink
Merge branch 'hotfix/bug_on_startup'
Browse files Browse the repository at this point in the history
  • Loading branch information
AvatarHurden committed Mar 3, 2018
2 parents db31f33 + f894d49 commit 8ce7e4b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/currency.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[defaults]

# Should the plugin try to convert currencies directly in search?
# * If no, you will have to select the catalog item before entering the
# * If false, you will have to select the catalog item before entering the
# currencies to convert
# * Default: true
#always_evaluate = true
Expand Down
28 changes: 13 additions & 15 deletions src/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,12 @@ def on_start(self):
def on_catalog(self):
catalog = []

catalog.append(self.create_item(
category=self.ITEMCAT_UPDATE,
label='Update Currency',
short_desc='Last updated at ' + self.broker.last_update.isoformat(),
target="updatecurrency",
args_hint=kp.ItemArgsHint.FORBIDDEN,
hit_hint=kp.ItemHitHint.IGNORE))

if self.default_item_enabled:
catalog.append(self._create_translate_item(
label=self.default_item_label))

self.set_catalog(catalog)
self._update_update_item()

def on_suggest(self, user_input, items_chain):
suggestions = []
Expand All @@ -120,6 +113,8 @@ def on_suggest(self, user_input, items_chain):
if not query['from_cur'] or not query['to_cur'] or not user_input:
return

if self.broker.tryUpdate():
self._update_update_item()
results = self.broker.convert(query['amount'], query['from_cur'], query['to_cur'])

for result in results:
Expand All @@ -143,13 +138,7 @@ def on_suggest(self, user_input, items_chain):
def on_execute(self, item, action):
if item.category() == self.ITEMCAT_UPDATE:
self.broker.update()
self.merge_catalog([self.create_item(
category=self.ITEMCAT_UPDATE,
label='Update Currency',
short_desc='Last updated at ' + self.broker.last_update.isoformat(),
target="updatecurrency",
args_hint=kp.ItemArgsHint.FORBIDDEN,
hit_hint=kp.ItemHitHint.IGNORE)])
self._update_update_item()
return
if item.category() != self.ITEMCAT_RESULT:
return
Expand Down Expand Up @@ -213,6 +202,15 @@ def _parse_and_merge_input(self, user_input=None, empty=False):
query['amount'] = float(m.group('amount').rstrip().replace(',', '.'))
return query

def _update_update_item(self):
self.merge_catalog([self.create_item(
category=self.ITEMCAT_UPDATE,
label='Update Currency',
short_desc='Last updated at ' + self.broker.last_update.isoformat(),
target="updatecurrency",
args_hint=kp.ItemArgsHint.FORBIDDEN,
hit_hint=kp.ItemHitHint.IGNORE)])

def _create_translate_item(self, label):

def joinCur(lst):
Expand Down
17 changes: 13 additions & 4 deletions src/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,28 @@ class ExchangeRates():

_file_path = None
last_update = None
update_freq = None
_currencies = {}

def __init__(self, path, update_freq):
self.update_freq = update_freq
self._file_path = os.path.join(path, 'rates.json')

if os.path.exists(self._file_path):
self.load_from_file()
else:
self.update()
try:
self.load_from_file()
except Exception as e:
self.update()

self.tryUpdate()

def tryUpdate(self):
time_diff = datetime.now() - self.last_update
if (update_freq.value == UpdateFreq.HOURLY.value and time_diff.total_seconds() >= 3600) or (update_freq.value == UpdateFreq.DAILY.value and time_diff.days >= 1):
if (self.update_freq.value == UpdateFreq.HOURLY.value and time_diff.total_seconds() >= 3600) or (self.update_freq.value == UpdateFreq.DAILY.value and time_diff.days >= 1):
self.update()
return True
else:
return False

def update(self):
self.load_from_url()
Expand Down

0 comments on commit 8ce7e4b

Please sign in to comment.