Skip to content

Commit

Permalink
Merge branch 'release/v2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
AvatarHurden committed Mar 21, 2019
2 parents bb36232 + 8df1943 commit 072aea8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ You can also use parentheses and the negative operator (`-(3 + 4) * 4`, for exam
For those familiar with BNF grammars and regex, below is grammar accepted by the parser (`prog` is the top-level expression):

```
prog := sources (to_key destinations)? extra?
prog := sources (to_key? destinations)? extra?
to_key := 'to' | 'in' | ':'
Expand All @@ -63,7 +63,7 @@ cur_code := ([^0-9\s+-/*^()]+)
extra := ('+' | '-' | '*' | '/' | '**' | '^' ) expr
sources := source ('+' | '-')? sources | source
sources := source ('+' | '-') sources | source
source := '(' source ')'
| cur_code expr
| expr (cur_code?)
Expand All @@ -80,6 +80,12 @@ number := (0|[1-9][0-9]*)([.,][0-9]+)?([eE][+-]?[0-9]+)?

## Change Log

### v2.1

* Improved grammar for more intuitive use
* Bug fixes
* Improved options to copy results to clipboard

### v2.0

* Improved parser. More flexible, and now you can specify your own separators in the config file
Expand Down
36 changes: 22 additions & 14 deletions src/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ class Currency(kp.Plugin):
DEFAULT_UPDATE_FREQ = 'daily'
DEFAULT_ALWAYS_EVALUATE = True
DEFAULT_ITEM_LABEL = 'Convert Currency'
DEFAULT_SEPARATORS = 'to, in, :'
DEFAULT_DESTINATION_SEPARATORS = 'and; &, ,'
DEFAULT_SEPARATORS = 'to in :'
DEFAULT_DESTINATION_SEPARATORS = 'and & ,'

default_item_enabled = DEFAULT_ITEM_ENABLED
update_freq = UpdateFreq(DEFAULT_UPDATE_FREQ)
always_evaluate = DEFAULT_ALWAYS_EVALUATE
default_item_label = DEFAULT_ITEM_LABEL

ACTION_COPY_RESULT = 'copy_result'
ACTION_COPY_EQUATION = 'copy_equation'
ACTION_COPY_AMOUNT = 'copy_amount'

broker = None
Expand All @@ -69,14 +70,18 @@ def on_start(self):
self._read_config()

actions = [
self.create_action(
name=self.ACTION_COPY_AMOUNT,
label="Copy result",
short_desc="Copy the result to clipboard"),
self.create_action(
name=self.ACTION_COPY_RESULT,
label="Copy result with code",
short_desc="Copy result (with code) to clipboard")]
short_desc="Copy result (with code) to clipboard"),
self.create_action(
name=self.ACTION_COPY_AMOUNT,
label="Copy numerical result",
short_desc="Copy numerical result to clipboard"),
self.create_action(
name=self.ACTION_COPY_EQUATION,
label="Copy conversion",
short_desc="Copy conversion equation to clipboard")]

self.set_actions(self.ITEMCAT_RESULT, actions)

Expand Down Expand Up @@ -132,7 +137,8 @@ def on_suggest(self, user_input, items_chain):
suggestions.append(self._create_result_item(
label=result['title'],
short_desc=result['description'],
target=result['title']
target=result['title'],
data_bag=result['description']
))
except Exception as exc:
suggestions.append(self.create_error_item(
Expand All @@ -156,12 +162,14 @@ def on_execute(self, item, action):

# browse or copy url
if action and action.name() == self.ACTION_COPY_AMOUNT:
amount = item.data_bag()[:-4]
amount = item.label()[:-4]

kpu.set_clipboard(amount)
elif action and action.name() == self.ACTION_COPY_EQUATION:
kpu.set_clipboard(item.data_bag() + ' = ' + item.label())
# default action: copy result (ACTION_COPY_RESULT)
else:
kpu.set_clipboard(item.data_bag())
kpu.set_clipboard(item.label())

def on_activated(self):
pass
Expand Down Expand Up @@ -234,15 +242,15 @@ def joinCur(lst):
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.NOARGS)

def _create_result_item(self, label, short_desc, target):
def _create_result_item(self, label, short_desc, target, data_bag):
return self.create_item(
category=self.ITEMCAT_RESULT,
label=label,
short_desc=short_desc,
target=target,
args_hint=kp.ItemArgsHint.REQUIRED,
args_hint=kp.ItemArgsHint.FORBIDDEN,
hit_hint=kp.ItemHitHint.NOARGS,
data_bag=label)
data_bag=data_bag)

def _read_config(self):
def _warn_cur_code(name, fallback):
Expand Down Expand Up @@ -315,7 +323,7 @@ def _warn_cur_code(name, fallback):

# aliases
self.broker.clear_aliases()

keys = settings.keys(self.ALIAS_SECTION)
for key in keys:
try:
Expand Down
2 changes: 1 addition & 1 deletion src/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ExchangeRates():
_aliases = {}

in_cur_fallback = 'USD'
out_cur_fallback = 'EUR, GBP'
out_cur_fallback = 'EUR GBP'

default_cur_in = 'USD'
default_curs_out = ['EUR', 'GBP']
Expand Down
23 changes: 16 additions & 7 deletions src/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Grammar
#
# prog := sources (to_key destinations)? extra?
# prog := sources (to_key? destinations)? extra?
#
# to_key := 'to' | 'in' | ':'
#
Expand All @@ -14,7 +14,7 @@
#
# extra := ('+' | '-' | '*' | '/' | '**' | '^' ) expr
#
# sources := source ('+' | '-')? sources | source
# sources := source ('+' | '-') sources | source
# source := '(' source ')'
# | cur_code expr
# | expr (cur_code?)
Expand Down Expand Up @@ -160,10 +160,19 @@ def source():
@generate
def sources():
first = yield lexeme(source)
op = yield (s('+') | s('-')).optional()
rest = yield sources.optional()
if op == '-' and rest:
rest[0]['amount'] *= -1

@generate
def more_sources():
op = yield (s('+') | s('-'))
rest = yield sources
return op, rest

more = yield more_sources.optional()
rest = None
if more:
op, rest = more
if op == '-':
rest[0]['amount'] *= -1
return [first] + (rest if rest else [])

@generate
Expand All @@ -190,7 +199,7 @@ def extra():
@generate
def parser():
source = yield sources
destination = yield (to_parser() >> destinations).optional()
destination = yield (to_parser().optional() >> destinations).optional()
extras = yield extra.optional()
if extras:
op = extras['operation']
Expand Down

0 comments on commit 072aea8

Please sign in to comment.