Skip to content

Commit

Permalink
Merge pull request #4 from deefrawley/hint_update
Browse files Browse the repository at this point in the history
Add conversion hints. Fixes #2. Some language file cleanup. Git ignores a scratch file I use for testing.
  • Loading branch information
deefrawley authored Aug 26, 2021
2 parents 43c57a5 + f820ebd commit 797426d
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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__/
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file modified plugin/translations/en/LC_MESSAGES/messages.mo
Binary file not shown.
22 changes: 13 additions & 9 deletions plugin/translations/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
@@ -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 <deefrawley@gmail.com>, 2020.
# CitizenDee <deefrawley@gmail.com>, 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 <deefrawley@gmail.com>\n"
"Language: en\n"
Expand All @@ -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 "<Hotkey> <Amount> <Source unit> <Destination unit>"
msgstr "<Hotkey> <Amount> <Source currency> <Destination currency>"

#: 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 ""

Binary file modified plugin/translations/zh/LC_MESSAGES/messages.mo
Binary file not shown.
32 changes: 18 additions & 14 deletions plugin/translations/zh/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
@@ -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 <EMAIL@ADDRESS>, 2021.
# English translations for Flow.plugin.currency.
# Copyright (C) 2020 CitizenDee (author)
# This file is distributed under the same license as the project.
# CitizenDee <deefrawley@gmail.com>, 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 <EMAIL@ADDRESS>\n"
"Last-Translator: CitizenDee <deefrawley@gmail.com>\n"
"Language: zh\n"
"Language-Team: zh <LL@li.org>\n"
"Plural-Forms: nplurals=1; plural=0\n"
Expand All @@ -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 "<Hotkey> <Amount> <Source unit> <Destination unit>"
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 "转换问题 {} 和 {}"

9 changes: 7 additions & 2 deletions plugin/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
95 changes: 36 additions & 59 deletions scratch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from plugin.settings import __package_title__, __version__

units = {
"m": {
"mm": 1000,
Expand All @@ -11,73 +9,52 @@
"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 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()
vv = get_hints("ox")
print(vv)

0 comments on commit 797426d

Please sign in to comment.