diff --git a/LICENSE b/LICENSE index 483ea73..a506857 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 deefrawley +Copyright (c) 2023 deefrawley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -20,8 +20,10 @@ 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 +Graduated Cylinder icon by AS Design from Noun Project +Area icon by matteo manenti from Noun Project +Distance icon by Anthony Ledoux from Noun Project +Weight icon by Adrien Coquet from Noun Project +Temperature icon by Alexander Skowalsky from Noun Project +Speed icon by Fahmi Somdi Std from Noun Project +Energy icon by Greg Cresnar from Noun Project diff --git a/README.md b/README.md index b67d681..749b2ce 100644 --- a/README.md +++ b/README.md @@ -1,13 +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, volume, distance, area, temperature converter for the [Flow Launcher](https://github.com/Flow-Launcher/Flow.Launcher) +General weight, volume, distance, area, temperature, speed and energy converter for the [Flow Launcher](https://github.com/Flow-Launcher/Flow.Launcher) ### About ### Requirements -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. +Python 3.9 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 @@ -29,7 +29,10 @@ Currently English and Chinese language supported. Edit the .env file to change t | ------------------------------------------------------------- | -------------------------------------------------- | | `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. +The from and to unit are case sensitive. + +Just entering the keyword will give you the full list of units to choose from in Flow. If you want to remove the keyword for this plugin and have it return +results when you simply start entering a number then go to the Plugin settings and turn this helper text off. Entering the keyword, amount and from unit will give you a subset list of units the from unit can be converted to #### Units @@ -41,10 +44,10 @@ The following units and their abbreviations can be used (each table can only con Subject Unit Abbreviation - Distance Logo + Distance Logo - Distance + Distance millimetre mm @@ -56,6 +59,10 @@ The following units and their abbreviations can be used (each table can only con metre m + + decimetre + dm + kilometre km @@ -83,10 +90,10 @@ The following units and their abbreviations can be used (each table can only con Subject Unit Abbreviation - Volume Logo + Volume Logo - Volume + Volume millilitre ml @@ -98,6 +105,10 @@ The following units and their abbreviations can be used (each table can only con litre l + + decalitre + decal + pint US pt @@ -154,6 +165,38 @@ The following units and their abbreviations can be used (each table can only con fluid ounce Imperial flozimp + + cubic decimetre + dm3 + + + cubic millimetre + mm3 + + + cubic centimetre + cm3 + + + cubic metre + m3 + + + cubic inch + in3 + + + cubic feet + ft3 + + + bushel UK + buuk + + + bushel US + buus + @@ -258,6 +301,66 @@ The following units and their abbreviations can be used (each table can only con
+ + + + + + + + + + + + + + + + + + + + +
SubjectUnitAbbreviationSpeed Logo
Speedkilometres per hourkm/h
miles per hourmp/h
knotskt
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SubjectUnitAbbreviationEnergy Logo
Energycaloriescal
kilocalorieskcal
kilojouleskJ
megajoulesMJ
gigajoulesGj
kilowatt hourskWh
British thermal unitsBTU
+ ### Problems, errors and feature requests Open an issue in this repo. diff --git a/SettingsTemplate.yaml b/SettingsTemplate.yaml index 73e3b89..7c05998 100644 --- a/SettingsTemplate.yaml +++ b/SettingsTemplate.yaml @@ -6,7 +6,6 @@ body: Convert between different types of units. - type: checkbox attributes: - attributes: name: show_helper_text label: "Show helper text of what can be converted" defaultValue: "true" diff --git a/assets/Energy.ico b/assets/Energy.ico new file mode 100644 index 0000000..139a6d0 Binary files /dev/null and b/assets/Energy.ico differ diff --git a/assets/Energy.png b/assets/Energy.png new file mode 100644 index 0000000..6e73e1a Binary files /dev/null and b/assets/Energy.png differ diff --git a/assets/Speed.ico b/assets/Speed.ico new file mode 100644 index 0000000..89c1701 Binary files /dev/null and b/assets/Speed.ico differ diff --git a/plugin.json b/plugin.json index 3b18e46..2e1b456 100644 --- a/plugin.json +++ b/plugin.json @@ -4,7 +4,7 @@ "Name": "General Converter", "Description": "General weights and measures converter", "Author": "deefrawley", - "Version": "1.1.4", + "Version": "2.0.0", "Language": "python", "Website": "https://github.com/deefrawley/Flow.Launcher.Plugin.GenConvert", "IcoPath": "assets/favicon.ico", diff --git a/plugin/general_converter.py b/plugin/general_converter.py index ca92f0b..d999264 100644 --- a/plugin/general_converter.py +++ b/plugin/general_converter.py @@ -1,5 +1,4 @@ import locale -import decimal import textwrap import re import units as gc_units @@ -23,9 +22,11 @@ def query(self, query): all_units = get_all_units() self.add_item( title=_("General Converter"), - subtitle=_(" "), + subtitle=_( + " " + ), ) - if not self.settings.get("show_helper_text"): + if self.settings.get("show_helper_text"): for cat in all_units: title = str(cat[0]) subtitle = ", ".join([str(elem) for elem in cat[1:]]) @@ -50,7 +51,7 @@ def query(self, query): ) # Keyword and first unit to convert from - show what it can be converted to elif len(args) == 2: - hints = get_hints_for_category(args[1].lower()) + hints = get_hints_for_category(args[1]) self.add_item( title=_("Available conversions"), subtitle=(f"{args[0]} {args[1]} to {', '.join(hints)}"), @@ -58,10 +59,7 @@ def query(self, query): # 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() + # Units are case sensitive. do_convert = gen_convert(float(args[0]), args[1], args[2]) if "Error" in do_convert: if do_convert["Error"] == _("To and from unit is the same"): @@ -132,7 +130,7 @@ def get_hints_for_category(from_unit: str): # Find the category it's in for u in gc_units.units: for u2 in gc_units.units[u]: - if u2[0] == from_unit: + if u2[0] == from_unit or u2[1] == from_unit or u2[2] == from_unit: category = str(u) for uu in gc_units.units[category]: if uu[0] != from_unit: @@ -176,9 +174,9 @@ def gen_convert(amount: float, from_unit: str, to_unit: str): return conversions for u in gc_units.units: for u2 in gc_units.units[u]: - if u2[0] == from_unit: + if u2[0] == from_unit or u2[1] == from_unit or u2[2] == from_unit: found_from = u2 - if u2[0] == to_unit: + if u2[0] == to_unit or u2[1] == to_unit or u2[2] == to_unit: found_to = u2 # If we haven't both in the same category, reset if found_to and found_from: diff --git a/plugin/units.py b/plugin/units.py index 326a6a7..1c37a52 100644 --- a/plugin/units.py +++ b/plugin/units.py @@ -21,6 +21,7 @@ # Base ["m", _("metre"), _("metres"), "x * 1", "x * 1"], # All below convert to/from base + ["dm", _("decimetre"), _("decimetres"), "x / 10", "x * 10"], ["mm", _("millimetre"), _("millimetres"), "x / 1000", "x * 1000"], ["cm", _("centimetre"), _("centimetres"), "x / 100", "x * 100"], ["km", _("kilometre"), _("kilometres"), "x / 0.001", "x * 0.001"], @@ -38,6 +39,7 @@ # All below convert to/from base ["g", _("gram"), _("grams"), "x * 1", "x * 1"], ["l", _("litre"), _("litres"), "x / 0.001", "x * 0.001"], + ["decal", _("decalitre"), _("decalitres"), "x / 0.0001", "x * 0.0001"], ["pt", _("pint US"), _("pints US"), "x / 0.002113383", "x * 0.002113383"], [ "ptimp", @@ -124,6 +126,62 @@ "x / 0.03519508", "x * 0.03519508", ], + [ + "dm3", + _("cubic decimetre"), + _("cubic decimetres"), + "x / 0.001", + "x * 0.001", + ], + [ + "mm3", + _("cubic millimetre"), + _("cubic millimetres"), + "x / 0.1000", + "x * 0.1000", + ], + [ + "cm3", + _("cubic centimetre"), + _("cubic centimetres"), + "x / 1", + "x * 1", + ], + [ + "m3", + _("cubic metre"), + _("cubic metres"), + "x / 0.000001", + "x * 0.000001", + ], + [ + "in3", + _("cubic inch"), + _("cubic inches"), + "x / 0.061024", + "x * 0.061024", + ], + [ + "ft3", + _("cubic feet"), + _("cubic feet"), + "x / 0.0000353147", + "x * 0.0000353147", + ], + [ + "buuk", + _("bushel UK"), + _("bushels UK"), + "x / 0.0000274961", + "x * 0.0000274961", + ], + [ + "buus", + _("bushel US"), + _("bushels US"), + "x / 0.0000283776", + "x * 0.0000283776", + ], ], "Area": [ # Base @@ -192,4 +250,82 @@ ["f", _("Farenheit"), _("Farenheit"), "(x - 32) / 1.8", "(x * 1.8) + 32"], ["k", _("Kelvin"), _("Kelvin"), "x - 273.15", "x + 273.15"], ], + "Speed": [ + # Base + ["km/h", _("kilometres per hour"), _("kilometres per hour"), "x * 1", "x * 1"], + [ + "m/s", + _("metres per second"), + _("metres per second"), + "x / 0.2777777778", + "x * 0.2777777778", + ], + [ + "mp/h", + _("miles per hour"), + _("miles per hour"), + "x / 0.6213711922", + "x * 0.6213711922", + ], + [ + "kt", + _("knot"), + _("knots"), + "x / 0.5399568035", + "x * 0.5399568035", + ], + ], + "Energy": [ + # Base + ["J", _("joule"), _("joules"), "x * 1", "x * 1"], + [ + "cal", + _("calorie"), + _("calories"), + "x / 0.2388459", + "x * 0.2388459", + ], + [ + "kcal", + _("kilocalorie"), + _("kilocalories"), + "x / 0.0002388459", + "x * 0.0002388459", + ], + [ + "kJ", + _("kilojoule"), + _("kilojoules"), + "x / 0.001", + "x * 0.001", + ], + [ + "MJ", + _("megajoule"), + _("megajoules"), + "x / 0.000001", + "x * 0.000001", + ], + [ + "Gj", + _("gigajoule"), + _("gigajoules"), + "x / 0.0000000010", + "x * 0.0000000010", + ], + [ + "kWh", + _("kilowatt hour"), + _("kilowatt hours"), + "x / 0.0000002778", + "x * 0.0000002778", + ], + [ + "BTU", + _("British thermal unit"), + _("British thermal units"), + "x / 0.0009478171", + "x * 0.0009478171", + ], + ], } diff --git a/plugin/utils.py b/plugin/utils.py deleted file mode 100644 index fb871ce..0000000 --- a/plugin/utils.py +++ /dev/null @@ -1,128 +0,0 @@ -import re -import plugin.units as gc_units -from plugin.extensions import _ - - -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) - for uu in gc_units.units[category]: - if uu[0] != from_unit: - c.append(uu[0]) - if category: - 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 - 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: - conversions["Error"] = _( - "Problem converting {} and {}".format(from_unit, to_unit) - ) - return conversions - - -def smart_precision(separator, amount, preferred=3): - str_amt = str(amount) - dec_places = str_amt[::-1].find(separator) - # whole number - if dec_places == -1: - return 0 - frac_part = str_amt[-dec_places::] - # fraction is just zeroes - if int(frac_part) == 0: - return 0 - fnz = re.search(r"[1-9]", frac_part).start() - if fnz < preferred: - return preferred - return fnz + 1 diff --git a/requirements-dev.txt b/requirements-dev.txt deleted file mode 100644 index 123fc3e..0000000 --- a/requirements-dev.txt +++ /dev/null @@ -1,2 +0,0 @@ -click -babel \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 26d477d..e251760 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ -flowlauncher -python-dotenv -typing \ No newline at end of file +flox-lib==0.19.6 +python-dotenv==0.20.0