diff --git a/.gitignore b/.gitignore index 9a1d6f0..61f71b6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ ### Project Specific ### scratch.py +app.xcf ### Python ### # Byte-compiled / optimized / DLL files diff --git a/LICENSE b/LICENSE index 70caa44..483ea73 100644 --- a/LICENSE +++ b/LICENSE @@ -19,3 +19,9 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Graduated Cylinder icon by AS Design from the Noun Project +area icon by matteo manenti from the Noun Project +distance icon by Anthony Ledoux from the Noun Project +Weight icon by Adrien Coquet from the Noun Project +Temperature icon by Alexander Skowalsky from the Noun Project diff --git a/README.md b/README.md index 22e5bdc..1c50ab2 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ +[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) # General Converter (Flow.Launcher.GenConvert) -General weight, distance, area, temperature converter for the [Flow Launcher](https://github.com/Flow-Launcher/Flow.Launcher) +General weight, volume, distance, area, temperature converter for the [Flow Launcher](https://github.com/Flow-Launcher/Flow.Launcher) ### About ### Requirements -Python 3.5 or later installed on your system, with python.exe in your PATH variable and this path updated in the Flow Launcher settings (this is a general requirement to use Python plugins with Flow). As of v1.8, Flow Launcher should take care of the installation of Python for you if it is not on your system. +Python 3.5 or later. As of Flow Launcher v1.8, Flow should take care of the installation of Python for you if it is not on your system. ### Installing @@ -26,8 +27,11 @@ Currently English and Chinese language supported. Edit the .env file to change t | Keyword | Description | | ----------------------------------- | -------------------------------------------------- | -| `gc ` | Convert the amount of the from unit to the to unit | +| `gc ` | Convert the amount of the from unit to the to unit. | +Just entering the keyword will give you the full list of units to choose from in Flow. + +Entering the keyword, amount and from unit will give you a subset list of units the from unit can be converted to #### Units The following units and their abbreviations can be used (each table can only convert among its' own units): @@ -37,12 +41,13 @@ The following units and their abbreviations can be used (each table can only con Subject Unit Abbreviation + Distance Logo - Distance + Distance millimetre mm - + centimetre cm @@ -78,43 +83,76 @@ The following units and their abbreviations can be used (each table can only con Subject Unit Abbreviation + Volume Logo - Fluids + Volume millilitre ml - + + + gram + gm + litre l - pint + pint US pt - quart + pint Imperial + ptimp + + + quart US qt - cup + quart Imperial + qtimp + + + cup US cup - teaspoon + cup Imperial + cupimp + + + teaspoon US tsp - tablespoon + teaspoon Imperial + tspimp + + + tablespoon US tbsp - gallon + tablespoon Imperial + tbspimp + + + gallon US gal - gram - gm + gallon Imperial + galimp + + + fluid ounce US + floz + + + fluid ounce Imperial + flozimp @@ -123,6 +161,7 @@ The following units and their abbreviations can be used (each table can only con Subject Unit Abbreviation + Area Logo Area @@ -168,6 +207,7 @@ The following units and their abbreviations can be used (each table can only con Subject Unit Abbreviation + Weight Logo Weight @@ -201,9 +241,10 @@ The following units and their abbreviations can be used (each table can only con Subject Unit Abbreviation + Temperature Logo - Temperature + Temperature celsius c @@ -211,6 +252,10 @@ The following units and their abbreviations can be used (each table can only con Farenheit f + + Kelvin + k + ### Problems, errors and feature requests diff --git a/assets/Area.ico b/assets/Area.ico new file mode 100644 index 0000000..4699ad6 Binary files /dev/null and b/assets/Area.ico differ diff --git a/assets/Distance.ico b/assets/Distance.ico new file mode 100644 index 0000000..29e4cce Binary files /dev/null and b/assets/Distance.ico differ diff --git a/assets/Temperature.ico b/assets/Temperature.ico new file mode 100644 index 0000000..29c9995 Binary files /dev/null and b/assets/Temperature.ico differ diff --git a/assets/Volume.ico b/assets/Volume.ico new file mode 100644 index 0000000..f03eb8b Binary files /dev/null and b/assets/Volume.ico differ diff --git a/assets/Weight.ico b/assets/Weight.ico new file mode 100644 index 0000000..378f6b9 Binary files /dev/null and b/assets/Weight.ico differ diff --git a/plugin.json b/plugin.json index ac7020d..ec9f19d 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.2", + "Version": "1.1.0", "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 ed4515b..804edba 100644 Binary files a/plugin/translations/en/LC_MESSAGES/messages.mo and b/plugin/translations/en/LC_MESSAGES/messages.mo differ diff --git a/plugin/translations/en/LC_MESSAGES/messages.po b/plugin/translations/en/LC_MESSAGES/messages.po index 5f47651..6e021ed 100644 --- a/plugin/translations/en/LC_MESSAGES/messages.po +++ b/plugin/translations/en/LC_MESSAGES/messages.po @@ -1,15 +1,15 @@ -# English translations for Flow.plugin.currency. -# Copyright (C) 2020 CitizenDee (author) +# English translations for Flow.plugin.genconvert. +# Copyright (C) 2020 deefrawley (author) # This file is distributed under the same license as the project. -# CitizenDee , 2021. +# deefrawley , 2021. # msgid "" msgstr "" -"Project-Id-Version: 1.0.2\n" +"Project-Id-Version: 1.1.0\n" "Report-Msgid-Bugs-To: deefrawley@gmail.com\n" -"POT-Creation-Date: 2021-08-26 18:04+1000\n" +"POT-Creation-Date: 2021-09-09 13:07+1000\n" "PO-Revision-Date: 2020-12-13 20:26+1100\n" -"Last-Translator: CitizenDee \n" +"Last-Translator: deefrawley \n" "Language: en\n" "Language-Team: en \n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" @@ -18,33 +18,363 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 2.9.1\n" -#: plugin/ui.py:42 +#: plugin/ui.py:46 plugin/ui.py:100 +msgid "General Converter" +msgstr "General Converter" + +#: plugin/ui.py:47 plugin/ui.py:101 +msgid " " +msgstr " " + +#: plugin/ui.py:65 msgid "Available conversions" -msgstr "" +msgstr "Available conversions" + +#: plugin/ui.py:79 +msgid "{}" +msgstr "{}" -#: plugin/ui.py:54 plugin/ui.py:63 +#: plugin/ui.py:80 +msgid "Choose two different units" +msgstr "Choose two different units" + +#: plugin/ui.py:85 plugin/ui.py:96 msgid "Error - {}" msgstr "Error - {}" -#: plugin/ui.py:55 +#: plugin/ui.py:86 msgid "Check documentation for accepted units" -msgstr "" +msgstr "Check documentation for accepted units" -#: plugin/ui.py:67 -#, fuzzy -msgid "General Converter" -msgstr "Currency Converter" +#: plugin/units.py:20 +msgid "Distance" +msgstr "Distance" -#: plugin/ui.py:68 -#, fuzzy -msgid " " -msgstr " " +#: plugin/units.py:22 +msgid "metre" +msgstr "metre" + +#: plugin/units.py:22 +msgid "metres" +msgstr "metres" + +#: plugin/units.py:24 +msgid "millimetre" +msgstr "millimetre" + +#: plugin/units.py:24 +msgid "millimetres" +msgstr "millimetres" + +#: plugin/units.py:25 +msgid "centimetre" +msgstr "centimetre" + +#: plugin/units.py:25 +msgid "centimetres" +msgstr "centimetres" + +#: plugin/units.py:26 +msgid "kilometre" +msgstr "kilometre" + +#: plugin/units.py:26 +msgid "kilometres" +msgstr "kilometres" + +#: plugin/units.py:27 +msgid "inch" +msgstr "inch" + +#: plugin/units.py:27 +msgid "inches" +msgstr "inches" + +#: plugin/units.py:28 +msgid "foot" +msgstr "foot" + +#: plugin/units.py:28 +msgid "feet" +msgstr "feet" + +#: plugin/units.py:29 +msgid "yard" +msgstr "yard" + +#: plugin/units.py:29 +msgid "yards" +msgstr "yards" + +#: plugin/units.py:30 +msgid "mile" +msgstr "mile" + +#: plugin/units.py:30 +msgid "miles" +msgstr "miles" + +#: plugin/units.py:32 +msgid "Volume" +msgstr "Volume" + +#: plugin/units.py:37 +msgid "millilitre" +msgstr "millilitre" + +#: plugin/units.py:37 +msgid "millilitres" +msgstr "millilitres" + +#: plugin/units.py:39 plugin/units.py:160 +msgid "gram" +msgstr "gram" + +#: plugin/units.py:39 plugin/units.py:160 +msgid "grams" +msgstr "grams" + +#: plugin/units.py:40 +msgid "litre" +msgstr "litre" + +#: plugin/units.py:40 +msgid "litres" +msgstr "litres" + +#: plugin/units.py:41 +msgid "pint US" +msgstr "pint US" + +#: plugin/units.py:41 +msgid "pints US" +msgstr "pints US" + +#: plugin/units.py:44 +msgid "pint Imperial" +msgstr "pint Imperial" + +#: plugin/units.py:45 +msgid "pints Imperial" +msgstr "pints Imperial" + +#: plugin/units.py:49 +msgid "quart US" +msgstr "quart US" + +#: plugin/units.py:49 +msgid "quarts US" +msgstr "quarts US" + +#: plugin/units.py:52 +msgid "quart Imperial" +msgstr "quart Imperial" + +#: plugin/units.py:53 +msgid "quarts Imperial" +msgstr "quarts Imperial" + +#: plugin/units.py:59 +msgid "cup US" +msgstr "cup US" + +#: plugin/units.py:60 +msgid "cups US" +msgstr "cups US" + +#: plugin/units.py:66 +msgid "cup Imperial" +msgstr "cup Imperial" + +#: plugin/units.py:67 +msgid "cups Imperial" +msgstr "cups Imperial" + +#: plugin/units.py:73 +msgid "tablespoon US" +msgstr "tablespoon US" + +#: plugin/units.py:74 +msgid "tabelspoons US" +msgstr "tabelspoons US" -#: plugin/utils.py:65 +#: plugin/units.py:80 +msgid "tablespoon Imperial" +msgstr "tablespoon Imperial" + +#: plugin/units.py:81 +msgid "tabelspoons Imperial" +msgstr "tabelspoons Imperial" + +#: plugin/units.py:87 +msgid "teaspoon US" +msgstr "teaspoon US" + +#: plugin/units.py:88 +msgid "teaspoons US" +msgstr "teaspoons US" + +#: plugin/units.py:94 +msgid "teaspoon Imperial" +msgstr "teaspoon Imperial" + +#: plugin/units.py:95 +msgid "teaspoons Imperial" +msgstr "teaspoons Imperial" + +#: plugin/units.py:101 +msgid "gallon US" +msgstr "gallon US" + +#: plugin/units.py:102 +msgid "gallons US" +msgstr "gallons US" + +#: plugin/units.py:108 +msgid "gallon Imperial" +msgstr "gallon Imperial" + +#: plugin/units.py:109 +msgid "gallons Imperial" +msgstr "gallons Imperial" + +#: plugin/units.py:115 +msgid "fluid ounce US" +msgstr "fluid ounce US" + +#: plugin/units.py:116 +msgid "fluid ounces US" +msgstr "fluid ounces US" + +#: plugin/units.py:122 +msgid "fluid ounce Imperial" +msgstr "fluid ounce Imperial" + +#: plugin/units.py:123 +msgid "fluid ounces Imperial" +msgstr "fluid ounces Imperial" + +#: plugin/units.py:130 +msgid "square metre" +msgstr "square metre" + +#: plugin/units.py:130 +msgid "square metres" +msgstr "square metres" + +#: plugin/units.py:131 +msgid "hectare" +msgstr "hectare" + +#: plugin/units.py:131 +msgid "hectares" +msgstr "hectares" + +#: plugin/units.py:132 +msgid "acre" +msgstr "acre" + +#: plugin/units.py:132 +msgid "acres" +msgstr "acres" + +#: plugin/units.py:135 +msgid "square centimetre" +msgstr "square centimetre" + +#: plugin/units.py:136 +msgid "square centimetres" +msgstr "square centimetres" + +#: plugin/units.py:142 +msgid "square kilometre" +msgstr "square kilometre" + +#: plugin/units.py:143 +msgid "square kilometres" +msgstr "square kilometres" + +#: plugin/units.py:147 +msgid "square inch" +msgstr "square inch" + +#: plugin/units.py:147 +msgid "square inches" +msgstr "square inches" + +#: plugin/units.py:150 +msgid "square mile" +msgstr "square mile" + +#: plugin/units.py:151 +msgid "square miles" +msgstr "square miles" + +#: plugin/units.py:155 +msgid "square foot" +msgstr "square foot" + +#: plugin/units.py:155 +msgid "square feet" +msgstr "square feet" + +#: plugin/units.py:156 +msgid "square yard" +msgstr "square yard" + +#: plugin/units.py:156 +msgid "square yards" +msgstr "square yards" + +#: plugin/units.py:162 +msgid "kilogram" +msgstr "kilogram" + +#: plugin/units.py:162 +msgid "kilograms" +msgstr "kilograms" + +#: plugin/units.py:163 +msgid "pound" +msgstr "pound" + +#: plugin/units.py:163 +msgid "pounds" +msgstr "pounds" + +#: plugin/units.py:164 +msgid "ounce" +msgstr "ounce" + +#: plugin/units.py:164 +msgid "ounces" +msgstr "ounces" + +#: plugin/units.py:165 +msgid "stone" +msgstr "stone" + +#: plugin/units.py:168 plugin/units.py:169 +msgid "ton" +msgstr "ton" + +#: plugin/units.py:176 +msgid "Celsius" +msgstr "Celsius" + +#: plugin/units.py:178 +msgid "Farenheit" +msgstr "Farenheit" + +#: plugin/units.py:179 +msgid "Kelvin" +msgstr "Kelvin" + +#: plugin/utils.py:84 msgid "To and from unit is the same" -msgstr "" +msgstr "To and from unit is the same" -#: plugin/utils.py:98 +#: plugin/utils.py:112 msgid "Problem converting {} and {}" -msgstr "" +msgstr "Problem converting {} and {}" diff --git a/plugin/translations/zh/LC_MESSAGES/messages.mo b/plugin/translations/zh/LC_MESSAGES/messages.mo index 68c56a8..0669365 100644 Binary files a/plugin/translations/zh/LC_MESSAGES/messages.mo and b/plugin/translations/zh/LC_MESSAGES/messages.mo differ diff --git a/plugin/translations/zh/LC_MESSAGES/messages.po b/plugin/translations/zh/LC_MESSAGES/messages.po index d10321a..a64cb0a 100644 --- a/plugin/translations/zh/LC_MESSAGES/messages.po +++ b/plugin/translations/zh/LC_MESSAGES/messages.po @@ -1,15 +1,15 @@ -# English translations for Flow.plugin.currency. -# Copyright (C) 2020 CitizenDee (author) +# Chinese translations for Flow.plugin.genconvert. +# Copyright (C) 2020 deefrawley (author) # This file is distributed under the same license as the project. -# CitizenDee , 2021. +# deefrawley , 2021. # msgid "" msgstr "" "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" +"POT-Creation-Date: 2021-09-09 13:07+1000\n" "PO-Revision-Date: 2021-06-07 15:16+1000\n" -"Last-Translator: CitizenDee \n" +"Last-Translator: deefrawley \n" "Language: zh\n" "Language-Team: zh \n" "Plural-Forms: nplurals=1; plural=0\n" @@ -18,31 +18,363 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 2.9.1\n" -#: plugin/ui.py:42 +#: plugin/ui.py:46 plugin/ui.py:100 +msgid "General Converter" +msgstr "通用单位换算器" + +#: plugin/ui.py:47 plugin/ui.py:101 +msgid " " +msgstr "<热键> <量> <源单元> <目的地单位>" + +#: plugin/ui.py:65 msgid "Available conversions" msgstr "可用转换" -#: plugin/ui.py:54 plugin/ui.py:63 +#: plugin/ui.py:79 +msgid "{}" +msgstr "" + +#: plugin/ui.py:80 +msgid "Choose two different units" +msgstr "选择两个不同的单位" + +#: plugin/ui.py:85 plugin/ui.py:96 msgid "Error - {}" msgstr "错误 - {}" -#: plugin/ui.py:55 +#: plugin/ui.py:86 msgid "Check documentation for accepted units" msgstr "检查文档以获取正确的度量单位" -#: plugin/ui.py:67 -msgid "General Converter" -msgstr "通用单位换算器" +#: plugin/units.py:20 +msgid "Distance" +msgstr "距离" -#: plugin/ui.py:68 -msgid " " -msgstr "<热键> <量> <源单元> <目的地单位>" +#: plugin/units.py:22 +msgid "metre" +msgstr "仪表" + +#: plugin/units.py:22 +msgid "metres" +msgstr "仪表" + +#: plugin/units.py:24 +msgid "millimetre" +msgstr "毫米" + +#: plugin/units.py:24 +msgid "millimetres" +msgstr "毫米" + +#: plugin/units.py:25 +msgid "centimetre" +msgstr "厘米" + +#: plugin/units.py:25 +msgid "centimetres" +msgstr "厘米" + +#: plugin/units.py:26 +msgid "kilometre" +msgstr "公里" + +#: plugin/units.py:26 +msgid "kilometres" +msgstr "公里" + +#: plugin/units.py:27 +msgid "inch" +msgstr "英寸" + +#: plugin/units.py:27 +msgid "inches" +msgstr "英寸" + +#: plugin/units.py:28 +msgid "foot" +msgstr "丈" + +#: plugin/units.py:28 +msgid "feet" +msgstr "丈" + +#: plugin/units.py:29 +msgid "yard" +msgstr "码尺" + +#: plugin/units.py:29 +msgid "yards" +msgstr "码尺" + +#: plugin/units.py:30 +msgid "mile" +msgstr "英里" + +#: plugin/units.py:30 +msgid "miles" +msgstr "英里" + +#: plugin/units.py:32 +msgid "Volume" +msgstr "量" + +#: plugin/units.py:37 +msgid "millilitre" +msgstr "毫升" + +#: plugin/units.py:37 +msgid "millilitres" +msgstr "毫升" + +#: plugin/units.py:39 plugin/units.py:160 +msgid "gram" +msgstr "公克" + +#: plugin/units.py:39 plugin/units.py:160 +msgid "grams" +msgstr "公克" + +#: plugin/units.py:40 +msgid "litre" +msgstr "升" + +#: plugin/units.py:40 +msgid "litres" +msgstr "升" + +#: plugin/units.py:41 +msgid "pint US" +msgstr "品脱 US" + +#: plugin/units.py:41 +msgid "pints US" +msgstr "品脱 US" + +#: plugin/units.py:44 +msgid "pint Imperial" +msgstr "英品脱" + +#: plugin/units.py:45 +msgid "pints Imperial" +msgstr "英品脱" + +#: plugin/units.py:49 +msgid "quart US" +msgstr "夸脱 US" + +#: plugin/units.py:49 +msgid "quarts US" +msgstr "夸脱 US" + +#: plugin/units.py:52 +msgid "quart Imperial" +msgstr "英夸脱" + +#: plugin/units.py:53 +msgid "quarts Imperial" +msgstr "英夸脱" + +#: plugin/units.py:59 +msgid "cup US" +msgstr "杯 US" + +#: plugin/units.py:60 +msgid "cups US" +msgstr "杯 US" + +#: plugin/units.py:66 +msgid "cup Imperial" +msgstr "英杯" + +#: plugin/units.py:67 +msgid "cups Imperial" +msgstr "英杯" + +#: plugin/units.py:73 +msgid "tablespoon US" +msgstr "汤匙 US" + +#: plugin/units.py:74 +msgid "tabelspoons US" +msgstr "汤匙 US" + +#: plugin/units.py:80 +msgid "tablespoon Imperial" +msgstr "英汤匙" + +#: plugin/units.py:81 +msgid "tabelspoons Imperial" +msgstr "英汤匙" + +#: plugin/units.py:87 +msgid "teaspoon US" +msgstr "茶匙 US" + +#: plugin/units.py:88 +msgid "teaspoons US" +msgstr "茶匙 US" + +#: plugin/units.py:94 +msgid "teaspoon Imperial" +msgstr "英茶匙" + +#: plugin/units.py:95 +msgid "teaspoons Imperial" +msgstr "英茶匙" + +#: plugin/units.py:101 +msgid "gallon US" +msgstr "加仑 US" + +#: plugin/units.py:102 +msgid "gallons US" +msgstr "加仑 US" + +#: plugin/units.py:108 +msgid "gallon Imperial" +msgstr "英加仑" + +#: plugin/units.py:109 +msgid "gallons Imperial" +msgstr "英加仑" + +#: plugin/units.py:115 +msgid "fluid ounce US" +msgstr "液盎司 US" + +#: plugin/units.py:116 +msgid "fluid ounces US" +msgstr "液盎司 US" + +#: plugin/units.py:122 +msgid "fluid ounce Imperial" +msgstr "英液盎司" + +#: plugin/units.py:123 +msgid "fluid ounces Imperial" +msgstr "英液盎司" + +#: plugin/units.py:130 +msgid "square metre" +msgstr "平方米" + +#: plugin/units.py:130 +msgid "square metres" +msgstr "平方米" + +#: plugin/units.py:131 +msgid "hectare" +msgstr "公顷" + +#: plugin/units.py:131 +msgid "hectares" +msgstr "公顷" + +#: plugin/units.py:132 +msgid "acre" +msgstr "英亩" + +#: plugin/units.py:132 +msgid "acres" +msgstr "英亩" + +#: plugin/units.py:135 +msgid "square centimetre" +msgstr "平方厘米" + +#: plugin/units.py:136 +msgid "square centimetres" +msgstr "平方厘米" + +#: plugin/units.py:142 +msgid "square kilometre" +msgstr "平方公里" + +#: plugin/units.py:143 +msgid "square kilometres" +msgstr "平方公里" + +#: plugin/units.py:147 +msgid "square inch" +msgstr "平方英寸" + +#: plugin/units.py:147 +msgid "square inches" +msgstr "平方英寸" + +#: plugin/units.py:150 +msgid "square mile" +msgstr "平方英里" + +#: plugin/units.py:151 +msgid "square miles" +msgstr "平方英里" + +#: plugin/units.py:155 +msgid "square foot" +msgstr "平方英尺" + +#: plugin/units.py:155 +msgid "square feet" +msgstr "平方英尺" + +#: plugin/units.py:156 +msgid "square yard" +msgstr "平方码" + +#: plugin/units.py:156 +msgid "square yards" +msgstr "平方码" + +#: plugin/units.py:162 +msgid "kilogram" +msgstr "公斤" + +#: plugin/units.py:162 +msgid "kilograms" +msgstr "公斤" + +#: plugin/units.py:163 +msgid "pound" +msgstr "磅" + +#: plugin/units.py:163 +msgid "pounds" +msgstr "磅" + +#: plugin/units.py:164 +msgid "ounce" +msgstr "盎司" + +#: plugin/units.py:164 +msgid "ounces" +msgstr "盎司" + +#: plugin/units.py:165 +msgid "stone" +msgstr "石头" + +#: plugin/units.py:168 plugin/units.py:169 +msgid "ton" +msgstr "吨" + +#: plugin/units.py:176 +msgid "Celsius" +msgstr "摄氏" + +#: plugin/units.py:178 +msgid "Farenheit" +msgstr "华氏度" + +#: plugin/units.py:179 +msgid "Kelvin" +msgstr "开尔文" -#: plugin/utils.py:65 +#: plugin/utils.py:84 msgid "To and from unit is the same" msgstr "源单元和目标单元相同" -#: plugin/utils.py:98 +#: plugin/utils.py:112 msgid "Problem converting {} and {}" msgstr "转换问题 {} 和 {}" diff --git a/plugin/ui.py b/plugin/ui.py index 0a47081..29f2947 100644 --- a/plugin/ui.py +++ b/plugin/ui.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- - +import textwrap import copy +import locale import plugin.utils from typing import List @@ -11,11 +12,15 @@ class Main(FlowLauncher): messages_queue = [] + locale.setlocale(locale.LC_NUMERIC, "") - def sendNormalMess(self, title: str, subtitle: str): + def sendNormalMess( + self, title: str, subtitle: str, iconpath: str = "assets/favicon.ico" + ): message = copy.deepcopy(RESULT_TEMPLATE) message["Title"] = title message["SubTitle"] = subtitle + message["IcoPath"] = iconpath self.messages_queue.append(message) @@ -36,29 +41,61 @@ 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) == 2: - hints = plugin.utils.get_hints(args[1].lower()) + # Just keyword - show all units + if len(args) == 1: + all_units = plugin.utils.get_all_units() + self.sendNormalMess( + (_("General Converter")), + _(" "), + ) + for cat in all_units: + title = str(cat[0]) + subtitle = ", ".join([str(elem) for elem in cat[1:]]) + lines = textwrap.wrap(subtitle, 110, break_long_words=False) + if len(lines) > 1: + self.sendNormalMess((title), (lines[0]), f"assets/{title}.ico") + for line in range(1, len(lines)): + self.sendNormalMess( + (title), (lines[line]), f"assets/{title}.ico" + ) + else: + self.sendNormalMess((title), (subtitle), f"assets/{title}.ico") + # Keyword and first unit to convert from - show what it can be converted to + elif len(args) == 2: + hints = plugin.utils.get_hints_for_category(args[1].lower()) self.sendNormalMess( _("Available conversions"), (f"{args[0]} {args[1]} to {', '.join(hints)}"), ) + # Keyword and two units to convert from and to - try to convert 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 args[1] = args[1].lower() args[2] = args[2].lower() - do_convert = plugin.utils.genConvert(float(args[0]), args[1], args[2]) + do_convert = plugin.utils.gen_convert(float(args[0]), args[1], args[2]) if "Error" in do_convert: - self.sendNormalMess( - _("Error - {}").format(do_convert["Error"]), - _("Check documentation for accepted units"), - ) + if do_convert["Error"] == "To and from unit is the same": + self.sendNormalMess( + _("{}".format(do_convert["Error"])), + _("Choose two different units"), + ) + else: + self.sendNormalMess( + # f strings seem to break babel so use string formatting instead + _("Error - {}").format(do_convert["Error"]), + _("Check documentation for accepted units"), + ) else: self.sendNormalMess( - (f"{args[0]} {args[1]} = {do_convert[args[2]]:.6f} {args[2]}"), - "", + (do_convert["category"]), + ( + f"{locale.format_string('%.6g', float(args[0]), grouping=True)} {args[1]} = {locale.format_string('%f', do_convert['converted'], grouping=True)} {args[2]}" + ), + f"assets/{do_convert['category']}.ico", ) + do_convert = [] except Exception as e: self.sendNormalMess(_("Error - {}").format(repr(e)), "") # Always show the usage while there isn't a valid query diff --git a/plugin/units.py b/plugin/units.py new file mode 100644 index 0000000..655cf78 --- /dev/null +++ b/plugin/units.py @@ -0,0 +1,181 @@ +from plugin.extensions import _ + +""" +Unit Syntax: +units = { + "Category description": [ + ["abbreviation", + "unit name (singular)", + "unit name (plural)", + "conversion formula TO base unit", + "conversion formula FROM base unit"] + ] +} + +The first entry for each category in the list of lists MUST be the base unit. +Descriptions wrapped in '_()' can, and should, be translated via the Python template +translation methods +""" +units = { + _("Distance"): [ + # Base + ["m", _("metre"), _("metres"), "x * 1", "x * 1"], + # All below convert to/from base + ["mm", _("millimetre"), _("millimetres"), "x / 1000", "x * 1000"], + ["cm", _("centimetre"), _("centimetres"), "x / 100", "x * 100"], + ["km", _("kilometre"), _("kilometres"), "x / 0.001", "x * 0.001"], + ["in", _("inch"), _("inches"), "x / 39.37008", "x * 39.37008"], + ["ft", _("foot"), _("feet"), "x / 3.28084", "x * 3.28084"], + ["yd", _("yard"), _("yards"), "x / 1.093613", "x * 1.093613"], + ["mi", _("mile"), _("miles"), "x / 0.0006213712", "x * 0.0006213712"], + ], + _("Volume"): [ + # NOTE :- Volumes are tricky because America. Non metric units often have a US and Imperial + # version. The default is US and the Imperial conversion can often be accessed by adding "imp" + # to the unit code. + # Base + ["ml", _("millilitre"), _("millilitres"), "x * 1", "x * 1"], + # All below convert to/from base + ["gm", _("gram"), _("grams"), "x * 1", "x * 1"], + ["l", _("litre"), _("litres"), "x / 0.001", "x * 0.001"], + ["pt", _("pint US"), _("pints US"), "x / 0.002113383", "x * 0.002113383"], + [ + "ptimp", + _("pint Imperial"), + _("pints Imperial"), + "x / 0.001759754", + "x * 0.001759754", + ], + ["qt", _("quart US"), _("quarts US"), "x / 0.001056691", "x * 0.001056691"], + [ + "qtimp", + _("quart Imperial"), + _("quarts Imperial"), + "x / 0.000879877", + "x * 0.000879877", + ], + [ + "cup", + _("cup US"), + _("cups US"), + "x / 0.0042267528198649", + "x * 0.0042267528198649", + ], + [ + "cupimp", + _("cup Imperial"), + _("cups Imperial"), + "x / 0.003519508", + "x * 0.003519508", + ], + [ + "tbsp", + _("tablespoon US"), + _("tabelspoons US"), + "x / 0.067628224", + "x * 0.067628224", + ], + [ + "tbspimp", + _("tablespoon Imperial"), + _("tabelspoons Imperial"), + "x / 0.05631213", + "x * 0.05631213", + ], + [ + "tsp", + _("teaspoon US"), + _("teaspoons US"), + "x / 0.2028846715942", + "x * 0.2028846715942", + ], + [ + "tspimp", + _("teaspoon Imperial"), + _("teaspoons Imperial"), + "x / 0.1689364", + "x * 0.1689364", + ], + [ + "gal", + _("gallon US"), + _("gallons US"), + "x / 0.0002641727499999601", + "x * 0.0002641727499999601", + ], + [ + "galimp", + _("gallon Imperial"), + _("gallons Imperial"), + "x / 0.0002199692", + "x * 0.0002199692", + ], + [ + "floz", + _("fluid ounce US"), + _("fluid ounces US"), + "x / 0.03381413", + "x * 0.03381413", + ], + [ + "flozimp", + _("fluid ounce Imperial"), + _("fluid ounces Imperial"), + "x / 0.03519508", + "x * 0.03519508", + ], + ], + "Area": [ + # Base + ["sqm", _("square metre"), _("square metres"), "x * 1", "x * 1"], + ["h", _("hectare"), _("hectares"), "x / 0.0001", "x * 0.0001"], + ["ac", _("acre"), _("acres"), "x / 0.0002471052", "x * 0.0002471052"], + [ + "sqcm", + _("square centimetre"), + _("square centimetres"), + "x / 10000", + "x * 10000", + ], + [ + "sqkm", + _("square kilometre"), + _("square kilometres"), + "x / 1000000", + "x * 1000000", + ], + ["sqin", _("square inch"), _("square inches"), "x / 1550.003", "x * 1550.003"], + [ + "sqmi", + _("square mile"), + _("square miles"), + "x / 0.0000003861022", + "x * 0.0000003861022", + ], + ["sqft", _("square foot"), _("square feet"), "x / 10.76391", "x * 10.76391"], + ["sqyd", _("square yard"), _("square yards"), "x / 1.19599", "x * 1.19599"], + ], + "Weight": [ + # Base + ["gm", _("gram"), _("grams"), "x * 1", "x * 1"], + # All below convert to/from base + ["kg", _("kilogram"), _("kilograms"), "x / 0.001", "x * 0.001"], + ["lb", _("pound"), _("pounds"), "x / 0.002205", "x * 0.001"], + ["oz", _("ounce"), _("ounces"), "x / 0.035274", " 0.035274"], + ["st", _("stone"), _("stone"), "x / 0.000157473", "x * 0.000157473"], + [ + "ton", + _("ton"), + _("ton"), + "x / 0.000001102310999995", + "x * 0.000001102310999995", + ], + ], + "Temperature": [ + # Base + ["c", _("Celsius"), _("Celsius"), "x * 1", "x * 1"], + # All below convert to/from base + ["f", _("Farenheit"), _("Farenheit"), "(x - 32) / 1.8", "(x * 1.8) + 32"], + ["k", _("Kelvin"), _("Kelvin"), "x - 273.15", "x + 273.15"], + ], +} diff --git a/plugin/utils.py b/plugin/utils.py index fa74745..9805d27 100644 --- a/plugin/utils.py +++ b/plugin/utils.py @@ -1,109 +1,114 @@ +import plugin.units as gc_units from plugin.extensions import _ -units = { - "m": { - "mm": 1000, - "cm": 100, - "km": 0.001, - "in": 39.37008, - "ft": 3.28084, - "yd": 1.093613, - "mi": 0.0006213712, - }, - "ml": { - "gm": 1, - "l": 0.001, - "pt": 0.002113383, - "qt": 0.001056691, - "cup": 0.004226764, - "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 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 """ + +def get_hints_for_category(from_unit: str): + """Takes an input unit and returns a list of units it can be converted to + + :param from_short: unit abbreviation + :type amount: str + + :rtype: list + :return: A list of other unit abbreviations in the same category + """ + c = [] + category = "" + + # Find the category it's in + for u in gc_units.units: + for u2 in gc_units.units[u]: + if u2[0] == from_unit: + category = str(u) + if category: + # Go back and iterate over the category again and get all the units that are not the from unit + for uu in gc_units.units[category]: + if uu[0] != from_unit: + c.append(uu[0]) + if not c: + return ["no valid units"] + return c + else: + return ["no valid units"] + + +def get_all_units(short=False): + """Returns all available units as a list of lists by category + + :param short: if True only unit abbreviations are returned, default is False + :type amount: bool + + :rtype: list of lists + :return: A list of lists for each category in units. Index 0 of each internal list + is the category description + """ + + full_list = [] + for u in gc_units.units: + cat_list = [] + cat_list.append(u) + for u2 in gc_units.units[u]: + cat_list.append(u2[0] if short else f"{u2[1]} ({u2[0]})") + full_list.append(cat_list) + return full_list + + +def gen_convert(amount: float, from_unit: str, to_unit: str): + """Converts from one unit to another + + :param amount: amount of source unit to convert + :type amount: float + :param from_unit: abbreviation of unit to convert from + :type from_unit: str + :param to_unit: abbreviation of unit to convert to + :type to_unit: str + + :rtype: dict + :return: if to_unit and from_unit are valid returns a dictionary + { + "category":{category of units}, + "converted":{converted amount}, + "fromabbrev":{from unit abbreviation}, + "fromlong":{from unit long name}, + "fromplural":{from unit plural name}, + "toabbrev":{to unit abbreviation}, + "tolong":{to unit long name}, + "toplural":{to unit plural name}, + } + + else returns a dictionary with error status + {"Error": {error text}} + """ conversions = {} + found_from = found_to = [] if from_unit == to_unit: conversions["Error"] = _("To and from unit is the same") return conversions - # Handle celsius and farenheit as special cases as they aren't converted by exponents - elif from_unit == "c" and to_unit == "f": - conversions[to_unit] = (amount * 1.8) + 32 - return conversions - elif from_unit == "f" and to_unit == "c": - conversions[to_unit] = (amount - 32) / 1.8 - return conversions - # Convert from key unit to sub-unit (e.g. cm to in) - 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) + for u in gc_units.units: + for u2 in gc_units.units[u]: + if u2[0] == from_unit: + found_from = u2 + if u2[0] == to_unit: + found_to = u2 + # If we haven't both in the same category, reset + if found_to and found_from: + found_category = u + break + else: + found_from = found_to = [] + if found_to and found_from: + base_unit_conversion = eval(found_from[3].replace("x", str(amount))) + final_conversion = eval(found_to[4].replace("x", str(base_unit_conversion))) + conversions["category"] = found_category + conversions["converted"] = final_conversion + conversions["fromabbrev"] = found_from[0] + conversions["fromlong"] = found_from[1] + conversions["fromplural"] = found_from[2] + conversions["toabbrev"] = found_to[0] + conversions["tolong"] = found_to[1] + conversions["toplural"] = found_to[2] + 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 - conversions[to_unit] = (1 / units[u][from_unit]) * ( - units[u][to_unit] * amount - ) - return conversions - conversions["Error"] = _("Problem converting {} and {}").format( - from_unit, to_unit + conversions["Error"] = _( + "Problem converting {} and {}".format(from_unit, to_unit) ) - return conversions - - -def listUnits(): - # Utility function to print out all conversions - 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="") + return conversions