|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import datetime |
| 4 | +import re |
| 5 | +import traceback |
| 6 | +from pathlib import Path |
| 7 | +from typing import List |
| 8 | + |
| 9 | +import albert |
| 10 | + |
| 11 | +from dates import format_date, parse_date |
| 12 | + |
| 13 | +__doc__ = """ |
| 14 | +Extension for converting between timezones |
| 15 | +
|
| 16 | +Synopsis: `<from_time> [to|in] <to_tz>` |
| 17 | +
|
| 18 | +Examples: |
| 19 | +`10pm PST to CST` |
| 20 | +`8am MST in New York` |
| 21 | +
|
| 22 | +24-hour time and timezone aliases can be set in config.py |
| 23 | +""" |
| 24 | +__title__ = "Timezone Convert" |
| 25 | +__version__ = "0.0.1" |
| 26 | +__authors__ = "Jonah Lawrence" |
| 27 | +__py_deps__ = ["dateparser"] |
| 28 | + |
| 29 | +local_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo |
| 30 | + |
| 31 | +timezone_regex = re.compile( |
| 32 | + r"(?P<from_time>.*(?:pm|am|\d:\d).*)\s(?P<seperator>to|in)\s(?P<to_tz>.*)", re.I |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +def get_icon_path() -> str: |
| 37 | + """ |
| 38 | + Get the path to the icon |
| 39 | +
|
| 40 | + Returns: |
| 41 | + str: The path to the icon. |
| 42 | + """ |
| 43 | + return str(Path(__file__).parent / "icons" / "clock.svg") |
| 44 | + |
| 45 | + |
| 46 | +def get_item(text: str, subtext: str) -> albert.Item: |
| 47 | + """ |
| 48 | + Create an albert.Item from a text and subtext. |
| 49 | +
|
| 50 | + Args: |
| 51 | + text (str): The text to display. |
| 52 | + subtext (str): The subtext to display. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + albert.Item: The item to be added to the list of results. |
| 56 | + """ |
| 57 | + return albert.Item( |
| 58 | + id=__title__, |
| 59 | + icon=get_icon_path(), |
| 60 | + text=text, |
| 61 | + subtext=subtext, |
| 62 | + actions=[albert.ClipAction("Copy result to clipboard", text)], |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def get_items(from_time: str, to_tz: str) -> List[albert.Item]: |
| 67 | + """ |
| 68 | + Generate the Albert items to display for the query. |
| 69 | +
|
| 70 | + Args: |
| 71 | + query_string (str): The query string to be parsed. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + List[albert.Item]: The list of items to display. |
| 75 | + """ |
| 76 | + # parse from_time by itself |
| 77 | + from_dt = parse_date(from_time) |
| 78 | + if not from_dt: |
| 79 | + return [get_item(f"Error: Could not parse date: {from_time}", "")] |
| 80 | + # parse time with target timezone |
| 81 | + result_dt = parse_date(from_time, to_tz=to_tz) |
| 82 | + if not result_dt: |
| 83 | + return [get_item(f"Error: Could not parse timezone: {to_tz}", "")] |
| 84 | + # format the results |
| 85 | + from_str = format_date(from_dt) |
| 86 | + result_str = format_date(result_dt) |
| 87 | + from_tz = from_dt.tzname() or local_timezone.tzname(from_dt) or "" |
| 88 | + result_tz = result_dt.tzname() or "" |
| 89 | + return [ |
| 90 | + get_item( |
| 91 | + f"{result_str} {result_tz}", |
| 92 | + f"Converted from {from_str} {from_tz}", |
| 93 | + ) |
| 94 | + ] |
| 95 | + |
| 96 | + |
| 97 | +def handleQuery(query: albert.Query) -> List[albert.Item]: |
| 98 | + """ |
| 99 | + Handler for a query received from Albert. |
| 100 | + """ |
| 101 | + query_string = query.string.strip() |
| 102 | + match = timezone_regex.fullmatch(query_string) |
| 103 | + if match: |
| 104 | + try: |
| 105 | + return get_items(match.group("from_time"), match.group("to_tz")) |
| 106 | + except Exception as error: |
| 107 | + albert.warning(f"Error: {error}") |
| 108 | + tb = "".join( |
| 109 | + traceback.format_exception(error.__class__, error, error.__traceback__) |
| 110 | + ) |
| 111 | + albert.warning(tb) |
| 112 | + albert.info( |
| 113 | + "Something went wrong. Make sure you're using the correct format." |
| 114 | + ) |
0 commit comments