From 10b5ed65febdc5278406ff7c1776e3da350ff900 Mon Sep 17 00:00:00 2001 From: JarbasAI <33701864+JarbasAl@users.noreply.github.com> Date: Mon, 29 Mar 2021 22:43:10 +0000 Subject: [PATCH] Feat/lingua nostra (#6)(#7) Co-authored-by: jarbasal 2021.4.2 --- .github/workflows/unit_test.yml | 68 -------------------------------- mycroft/skills/__main__.py | 4 ++ mycroft/skills/intent_service.py | 5 ++- mycroft/util/parse.py | 2 +- mycroft/util/time.py | 61 ++++------------------------ requirements/minimal.txt | 6 +-- requirements/requirements.txt | 2 +- setup.py | 6 +-- 8 files changed, 22 insertions(+), 132 deletions(-) delete mode 100644 .github/workflows/unit_test.yml diff --git a/.github/workflows/unit_test.yml b/.github/workflows/unit_test.yml deleted file mode 100644 index 858f23c0f89b..000000000000 --- a/.github/workflows/unit_test.yml +++ /dev/null @@ -1,68 +0,0 @@ -# Perform linting, code style checking, and unit tests on mycroft-core -# -# This is only part of the continuous integration (CI) process for mycroft-core -# behavioral tests (Voight-Kampff) are run in a Jenkins job. -name: Unit Tests - -on: - push: - branches: - - dev - - master - pull_request: - branches: - - dev - - master - -jobs: - build: - runs-on: ubuntu-latest - strategy: - matrix: - # Run for all versions of Python officially supported by mycroft-core - python-version: [3.6, 3.7, 3.8, 3.9] - steps: - - uses: actions/checkout@v2 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - name: Cache pip packages - uses: actions/cache@v2 - env: - cache-name: cache-pypi-modules - with: - # pip cache files are stored in `~/.cache/pip` on Linux - path: ~/.cache/pip - key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('requirements/requirements.txt', 'requirements/tests.txt') }} - restore-keys: | - ${{ runner.os }}-build-${{ env.cache-name }}- - ${{ runner.os }}-build- - ${{ runner.os }}- - - name: Install compilers - run: | - sudo apt-get update -qq - sudo apt-get install -y gcc g++ - - name: Install Python packages for testing - run: pip install -r requirements/tests.txt - - name: Setup mycroft core - # Only build mimic for one Python version; others should not differ - run: | - if [[ ${{ matrix.python-version }} == 3.9 ]]; then ./dev_setup.sh; fi - if [[ ${{ matrix.python-version }} != 3.9 ]]; then ./dev_setup.sh -sm; fi - env: - CI: true - - name: Linting and code style - run: | - pycodestyle mycroft test - flake8 mycroft test --count --select=E9,F63,F7,F82 --show-source --statistics - - name: Run unit tests - # Only generate the html report for one of the versions, others should not differ - run: | - if [[ ${{ matrix.python-version }} == 3.9 ]]; then ./start-mycroft.sh unittest --cov-report html; fi - if [[ ${{ matrix.python-version }} != 3.9 ]]; then ./start-mycroft.sh unittest; fi - - name: Upload code coverage - # Only upload the report for the Python version that generated it - run: if [[ ${{ matrix.python-version }} == 3.9 ]]; then bash <(curl -s https://codecov.io/bash); fi - env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/mycroft/skills/__main__.py b/mycroft/skills/__main__.py index 85b9bd282340..b3f22febf3ce 100644 --- a/mycroft/skills/__main__.py +++ b/mycroft/skills/__main__.py @@ -37,6 +37,7 @@ wait_for_exit_signal ) from mycroft.util.lang import set_default_lang +from mycroft.util.time import set_default_tz from mycroft.util.log import LOG from mycroft.util.process_utils import ProcessStatus, StatusCallbackMap @@ -202,6 +203,9 @@ def main(alive_hook=on_alive, started_hook=on_started, ready_hook=on_ready, # Set the active lang to match the configured one set_default_lang(config.get('lang', 'en-us')) + # Set the default timezone to match the configured one + set_default_tz() + # Connect this process to the Mycroft message bus bus = start_message_bus_client("SKILLS") _register_intent_services(bus) diff --git a/mycroft/skills/intent_service.py b/mycroft/skills/intent_service.py index 378183e5981e..65bedb71576f 100644 --- a/mycroft/skills/intent_service.py +++ b/mycroft/skills/intent_service.py @@ -18,6 +18,7 @@ from mycroft.configuration import Configuration from mycroft.util.lang import set_default_lang +from mycroft.util.time import set_default_tz from mycroft.util.log import LOG from mycroft.util.parse import normalize from mycroft.metrics import report_timing, Stopwatch @@ -153,7 +154,8 @@ def get_skill_name(self, skill_id): def reset_converse(self, message): """Let skills know there was a problem with speech recognition""" lang = _get_message_lang(message) - set_default_lang(lang) + set_default_lang(lang) # restore default lang + set_default_tz() # restore default timezone for skill in copy(self.active_skills): self.do_converse(None, skill[0], lang, message) @@ -275,6 +277,7 @@ def handle_utterance(self, message): try: lang = _get_message_lang(message) set_default_lang(lang) + set_default_tz() # set default timezone utterances = message.data.get('utterances', []) combined = _normalize_all_utterances(utterances) diff --git a/mycroft/util/parse.py b/mycroft/util/parse.py index 79dd902b661b..3112b6d0b8b0 100644 --- a/mycroft/util/parse.py +++ b/mycroft/util/parse.py @@ -30,7 +30,7 @@ from difflib import SequenceMatcher from warnings import warn -import lingua_franca.parse + from lingua_franca import get_default_lang, get_primary_lang_code from lingua_franca.parse import extract_number, extract_numbers, \ extract_duration, get_gender, normalize diff --git a/mycroft/util/time.py b/mycroft/util/time.py index 07e06203986c..b6cd8d72a1ba 100644 --- a/mycroft/util/time.py +++ b/mycroft/util/time.py @@ -19,6 +19,12 @@ """ from datetime import datetime from dateutil.tz import gettz, tzlocal +from lingua_franca.time import now_utc, now_local, to_local, to_utc, \ + default_timezone as _default_tz + + +def set_default_tz(tz=None): + """ placeholder waiting for lingua_nostra version bump """ def default_timezone(): @@ -42,60 +48,7 @@ def default_timezone(): return gettz(code) except Exception: # Just go with system default timezone - return tzlocal() - - -def now_utc(): - """Retrieve the current time in UTC - - Returns: - (datetime): The current time in Universal Time, aka GMT - """ - return to_utc(datetime.utcnow()) - - -def now_local(tz=None): - """Retrieve the current time - - Arguments: - tz (datetime.tzinfo, optional): Timezone, default to user's settings - - Returns: - (datetime): The current time - """ - if not tz: - tz = default_timezone() - return datetime.now(tz) - - -def to_utc(dt): - """Convert a datetime with timezone info to a UTC datetime - - Arguments: - dt (datetime): A datetime (presumably in some local zone) - Returns: - (datetime): time converted to UTC - """ - tzUTC = gettz("UTC") - if dt.tzinfo: - return dt.astimezone(tzUTC) - else: - return dt.replace(tzinfo=gettz("UTC")).astimezone(tzUTC) - - -def to_local(dt): - """Convert a datetime to the user's local timezone - - Arguments: - dt (datetime): A datetime (if no timezone, defaults to UTC) - Returns: - (datetime): time converted to the local timezone - """ - tz = default_timezone() - if dt.tzinfo: - return dt.astimezone(tz) - else: - return dt.replace(tzinfo=gettz("UTC")).astimezone(tz) + return _default_tz() def to_system(dt): diff --git a/requirements/minimal.txt b/requirements/minimal.txt index 37ef35b59816..3e23c67c0c84 100644 --- a/requirements/minimal.txt +++ b/requirements/minimal.txt @@ -1,10 +1,8 @@ requests pyee -#lingua-franca -git+https://github.com/HelloChatterbox/lingua-franca@0.4.0rc1 +lingua-nostra==0.4.0a2 pyxdg -#mycroft-messagebus-client -git+https://github.com/HelloChatterbox/mycroft-messagebus-client +mycroft-messagebus-client>=0.9.1 inflection psutil fasteners diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 30c2a8d1a27c..1a2303e10c24 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -15,7 +15,7 @@ python-dateutil==2.6.0 fasteners==0.14.1 PyYAML==5.4 -lingua-franca==0.2.2 +lingua-nostra==0.4.0a2 msm==0.8.8 msk==0.3.16 mycroft-messagebus-client==0.9.1 diff --git a/setup.py b/setup.py index 2948ca4cc5eb..8a4e29cb02d1 100644 --- a/setup.py +++ b/setup.py @@ -16,15 +16,15 @@ setup( name='mycroft-lib', - version="2021.1.11a5", + version="2021.4.2a1", license='Apache-2.0', url='https://github.com/HelloChatterbox/mycroft-lib', description='Mycroft packaged as a library', install_requires=["requests", "pyee", - "lingua-franca>=0.3.1", + "lingua-nostra==0.4.0a2", "pyxdg", - "mycroft-messagebus-client", + "mycroft-messagebus-client>=0.9.1", "inflection", "psutil", "fasteners",