def handle_delimited_query(query): """Process sub-commands. Args: query (str): User query """ # Currencies or decimal places if query.endswith(DELIMITER): # User deleted trailing space run_trigger('config') # subprocess.call(['osascript', '-e', ALFRED_AS]) # return mode, query = [s.strip() for s in query.split(DELIMITER)] if mode == 'currencies': currencies = sorted([(name, symbol) for (symbol, name) in CURRENCIES.items()] + [(name, symbol) for (symbol, name) in CRYPTO_CURRENCIES.items()]) if query: currencies = wf.filter(query, currencies, key=lambda t: ' '.join(t), match_on=MATCH_ALL ^ MATCH_ALLCHARS, min_score=30) else: # Show last update time age = wf.cached_data_age(CURRENCY_CACHE_NAME) if age > 0: # Exchange rates in cache td = timedelta(seconds=age) wf.add_item('Exchange rates updated {}'.format( human_timedelta(td)), icon=ICON_INFO) if not currencies: wf.add_item('No matching currencies', 'Try a different query', icon=ICON_WARNING) for name, symbol in currencies: wf.add_item(u'{} // {}'.format(name, symbol), u'Use `{}` in conversions'.format(symbol), copytext=symbol, valid=False, icon=ICON_CURRENCY) wf.send_feedback()
def main(wf): """Run Script Filter. Args: wf (workflow.Workflow): Workflow object. Returns: int: Exit status. """ args = docopt(__doc__, wf.args) log.debug('args : {!r}'.format(args)) query = args.get('<query>') if args.get('--openhelp'): subprocess.call(['open', README_URL]) return 0 if args.get('--openunits'): path = wf.datafile(CUSTOM_DEFINITIONS_FILENAME) if not os.path.exists(path): shutil.copy( wf.workflowfile('{0}.sample'.format( CUSTOM_DEFINITIONS_FILENAME)), path) subprocess.call(['open', path]) return 0 if args.get('--places'): value = int(query) log.debug('Setting `decimal_places` to {!r}'.format(value)) wf.settings['decimal_places'] = value print('Set decimal places to {}'.format(value)) # subprocess.call(['osascript', '-e', ALFRED_AS]) return 0 if not query or not query.strip(): wf.add_item('View Help File', 'Open help file in your browser', valid=True, arg='--openhelp', icon=ICON_HELP) wf.add_item('View Supported Currencies', 'View and search list of supported currencies', autocomplete=' currencies {0} '.format(DELIMITER), icon=ICON_CURRENCY) wf.add_item(('Decimal Places in Results ' '(current : {0})'.format(wf.settings.get( 'decimal_places', DECIMAL_PLACES_DEFAULT))), 'View and search list of supported currencies', autocomplete=' places {0} '.format(DELIMITER), icon=ICON_SETTINGS) wf.add_item('Edit Custom Units', 'Add and edit your own custom units', valid=True, arg='--openunits', icon='icon.png') wf.send_feedback() return 0 else: # Currencies or decimal places if query.endswith(DELIMITER): # User deleted trailing space subprocess.call(['osascript', '-e', ALFRED_AS]) return 0 mode, query = [s.strip() for s in query.split(DELIMITER)] if mode == 'currencies': currencies = sorted([(name, symbol) for (symbol, name) in CURRENCIES.items()]) if query: currencies = wf.filter(query, currencies, key=lambda t: ' '.join(t), match_on=MATCH_ALL ^ MATCH_ALLCHARS, min_score=30) else: # Show last update time age = wf.cached_data_age(CURRENCY_CACHE_NAME) if age > 0: # Exchange rates in cache td = timedelta(seconds=age) wf.add_item('Exchange rates updated {}'.format( human_timedelta(td)), icon=ICON_INFO) if not currencies: wf.add_item('No matching currencies', 'Try a different query', icon=ICON_WARNING) for name, symbol in currencies: wf.add_item('{0} // {1}'.format(name, symbol), 'Use `{0}` in conversions'.format(symbol), icon=ICON_CURRENCY) wf.send_feedback() elif mode == 'places': if query: if not query.isdigit(): wf.add_item('Invalid number : {0}'.format(query), 'Please enter a number', icon=ICON_WARNING) else: wf.add_item('Set decimal places to : {0}'.format(query), 'Hit `ENTER` to save', valid=True, arg='--places {0}'.format(query), icon=ICON_SETTINGS) else: wf.add_item('Enter a number of decimal places', 'Current number is {0}'.format( wf.settings.get('decimal_places', DECIMAL_PLACES_DEFAULT)), icon=ICON_INFO) wf.send_feedback()