From e7aa0a64c7dd9a7b597d0634305efeb5b5c225a4 Mon Sep 17 00:00:00 2001 From: prash3r Date: Sat, 24 Sep 2022 15:19:34 +0200 Subject: [PATCH] repairs log_file_controller.py. I seem to have exidentally search and replaced some needed glob.glob calls when adding dinamic log location, which should now be repaired. There also was a mysterious cbpi_dashboard_1.json appearing in the wrong folder which now inside the dashboard folder. I also figured out how to run tests locally, they should now respect the dynamic folder paths. --- .vscode/launch.json | 8 ++++++++ cbpi/configFolder.py | 3 +++ cbpi/controller/dashboard_controller.py | 10 +++++----- cbpi/controller/log_file_controller.py | 8 ++++---- tests/cbpi_config_fixture.py | 3 ++- tests/test_cli.py | 2 +- tests/test_logger.py | 4 ++-- 7 files changed, 25 insertions(+), 13 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 1dd8992..e24e1e9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -28,6 +28,14 @@ "request": "launch", "module": "run", "args": ["--config-folder-path=./.devcontainer/cbpi-dev-config", "setup"] + }, + + { + "name": "run tests", + "type": "python", + "request": "launch", + "module": "pytest", + "args": ["tests"] } ] } \ No newline at end of file diff --git a/cbpi/configFolder.py b/cbpi/configFolder.py index 954f1a3..ef2169b 100644 --- a/cbpi/configFolder.py +++ b/cbpi/configFolder.py @@ -22,6 +22,9 @@ class ConfigFolder: def get_file_path(self, file): return os.path.join(self.configFolderPath, file) + + def get_dashboard_path(self, file): + return os.path.join(self.configFolderPath, "dashboard", file) def get_upload_file(self, file): return os.path.join(self.configFolderPath, 'upload', file) diff --git a/cbpi/controller/dashboard_controller.py b/cbpi/controller/dashboard_controller.py index 2f39af5..feef806 100644 --- a/cbpi/controller/dashboard_controller.py +++ b/cbpi/controller/dashboard_controller.py @@ -18,14 +18,14 @@ class DashboardController: self.logger = logging.getLogger(__name__) self.cbpi.register(self) - self.path = cbpi.config_folder.get_file_path("cbpi_dashboard_1.json") + self.path = cbpi.config_folder.get_dashboard_path("cbpi_dashboard_1.json") async def init(self): pass async def get_content(self, dashboard_id): try: - self.path = self.cbpi.config_folder.get_file_path("cbpi_dashboard_"+ str(dashboard_id) +".json") + self.path = self.cbpi.config_folder.get_dashboard_path("cbpi_dashboard_"+ str(dashboard_id) +".json") logging.info(self.path) with open(self.path) as json_file: data = json.load(json_file) @@ -35,21 +35,21 @@ class DashboardController: async def add_content(self, dashboard_id, data): print(data) - self.path = self.cbpi.config_folder.get_file_path("cbpi_dashboard_" + str(dashboard_id)+ ".json") + self.path = self.cbpi.config_folder.get_dashboard_path("cbpi_dashboard_" + str(dashboard_id)+ ".json") with open(self.path, 'w') as outfile: json.dump(data, outfile, indent=4, sort_keys=True) self.cbpi.notify(title="Dashboard {}".format(dashboard_id), message="Saved Successfully", type=NotificationType.SUCCESS) return {"status": "OK"} async def delete_content(self, dashboard_id): - self.path = self.cbpi.config_folder.get_file_path("cbpi_dashboard_"+ str(dashboard_id)+ ".json") + self.path = self.cbpi.config_folder.get_dashboard_path("cbpi_dashboard_"+ str(dashboard_id)+ ".json") if os.path.exists(self.path): os.remove(self.path) self.cbpi.notify(title="Dashboard {}".format(dashboard_id), message="Deleted Successfully", type=NotificationType.SUCCESS) async def get_custom_widgets(self): - path = os.path.join(self.cbpi.config_folder.get_file_path("dashboard"), "widgets") + path = self.cbpi.config_folder.get_dashboard_path("widgets") onlyfiles = [os.path.splitext(f)[0] for f in sorted(listdir(path)) if isfile(join(path, f)) and f.endswith(".svg")] return onlyfiles diff --git a/cbpi/controller/log_file_controller.py b/cbpi/controller/log_file_controller.py index 05e9d32..50790e2 100644 --- a/cbpi/controller/log_file_controller.py +++ b/cbpi/controller/log_file_controller.py @@ -118,7 +118,7 @@ class LogController: for name in names: # get all log names - all_filenames = os.path.join(self.logsFolderPath, f"sensor_{name}.log*") + all_filenames = glob.glob(os.path.join(self.logsFolderPath, f"sensor_{name}.log*")) # concat all logs df = pd.concat([pd.read_csv(f, parse_dates=True, date_parser=dateparse, index_col='DateTime', names=['DateTime', name], header=None) for f in all_filenames]) logging.info("Read all files for {}".format(names)) @@ -176,7 +176,7 @@ class LogController: :return: list of log file names ''' - return [os.path.basename(x) for x in os.path.join(self.logsFolderPath, f"sensor_{name}.log*")] + return [os.path.basename(x) for x in glob.glob(os.path.join(self.logsFolderPath, f"sensor_{name}.log*"))] def clear_log(self, name:str ) -> str: all_filenames = glob.glob(os.path.join(self.logsFolderPath, f"sensor_{name}.log*")) @@ -215,9 +215,9 @@ class LogController: """ formatted_time = strftime("%Y-%m-%d-%H_%M_%S", localtime()) - file_name = os.path.join(self.logsFolderPath, f"{formatted_time}-sensor-{name}.zip" % (formatted_time, name)) + file_name = os.path.join(self.logsFolderPath, f"{formatted_time}-sensor-{name}.zip") zip = zipfile.ZipFile(file_name, 'w', zipfile.ZIP_DEFLATED) - all_filenames = os.path.join(self.logsFolderPath, f"sensor_{name}.log*") + all_filenames = glob.glob(os.path.join(self.logsFolderPath, f"sensor_{name}.log*")) for f in all_filenames: zip.write(os.path.join(f)) zip.close() diff --git a/tests/cbpi_config_fixture.py b/tests/cbpi_config_fixture.py index b9824ba..2510c21 100644 --- a/tests/cbpi_config_fixture.py +++ b/tests/cbpi_config_fixture.py @@ -19,5 +19,6 @@ class CraftBeerPiTestCase(AioHTTPTestCase): def configuration(self): test_directory = os.path.dirname(__file__) test_config_directory = os.path.join(test_directory, 'cbpi-test-config') - configFolder = ConfigFolder(test_config_directory) + test_logs_directory = os.path.join(test_directory, 'logs') + configFolder = ConfigFolder(test_config_directory, test_logs_directory) return configFolder diff --git a/tests/test_cli.py b/tests/test_cli.py index 0a9f705..5de4f03 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -9,7 +9,7 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %( class CLITest(unittest.TestCase): def test_list(self): - cli = CraftBeerPiCli(ConfigFolder("./cbpi-test-config")) + cli = CraftBeerPiCli(ConfigFolder("./cbpi-test-config", './logs')) # inside tests folder cli.plugins_list() if __name__ == '__main__': diff --git a/tests/test_logger.py b/tests/test_logger.py index d626ab0..2f84642 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -10,11 +10,11 @@ class LoggerTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_log_data(self): - os.makedirs("./logs", exist_ok=True) + os.makedirs(os.path.join(".", "tests", "logs"), exist_ok=True) log_name = "test" #clear all logs self.cbpi.log.clear_log(log_name) - assert len(glob.glob('./logs/sensor_%s.log*' % log_name)) == 0 + assert len(glob.glob(os.path.join(".", "tests", "logs", f"sensor_{log_name}.log*"))) == 0 # write log entries for i in range(5):