From a174c3c82216cfbb53445988efae53669ede1c55 Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Thu, 22 Oct 2020 12:12:09 +0100 Subject: [PATCH 01/17] replaced hard coded /tmp --- mycroft/client/enclosure/mark1/__init__.py | 6 ++++-- mycroft/lock/__init__.py | 2 +- mycroft/skills/msm_wrapper.py | 2 +- mycroft/skills/skill_updater.py | 2 +- mycroft/tts/tts.py | 2 +- mycroft/util/audio_test.py | 4 ++-- test/unittests/audio/test_utils.py | 4 ++-- test/unittests/lock/test_lock.py | 17 ++++++++--------- test/unittests/util/test_audio_utils.py | 4 ++-- test/unittests/util/test_download.py | 15 ++++++++------- 10 files changed, 30 insertions(+), 28 deletions(-) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index 26a7311c7916..896b3eda9666 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -15,6 +15,8 @@ import subprocess import time import sys +import os +import tempfile from alsaaudio import Mixer from threading import Thread, Timer @@ -130,9 +132,9 @@ def process(self, data): 'utterance': "I am testing one two three"})) time.sleep(0.5) # Prevents recording the loud button press - record("/tmp/test.wav", 3.0) + record(os.path.join(tempfile.gettempdir(), "test.wav"), 3.0) mixer.setvolume(prev_vol) - play_wav("/tmp/test.wav").communicate() + play_wavv(os.path.join(tempfile.gettempdir(), "test.wav").communicate() # Test audio muting on arduino subprocess.call('speaker-test -P 10 -l 0 -s 1', shell=True) diff --git a/mycroft/lock/__init__.py b/mycroft/lock/__init__.py index 24fef32234e6..7b3b1428f148 100644 --- a/mycroft/lock/__init__.py +++ b/mycroft/lock/__init__.py @@ -98,7 +98,7 @@ class Lock: # python 3+ 'class Lock' # # Class constants - DIRECTORY = '/tmp/mycroft' + DIRECTORY = os.path.join(tempfile.gettempdir(), "mycroft", "ipc") #'/tmp/mycroft' FILE = '/{}.pid' # diff --git a/mycroft/skills/msm_wrapper.py b/mycroft/skills/msm_wrapper.py index 4f579f85ac5f..aeeb605bec5e 100644 --- a/mycroft/skills/msm_wrapper.py +++ b/mycroft/skills/msm_wrapper.py @@ -44,7 +44,7 @@ def _init_msm_lock(): msm_lock = None try: - msm_lock = ComboLock('/tmp/mycroft-msm.lck') + msm_lock = ComboLock(os.path.join(tempfile.gettempdir(), "mycroft-msm.lck")) LOG.debug('mycroft-msm combo lock instantiated') except Exception: LOG.exception('Failed to create msm lock!') diff --git a/mycroft/skills/skill_updater.py b/mycroft/skills/skill_updater.py index ace247e2db4b..542a74c6fcc0 100644 --- a/mycroft/skills/skill_updater.py +++ b/mycroft/skills/skill_updater.py @@ -48,7 +48,7 @@ class SkillUpdater: _msm = None def __init__(self, bus=None): - self.msm_lock = ComboLock('/tmp/mycroft-msm.lck') + self.msm_lock = ComboLock(os.path.join(tempfile.gettempdir(), "mycroft-msm.lck")) self.install_retries = 0 self.config = Configuration.get() update_interval = self.config['skills']['update_interval'] diff --git a/mycroft/tts/tts.py b/mycroft/tts/tts.py index e1d1f6f66ca6..c37dd26c8396 100644 --- a/mycroft/tts/tts.py +++ b/mycroft/tts/tts.py @@ -176,7 +176,7 @@ def __init__(self, lang, config, validator, audio_ext='wav', self.ssml_tags = ssml_tags or [] self.voice = config.get("voice") - self.filename = '/tmp/tts.wav' + self.filename = 'os.path.join(tempfile.gettempdir(), "tts.wav")' self.enclosure = None random.seed() self.queue = Queue() diff --git a/mycroft/util/audio_test.py b/mycroft/util/audio_test.py index 2e41efbecba1..e4d06123430b 100644 --- a/mycroft/util/audio_test.py +++ b/mycroft/util/audio_test.py @@ -72,8 +72,8 @@ def record(filename, duration): def main(): parser = argparse.ArgumentParser() parser.add_argument( - '-f', '--filename', dest='filename', default="/tmp/test.wav", - help="Filename for saved audio (Default: /tmp/test.wav)") + '-f', '--filename', dest='filename', default= os.path.join(tempfile.gettempdir(), "test.wav"), + help="Filename for saved audio (Default: os.path.join(tempfile.gettempdir(), "test.wav")") parser.add_argument( '-d', '--duration', dest='duration', type=int, default=10, help="Duration of recording in seconds (Default: 10)") diff --git a/test/unittests/audio/test_utils.py b/test/unittests/audio/test_utils.py index 4bc97083d1e8..67ab266a103c 100644 --- a/test/unittests/audio/test_utils.py +++ b/test/unittests/audio/test_utils.py @@ -38,8 +38,8 @@ def wait_while_speaking_thread(): class TestInterface(unittest.TestCase): def setUp(self): - if exists('/tmp/mycroft'): - rmtree('/tmp/mycroft') + if exists(os.path.join(tempfile.gettempdir(), "mycroft")): + rmtree(os.path.join(tempfile.gettempdir(), "mycroft")) def test_is_speaking(self): create_signal('isSpeaking') diff --git a/test/unittests/lock/test_lock.py b/test/unittests/lock/test_lock.py index 5dbb2e900d62..7a7c587494f9 100644 --- a/test/unittests/lock/test_lock.py +++ b/test/unittests/lock/test_lock.py @@ -25,24 +25,24 @@ class TestLock(unittest.TestCase): def setUp(self): - if exists('/tmp/mycroft'): - rmtree('/tmp/mycroft') + if exists(os.path.join(tempfile.gettempdir(), "mycroft")): + rmtree(os.path.join(tempfile.gettempdir(), "mycroft")) def test_create_lock(self): l1 = Lock('test') - self.assertTrue(isfile('/tmp/mycroft/test.pid')) + self.assertTrue(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) def test_delete_lock(self): l1 = Lock('test') - self.assertTrue(isfile('/tmp/mycroft/test.pid')) + self.assertTrue(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) l1.delete() - self.assertFalse(isfile('/tmp/mycroft/test.pid')) + self.assertFalse(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) @patch('os.kill') def test_existing_lock(self, mock_kill): """ Test that an existing lock will kill the old pid. """ l1 = Lock('test') - self.assertTrue(isfile('/tmp/mycroft/test.pid')) + self.assertTrue(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid")) ) l2 = Lock('test2') self.assertFalse(mock_kill.called) l2 = Lock('test') @@ -50,13 +50,12 @@ def test_existing_lock(self, mock_kill): def test_keyboard_interrupt(self): l1 = Lock('test') - self.assertTrue(isfile('/tmp/mycroft/test.pid')) + self.assertTrue(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid")) ) try: os.kill(os.getpid(), signal.SIGINT) except KeyboardInterrupt: pass - self.assertFalse(isfile('/tmp/mycroft/test.pid')) - + self.assertFalse(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid")) ) if __name__ == '__main__': unittest.main() diff --git a/test/unittests/util/test_audio_utils.py b/test/unittests/util/test_audio_utils.py index 23ead2b1891e..499416068062 100644 --- a/test/unittests/util/test_audio_utils.py +++ b/test/unittests/util/test_audio_utils.py @@ -137,7 +137,7 @@ def test_record_with_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = '/tmp/test.wav' + filename = os.path.join(tempfile.gettempdir(), "test.wav") duration = 42 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', @@ -152,7 +152,7 @@ def test_record_without_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = '/tmp/test.wav' + filename = os.path.join(tempfile.gettempdir(), "test.wav") duration = 0 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index fddde0054219..3b7d152b99bf 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -5,7 +5,7 @@ _get_download_tmp) TEST_URL = 'http://example.com/mycroft-test.tar.gz' -TEST_DEST = '/tmp/file.tar.gz' +TEST_DEST = os.path.join(tempfile.gettempdir(), "file.tar.gz") @mock.patch('mycroft.util.download.subprocess') @@ -90,16 +90,17 @@ def wget_call(*args, **kwargs): class TestGetTemp(TestCase): def test_no_existing(self, mock_glob): mock_glob.return_value = [] - dest = '/tmp/test' + dest = os.path.join(tempfile.gettempdir(), "test") self.assertEqual(_get_download_tmp(dest), dest + '.part') def test_existing(self, mock_glob): - mock_glob.return_value = ['/tmp/test.part'] - dest = '/tmp/test' + mock_glob.return_value = [os.path.join(tempfile.gettempdir(), "test.part")] + dest = os.path.join(tempfile.gettempdir(), "test") self.assertEqual(_get_download_tmp(dest), dest + '.part.1') def test_multiple_existing(self, mock_glob): - mock_glob.return_value = ['/tmp/test.part', '/tmp/test.part.1', - '/tmp/test.part.2'] - dest = '/tmp/test' + mock_glob.return_value = [os.path.join(tempfile.gettempdir(), "test.part"), os.path.join(tempfile.gettempdir(), "test.part.1"), + os.path.join(tempfile.gettempdir(), "test.part.2")] + + dest = os.path.join(tempfile.gettempdir(), "test") self.assertEqual(_get_download_tmp(dest), dest + '.part.3') From 32f666edd459b126051ccc29f6753e403566045a Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Sun, 25 Oct 2020 13:29:52 +0100 Subject: [PATCH 02/17] Issue-2727 - fixing hard coded /tmp --- mycroft/client/enclosure/mark1/__init__.py | 2 ++ mycroft/lock/__init__.py | 1 + mycroft/skills/msm_wrapper.py | 3 +++ mycroft/skills/skill_updater.py | 1 + mycroft/tts/tts.py | 1 + mycroft/util/audio_test.py | 1 + test/unittests/audio/test_utils.py | 2 ++ test/unittests/lock/test_lock.py | 1 + test/unittests/util/test_audio_utils.py | 4 ++++ test/unittests/util/test_download.py | 3 +++ 10 files changed, 19 insertions(+) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index 896b3eda9666..5c98f356da62 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -14,6 +14,8 @@ # import subprocess import time +import os +import tempfile import sys import os import tempfile diff --git a/mycroft/lock/__init__.py b/mycroft/lock/__init__.py index 7b3b1428f148..0e87d0210225 100644 --- a/mycroft/lock/__init__.py +++ b/mycroft/lock/__init__.py @@ -16,6 +16,7 @@ SIG_DFL, default_int_handler, SIG_IGN # signals import os # Operating System functions +import tempfile # diff --git a/mycroft/skills/msm_wrapper.py b/mycroft/skills/msm_wrapper.py index aeeb605bec5e..eab9c95fd813 100644 --- a/mycroft/skills/msm_wrapper.py +++ b/mycroft/skills/msm_wrapper.py @@ -19,6 +19,9 @@ take. This is especially true at boot time when MSM is instantiated frequently. To improve performance, the MSM instance is cached. """ + +import os +import tempfile from collections import namedtuple from functools import lru_cache from os import path, makedirs diff --git a/mycroft/skills/skill_updater.py b/mycroft/skills/skill_updater.py index 542a74c6fcc0..36faae144a1e 100644 --- a/mycroft/skills/skill_updater.py +++ b/mycroft/skills/skill_updater.py @@ -14,6 +14,7 @@ # """Periodically run by skill manager to update skills and post the manifest.""" import os +import tempfile import sys from datetime import datetime from time import time diff --git a/mycroft/tts/tts.py b/mycroft/tts/tts.py index c37dd26c8396..061968f343fb 100644 --- a/mycroft/tts/tts.py +++ b/mycroft/tts/tts.py @@ -14,6 +14,7 @@ # from copy import deepcopy import os +import tempfile import random import re from abc import ABCMeta, abstractmethod diff --git a/mycroft/util/audio_test.py b/mycroft/util/audio_test.py index e4d06123430b..7c7294c92f4b 100644 --- a/mycroft/util/audio_test.py +++ b/mycroft/util/audio_test.py @@ -14,6 +14,7 @@ # import argparse import os +import tempfile import pyaudio from contextlib import contextmanager diff --git a/test/unittests/audio/test_utils.py b/test/unittests/audio/test_utils.py index 67ab266a103c..32c5df98507a 100644 --- a/test/unittests/audio/test_utils.py +++ b/test/unittests/audio/test_utils.py @@ -14,6 +14,8 @@ # import unittest import unittest.mock as mock +import os +import tempfile from shutil import rmtree from threading import Thread diff --git a/test/unittests/lock/test_lock.py b/test/unittests/lock/test_lock.py index 7a7c587494f9..4ee154a68aa4 100644 --- a/test/unittests/lock/test_lock.py +++ b/test/unittests/lock/test_lock.py @@ -13,6 +13,7 @@ # limitations under the License. # import signal +import tempfile import unittest from shutil import rmtree diff --git a/test/unittests/util/test_audio_utils.py b/test/unittests/util/test_audio_utils.py index 499416068062..84c5c8666b6e 100644 --- a/test/unittests/util/test_audio_utils.py +++ b/test/unittests/util/test_audio_utils.py @@ -1,3 +1,7 @@ + +import os +import tempfile + from unittest import TestCase, mock from test.util import Anything diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index 3b7d152b99bf..fddfa683d92d 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -1,3 +1,6 @@ + +import os +import tempfile from threading import Event from unittest import TestCase, mock From 92521588293168d4e40bc3b10f3d649862f4f3cd Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Thu, 29 Oct 2020 16:14:02 +0100 Subject: [PATCH 03/17] rectified pep8 issues --- buggix | 0 mycroft/client/enclosure/mark1/__init__.py | 59 +++++++++++----------- mycroft/lock/__init__.py | 5 +- mycroft/skills/msm_wrapper.py | 3 +- mycroft/skills/skill_updater.py | 1 - mycroft/tts/tts.py | 4 +- mycroft/util/audio_test.py | 4 +- test/unittests/lock/test_lock.py | 23 ++++++--- test/unittests/util/test_audio_utils.py | 8 +-- test/unittests/util/test_download.py | 19 +++---- 10 files changed, 68 insertions(+), 58 deletions(-) create mode 100644 buggix diff --git a/buggix b/buggix new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index 5c98f356da62..3efe2298ff1d 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -14,8 +14,7 @@ # import subprocess import time -import os -import tempfile +import tempfile import sys import os import tempfile @@ -134,9 +133,9 @@ def process(self, data): 'utterance': "I am testing one two three"})) time.sleep(0.5) # Prevents recording the loud button press - record(os.path.join(tempfile.gettempdir(), "test.wav"), 3.0) + record(os.path.join(tempfile.gettempdir(), "test.wav"), 3.0) mixer.setvolume(prev_vol) - play_wavv(os.path.join(tempfile.gettempdir(), "test.wav").communicate() + play_wavv(os.path.join(tempfile.gettempdir(), "test.wav").communicate() # Test audio muting on arduino subprocess.call('speaker-test -P 10 -l 0 -s 1', shell=True) @@ -192,12 +191,12 @@ def process(self, data): 'utterance': mycroft.dialog.get("ssh disabled")})) if "unit.enable-learning" in data or "unit.disable-learning" in data: - enable = 'enable' in data - word = 'enabled' if enable else 'disabled' + enable='enable' in data + word='enabled' if enable else 'disabled' LOG.info("Setting opt_in to: " + word) - new_config = {'opt_in': enable} - user_config = LocalConf(USER_CONFIG) + new_config={'opt_in': enable} + user_config=LocalConf(USER_CONFIG) user_config.merge(new_config) user_config.store() @@ -205,7 +204,7 @@ def process(self, data): 'utterance': mycroft.dialog.get("learning " + word)})) def stop(self): - self.alive = False + self.alive=False class EnclosureWriter(Thread): @@ -226,17 +225,17 @@ class EnclosureWriter(Thread): def __init__(self, serial, bus, size=16): super(EnclosureWriter, self).__init__(target=self.flush) - self.alive = True - self.daemon = True - self.serial = serial - self.bus = bus - self.commands = Queue(size) + self.alive=True + self.daemon=True + self.serial=serial + self.bus=bus + self.commands=Queue(size) self.start() def flush(self): while self.alive: try: - cmd = self.commands.get() + '\n' + cmd=self.commands.get() + '\n' self.serial.write(cmd.encode()) self.commands.task_done() except Exception as e: @@ -246,7 +245,7 @@ def write(self, command): self.commands.put(str(command)) def stop(self): - self.alive = False + self.alive=False class EnclosureMark1(Enclosure): @@ -263,19 +262,19 @@ class EnclosureMark1(Enclosure): E.g. Start and Stop talk animation """ - _last_internet_notification = 0 + _last_internet_notification=0 def __init__(self): super().__init__() self.__init_serial() - self.reader = EnclosureReader(self.serial, self.bus, self.lang) - self.writer = EnclosureWriter(self.serial, self.bus) + self.reader=EnclosureReader(self.serial, self.bus, self.lang) + self.writer=EnclosureWriter(self.serial, self.bus) # Prepare to receive message when the Arduino responds to the # following "system.version" self.bus.on("enclosure.started", self.on_arduino_responded) - self.arduino_responded = False + self.arduino_responded=False # Send a message to the Arduino across the serial line asking # for a reply with version info. self.writer.write("system.version") @@ -293,12 +292,12 @@ def __init__(self): init_display_manager_bus_connection() def on_arduino_responded(self, event=None): - self.eyes = EnclosureEyes(self.bus, self.writer) - self.mouth = EnclosureMouth(self.bus, self.writer) - self.system = EnclosureArduino(self.bus, self.writer) + self.eyes=EnclosureEyes(self.bus, self.writer) + self.mouth=EnclosureMouth(self.bus, self.writer) + self.system=EnclosureArduino(self.bus, self.writer) self.__register_events() self.__reset() - self.arduino_responded = True + self.arduino_responded=True # verify internet connection and prompt user on bootup if needed if not connected(): @@ -317,7 +316,7 @@ def on_no_internet(self, event=None): # don't bother the user with multiple notifications with 30 secs return - Enclosure._last_internet_notification = time.time() + Enclosure._last_internet_notification=time.time() # TODO: This should go into EnclosureMark1 subclass of Enclosure. if has_been_paired(): @@ -333,10 +332,10 @@ def on_no_internet(self, event=None): def __init_serial(self): try: - self.port = self.config.get("port") - self.rate = self.config.get("rate") - self.timeout = self.config.get("timeout") - self.serial = serial.serial_for_url( + self.port=self.config.get("port") + self.rate=self.config.get("rate") + self.timeout=self.config.get("timeout") + self.serial=serial.serial_for_url( url=self.port, baudrate=self.rate, timeout=self.timeout) LOG.info("Connected to: %s rate: %s timeout: %s" % (self.port, self.rate, self.timeout)) @@ -420,5 +419,5 @@ def _do_net_check(self): time.sleep(2) # a pause sounds better than just jumping in # Kick off wifi-setup automatically - data = {'allow_timeout': False, 'lang': self.lang} + data={'allow_timeout': False, 'lang': self.lang} self.bus.emit(Message('system.wifi.setup', data)) diff --git a/mycroft/lock/__init__.py b/mycroft/lock/__init__.py index 0e87d0210225..19b73a72cefe 100644 --- a/mycroft/lock/__init__.py +++ b/mycroft/lock/__init__.py @@ -16,7 +16,7 @@ SIG_DFL, default_int_handler, SIG_IGN # signals import os # Operating System functions -import tempfile +import tempfile # @@ -99,7 +99,8 @@ class Lock: # python 3+ 'class Lock' # # Class constants - DIRECTORY = os.path.join(tempfile.gettempdir(), "mycroft", "ipc") #'/tmp/mycroft' + DIRECTORY = os.path.join(tempfile.gettempdir(), + "mycroft", "ipc") # '/tmp/mycroft' FILE = '/{}.pid' # diff --git a/mycroft/skills/msm_wrapper.py b/mycroft/skills/msm_wrapper.py index eab9c95fd813..da36626a13a0 100644 --- a/mycroft/skills/msm_wrapper.py +++ b/mycroft/skills/msm_wrapper.py @@ -47,7 +47,8 @@ def _init_msm_lock(): msm_lock = None try: - msm_lock = ComboLock(os.path.join(tempfile.gettempdir(), "mycroft-msm.lck")) + msm_lock = ComboLock(os.path.join( + tempfile.gettempdir(), "mycroft-msm.lck")) LOG.debug('mycroft-msm combo lock instantiated') except Exception: LOG.exception('Failed to create msm lock!') diff --git a/mycroft/skills/skill_updater.py b/mycroft/skills/skill_updater.py index 36faae144a1e..7c3712fdc041 100644 --- a/mycroft/skills/skill_updater.py +++ b/mycroft/skills/skill_updater.py @@ -18,7 +18,6 @@ import sys from datetime import datetime from time import time - from msm import MsmException from mycroft.api import DeviceApi, is_paired diff --git a/mycroft/tts/tts.py b/mycroft/tts/tts.py index 061968f343fb..c30fd0d1b665 100644 --- a/mycroft/tts/tts.py +++ b/mycroft/tts/tts.py @@ -165,6 +165,7 @@ class TTS(metaclass=ABCMeta): phonetic_spelling (bool): Whether to spell certain words phonetically ssml_tags (list): Supported ssml properties. Ex. ['speak', 'prosody'] """ + def __init__(self, lang, config, validator, audio_ext='wav', phonetic_spelling=True, ssml_tags=None): super(TTS, self).__init__() @@ -177,7 +178,7 @@ def __init__(self, lang, config, validator, audio_ext='wav', self.ssml_tags = ssml_tags or [] self.voice = config.get("voice") - self.filename = 'os.path.join(tempfile.gettempdir(), "tts.wav")' + self.filename = 'os.path.join(tempfile.gettempdir(), "tts.wav")' self.enclosure = None random.seed() self.queue = Queue() @@ -472,6 +473,7 @@ class TTSValidator(metaclass=ABCMeta): It exposes and implements ``validate(tts)`` function as a template to validate the TTS engines. """ + def __init__(self, tts): self.tts = tts diff --git a/mycroft/util/audio_test.py b/mycroft/util/audio_test.py index 7c7294c92f4b..7906375904f8 100644 --- a/mycroft/util/audio_test.py +++ b/mycroft/util/audio_test.py @@ -73,8 +73,8 @@ def record(filename, duration): def main(): parser = argparse.ArgumentParser() parser.add_argument( - '-f', '--filename', dest='filename', default= os.path.join(tempfile.gettempdir(), "test.wav"), - help="Filename for saved audio (Default: os.path.join(tempfile.gettempdir(), "test.wav")") + '-f', '--filename', dest='filename', default=os.path.join(tempfile.gettempdir(), "test.wav"), + help="Filename for saved audio (Default: os.path.join(tempfile.gettempdir(), "test.wav")") parser.add_argument( '-d', '--duration', dest='duration', type=int, default=10, help="Duration of recording in seconds (Default: 10)") diff --git a/test/unittests/lock/test_lock.py b/test/unittests/lock/test_lock.py index 4ee154a68aa4..2961f638fdc2 100644 --- a/test/unittests/lock/test_lock.py +++ b/test/unittests/lock/test_lock.py @@ -26,24 +26,28 @@ class TestLock(unittest.TestCase): def setUp(self): - if exists(os.path.join(tempfile.gettempdir(), "mycroft")): - rmtree(os.path.join(tempfile.gettempdir(), "mycroft")) + if exists(os.path.join(tempfile.gettempdir(), "mycroft")): + rmtree(os.path.join(tempfile.gettempdir(), "mycroft")) def test_create_lock(self): l1 = Lock('test') - self.assertTrue(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + self.assertTrue( + isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) def test_delete_lock(self): l1 = Lock('test') - self.assertTrue(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + self.assertTrue( + isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) l1.delete() - self.assertFalse(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + self.assertFalse( + isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) @patch('os.kill') def test_existing_lock(self, mock_kill): """ Test that an existing lock will kill the old pid. """ l1 = Lock('test') - self.assertTrue(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid")) ) + self.assertTrue( + isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) l2 = Lock('test2') self.assertFalse(mock_kill.called) l2 = Lock('test') @@ -51,12 +55,15 @@ def test_existing_lock(self, mock_kill): def test_keyboard_interrupt(self): l1 = Lock('test') - self.assertTrue(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid")) ) + self.assertTrue( + isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) try: os.kill(os.getpid(), signal.SIGINT) except KeyboardInterrupt: pass - self.assertFalse(isfile (os.path.join(tempfile.gettempdir(), "mycroft", "test.pid")) ) + self.assertFalse( + isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + if __name__ == '__main__': unittest.main() diff --git a/test/unittests/util/test_audio_utils.py b/test/unittests/util/test_audio_utils.py index 84c5c8666b6e..4eb21826f3c5 100644 --- a/test/unittests/util/test_audio_utils.py +++ b/test/unittests/util/test_audio_utils.py @@ -1,6 +1,6 @@ -import os -import tempfile +import os +import tempfile from unittest import TestCase, mock @@ -141,7 +141,7 @@ def test_record_with_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = os.path.join(tempfile.gettempdir(), "test.wav") + filename = os.path.join(tempfile.gettempdir(), "test.wav") duration = 42 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', @@ -156,7 +156,7 @@ def test_record_without_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = os.path.join(tempfile.gettempdir(), "test.wav") + filename = os.path.join(tempfile.gettempdir(), "test.wav") duration = 0 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index fddfa683d92d..7691ab0789cf 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -1,6 +1,6 @@ -import os -import tempfile +import os +import tempfile from threading import Event from unittest import TestCase, mock @@ -8,7 +8,7 @@ _get_download_tmp) TEST_URL = 'http://example.com/mycroft-test.tar.gz' -TEST_DEST = os.path.join(tempfile.gettempdir(), "file.tar.gz") +TEST_DEST = os.path.join(tempfile.gettempdir(), "file.tar.gz") @mock.patch('mycroft.util.download.subprocess') @@ -93,17 +93,18 @@ def wget_call(*args, **kwargs): class TestGetTemp(TestCase): def test_no_existing(self, mock_glob): mock_glob.return_value = [] - dest = os.path.join(tempfile.gettempdir(), "test") + dest = os.path.join(tempfile.gettempdir(), "test") self.assertEqual(_get_download_tmp(dest), dest + '.part') def test_existing(self, mock_glob): - mock_glob.return_value = [os.path.join(tempfile.gettempdir(), "test.part")] - dest = os.path.join(tempfile.gettempdir(), "test") + mock_glob.return_value = [os.path.join( + tempfile.gettempdir(), "test.part")] + dest = os.path.join(tempfile.gettempdir(), "test") self.assertEqual(_get_download_tmp(dest), dest + '.part.1') def test_multiple_existing(self, mock_glob): mock_glob.return_value = [os.path.join(tempfile.gettempdir(), "test.part"), os.path.join(tempfile.gettempdir(), "test.part.1"), - os.path.join(tempfile.gettempdir(), "test.part.2")] - - dest = os.path.join(tempfile.gettempdir(), "test") + os.path.join(tempfile.gettempdir(), "test.part.2")] + + dest = os.path.join(tempfile.gettempdir(), "test") self.assertEqual(_get_download_tmp(dest), dest + '.part.3') From 621b4ab6505b0ccdf1b6f9a92cbb3da43ec4e193 Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Tue, 3 Nov 2020 20:45:10 +0100 Subject: [PATCH 04/17] spaces added and uniformity in code --- mycroft/client/enclosure/mark1/__init__.py | 58 +++++++++++----------- mycroft/lock/__init__.py | 2 +- mycroft/skills/msm_wrapper.py | 2 +- mycroft/skills/skill_updater.py | 3 +- mycroft/tts/tts.py | 2 +- mycroft/util/audio_test.py | 4 +- test/unittests/audio/test_utils.py | 6 +-- test/unittests/lock/test_lock.py | 16 +++--- test/unittests/util/test_audio_utils.py | 4 +- test/unittests/util/test_download.py | 14 +++--- 10 files changed, 56 insertions(+), 55 deletions(-) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index 3efe2298ff1d..d1d0c1de3f05 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -133,9 +133,9 @@ def process(self, data): 'utterance': "I am testing one two three"})) time.sleep(0.5) # Prevents recording the loud button press - record(os.path.join(tempfile.gettempdir(), "test.wav"), 3.0) + record(os.path.join(tempfile.gettempdir(), 'play.wav'), 3.0) mixer.setvolume(prev_vol) - play_wavv(os.path.join(tempfile.gettempdir(), "test.wav").communicate() + play_wavv(os.path.join(tempfile.gettempdir(), 'play.wav').communicate() # Test audio muting on arduino subprocess.call('speaker-test -P 10 -l 0 -s 1', shell=True) @@ -191,12 +191,12 @@ def process(self, data): 'utterance': mycroft.dialog.get("ssh disabled")})) if "unit.enable-learning" in data or "unit.disable-learning" in data: - enable='enable' in data - word='enabled' if enable else 'disabled' + enable = 'enable' in data + word = 'enabled' if enable else 'disabled' LOG.info("Setting opt_in to: " + word) - new_config={'opt_in': enable} - user_config=LocalConf(USER_CONFIG) + new_config = {'opt_in': enable} + user_config = LocalConf(USER_CONFIG) user_config.merge(new_config) user_config.store() @@ -223,19 +223,19 @@ class EnclosureWriter(Thread): Note: A command has to end with a line break """ - def __init__(self, serial, bus, size=16): + def __init__(self, serial, bus, size = 16): super(EnclosureWriter, self).__init__(target=self.flush) - self.alive=True - self.daemon=True - self.serial=serial - self.bus=bus - self.commands=Queue(size) + self.alive = True + self.daemon = True + self.serial = serial + self.bus = bus + self.commands = Queue(size) self.start() def flush(self): while self.alive: try: - cmd=self.commands.get() + '\n' + cmd = self.commands.get() + '\n' self.serial.write(cmd.encode()) self.commands.task_done() except Exception as e: @@ -245,7 +245,7 @@ def write(self, command): self.commands.put(str(command)) def stop(self): - self.alive=False + self.alive = False class EnclosureMark1(Enclosure): @@ -268,13 +268,13 @@ def __init__(self): super().__init__() self.__init_serial() - self.reader=EnclosureReader(self.serial, self.bus, self.lang) - self.writer=EnclosureWriter(self.serial, self.bus) + self.reader = EnclosureReader(self.serial, self.bus, self.lang) + self.writer = EnclosureWriter(self.serial, self.bus) # Prepare to receive message when the Arduino responds to the # following "system.version" self.bus.on("enclosure.started", self.on_arduino_responded) - self.arduino_responded=False + self.arduino_responded = False # Send a message to the Arduino across the serial line asking # for a reply with version info. self.writer.write("system.version") @@ -291,13 +291,13 @@ def __init__(self): # NOTE: this is a temporary place to connect the display manager init_display_manager_bus_connection() - def on_arduino_responded(self, event=None): - self.eyes=EnclosureEyes(self.bus, self.writer) - self.mouth=EnclosureMouth(self.bus, self.writer) - self.system=EnclosureArduino(self.bus, self.writer) + def on_arduino_responded(self, event = None): + self.eyes = EnclosureEyes(self.bus, self.writer) + self.mouth = EnclosureMouth(self.bus, self.writer) + self.system = EnclosureArduino(self.bus, self.writer) self.__register_events() self.__reset() - self.arduino_responded=True + self.arduino_responded = True # verify internet connection and prompt user on bootup if needed if not connected(): @@ -316,7 +316,7 @@ def on_no_internet(self, event=None): # don't bother the user with multiple notifications with 30 secs return - Enclosure._last_internet_notification=time.time() + Enclosure._last_internet_notification = time.time() # TODO: This should go into EnclosureMark1 subclass of Enclosure. if has_been_paired(): @@ -332,11 +332,11 @@ def on_no_internet(self, event=None): def __init_serial(self): try: - self.port=self.config.get("port") - self.rate=self.config.get("rate") - self.timeout=self.config.get("timeout") - self.serial=serial.serial_for_url( - url=self.port, baudrate=self.rate, timeout=self.timeout) + self.port = self.config.get("port") + self.rate = self.config.get("rate") + self.timeout = self.config.get("timeout") + self.serial = serial.serial_for_url( + url = self.port, baudrate=self.rate, timeout = self.timeout) LOG.info("Connected to: %s rate: %s timeout: %s" % (self.port, self.rate, self.timeout)) except Exception: @@ -419,5 +419,5 @@ def _do_net_check(self): time.sleep(2) # a pause sounds better than just jumping in # Kick off wifi-setup automatically - data={'allow_timeout': False, 'lang': self.lang} + data = {'allow_timeout': False, 'lang': self.lang} self.bus.emit(Message('system.wifi.setup', data)) diff --git a/mycroft/lock/__init__.py b/mycroft/lock/__init__.py index 19b73a72cefe..9b40dfe1e4ad 100644 --- a/mycroft/lock/__init__.py +++ b/mycroft/lock/__init__.py @@ -100,7 +100,7 @@ class Lock: # python 3+ 'class Lock' # # Class constants DIRECTORY = os.path.join(tempfile.gettempdir(), - "mycroft", "ipc") # '/tmp/mycroft' + 'mycroft') FILE = '/{}.pid' # diff --git a/mycroft/skills/msm_wrapper.py b/mycroft/skills/msm_wrapper.py index da36626a13a0..5c192c2f32cf 100644 --- a/mycroft/skills/msm_wrapper.py +++ b/mycroft/skills/msm_wrapper.py @@ -48,7 +48,7 @@ def _init_msm_lock(): msm_lock = None try: msm_lock = ComboLock(os.path.join( - tempfile.gettempdir(), "mycroft-msm.lck")) + tempfile.gettempdir(), 'mycroft-msm.lck')) LOG.debug('mycroft-msm combo lock instantiated') except Exception: LOG.exception('Failed to create msm lock!') diff --git a/mycroft/skills/skill_updater.py b/mycroft/skills/skill_updater.py index 7c3712fdc041..e8c7cbc40426 100644 --- a/mycroft/skills/skill_updater.py +++ b/mycroft/skills/skill_updater.py @@ -18,6 +18,7 @@ import sys from datetime import datetime from time import time + from msm import MsmException from mycroft.api import DeviceApi, is_paired @@ -48,7 +49,7 @@ class SkillUpdater: _msm = None def __init__(self, bus=None): - self.msm_lock = ComboLock(os.path.join(tempfile.gettempdir(), "mycroft-msm.lck")) + self.msm_lock = ComboLock(os.path.join(tempfile.gettempdir(), 'mycroft-msm.lck')) self.install_retries = 0 self.config = Configuration.get() update_interval = self.config['skills']['update_interval'] diff --git a/mycroft/tts/tts.py b/mycroft/tts/tts.py index c30fd0d1b665..8b47138a9e21 100644 --- a/mycroft/tts/tts.py +++ b/mycroft/tts/tts.py @@ -178,7 +178,7 @@ def __init__(self, lang, config, validator, audio_ext='wav', self.ssml_tags = ssml_tags or [] self.voice = config.get("voice") - self.filename = 'os.path.join(tempfile.gettempdir(), "tts.wav")' + self.filename = os.path.join(tempfile.gettempdir(), 'tts.wav') self.enclosure = None random.seed() self.queue = Queue() diff --git a/mycroft/util/audio_test.py b/mycroft/util/audio_test.py index 7906375904f8..268c0c701b34 100644 --- a/mycroft/util/audio_test.py +++ b/mycroft/util/audio_test.py @@ -73,8 +73,8 @@ def record(filename, duration): def main(): parser = argparse.ArgumentParser() parser.add_argument( - '-f', '--filename', dest='filename', default=os.path.join(tempfile.gettempdir(), "test.wav"), - help="Filename for saved audio (Default: os.path.join(tempfile.gettempdir(), "test.wav")") + '-f', '--filename', dest='filename', default = os.path.join(tempfile.gettempdir(), 'test.wav'), + help="Filename for saved audio (Default:{}".format(os.path.join(tempfile.gettempdir(), 'test.wav'))) parser.add_argument( '-d', '--duration', dest='duration', type=int, default=10, help="Duration of recording in seconds (Default: 10)") diff --git a/test/unittests/audio/test_utils.py b/test/unittests/audio/test_utils.py index 32c5df98507a..69409929c403 100644 --- a/test/unittests/audio/test_utils.py +++ b/test/unittests/audio/test_utils.py @@ -19,7 +19,7 @@ from shutil import rmtree from threading import Thread -from time import sleep +from time import sleepfarstack:bugfix/issue-2727 from os.path import exists @@ -40,8 +40,8 @@ def wait_while_speaking_thread(): class TestInterface(unittest.TestCase): def setUp(self): - if exists(os.path.join(tempfile.gettempdir(), "mycroft")): - rmtree(os.path.join(tempfile.gettempdir(), "mycroft")) + if exists(os.path.join(tempfile.gettempdir(), 'mycroft')): + rmtree(os.path.join(tempfile.gettempdir(), 'mycroft')) def test_is_speaking(self): create_signal('isSpeaking') diff --git a/test/unittests/lock/test_lock.py b/test/unittests/lock/test_lock.py index 2961f638fdc2..50c0afa3aa0d 100644 --- a/test/unittests/lock/test_lock.py +++ b/test/unittests/lock/test_lock.py @@ -26,28 +26,28 @@ class TestLock(unittest.TestCase): def setUp(self): - if exists(os.path.join(tempfile.gettempdir(), "mycroft")): - rmtree(os.path.join(tempfile.gettempdir(), "mycroft")) + if exists(os.path.join(tempfile.gettempdir(), 'mycroft')): + rmtree(os.path.join(tempfile.gettempdir(), 'mycroft')) def test_create_lock(self): l1 = Lock('test') self.assertTrue( - isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) def test_delete_lock(self): l1 = Lock('test') self.assertTrue( - isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) l1.delete() self.assertFalse( - isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) @patch('os.kill') def test_existing_lock(self, mock_kill): """ Test that an existing lock will kill the old pid. """ l1 = Lock('test') self.assertTrue( - isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) l2 = Lock('test2') self.assertFalse(mock_kill.called) l2 = Lock('test') @@ -56,13 +56,13 @@ def test_existing_lock(self, mock_kill): def test_keyboard_interrupt(self): l1 = Lock('test') self.assertTrue( - isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) try: os.kill(os.getpid(), signal.SIGINT) except KeyboardInterrupt: pass self.assertFalse( - isfile(os.path.join(tempfile.gettempdir(), "mycroft", "test.pid"))) + isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) if __name__ == '__main__': diff --git a/test/unittests/util/test_audio_utils.py b/test/unittests/util/test_audio_utils.py index 4eb21826f3c5..f716390211eb 100644 --- a/test/unittests/util/test_audio_utils.py +++ b/test/unittests/util/test_audio_utils.py @@ -141,7 +141,7 @@ def test_record_with_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = os.path.join(tempfile.gettempdir(), "test.wav") + filename = os.path.join(tempfile.gettempdir(), 'test.wav') duration = 42 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', @@ -156,7 +156,7 @@ def test_record_without_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = os.path.join(tempfile.gettempdir(), "test.wav") + filename = os.path.join(tempfile.gettempdir(), 'test.wav') duration = 0 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index 7691ab0789cf..822930b4ff26 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -8,7 +8,7 @@ _get_download_tmp) TEST_URL = 'http://example.com/mycroft-test.tar.gz' -TEST_DEST = os.path.join(tempfile.gettempdir(), "file.tar.gz") +TEST_DEST = os.path.join(tempfile.gettempdir(), 'file.tar.gz') @mock.patch('mycroft.util.download.subprocess') @@ -93,18 +93,18 @@ def wget_call(*args, **kwargs): class TestGetTemp(TestCase): def test_no_existing(self, mock_glob): mock_glob.return_value = [] - dest = os.path.join(tempfile.gettempdir(), "test") + dest = os.path.join(tempfile.gettempdir(), 'test') self.assertEqual(_get_download_tmp(dest), dest + '.part') def test_existing(self, mock_glob): mock_glob.return_value = [os.path.join( - tempfile.gettempdir(), "test.part")] - dest = os.path.join(tempfile.gettempdir(), "test") + tempfile.gettempdir(), 'test.part')] + dest = os.path.join(tempfile.gettempdir(), 'test') self.assertEqual(_get_download_tmp(dest), dest + '.part.1') def test_multiple_existing(self, mock_glob): - mock_glob.return_value = [os.path.join(tempfile.gettempdir(), "test.part"), os.path.join(tempfile.gettempdir(), "test.part.1"), - os.path.join(tempfile.gettempdir(), "test.part.2")] + mock_glob.return_value = [os.path.join(tempfile.gettempdir(), 'test.part'), os.path.join(tempfile.gettempdir(), 'test.part.1'), + os.path.join(tempfile.gettempdir(), 'test.part.2')] - dest = os.path.join(tempfile.gettempdir(), "test") + dest = os.path.join(tempfile.gettempdir(), 'test') self.assertEqual(_get_download_tmp(dest), dest + '.part.3') From d65917964573a328a64265893cc8cdaa99138869 Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Tue, 3 Nov 2020 21:05:31 +0100 Subject: [PATCH 05/17] more fixes --- mycroft/client/enclosure/mark1/__init__.py | 8 ++++---- test/unittests/audio/test_utils.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index d1d0c1de3f05..c1d094c9aa2e 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -133,9 +133,9 @@ def process(self, data): 'utterance': "I am testing one two three"})) time.sleep(0.5) # Prevents recording the loud button press - record(os.path.join(tempfile.gettempdir(), 'play.wav'), 3.0) + record(os.path.join(tempfile.gettempdir(), 'test.wav'), 3.0) mixer.setvolume(prev_vol) - play_wavv(os.path.join(tempfile.gettempdir(), 'play.wav').communicate() + play_wav(os.path.join(tempfile.gettempdir(), 'test.wav').communicate() # Test audio muting on arduino subprocess.call('speaker-test -P 10 -l 0 -s 1', shell=True) @@ -204,7 +204,7 @@ def process(self, data): 'utterance': mycroft.dialog.get("learning " + word)})) def stop(self): - self.alive=False + self.alive = False class EnclosureWriter(Thread): @@ -262,7 +262,7 @@ class EnclosureMark1(Enclosure): E.g. Start and Stop talk animation """ - _last_internet_notification=0 + _last_internet_notification = 0 def __init__(self): super().__init__() diff --git a/test/unittests/audio/test_utils.py b/test/unittests/audio/test_utils.py index 69409929c403..45e658fd1529 100644 --- a/test/unittests/audio/test_utils.py +++ b/test/unittests/audio/test_utils.py @@ -19,7 +19,7 @@ from shutil import rmtree from threading import Thread -from time import sleepfarstack:bugfix/issue-2727 +from time import sleep from os.path import exists From 304439ed4b8a4ab9ce42951d635e963cab9ed3c3 Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Wed, 4 Nov 2020 19:33:32 +0100 Subject: [PATCH 06/17] minor fixes --- mycroft/client/enclosure/mark1/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index c1d094c9aa2e..a423bbad8d3c 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -14,7 +14,6 @@ # import subprocess import time -import tempfile import sys import os import tempfile @@ -135,7 +134,7 @@ def process(self, data): time.sleep(0.5) # Prevents recording the loud button press record(os.path.join(tempfile.gettempdir(), 'test.wav'), 3.0) mixer.setvolume(prev_vol) - play_wav(os.path.join(tempfile.gettempdir(), 'test.wav').communicate() + play_wav(os.path.join(tempfile.gettempdir(), 'test.wav').communicate()) # Test audio muting on arduino subprocess.call('speaker-test -P 10 -l 0 -s 1', shell=True) @@ -223,7 +222,7 @@ class EnclosureWriter(Thread): Note: A command has to end with a line break """ - def __init__(self, serial, bus, size = 16): + def __init__(self, serial, bus, size=16): super(EnclosureWriter, self).__init__(target=self.flush) self.alive = True self.daemon = True From 3f745c52ade1b30f19c5d2f5a86bea9d6df19da0 Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Tue, 10 Nov 2020 20:22:51 +0100 Subject: [PATCH 07/17] created temp path function --- mycroft/client/enclosure/mark1/__init__.py | 7 +++---- mycroft/lock/__init__.py | 5 ++--- mycroft/skills/msm_wrapper.py | 7 ++----- mycroft/skills/skill_updater.py | 4 ++-- mycroft/tts/tts.py | 4 ++-- mycroft/util/audio_test.py | 6 +++--- mycroft/util/file_utils.py | 10 +++++++++- test/unittests/audio/test_utils.py | 7 +++---- test/unittests/lock/test_lock.py | 18 +++++++++--------- test/unittests/util/test_audio_utils.py | 8 +++----- test/unittests/util/test_download.py | 18 ++++++++---------- 11 files changed, 46 insertions(+), 48 deletions(-) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index a423bbad8d3c..6c4be8223988 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -15,8 +15,6 @@ import subprocess import time import sys -import os -import tempfile from alsaaudio import Mixer from threading import Thread, Timer @@ -37,6 +35,7 @@ from mycroft.util.audio_test import record from mycroft.util.log import LOG from queue import Queue +from mycroft.util import create_temp_path # The Mark 1 hardware consists of a Raspberry Pi main CPU which is connected # to an Arduino over the serial port. A custom serial protocol sends @@ -132,9 +131,9 @@ def process(self, data): 'utterance': "I am testing one two three"})) time.sleep(0.5) # Prevents recording the loud button press - record(os.path.join(tempfile.gettempdir(), 'test.wav'), 3.0) + record(create_temp_path('test.wav', 3.0) mixer.setvolume(prev_vol) - play_wav(os.path.join(tempfile.gettempdir(), 'test.wav').communicate()) + play_wav(creat_temp_path('test.wav')).communicate() # Test audio muting on arduino subprocess.call('speaker-test -P 10 -l 0 -s 1', shell=True) diff --git a/mycroft/lock/__init__.py b/mycroft/lock/__init__.py index 9b40dfe1e4ad..310a68f8607b 100644 --- a/mycroft/lock/__init__.py +++ b/mycroft/lock/__init__.py @@ -16,13 +16,13 @@ SIG_DFL, default_int_handler, SIG_IGN # signals import os # Operating System functions -import tempfile # # Wrapper around chain of handler functions for a specific system level signal. # Often used to trap Ctrl-C for specific application purposes. from mycroft.util import LOG +from mycroft.util import create_temp_path class Signal: # python 3+ class Signal @@ -99,8 +99,7 @@ class Lock: # python 3+ 'class Lock' # # Class constants - DIRECTORY = os.path.join(tempfile.gettempdir(), - 'mycroft') + DIRECTORY = create_temp_path('mycroft') FILE = '/{}.pid' # diff --git a/mycroft/skills/msm_wrapper.py b/mycroft/skills/msm_wrapper.py index 5c192c2f32cf..59424b99b270 100644 --- a/mycroft/skills/msm_wrapper.py +++ b/mycroft/skills/msm_wrapper.py @@ -19,9 +19,6 @@ take. This is especially true at boot time when MSM is instantiated frequently. To improve performance, the MSM instance is cached. """ - -import os -import tempfile from collections import namedtuple from functools import lru_cache from os import path, makedirs @@ -30,6 +27,7 @@ from mycroft.util.combo_lock import ComboLock from mycroft.util.log import LOG +from mycroft.util import create_temp_path MsmConfig = namedtuple( 'MsmConfig', @@ -47,8 +45,7 @@ def _init_msm_lock(): msm_lock = None try: - msm_lock = ComboLock(os.path.join( - tempfile.gettempdir(), 'mycroft-msm.lck')) + msm_lock = ComboLock(create_temp_path('mycroft-msm.lck')) LOG.debug('mycroft-msm combo lock instantiated') except Exception: LOG.exception('Failed to create msm lock!') diff --git a/mycroft/skills/skill_updater.py b/mycroft/skills/skill_updater.py index e8c7cbc40426..4beb985290d1 100644 --- a/mycroft/skills/skill_updater.py +++ b/mycroft/skills/skill_updater.py @@ -14,7 +14,6 @@ # """Periodically run by skill manager to update skills and post the manifest.""" import os -import tempfile import sys from datetime import datetime from time import time @@ -27,6 +26,7 @@ from mycroft.util.combo_lock import ComboLock from mycroft.util.log import LOG from .msm_wrapper import build_msm_config, create_msm +from mycroft.util import create_temp_path ONE_HOUR = 3600 FIVE_MINUTES = 300 # number of seconds in a minute @@ -49,7 +49,7 @@ class SkillUpdater: _msm = None def __init__(self, bus=None): - self.msm_lock = ComboLock(os.path.join(tempfile.gettempdir(), 'mycroft-msm.lck')) + self.msm_lock = ComboLock(create_temp_path('mycroft-msm.lck')) self.install_retries = 0 self.config = Configuration.get() update_interval = self.config['skills']['update_interval'] diff --git a/mycroft/tts/tts.py b/mycroft/tts/tts.py index 8b47138a9e21..55b66ff1a7cd 100644 --- a/mycroft/tts/tts.py +++ b/mycroft/tts/tts.py @@ -14,7 +14,6 @@ # from copy import deepcopy import os -import tempfile import random import re from abc import ABCMeta, abstractmethod @@ -32,6 +31,7 @@ from mycroft.util import ( play_wav, play_mp3, check_for_signal, create_signal, resolve_resource_file ) +from mycroft.util import create_temp_path from mycroft.util.log import LOG from mycroft.util.plugins import load_plugin from queue import Queue, Empty @@ -178,7 +178,7 @@ def __init__(self, lang, config, validator, audio_ext='wav', self.ssml_tags = ssml_tags or [] self.voice = config.get("voice") - self.filename = os.path.join(tempfile.gettempdir(), 'tts.wav') + self.filename = create_temp_path('tts.wav') self.enclosure = None random.seed() self.queue = Queue() diff --git a/mycroft/util/audio_test.py b/mycroft/util/audio_test.py index 268c0c701b34..22ff0427be2a 100644 --- a/mycroft/util/audio_test.py +++ b/mycroft/util/audio_test.py @@ -14,7 +14,6 @@ # import argparse import os -import tempfile import pyaudio from contextlib import contextmanager @@ -25,6 +24,7 @@ from mycroft.util.audio_utils import play_wav from mycroft.util.log import LOG import logging +from mycroft.util import create_temp_path """ Audio Test @@ -73,8 +73,8 @@ def record(filename, duration): def main(): parser = argparse.ArgumentParser() parser.add_argument( - '-f', '--filename', dest='filename', default = os.path.join(tempfile.gettempdir(), 'test.wav'), - help="Filename for saved audio (Default:{}".format(os.path.join(tempfile.gettempdir(), 'test.wav'))) + '-f', '--filename', dest='filename', default = create_temp_path('test.wav'), + help="Filename for saved audio (Default:{}".format(create_temp_path('test.wav'))) parser.add_argument( '-d', '--duration', dest='duration', type=int, default=10, help="Duration of recording in seconds (Default: 10)") diff --git a/mycroft/util/file_utils.py b/mycroft/util/file_utils.py index ad8ecf56bc80..ccc28940e648 100644 --- a/mycroft/util/file_utils.py +++ b/mycroft/util/file_utils.py @@ -77,6 +77,14 @@ def resolve_resource_file(res_name): return None # Resource cannot be resolved +def create_temp_path(*args): + try: + path = os.path.join(tempfile.gettempdir(), *args) + except TypeError: + path = None + LOG.error('Could not create a temp path, create_temp_path() only accepts Strings') + return path + def read_stripped_lines(filename): """Read a file and return a list of stripped lines. @@ -231,7 +239,7 @@ def get_cache_directory(domain=None): directory = config.get("cache_path") if not directory: # If not defined, use /tmp/mycroft/cache - directory = os.path.join(tempfile.gettempdir(), "mycroft", "cache") + directory = create_temp_path('mycroft', 'cache') return ensure_directory_exists(directory, domain) diff --git a/test/unittests/audio/test_utils.py b/test/unittests/audio/test_utils.py index 45e658fd1529..48bc526fec17 100644 --- a/test/unittests/audio/test_utils.py +++ b/test/unittests/audio/test_utils.py @@ -14,8 +14,6 @@ # import unittest import unittest.mock as mock -import os -import tempfile from shutil import rmtree from threading import Thread @@ -25,6 +23,7 @@ import mycroft.audio from mycroft.util import create_signal, check_for_signal +from mycroft.util import create_temp_path """Tests for public audio service utils.""" @@ -40,8 +39,8 @@ def wait_while_speaking_thread(): class TestInterface(unittest.TestCase): def setUp(self): - if exists(os.path.join(tempfile.gettempdir(), 'mycroft')): - rmtree(os.path.join(tempfile.gettempdir(), 'mycroft')) + if exists(create_temp_path('mycroft')): + rmtree(create_temp_path('mycroft')) def test_is_speaking(self): create_signal('isSpeaking') diff --git a/test/unittests/lock/test_lock.py b/test/unittests/lock/test_lock.py index 50c0afa3aa0d..c3aa8021b05d 100644 --- a/test/unittests/lock/test_lock.py +++ b/test/unittests/lock/test_lock.py @@ -13,7 +13,6 @@ # limitations under the License. # import signal -import tempfile import unittest from shutil import rmtree @@ -22,32 +21,33 @@ from os.path import exists, isfile from mycroft.lock import Lock +from mycroft.util import create_temp_path class TestLock(unittest.TestCase): def setUp(self): - if exists(os.path.join(tempfile.gettempdir(), 'mycroft')): - rmtree(os.path.join(tempfile.gettempdir(), 'mycroft')) + if exists(create_temp_path('mycroft')): + rmtree(create_temp_path('mycroft')) def test_create_lock(self): l1 = Lock('test') self.assertTrue( - isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) + isfile(create_temp_path('mycroft', 'test.pid'))) def test_delete_lock(self): l1 = Lock('test') self.assertTrue( - isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) + isfile(create_temp_path('mycroft', 'test.pid'))) l1.delete() self.assertFalse( - isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) + isfile(create_temp_path('mycroft', 'test.pid'))) @patch('os.kill') def test_existing_lock(self, mock_kill): """ Test that an existing lock will kill the old pid. """ l1 = Lock('test') self.assertTrue( - isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) + isfile(create_temp_path('mycroft', 'test.pid'))) l2 = Lock('test2') self.assertFalse(mock_kill.called) l2 = Lock('test') @@ -56,13 +56,13 @@ def test_existing_lock(self, mock_kill): def test_keyboard_interrupt(self): l1 = Lock('test') self.assertTrue( - isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) + isfile(create_temp_path('mycroft', 'test.pid'))) try: os.kill(os.getpid(), signal.SIGINT) except KeyboardInterrupt: pass self.assertFalse( - isfile(os.path.join(tempfile.gettempdir(), 'mycroft', 'test.pid'))) + isfile(create_temp_path('mycroft', 'test.pid'))) if __name__ == '__main__': diff --git a/test/unittests/util/test_audio_utils.py b/test/unittests/util/test_audio_utils.py index f716390211eb..96860f1d3009 100644 --- a/test/unittests/util/test_audio_utils.py +++ b/test/unittests/util/test_audio_utils.py @@ -1,12 +1,10 @@ -import os -import tempfile - from unittest import TestCase, mock from test.util import Anything from mycroft.util import (play_ogg, play_mp3, play_wav, play_audio_file, record) +from mycroft.util import create_temp_path test_config = { 'play_wav_cmdline': 'mock_wav %1', @@ -141,7 +139,7 @@ def test_record_with_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = os.path.join(tempfile.gettempdir(), 'test.wav') + filename = create_temp_path('test.wav') duration = 42 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', @@ -156,7 +154,7 @@ def test_record_without_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = os.path.join(tempfile.gettempdir(), 'test.wav') + filename = create_temp_path('test.wav') duration = 0 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index 822930b4ff26..c3c0189fe967 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -1,14 +1,13 @@ -import os -import tempfile from threading import Event from unittest import TestCase, mock from mycroft.util.download import (download, _running_downloads, _get_download_tmp) +from mycroft.util import create_temp_path TEST_URL = 'http://example.com/mycroft-test.tar.gz' -TEST_DEST = os.path.join(tempfile.gettempdir(), 'file.tar.gz') +TEST_DEST = create_temp_path('file.tar.gz') @mock.patch('mycroft.util.download.subprocess') @@ -93,18 +92,17 @@ def wget_call(*args, **kwargs): class TestGetTemp(TestCase): def test_no_existing(self, mock_glob): mock_glob.return_value = [] - dest = os.path.join(tempfile.gettempdir(), 'test') + dest = create_temp_path('test') self.assertEqual(_get_download_tmp(dest), dest + '.part') def test_existing(self, mock_glob): - mock_glob.return_value = [os.path.join( - tempfile.gettempdir(), 'test.part')] - dest = os.path.join(tempfile.gettempdir(), 'test') + mock_glob.return_value = [create_temp_path('test.part')] + dest = create_temp_path('test') self.assertEqual(_get_download_tmp(dest), dest + '.part.1') def test_multiple_existing(self, mock_glob): - mock_glob.return_value = [os.path.join(tempfile.gettempdir(), 'test.part'), os.path.join(tempfile.gettempdir(), 'test.part.1'), - os.path.join(tempfile.gettempdir(), 'test.part.2')] + mock_glob.return_value = [create_temp_path('test.part'), create_temp_path('test.part.1'), + create_temp_path('test.part.2')] - dest = os.path.join(tempfile.gettempdir(), 'test') + dest = create_temp_path('test') self.assertEqual(_get_download_tmp(dest), dest + '.part.3') From 7765d11224c57ee73376ebca52890115cd995d8b Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Wed, 11 Nov 2020 19:10:13 +0100 Subject: [PATCH 08/17] ran autopep8 --- mycroft/client/enclosure/mark1/__init__.py | 58 +++++++++++----------- mycroft/lock/__init__.py | 2 +- mycroft/skills/msm_wrapper.py | 2 +- mycroft/skills/skill_updater.py | 8 +-- mycroft/tts/tts.py | 2 +- mycroft/util/audio_test.py | 4 +- mycroft/util/file_utils.py | 4 +- test/unittests/audio/test_utils.py | 2 +- test/unittests/lock/test_lock.py | 2 +- test/unittests/util/test_audio_utils.py | 2 +- 10 files changed, 44 insertions(+), 42 deletions(-) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index 6c4be8223988..1018e7a5c8b4 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -35,7 +35,7 @@ from mycroft.util.audio_test import record from mycroft.util.log import LOG from queue import Queue -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path # The Mark 1 hardware consists of a Raspberry Pi main CPU which is connected # to an Arduino over the serial port. A custom serial protocol sends @@ -189,12 +189,12 @@ def process(self, data): 'utterance': mycroft.dialog.get("ssh disabled")})) if "unit.enable-learning" in data or "unit.disable-learning" in data: - enable = 'enable' in data - word = 'enabled' if enable else 'disabled' + enable='enable' in data + word='enabled' if enable else 'disabled' LOG.info("Setting opt_in to: " + word) - new_config = {'opt_in': enable} - user_config = LocalConf(USER_CONFIG) + new_config={'opt_in': enable} + user_config=LocalConf(USER_CONFIG) user_config.merge(new_config) user_config.store() @@ -202,7 +202,7 @@ def process(self, data): 'utterance': mycroft.dialog.get("learning " + word)})) def stop(self): - self.alive = False + self.alive=False class EnclosureWriter(Thread): @@ -223,17 +223,17 @@ class EnclosureWriter(Thread): def __init__(self, serial, bus, size=16): super(EnclosureWriter, self).__init__(target=self.flush) - self.alive = True - self.daemon = True - self.serial = serial - self.bus = bus - self.commands = Queue(size) + self.alive=True + self.daemon=True + self.serial=serial + self.bus=bus + self.commands=Queue(size) self.start() def flush(self): while self.alive: try: - cmd = self.commands.get() + '\n' + cmd=self.commands.get() + '\n' self.serial.write(cmd.encode()) self.commands.task_done() except Exception as e: @@ -243,7 +243,7 @@ def write(self, command): self.commands.put(str(command)) def stop(self): - self.alive = False + self.alive=False class EnclosureMark1(Enclosure): @@ -260,19 +260,19 @@ class EnclosureMark1(Enclosure): E.g. Start and Stop talk animation """ - _last_internet_notification = 0 + _last_internet_notification=0 def __init__(self): super().__init__() self.__init_serial() - self.reader = EnclosureReader(self.serial, self.bus, self.lang) - self.writer = EnclosureWriter(self.serial, self.bus) + self.reader=EnclosureReader(self.serial, self.bus, self.lang) + self.writer=EnclosureWriter(self.serial, self.bus) # Prepare to receive message when the Arduino responds to the # following "system.version" self.bus.on("enclosure.started", self.on_arduino_responded) - self.arduino_responded = False + self.arduino_responded=False # Send a message to the Arduino across the serial line asking # for a reply with version info. self.writer.write("system.version") @@ -289,13 +289,13 @@ def __init__(self): # NOTE: this is a temporary place to connect the display manager init_display_manager_bus_connection() - def on_arduino_responded(self, event = None): - self.eyes = EnclosureEyes(self.bus, self.writer) - self.mouth = EnclosureMouth(self.bus, self.writer) - self.system = EnclosureArduino(self.bus, self.writer) + def on_arduino_responded(self, event=None): + self.eyes=EnclosureEyes(self.bus, self.writer) + self.mouth=EnclosureMouth(self.bus, self.writer) + self.system=EnclosureArduino(self.bus, self.writer) self.__register_events() self.__reset() - self.arduino_responded = True + self.arduino_responded=True # verify internet connection and prompt user on bootup if needed if not connected(): @@ -314,7 +314,7 @@ def on_no_internet(self, event=None): # don't bother the user with multiple notifications with 30 secs return - Enclosure._last_internet_notification = time.time() + Enclosure._last_internet_notification=time.time() # TODO: This should go into EnclosureMark1 subclass of Enclosure. if has_been_paired(): @@ -330,11 +330,11 @@ def on_no_internet(self, event=None): def __init_serial(self): try: - self.port = self.config.get("port") - self.rate = self.config.get("rate") - self.timeout = self.config.get("timeout") - self.serial = serial.serial_for_url( - url = self.port, baudrate=self.rate, timeout = self.timeout) + self.port=self.config.get("port") + self.rate=self.config.get("rate") + self.timeout=self.config.get("timeout") + self.serial=serial.serial_for_url( + url=self.port, baudrate=self.rate, timeout=self.timeout) LOG.info("Connected to: %s rate: %s timeout: %s" % (self.port, self.rate, self.timeout)) except Exception: @@ -417,5 +417,5 @@ def _do_net_check(self): time.sleep(2) # a pause sounds better than just jumping in # Kick off wifi-setup automatically - data = {'allow_timeout': False, 'lang': self.lang} + data={'allow_timeout': False, 'lang': self.lang} self.bus.emit(Message('system.wifi.setup', data)) diff --git a/mycroft/lock/__init__.py b/mycroft/lock/__init__.py index 310a68f8607b..f7ef3e4a6a48 100644 --- a/mycroft/lock/__init__.py +++ b/mycroft/lock/__init__.py @@ -22,7 +22,7 @@ # Wrapper around chain of handler functions for a specific system level signal. # Often used to trap Ctrl-C for specific application purposes. from mycroft.util import LOG -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path class Signal: # python 3+ class Signal diff --git a/mycroft/skills/msm_wrapper.py b/mycroft/skills/msm_wrapper.py index 59424b99b270..dc0674589846 100644 --- a/mycroft/skills/msm_wrapper.py +++ b/mycroft/skills/msm_wrapper.py @@ -27,7 +27,7 @@ from mycroft.util.combo_lock import ComboLock from mycroft.util.log import LOG -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path MsmConfig = namedtuple( 'MsmConfig', diff --git a/mycroft/skills/skill_updater.py b/mycroft/skills/skill_updater.py index 4beb985290d1..09d4634580d6 100644 --- a/mycroft/skills/skill_updater.py +++ b/mycroft/skills/skill_updater.py @@ -26,7 +26,7 @@ from mycroft.util.combo_lock import ComboLock from mycroft.util.log import LOG from .msm_wrapper import build_msm_config, create_msm -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path ONE_HOUR = 3600 FIVE_MINUTES = 300 # number of seconds in a minute @@ -49,7 +49,7 @@ class SkillUpdater: _msm = None def __init__(self, bus=None): - self.msm_lock = ComboLock(create_temp_path('mycroft-msm.lck')) + self.msm_lock = ComboLock(create_temp_path('mycroft-msm.lck')) self.install_retries = 0 self.config = Configuration.get() update_interval = self.config['skills']['update_interval'] @@ -73,8 +73,8 @@ def _determine_next_download_time(self): otherwise use the timestamp on .msm as a basis. """ msm_files_exist = ( - os.path.exists(self.dot_msm_path) and - os.path.exists(self.installed_skills_file_path) + os.path.exists(self.dot_msm_path) and + os.path.exists(self.installed_skills_file_path) ) if msm_files_exist: mtime = os.path.getmtime(self.dot_msm_path) diff --git a/mycroft/tts/tts.py b/mycroft/tts/tts.py index 55b66ff1a7cd..7865479f63cd 100644 --- a/mycroft/tts/tts.py +++ b/mycroft/tts/tts.py @@ -31,7 +31,7 @@ from mycroft.util import ( play_wav, play_mp3, check_for_signal, create_signal, resolve_resource_file ) -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path from mycroft.util.log import LOG from mycroft.util.plugins import load_plugin from queue import Queue, Empty diff --git a/mycroft/util/audio_test.py b/mycroft/util/audio_test.py index 22ff0427be2a..3f1cdf5a9ad1 100644 --- a/mycroft/util/audio_test.py +++ b/mycroft/util/audio_test.py @@ -24,7 +24,7 @@ from mycroft.util.audio_utils import play_wav from mycroft.util.log import LOG import logging -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path """ Audio Test @@ -73,7 +73,7 @@ def record(filename, duration): def main(): parser = argparse.ArgumentParser() parser.add_argument( - '-f', '--filename', dest='filename', default = create_temp_path('test.wav'), + '-f', '--filename', dest='filename', default=create_temp_path('test.wav'), help="Filename for saved audio (Default:{}".format(create_temp_path('test.wav'))) parser.add_argument( '-d', '--duration', dest='duration', type=int, default=10, diff --git a/mycroft/util/file_utils.py b/mycroft/util/file_utils.py index ccc28940e648..7eeafc0dfed0 100644 --- a/mycroft/util/file_utils.py +++ b/mycroft/util/file_utils.py @@ -82,9 +82,11 @@ def create_temp_path(*args): path = os.path.join(tempfile.gettempdir(), *args) except TypeError: path = None - LOG.error('Could not create a temp path, create_temp_path() only accepts Strings') + LOG.error( + 'Could not create a temp path, create_temp_path() only accepts Strings') return path + def read_stripped_lines(filename): """Read a file and return a list of stripped lines. diff --git a/test/unittests/audio/test_utils.py b/test/unittests/audio/test_utils.py index 48bc526fec17..a4defbec72e2 100644 --- a/test/unittests/audio/test_utils.py +++ b/test/unittests/audio/test_utils.py @@ -23,7 +23,7 @@ import mycroft.audio from mycroft.util import create_signal, check_for_signal -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path """Tests for public audio service utils.""" diff --git a/test/unittests/lock/test_lock.py b/test/unittests/lock/test_lock.py index c3aa8021b05d..45e1901702b8 100644 --- a/test/unittests/lock/test_lock.py +++ b/test/unittests/lock/test_lock.py @@ -21,7 +21,7 @@ from os.path import exists, isfile from mycroft.lock import Lock -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path class TestLock(unittest.TestCase): diff --git a/test/unittests/util/test_audio_utils.py b/test/unittests/util/test_audio_utils.py index 96860f1d3009..d3b7b1134867 100644 --- a/test/unittests/util/test_audio_utils.py +++ b/test/unittests/util/test_audio_utils.py @@ -4,7 +4,7 @@ from test.util import Anything from mycroft.util import (play_ogg, play_mp3, play_wav, play_audio_file, record) -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path test_config = { 'play_wav_cmdline': 'mock_wav %1', From 4547752c660895731136504705fa37c24f741c43 Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Thu, 12 Nov 2020 16:41:44 +0100 Subject: [PATCH 09/17] minor errors fixed --- mycroft/client/enclosure/mark1/__init__.py | 2 +- test/unittests/util/test_download.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index 1018e7a5c8b4..2939406e5942 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -133,7 +133,7 @@ def process(self, data): time.sleep(0.5) # Prevents recording the loud button press record(create_temp_path('test.wav', 3.0) mixer.setvolume(prev_vol) - play_wav(creat_temp_path('test.wav')).communicate() + play_wav(create_temp_path('test.wav')).communicate() # Test audio muting on arduino subprocess.call('speaker-test -P 10 -l 0 -s 1', shell=True) diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index c3c0189fe967..105c35f791b1 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -4,7 +4,7 @@ from mycroft.util.download import (download, _running_downloads, _get_download_tmp) -from mycroft.util import create_temp_path +from mycroft.util.file_utils import create_temp_path TEST_URL = 'http://example.com/mycroft-test.tar.gz' TEST_DEST = create_temp_path('file.tar.gz') From 241a8b89dfa450900d957d7e2596b57fa504d7d5 Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Tue, 17 Nov 2020 16:54:37 +0100 Subject: [PATCH 10/17] pep8 error rectified --- test/unittests/util/test_download.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index 105c35f791b1..cb6bf3eeb408 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -101,8 +101,9 @@ def test_existing(self, mock_glob): self.assertEqual(_get_download_tmp(dest), dest + '.part.1') def test_multiple_existing(self, mock_glob): - mock_glob.return_value = [create_temp_path('test.part'), create_temp_path('test.part.1'), + mock_glob.return_value = [create_temp_path('test.part'), + create_temp_path('test.part.1'), create_temp_path('test.part.2')] dest = create_temp_path('test') - self.assertEqual(_get_download_tmp(dest), dest + '.part.3') + self.assertEqual(_get_download_tmp(dest), dest + '.part.3') \ No newline at end of file From 77d3969dc3392dc6938f5ab1464c61f3ab4ce2ba Mon Sep 17 00:00:00 2001 From: dzekem christa Date: Wed, 18 Nov 2020 16:57:04 +0100 Subject: [PATCH 11/17] newline added --- test/unittests/util/test_download.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index cb6bf3eeb408..7d7a0a671f6b 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -101,9 +101,9 @@ def test_existing(self, mock_glob): self.assertEqual(_get_download_tmp(dest), dest + '.part.1') def test_multiple_existing(self, mock_glob): - mock_glob.return_value = [create_temp_path('test.part'), + mock_glob.return_value = [create_temp_path('test.part'), create_temp_path('test.part.1'), create_temp_path('test.part.2')] dest = create_temp_path('test') - self.assertEqual(_get_download_tmp(dest), dest + '.part.3') \ No newline at end of file + self.assertEqual(_get_download_tmp(dest), dest + '.part.3') From d49ccc8b7a5d93aa1fd9df800c3d6024c8a70443 Mon Sep 17 00:00:00 2001 From: Kris Gesling Date: Fri, 30 Apr 2021 15:58:10 +0930 Subject: [PATCH 12/17] Fix typo and auto-format --- mycroft/client/enclosure/mark1/__init__.py | 74 +++++++++++----------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index 2939406e5942..bb2662a69fff 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -54,9 +54,9 @@ class EnclosureReader(Thread): Mycroft Core. E.g. Mycroft Stop Feature - #. Arduino sends a Stop command after a button press on a Mycroft unit - #. ``EnclosureReader`` captures the Stop command - #. Notify all Mycroft Core processes (e.g. skills) to be stopped + # . Arduino sends a Stop command after a button press on a Mycroft unit + # . ``EnclosureReader`` captures the Stop command + # . Notify all Mycroft Core processes (e.g. skills) to be stopped Note: A command is identified by a line break """ @@ -131,7 +131,7 @@ def process(self, data): 'utterance': "I am testing one two three"})) time.sleep(0.5) # Prevents recording the loud button press - record(create_temp_path('test.wav', 3.0) + record(create_temp_path('test.wav', 3.0)) mixer.setvolume(prev_vol) play_wav(create_temp_path('test.wav')).communicate() @@ -189,12 +189,12 @@ def process(self, data): 'utterance': mycroft.dialog.get("ssh disabled")})) if "unit.enable-learning" in data or "unit.disable-learning" in data: - enable='enable' in data - word='enabled' if enable else 'disabled' + enable = 'enable' in data + word = 'enabled' if enable else 'disabled' LOG.info("Setting opt_in to: " + word) - new_config={'opt_in': enable} - user_config=LocalConf(USER_CONFIG) + new_config = {'opt_in': enable} + user_config = LocalConf(USER_CONFIG) user_config.merge(new_config) user_config.store() @@ -202,38 +202,38 @@ def process(self, data): 'utterance': mycroft.dialog.get("learning " + word)})) def stop(self): - self.alive=False + self.alive = False class EnclosureWriter(Thread): """ Writes data to Serial port. - #. Enqueues all commands received from Mycroft enclosures + # . Enqueues all commands received from Mycroft enclosures implementation - #. Process them on the received order by writing on the Serial port + # . Process them on the received order by writing on the Serial port E.g. Displaying a text on Mycroft's Mouth - #. ``EnclosureMouth`` sends a text command - #. ``EnclosureWriter`` captures and enqueue the command - #. ``EnclosureWriter`` removes the next command from the queue - #. ``EnclosureWriter`` writes the command to Serial port + # . ``EnclosureMouth`` sends a text command + # . ``EnclosureWriter`` captures and enqueue the command + # . ``EnclosureWriter`` removes the next command from the queue + # . ``EnclosureWriter`` writes the command to Serial port Note: A command has to end with a line break """ def __init__(self, serial, bus, size=16): super(EnclosureWriter, self).__init__(target=self.flush) - self.alive=True - self.daemon=True - self.serial=serial - self.bus=bus - self.commands=Queue(size) + self.alive = True + self.daemon = True + self.serial = serial + self.bus = bus + self.commands = Queue(size) self.start() def flush(self): while self.alive: try: - cmd=self.commands.get() + '\n' + cmd = self.commands.get() + '\n' self.serial.write(cmd.encode()) self.commands.task_done() except Exception as e: @@ -243,7 +243,7 @@ def write(self, command): self.commands.put(str(command)) def stop(self): - self.alive=False + self.alive = False class EnclosureMark1(Enclosure): @@ -260,19 +260,19 @@ class EnclosureMark1(Enclosure): E.g. Start and Stop talk animation """ - _last_internet_notification=0 + _last_internet_notification = 0 def __init__(self): super().__init__() self.__init_serial() - self.reader=EnclosureReader(self.serial, self.bus, self.lang) - self.writer=EnclosureWriter(self.serial, self.bus) + self.reader = EnclosureReader(self.serial, self.bus, self.lang) + self.writer = EnclosureWriter(self.serial, self.bus) # Prepare to receive message when the Arduino responds to the # following "system.version" self.bus.on("enclosure.started", self.on_arduino_responded) - self.arduino_responded=False + self.arduino_responded = False # Send a message to the Arduino across the serial line asking # for a reply with version info. self.writer.write("system.version") @@ -290,12 +290,12 @@ def __init__(self): init_display_manager_bus_connection() def on_arduino_responded(self, event=None): - self.eyes=EnclosureEyes(self.bus, self.writer) - self.mouth=EnclosureMouth(self.bus, self.writer) - self.system=EnclosureArduino(self.bus, self.writer) + self.eyes = EnclosureEyes(self.bus, self.writer) + self.mouth = EnclosureMouth(self.bus, self.writer) + self.system = EnclosureArduino(self.bus, self.writer) self.__register_events() self.__reset() - self.arduino_responded=True + self.arduino_responded = True # verify internet connection and prompt user on bootup if needed if not connected(): @@ -314,7 +314,7 @@ def on_no_internet(self, event=None): # don't bother the user with multiple notifications with 30 secs return - Enclosure._last_internet_notification=time.time() + Enclosure._last_internet_notification = time.time() # TODO: This should go into EnclosureMark1 subclass of Enclosure. if has_been_paired(): @@ -330,15 +330,15 @@ def on_no_internet(self, event=None): def __init_serial(self): try: - self.port=self.config.get("port") - self.rate=self.config.get("rate") - self.timeout=self.config.get("timeout") - self.serial=serial.serial_for_url( + self.port = self.config.get("port") + self.rate = self.config.get("rate") + self.timeout = self.config.get("timeout") + self.serial = serial.serial_for_url( url=self.port, baudrate=self.rate, timeout=self.timeout) LOG.info("Connected to: %s rate: %s timeout: %s" % (self.port, self.rate, self.timeout)) except Exception: - LOG.error("Impossible to connect to serial port: "+str(self.port)) + LOG.error("Impossible to connect to serial port: " + str(self.port)) raise def __register_events(self): @@ -417,5 +417,5 @@ def _do_net_check(self): time.sleep(2) # a pause sounds better than just jumping in # Kick off wifi-setup automatically - data={'allow_timeout': False, 'lang': self.lang} + data = {'allow_timeout': False, 'lang': self.lang} self.bus.emit(Message('system.wifi.setup', data)) From d9adf73db7bae3634a3f61c761ddba2366b9cee6 Mon Sep 17 00:00:00 2001 From: Kris Gesling Date: Fri, 30 Apr 2021 16:21:13 +0930 Subject: [PATCH 13/17] Add unit test for create_temp_file --- test/unittests/util/test_file_utils.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/unittests/util/test_file_utils.py b/test/unittests/util/test_file_utils.py index fddef6e7b499..a14bd83eea68 100644 --- a/test/unittests/util/test_file_utils.py +++ b/test/unittests/util/test_file_utils.py @@ -6,8 +6,14 @@ from unittest import TestCase, mock from mycroft import MYCROFT_ROOT_PATH -from mycroft.util import (resolve_resource_file, curate_cache, create_file, - get_cache_directory, read_stripped_lines, read_dict) +from mycroft.util import ( + resolve_resource_file, + curate_cache, + create_file, + create_temp_path, + get_cache_directory, + read_stripped_lines, + read_dict) test_config = { @@ -195,5 +201,12 @@ def test_create_file_in_nonexisting_dir(self): create_file(test_path) self.assertTrue(exists(test_path)) + def test_create_temp_path(self): + temp_dir = tempfile.gettempdir() + temp_path = create_temp_path('mycroft', 'example.txt') + self.assertEqual(temp_path, f'{temp_dir}/mycroft/example.txt') + failed_temp_path = create_temp_path(1) + self.assertEqual(failed_temp_path, None) + def tearDownClass(): shutil.rmtree(TEST_CREATE_FILE_DIR, ignore_errors=True) From 0fb90edd2862fd9b2f9f1302ce1c1df1fc31c26e Mon Sep 17 00:00:00 2001 From: Kris Gesling Date: Fri, 30 Apr 2021 16:35:21 +0930 Subject: [PATCH 14/17] Rename to get_temp_path and add docstring --- mycroft/client/enclosure/mark1/__init__.py | 9 +++--- mycroft/lock/__init__.py | 4 +-- mycroft/skills/msm_wrapper.py | 4 +-- mycroft/skills/skill_updater.py | 4 +-- mycroft/tts/tts.py | 4 +-- mycroft/util/__init__.py | 12 +++++-- mycroft/util/audio_test.py | 10 ++++-- mycroft/util/file_utils.py | 37 +++++++++++++++------- test/unittests/audio/test_utils.py | 6 ++-- test/unittests/lock/test_lock.py | 18 +++++------ test/unittests/util/test_audio_utils.py | 6 ++-- test/unittests/util/test_download.py | 18 +++++------ test/unittests/util/test_file_utils.py | 12 ++++--- 13 files changed, 86 insertions(+), 58 deletions(-) diff --git a/mycroft/client/enclosure/mark1/__init__.py b/mycroft/client/enclosure/mark1/__init__.py index bb2662a69fff..2741aa0eed1c 100644 --- a/mycroft/client/enclosure/mark1/__init__.py +++ b/mycroft/client/enclosure/mark1/__init__.py @@ -35,7 +35,7 @@ from mycroft.util.audio_test import record from mycroft.util.log import LOG from queue import Queue -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path # The Mark 1 hardware consists of a Raspberry Pi main CPU which is connected # to an Arduino over the serial port. A custom serial protocol sends @@ -131,9 +131,9 @@ def process(self, data): 'utterance': "I am testing one two three"})) time.sleep(0.5) # Prevents recording the loud button press - record(create_temp_path('test.wav', 3.0)) + record(get_temp_path('test.wav', 3.0)) mixer.setvolume(prev_vol) - play_wav(create_temp_path('test.wav')).communicate() + play_wav(get_temp_path('test.wav')).communicate() # Test audio muting on arduino subprocess.call('speaker-test -P 10 -l 0 -s 1', shell=True) @@ -338,7 +338,8 @@ def __init_serial(self): LOG.info("Connected to: %s rate: %s timeout: %s" % (self.port, self.rate, self.timeout)) except Exception: - LOG.error("Impossible to connect to serial port: " + str(self.port)) + LOG.error("Impossible to connect to serial port: " + + str(self.port)) raise def __register_events(self): diff --git a/mycroft/lock/__init__.py b/mycroft/lock/__init__.py index f7ef3e4a6a48..856e4c86857d 100644 --- a/mycroft/lock/__init__.py +++ b/mycroft/lock/__init__.py @@ -22,7 +22,7 @@ # Wrapper around chain of handler functions for a specific system level signal. # Often used to trap Ctrl-C for specific application purposes. from mycroft.util import LOG -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path class Signal: # python 3+ class Signal @@ -99,7 +99,7 @@ class Lock: # python 3+ 'class Lock' # # Class constants - DIRECTORY = create_temp_path('mycroft') + DIRECTORY = get_temp_path('mycroft') FILE = '/{}.pid' # diff --git a/mycroft/skills/msm_wrapper.py b/mycroft/skills/msm_wrapper.py index dc0674589846..7878e9c8e8f9 100644 --- a/mycroft/skills/msm_wrapper.py +++ b/mycroft/skills/msm_wrapper.py @@ -27,7 +27,7 @@ from mycroft.util.combo_lock import ComboLock from mycroft.util.log import LOG -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path MsmConfig = namedtuple( 'MsmConfig', @@ -45,7 +45,7 @@ def _init_msm_lock(): msm_lock = None try: - msm_lock = ComboLock(create_temp_path('mycroft-msm.lck')) + msm_lock = ComboLock(get_temp_path('mycroft-msm.lck')) LOG.debug('mycroft-msm combo lock instantiated') except Exception: LOG.exception('Failed to create msm lock!') diff --git a/mycroft/skills/skill_updater.py b/mycroft/skills/skill_updater.py index 09d4634580d6..b856739a1cb1 100644 --- a/mycroft/skills/skill_updater.py +++ b/mycroft/skills/skill_updater.py @@ -26,7 +26,7 @@ from mycroft.util.combo_lock import ComboLock from mycroft.util.log import LOG from .msm_wrapper import build_msm_config, create_msm -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path ONE_HOUR = 3600 FIVE_MINUTES = 300 # number of seconds in a minute @@ -49,7 +49,7 @@ class SkillUpdater: _msm = None def __init__(self, bus=None): - self.msm_lock = ComboLock(create_temp_path('mycroft-msm.lck')) + self.msm_lock = ComboLock(get_temp_path('mycroft-msm.lck')) self.install_retries = 0 self.config = Configuration.get() update_interval = self.config['skills']['update_interval'] diff --git a/mycroft/tts/tts.py b/mycroft/tts/tts.py index 7865479f63cd..867131d9362e 100644 --- a/mycroft/tts/tts.py +++ b/mycroft/tts/tts.py @@ -31,7 +31,7 @@ from mycroft.util import ( play_wav, play_mp3, check_for_signal, create_signal, resolve_resource_file ) -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path from mycroft.util.log import LOG from mycroft.util.plugins import load_plugin from queue import Queue, Empty @@ -178,7 +178,7 @@ def __init__(self, lang, config, validator, audio_ext='wav', self.ssml_tags = ssml_tags or [] self.voice = config.get("voice") - self.filename = create_temp_path('tts.wav') + self.filename = get_temp_path('tts.wav') self.enclosure = None random.seed() self.queue = Queue() diff --git a/mycroft/util/__init__.py b/mycroft/util/__init__.py index 3f396dc32366..8cb5ab71951c 100644 --- a/mycroft/util/__init__.py +++ b/mycroft/util/__init__.py @@ -25,9 +25,15 @@ from .string_utils import camel_case_split from .audio_utils import (play_audio_file, play_wav, play_ogg, play_mp3, record, find_input_device) -from .file_utils import (resolve_resource_file, read_stripped_lines, read_dict, - create_file, ensure_directory_exists, - curate_cache, get_cache_directory) +from .file_utils import ( + resolve_resource_file, + read_stripped_lines, + read_dict, + create_file, + get_temp_path, + ensure_directory_exists, + curate_cache, + get_cache_directory) from .network_utils import connected from .process_utils import (reset_sigint_handler, create_daemon, wait_for_exit_signal, create_echo_function, diff --git a/mycroft/util/audio_test.py b/mycroft/util/audio_test.py index 3f1cdf5a9ad1..2a8b77782c83 100644 --- a/mycroft/util/audio_test.py +++ b/mycroft/util/audio_test.py @@ -24,7 +24,7 @@ from mycroft.util.audio_utils import play_wav from mycroft.util.log import LOG import logging -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path """ Audio Test @@ -73,8 +73,12 @@ def record(filename, duration): def main(): parser = argparse.ArgumentParser() parser.add_argument( - '-f', '--filename', dest='filename', default=create_temp_path('test.wav'), - help="Filename for saved audio (Default:{}".format(create_temp_path('test.wav'))) + '-f', + '--filename', + dest='filename', + default=get_temp_path('test.wav'), + help="Filename for saved audio (Default:{}".format( + get_temp_path('test.wav'))) parser.add_argument( '-d', '--duration', dest='duration', type=int, default=10, help="Duration of recording in seconds (Default: 10)") diff --git a/mycroft/util/file_utils.py b/mycroft/util/file_utils.py index 7eeafc0dfed0..136c7ab7ae48 100644 --- a/mycroft/util/file_utils.py +++ b/mycroft/util/file_utils.py @@ -77,16 +77,6 @@ def resolve_resource_file(res_name): return None # Resource cannot be resolved -def create_temp_path(*args): - try: - path = os.path.join(tempfile.gettempdir(), *args) - except TypeError: - path = None - LOG.error( - 'Could not create a temp path, create_temp_path() only accepts Strings') - return path - - def read_stripped_lines(filename): """Read a file and return a list of stripped lines. @@ -241,7 +231,7 @@ def get_cache_directory(domain=None): directory = config.get("cache_path") if not directory: # If not defined, use /tmp/mycroft/cache - directory = create_temp_path('mycroft', 'cache') + directory = get_temp_path('mycroft', 'cache') return ensure_directory_exists(directory, domain) @@ -285,3 +275,28 @@ def create_file(filename): ensure_directory_exists(os.path.dirname(filename), permissions=0o775) with open(filename, 'w') as f: f.write('') + + +def get_temp_path(*args): + """Generate a valid path in the system temp directory. + + This method accepts one or more strings as arguments. The arguments are + joined and returned as a complete path inside the systems temp directory. + Importantly, this will not create any directories or files. + + Example usage: get_temp_path('mycroft', 'audio', 'example.wav') + Will return the equivalent of: '/tmp/mycroft/audio/example.wav' + + Args: + path_element (str): directories and/or filename + + Returns: + (str) a valid path in the systems temp directory + """ + try: + path = os.path.join(tempfile.gettempdir(), *args) + except TypeError: + path = None + LOG.error('Could not create a temp path, ' + 'get_temp_path() only accepts Strings') + return path diff --git a/test/unittests/audio/test_utils.py b/test/unittests/audio/test_utils.py index a4defbec72e2..04e838f24cec 100644 --- a/test/unittests/audio/test_utils.py +++ b/test/unittests/audio/test_utils.py @@ -23,7 +23,7 @@ import mycroft.audio from mycroft.util import create_signal, check_for_signal -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path """Tests for public audio service utils.""" @@ -39,8 +39,8 @@ def wait_while_speaking_thread(): class TestInterface(unittest.TestCase): def setUp(self): - if exists(create_temp_path('mycroft')): - rmtree(create_temp_path('mycroft')) + if exists(get_temp_path('mycroft')): + rmtree(get_temp_path('mycroft')) def test_is_speaking(self): create_signal('isSpeaking') diff --git a/test/unittests/lock/test_lock.py b/test/unittests/lock/test_lock.py index 45e1901702b8..9e918a32570a 100644 --- a/test/unittests/lock/test_lock.py +++ b/test/unittests/lock/test_lock.py @@ -21,33 +21,33 @@ from os.path import exists, isfile from mycroft.lock import Lock -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path class TestLock(unittest.TestCase): def setUp(self): - if exists(create_temp_path('mycroft')): - rmtree(create_temp_path('mycroft')) + if exists(get_temp_path('mycroft')): + rmtree(get_temp_path('mycroft')) def test_create_lock(self): l1 = Lock('test') self.assertTrue( - isfile(create_temp_path('mycroft', 'test.pid'))) + isfile(get_temp_path('mycroft', 'test.pid'))) def test_delete_lock(self): l1 = Lock('test') self.assertTrue( - isfile(create_temp_path('mycroft', 'test.pid'))) + isfile(get_temp_path('mycroft', 'test.pid'))) l1.delete() self.assertFalse( - isfile(create_temp_path('mycroft', 'test.pid'))) + isfile(get_temp_path('mycroft', 'test.pid'))) @patch('os.kill') def test_existing_lock(self, mock_kill): """ Test that an existing lock will kill the old pid. """ l1 = Lock('test') self.assertTrue( - isfile(create_temp_path('mycroft', 'test.pid'))) + isfile(get_temp_path('mycroft', 'test.pid'))) l2 = Lock('test2') self.assertFalse(mock_kill.called) l2 = Lock('test') @@ -56,13 +56,13 @@ def test_existing_lock(self, mock_kill): def test_keyboard_interrupt(self): l1 = Lock('test') self.assertTrue( - isfile(create_temp_path('mycroft', 'test.pid'))) + isfile(get_temp_path('mycroft', 'test.pid'))) try: os.kill(os.getpid(), signal.SIGINT) except KeyboardInterrupt: pass self.assertFalse( - isfile(create_temp_path('mycroft', 'test.pid'))) + isfile(get_temp_path('mycroft', 'test.pid'))) if __name__ == '__main__': diff --git a/test/unittests/util/test_audio_utils.py b/test/unittests/util/test_audio_utils.py index d3b7b1134867..84b8e39b320f 100644 --- a/test/unittests/util/test_audio_utils.py +++ b/test/unittests/util/test_audio_utils.py @@ -4,7 +4,7 @@ from test.util import Anything from mycroft.util import (play_ogg, play_mp3, play_wav, play_audio_file, record) -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path test_config = { 'play_wav_cmdline': 'mock_wav %1', @@ -139,7 +139,7 @@ def test_record_with_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = create_temp_path('test.wav') + filename = get_temp_path('test.wav') duration = 42 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', @@ -154,7 +154,7 @@ def test_record_without_duration(self, mock_subprocess): mock_subprocess.Popen.return_value = mock_proc rate = 16000 channels = 1 - filename = create_temp_path('test.wav') + filename = get_temp_path('test.wav') duration = 0 res = record(filename, duration, rate, channels) mock_subprocess.Popen.assert_called_once_with(['arecord', diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index 7d7a0a671f6b..7b11dbb5d455 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -4,10 +4,10 @@ from mycroft.util.download import (download, _running_downloads, _get_download_tmp) -from mycroft.util.file_utils import create_temp_path +from mycroft.util.file_utils import get_temp_path TEST_URL = 'http://example.com/mycroft-test.tar.gz' -TEST_DEST = create_temp_path('file.tar.gz') +TEST_DEST = get_temp_path('file.tar.gz') @mock.patch('mycroft.util.download.subprocess') @@ -92,18 +92,18 @@ def wget_call(*args, **kwargs): class TestGetTemp(TestCase): def test_no_existing(self, mock_glob): mock_glob.return_value = [] - dest = create_temp_path('test') + dest = get_temp_path('test') self.assertEqual(_get_download_tmp(dest), dest + '.part') def test_existing(self, mock_glob): - mock_glob.return_value = [create_temp_path('test.part')] - dest = create_temp_path('test') + mock_glob.return_value = [get_temp_path('test.part')] + dest = get_temp_path('test') self.assertEqual(_get_download_tmp(dest), dest + '.part.1') def test_multiple_existing(self, mock_glob): - mock_glob.return_value = [create_temp_path('test.part'), - create_temp_path('test.part.1'), - create_temp_path('test.part.2')] + mock_glob.return_value = [get_temp_path('test.part'), + get_temp_path('test.part.1'), + get_temp_path('test.part.2')] - dest = create_temp_path('test') + dest = get_temp_path('test') self.assertEqual(_get_download_tmp(dest), dest + '.part.3') diff --git a/test/unittests/util/test_file_utils.py b/test/unittests/util/test_file_utils.py index a14bd83eea68..7244da38e481 100644 --- a/test/unittests/util/test_file_utils.py +++ b/test/unittests/util/test_file_utils.py @@ -10,7 +10,7 @@ resolve_resource_file, curate_cache, create_file, - create_temp_path, + get_temp_path, get_cache_directory, read_stripped_lines, read_dict) @@ -201,11 +201,13 @@ def test_create_file_in_nonexisting_dir(self): create_file(test_path) self.assertTrue(exists(test_path)) - def test_create_temp_path(self): + def test_get_temp_path(self): temp_dir = tempfile.gettempdir() - temp_path = create_temp_path('mycroft', 'example.txt') - self.assertEqual(temp_path, f'{temp_dir}/mycroft/example.txt') - failed_temp_path = create_temp_path(1) + temp_file = get_temp_path('example.txt') + self.assertEqual(temp_file, temp_dir + '/example.txt') + temp_path = get_temp_path('mycroft', 'audio', 'example.wav') + self.assertEqual(temp_path, temp_dir + '/mycroft/audio/example.wav') + failed_temp_path = get_temp_path(1) self.assertEqual(failed_temp_path, None) def tearDownClass(): From 784f166bc20c67c0c9bd2ea98c3a89008b2ffb23 Mon Sep 17 00:00:00 2001 From: Kris Gesling Date: Mon, 3 May 2021 20:13:21 +0930 Subject: [PATCH 15/17] Raise TypeError rather than quietly fail --- mycroft/util/file_utils.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mycroft/util/file_utils.py b/mycroft/util/file_utils.py index 136c7ab7ae48..120c2215c3b6 100644 --- a/mycroft/util/file_utils.py +++ b/mycroft/util/file_utils.py @@ -296,7 +296,6 @@ def get_temp_path(*args): try: path = os.path.join(tempfile.gettempdir(), *args) except TypeError: - path = None - LOG.error('Could not create a temp path, ' - 'get_temp_path() only accepts Strings') + raise TypeError("Could not create a temp path, get_temp_path() only " + "accepts Strings") return path From 5ee975500261fc87d1468e7b7067d4c5aeafeae6 Mon Sep 17 00:00:00 2001 From: Kris Gesling Date: Mon, 3 May 2021 20:17:03 +0930 Subject: [PATCH 16/17] remove stray file --- buggix | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 buggix diff --git a/buggix b/buggix deleted file mode 100644 index e69de29bb2d1..000000000000 From 5d3b9d479b0b1acbd4df2fe8b51951869e3c6eef Mon Sep 17 00:00:00 2001 From: Kris Gesling Date: Fri, 7 May 2021 15:34:29 +0930 Subject: [PATCH 17/17] Fix test for expected TypeError --- test/unittests/util/test_file_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unittests/util/test_file_utils.py b/test/unittests/util/test_file_utils.py index 7244da38e481..8da989ae8335 100644 --- a/test/unittests/util/test_file_utils.py +++ b/test/unittests/util/test_file_utils.py @@ -207,8 +207,8 @@ def test_get_temp_path(self): self.assertEqual(temp_file, temp_dir + '/example.txt') temp_path = get_temp_path('mycroft', 'audio', 'example.wav') self.assertEqual(temp_path, temp_dir + '/mycroft/audio/example.wav') - failed_temp_path = get_temp_path(1) - self.assertEqual(failed_temp_path, None) + with self.assertRaises(TypeError): + failed_temp_path = get_temp_path(1) def tearDownClass(): shutil.rmtree(TEST_CREATE_FILE_DIR, ignore_errors=True)