From 2d1af2a513f17ba05429e6be4153f34aa934d56c Mon Sep 17 00:00:00 2001 From: Oleh Onyshchenko <110289150+seesmof@users.noreply.github.com> Date: Fri, 22 Dec 2023 17:16:11 +0200 Subject: [PATCH] fix: files not opening due to relative path issues --- .gitignore | 4 +--- Run.bat | 4 +--- data/latest.md | 0 src/components/PopularWords.py | 6 +++++- src/ui/ui.py | 31 +++++++++++++++++-------------- src/util/utils.py | 7 +++++-- 6 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 data/latest.md diff --git a/.gitignore b/.gitignore index 444c29a..b176143 100644 --- a/.gitignore +++ b/.gitignore @@ -149,6 +149,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ - -data/ \ No newline at end of file +#.idea/ \ No newline at end of file diff --git a/Run.bat b/Run.bat index 361dc41..7d009aa 100644 --- a/Run.bat +++ b/Run.bat @@ -1,5 +1,3 @@ -@echo off - if not exist "./data/requirements.txt" ( echo Installing requirements... pip install -r requirements.txt @@ -7,4 +5,4 @@ if not exist "./data/requirements.txt" ( ) python ./src/main.py -exit \ No newline at end of file +pause \ No newline at end of file diff --git a/data/latest.md b/data/latest.md new file mode 100644 index 0000000..e69de29 diff --git a/src/components/PopularWords.py b/src/components/PopularWords.py index b32b675..e4a5eb0 100644 --- a/src/components/PopularWords.py +++ b/src/components/PopularWords.py @@ -1,3 +1,4 @@ +from os import path from customtkinter import * from util.utils import getPopularWords @@ -30,7 +31,10 @@ def updateWords(self): widget.destroy() words = [] - with open("data/latest.md", encoding="utf-8") as f: + currentDir = path.dirname(path.abspath(__file__)) + dataFile = path.join(currentDir, "..", "..", "data", "latest.md") + + with open(dataFile, "r", encoding="utf-8") as f: for line in f: words.extend(line.split()) diff --git a/src/ui/ui.py b/src/ui/ui.py index 038f2a6..36f5c01 100644 --- a/src/ui/ui.py +++ b/src/ui/ui.py @@ -1,3 +1,4 @@ +from os import path from customtkinter import * from rich.console import Console @@ -137,7 +138,9 @@ def showPopularWords(text): def saveTextToFile(text): try: - filePath = f"data/{generateFileName(text)}.md" + fileName = generateFileName(text) + currentDir = path.dirname(path.abspath(__file__)) + filePath = path.join(currentDir, "..", "..", "data", fileName + ".md") except Exception as e: console.log( "Failed to save text to file, most likely due to input field being empty" @@ -202,16 +205,16 @@ def renderMainTab(root) -> CTkTextbox: ), ) - try: - with open("data/latest.md", encoding="utf-8") as f: - loadedText = f.read() - getTextInput.insert("0.0", loadedText) - updateMetrics( - loadedText, - resultsLines, - resultsSymbols, - resultsWords, - resultsReadingTime, - ) - except Exception as e: - console.log("Failed to load file") + currentDir = path.dirname(path.abspath(__file__)) + dataFile = path.join(currentDir, "..", "..", "data", "latest.md") + + with open(dataFile, "r", encoding="utf-8") as f: + loadedText = f.read() + getTextInput.insert("0.0", loadedText) + updateMetrics( + loadedText, + resultsLines, + resultsSymbols, + resultsWords, + resultsReadingTime, + ) diff --git a/src/util/utils.py b/src/util/utils.py index e8972dc..b2a53db 100644 --- a/src/util/utils.py +++ b/src/util/utils.py @@ -1,4 +1,5 @@ from datetime import date +from os import path from pathlib import Path from string import punctuation from rich.console import Console @@ -75,5 +76,7 @@ def generateFileName(text: str) -> str: def saveCurrentText(text: str) -> None: text = [line.strip() for line in text.split("\n") if line.strip()] - filePath = Path("data/latest.md") - filePath.write_text("\n".join(text), encoding="utf-8") + currentDir = path.dirname(path.abspath(__file__)) + dataFile = path.join(currentDir, "..", "..", "data", "latest.md") + with open(dataFile, "w", encoding="utf-8") as f: + f.write("\n".join(text))