From f820ebd4c5501104736f77cbed766278818ecb39 Mon Sep 17 00:00:00 2001 From: Damien Date: Thu, 26 Aug 2021 18:11:02 +1000 Subject: [PATCH] Add conversion hints. Fixes #2 --- .gitignore | 3 + plugin.json | 2 +- .../translations/en/LC_MESSAGES/messages.mo | Bin 487 -> 487 bytes .../translations/en/LC_MESSAGES/messages.po | 22 ++-- .../translations/zh/LC_MESSAGES/messages.mo | Bin 885 -> 942 bytes .../translations/zh/LC_MESSAGES/messages.po | 32 +++--- plugin/ui.py | 9 +- plugin/utils.py | 14 +++ scratch.py | 95 +++++++----------- 9 files changed, 92 insertions(+), 85 deletions(-) diff --git a/.gitignore b/.gitignore index 0023b68..9a1d6f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Created by https://www.toptal.com/developers/gitignore/api/python,vscode # Edit at https://www.toptal.com/developers/gitignore?templates=python,vscode +### Project Specific ### +scratch.py + ### Python ### # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/plugin.json b/plugin.json index 933850e..ac7020d 100644 --- a/plugin.json +++ b/plugin.json @@ -4,7 +4,7 @@ "Name": "General Converter", "Description": "General weights and measures converter", "Author": "deefrawley", - "Version": "1.0.1", + "Version": "1.0.2", "Language": "python", "Website": "https://github.com/deefrawley/Flow.Launcher.Plugin.GenConvert", "IcoPath": "assets/favicon.ico", diff --git a/plugin/translations/en/LC_MESSAGES/messages.mo b/plugin/translations/en/LC_MESSAGES/messages.mo index fde4e09d230d11b21730665d8ea48c9f2f98fa0d..ed4515bc02538e80cadeff7c47ed82fb3e29c0ea 100644 GIT binary patch delta 30 lcmaFP{G540EUTfOfu7OCBv&pAT_ZCELklYdlZ_kA7y*tp2!#Lu delta 30 lcmaFP{G540EUS^8fu6y{Bv&poT?2CkLsKh5vyB_g7y*s;2!Q|q diff --git a/plugin/translations/en/LC_MESSAGES/messages.po b/plugin/translations/en/LC_MESSAGES/messages.po index c1b55af..5f47651 100644 --- a/plugin/translations/en/LC_MESSAGES/messages.po +++ b/plugin/translations/en/LC_MESSAGES/messages.po @@ -1,13 +1,13 @@ # English translations for Flow.plugin.currency. # Copyright (C) 2020 CitizenDee (author) # This file is distributed under the same license as the project. -# CitizenDee , 2020. +# CitizenDee , 2021. # msgid "" msgstr "" -"Project-Id-Version: 2.0.0\n" +"Project-Id-Version: 1.0.2\n" "Report-Msgid-Bugs-To: deefrawley@gmail.com\n" -"POT-Creation-Date: 2021-06-07 15:16+1000\n" +"POT-Creation-Date: 2021-08-26 18:04+1000\n" "PO-Revision-Date: 2020-12-13 20:26+1100\n" "Last-Translator: CitizenDee \n" "Language: en\n" @@ -18,29 +18,33 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 2.9.1\n" -#: plugin/ui.py:49 plugin/ui.py:58 +#: plugin/ui.py:42 +msgid "Available conversions" +msgstr "" + +#: plugin/ui.py:54 plugin/ui.py:63 msgid "Error - {}" msgstr "Error - {}" -#: plugin/ui.py:50 +#: plugin/ui.py:55 msgid "Check documentation for accepted units" msgstr "" -#: plugin/ui.py:62 +#: plugin/ui.py:67 #, fuzzy msgid "General Converter" msgstr "Currency Converter" -#: plugin/ui.py:63 +#: plugin/ui.py:68 #, fuzzy msgid " " msgstr " " -#: plugin/utils.py:50 +#: plugin/utils.py:65 msgid "To and from unit is the same" msgstr "" -#: plugin/utils.py:83 +#: plugin/utils.py:98 msgid "Problem converting {} and {}" msgstr "" diff --git a/plugin/translations/zh/LC_MESSAGES/messages.mo b/plugin/translations/zh/LC_MESSAGES/messages.mo index 2d35d630e37a2ca7b8c2921577cf111e174fd697..68c56a8aec83e46d38cf25c96cb832207e121573 100644 GIT binary patch delta 312 zcmey$wvN62o)F7a1|Z-7Vi_Qg0dbJP93a~Wi01;aC=g!&Vl^Os2E<%I%*)8YAPA%- zfHX+I7L;}e(mg<&0>nB%@sCUl3_L(ujv1&1NNWRWZUzP^hA}eUBQpg<3o8SY$txM{6`V6m mGOJSaTvAgNY_J(LiRly1)Bg3(r>uCfcg?fjMUy`=wEzIp$VR3B delta 234 zcmZ3-{*|r%o)F7a1|VPuVi_O~0dbH(50GsH#JxbQ2E+$|m(brYM21V&)9i~r{wV9g%AqgpF diff --git a/plugin/translations/zh/LC_MESSAGES/messages.po b/plugin/translations/zh/LC_MESSAGES/messages.po index 93bc741..d10321a 100644 --- a/plugin/translations/zh/LC_MESSAGES/messages.po +++ b/plugin/translations/zh/LC_MESSAGES/messages.po @@ -1,15 +1,15 @@ -# Chinese translations for PROJECT. -# Copyright (C) 2021 ORGANIZATION -# This file is distributed under the same license as the PROJECT project. -# FIRST AUTHOR , 2021. +# English translations for Flow.plugin.currency. +# Copyright (C) 2020 CitizenDee (author) +# This file is distributed under the same license as the project. +# CitizenDee , 2021. # msgid "" msgstr "" -"Project-Id-Version: PROJECT VERSION\n" -"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2021-06-07 15:16+1000\n" +"Project-Id-Version: 1.0.2\n" +"Report-Msgid-Bugs-To: deefrawley@gmail.com\n" +"POT-Creation-Date: 2021-08-26 18:04+1000\n" "PO-Revision-Date: 2021-06-07 15:16+1000\n" -"Last-Translator: FULL NAME \n" +"Last-Translator: CitizenDee \n" "Language: zh\n" "Language-Team: zh \n" "Plural-Forms: nplurals=1; plural=0\n" @@ -18,27 +18,31 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 2.9.1\n" -#: plugin/ui.py:49 plugin/ui.py:58 +#: plugin/ui.py:42 +msgid "Available conversions" +msgstr "可用转换" + +#: plugin/ui.py:54 plugin/ui.py:63 msgid "Error - {}" msgstr "错误 - {}" -#: plugin/ui.py:50 +#: plugin/ui.py:55 msgid "Check documentation for accepted units" msgstr "检查文档以获取正确的度量单位" -#: plugin/ui.py:62 +#: plugin/ui.py:67 msgid "General Converter" msgstr "通用单位换算器" -#: plugin/ui.py:63 +#: plugin/ui.py:68 msgid " " msgstr "<热键> <量> <源单元> <目的地单位>" -#: plugin/utils.py:50 +#: plugin/utils.py:65 msgid "To and from unit is the same" msgstr "源单元和目标单元相同" -#: plugin/utils.py:83 +#: plugin/utils.py:98 msgid "Problem converting {} and {}" msgstr "转换问题 {} 和 {}" diff --git a/plugin/ui.py b/plugin/ui.py index 33a3be4..0a47081 100644 --- a/plugin/ui.py +++ b/plugin/ui.py @@ -36,8 +36,13 @@ def sendActionMess(self, title: str, subtitle: str, method: str, value: List): def query(self, param: str) -> List[dict]: q = param.strip() args = q.split(" ") - - if len(args) == 3: + if len(args) == 2: + hints = plugin.utils.get_hints(args[1].lower()) + self.sendNormalMess( + _("Available conversions"), + (f"{args[0]} {args[1]} to {', '.join(hints)}"), + ) + elif len(args) == 3: try: # Units are currently case insensitive. May need to change this if in future new units # with official upper case shorthand are catered for diff --git a/plugin/utils.py b/plugin/utils.py index 6525982..fa74745 100644 --- a/plugin/utils.py +++ b/plugin/utils.py @@ -45,7 +45,21 @@ } +def get_hints(from_unit): + """ Takes an input unit and returns a list of units it can be converted to """ + for u in units: + if u == from_unit: + return [x for x in units[from_unit].keys()] + for u2 in units[u]: + if u2 == from_unit: + c = [x for x in units[u].keys() if x != from_unit] + c.append(u) + return c + return ["no valid units"] + + def genConvert(amount, from_unit, to_unit): + """ Convert between units """ conversions = {} if from_unit == to_unit: conversions["Error"] = _("To and from unit is the same") diff --git a/scratch.py b/scratch.py index d3924be..65812c6 100644 --- a/scratch.py +++ b/scratch.py @@ -1,5 +1,3 @@ -from plugin.settings import __package_title__, __version__ - units = { "m": { "mm": 1000, @@ -11,6 +9,7 @@ "mi": 0.0006213712, }, "ml": { + "gm": 1, "l": 0.001, "pt": 0.002113383, "qt": 0.001056691, @@ -18,66 +17,44 @@ "tbsp": 0.067628224, "tsp": 0.2028846715942, "gal": 0.0002641727499999601, + "floz": 0.03381413, + }, + "sqm": { + "h": 0.0001, + "ac": 0.0002471052, + "sqcm": 10000, + "sqkm": 1000000, + "sqin": 1550.003, + "sqmi": 0.0000003861022, + "sqft": 10.76391, + "sqyd": 1.19599, + }, + "gm": { + "kg": 0.001, + "lb": 0.002205, + "oz": 0.035274, + "st": 0.000157473, + "ton": 0.000001102310999995, + }, + # Special case temp C to F - actually handled in code + "c": { + "f": 1.8, }, } -def genConvert(amount, from_unit, to_unit): - conversions = {} - if from_unit == to_unit: - conversions["Error"] = "To and from unit is the same" - return conversions - # Convert from key unit to sub-unit (e.g. cm to in)gc 1 km mi - elif from_unit in units and to_unit in units[from_unit]: - conversions[to_unit] = units[from_unit][to_unit] * amount - return conversions - # Convert from sub-unit to key unit (e.g. in to cm) - elif to_unit in units and from_unit in units[to_unit]: - conversions[to_unit] = (1 / units[to_unit][from_unit]) * amount - return conversions - # Convert from sub-unit to sub-unit (e.g. in to ft) - else: - for u in units: - to_sub = False - from_sub = False - for u2 in units[u]: - if u2 == to_unit: - to_sub = True - elif u2 == from_unit: - from_sub = True - if to_sub and from_sub: - # Convert via the key unit - key_convert = ((1 / units[u][from_unit]) * amount) * 10 - key_sub = units[u][to_unit] * key_convert - print(f"key_convert {amount} {from_unit} = {key_convert}") - print(f"key_sub = {key_sub}") - - conversions[to_unit] = (1 / units[u][from_unit]) * ( - units[u][to_unit] * amount - ) - return conversions - if not to_sub or not from_sub: - conversions["Error"] = "Unit not found" - return conversions - - -def listUnits(): - # Utility function to print out all conversions - print(f"{__package_title__} v{__version__} Units") - print("==============================") - for all_units in units.keys(): - print(f"\n{all_units}, ", end="") - for all_to_units in units[all_units].keys(): - print(f"{all_to_units}, ", end="") - +def get_hints(from_unit): + """ Takes an input unit and returns a list of units it can be converted to """ + for u in units: + if u == from_unit: + return [x for x in units[from_unit].keys()] + for u2 in units[u]: + if u2 == from_unit: + c = [x for x in units[u].keys() if x != from_unit] + c.append(u) + return c + return ["no valid units"] -""" result = input(" > ") -q = result.strip() -args = q.split(" ") -result2 = genConvert(float(args[1]), args[2], args[3]) -if "Error" in result2: - print(f"Error - {result2['Error']}") -else: - print(f"{args[1]} {args[2]} = {result2[args[3]]:.6f} {args[3]}") """ -listUnits() \ No newline at end of file +vv = get_hints("ox") +print(vv) \ No newline at end of file