From eb4ca3264b7873cc596f7dae273b76723cb0a055 Mon Sep 17 00:00:00 2001 From: Sarahtonin Date: Thu, 2 Apr 2026 14:57:52 +0200 Subject: [PATCH 1/3] Removed and commented some Parameters in Config, and added much more to SaraSortd.py --- Config.toml | 26 +++++++----- SaraSortd.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 119 insertions(+), 17 deletions(-) diff --git a/Config.toml b/Config.toml index af8c045..ad7e0bd 100644 --- a/Config.toml +++ b/Config.toml @@ -1,5 +1,8 @@ Title = "SaraSortd Config" +SafeMode = 1 # If SaraSortd should stop if an error with the config appears. 1 is stop, 0 is continue. +SilentMode = 0 # If SaraSortd should write stuff to the terminal as well. + [Variables] # Only use these in config files, not the actual file names themselves.. NextNum = "*" # The next available number. If 1 already exists, 2 will be used. Starts at 1. NextChar = "&" # The next available character. If A already exists, B will be used. @@ -10,10 +13,6 @@ OrgFileName = "~" # The original name of the sorted file. If "blahblah.txt" gets OrgFileType = "^" # The original file type of the sorted file. If "example.png" gets sorted, "^" would be equal to ".png". Year = "[y]" # The current year, E.G. "2026". - -MonthName = "[M]" # The name of the current month, E.G. "March". -DayName = "[D]" # The name of the current day, E.G. "Friday". - Month = "[m]" # The date of the current month, E.G. "03". Day = "[d]" # The date of the current day, E.G. "27". @@ -22,12 +21,13 @@ Minute = "[n]" # The current minute, E.G. "44". Second = "[s]" # The current second, E.G. "11". VarCall = "%%" # The characters used at the start and end of variable calls. The first character is to the left; second is to the right. +# NOTE: If any parameters in the config uses just "%%", it gets a variable from the program itself, not from the config. [DirectoryPaths] -RootDir = "Unset" -InputDir = "%RootDir%" -OutputDir = "%RootDir%" -LogDir = ".%RootDir%/Logs" +RootDir = "Unset" # Parent directory. Folder where all SaraSortd folders live. +InputDir = ["%RootDir%"] # If files get put here, they'll be sorted. +OutputDir = ["%RootDir%"] # Where sortted files go. +LogDir = ".%RootDir%/Logs" # Where log files from SaraSortd go. FailedDir =".%RootDir%/Failed" # For files that failed to be sorted. [Names] @@ -36,4 +36,12 @@ LogFileName = "[y].[m].[d]_*" # Name of log files. ConfFileName = ".@Config" # Name of config files for directories. [Log] -Sorted = "Sorted ~^ to %SortDir%" \ No newline at end of file +All = "[h]:[n]:[s] | " # What to put at the start of all lines in Log file. + +Start = "Started: SaraSortd" # When SaraSortd starts. +NotStart = "Start Failed: SaraSortd." # When SaraSortd fails to start for some reason. + +Sorted = "Sorted: ~^ to %SortDir%." # When a file gets sorted. + +Unset = "Error: %% is not set." # When a parameter in the config is not set. %% is the parameter that wasn't set. +Set = "Set: %% to %%." # When a previously unset parameter in the config is set. %% is the parameter that got set. \ No newline at end of file diff --git a/SaraSortd.py b/SaraSortd.py index e6f407b..ebe0d73 100644 --- a/SaraSortd.py +++ b/SaraSortd.py @@ -1,6 +1,12 @@ -import toml, os +import toml, os, datetime Conf = None +ConfVars = None +ConfDirs = None +ConfNames = None +ConfLog = None + +Start = True def Clear(): if os.name == 'nt': @@ -10,25 +16,113 @@ def Clear(): def LoadConf(): - global Conf + global Conf, ConfVars, ConfDirs, ConfNames, ConfLog LocalConf = toml.load("Config.toml") Conf = LocalConf + ConfVars = Conf["Variables"] + ConfDirs = Conf["DirectoryPaths"] + ConfNames = Conf["Names"] + ConfLog = Conf["Log"] + + +def Error(): + global Start + if (Conf["SafeMode"] == 1): + Start = False + + +def Speak(Text): + if (Conf["SilentMode"] != 1): + print(Text) + + +def Parse(String = None, Var = None, OrgFile = None, OutputDir = None): + Now = datetime.datetime.now() + + ParseBook = { + ConfVars["Year"]: str(Now.year), + ConfVars["Month"]: str(Now.month), + ConfVars["Day"]: str(Now.day), + ConfVars["Hour"]: str(Now.hour), + ConfVars["Minute"]: str(Now.minute), + ConfVars["Second"]: str(Now.second), + ConfVars["VarCall"]: Var, + ConfVars["NextNum"]: "NOT ADDED", + ConfVars["NextChar"]: "NOT ADDED", + ConfVars["Parent"]: "NOT ADDED", + ConfVars["OrgFileName"]: "NOT ADDED", + ConfVars["OrgFileType"]: "NOT ADDED", + } + + for Key, Value in ParseBook.items(): + if Key in String and Value is not None: + String = String.replace(Key, Value) + + return String + + +def Dir(Name): + os.makedirs(Name, exist_ok=True) + + +def LogWrite(Text): + if (ConfDirs["LogDir"] != None and ConfNames != "Unset"): + if (ConfNames["LogFileName"] != None and ConfNames["LogFileName"] != "Unset"): + Dir(Parse(ConfDirs["LogDir"])) + LogFile = f"{Parse(ConfDirs["LogDir"])}/{Parse(ConfNames["LogFileName"])}.log" + with open(LogFile, "w", encoding="utf-8") as File: + File.write(f"{Parse(ConfLog["All"])}{Text}\n") + + +def ConfCheck(): + for Item in ConfVars: + if (Item == None or Item == "Unset"): + TextOutput = Parse(ConfLog["Unset"], Item) + LogWrite(TextOutput) + Speak(TextOutput) + Error() + + for Item in ConfDirs: + if (Item == None or Item == "Unset"): + TextOutput = Parse(ConfLog["Unset"], Item) + LogWrite(TextOutput) + Speak(TextOutput) + Error() + + for Item in ConfLog: + if (Item == None or Item == "Unset"): + TextOutput = Parse(ConfLog["Unset"], Item) + LogWrite(TextOutput) + Speak(TextOutput) + Error() def Init(): - global Conf + global Conf, ConfVars, ConfDirs, ConfNames, ConfLog LoadConf() if (Conf != None): - Main() + ConfCheck() + + TextOutput = Parse(ConfLog["Start"]) + LogWrite(TextOutput) + Speak(TextOutput) + + if (Start): + Main() + else: + TextOutput = Parse(ConfLog["NotStart"]) + LogWrite(TextOutput) + Speak(TextOutput) + + else: + print("NO CONFIG FOUND! Make sure the 'Config.toml' is in the same directory as 'SaraSortd.py'") def Main(): - global Conf - - Clear() - print(Conf) + while True: + pass Init() \ No newline at end of file From 8abe270d93271c2843b70d9454d0ce27d2ae6c52 Mon Sep 17 00:00:00 2001 From: Sarahtonin Date: Thu, 2 Apr 2026 15:31:00 +0200 Subject: [PATCH 2/3] Added a replacer function in Parse() to check for %RootDir% and others. --- SaraSortd.py | 62 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/SaraSortd.py b/SaraSortd.py index ebe0d73..c5a6544 100644 --- a/SaraSortd.py +++ b/SaraSortd.py @@ -1,4 +1,4 @@ -import toml, os, datetime +import toml, os, datetime, re Conf = None ConfVars = None @@ -37,28 +37,63 @@ def Speak(Text): print(Text) +def GetConf(Parameter): + if Parameter in Conf: + return str(Conf[Parameter]) + + elif Parameter in ConfVars: + return str(ConfVars[Parameter]) + + elif Parameter in ConfDirs: + return str(ConfDirs[Parameter]) + + elif Parameter in ConfNames: + return str(ConfNames[Parameter]) + + elif Parameter in ConfLog: + return str(ConfLog[Parameter]) + + return None + + def Parse(String = None, Var = None, OrgFile = None, OutputDir = None): + def Replacer(Match): + Content = Match.group(1) + + if (Content == None or Content == ""): + if (Var != None): + return str(Var) + else: + return "" + + Value = GetConf(Content) + if (Value != None): + return Value + + Now = datetime.datetime.now() - ParseBook = { + VarValues = { + ConfVars["NextNum"]: "NOT ADDED", + ConfVars["NextChar"]: "NOT ADDED", + ConfVars["Parent"]: "NOT ADDED", + ConfVars["OrgFileName"]: "NOT ADDED", + ConfVars["OrgFileType"]: "NOT ADDED", ConfVars["Year"]: str(Now.year), ConfVars["Month"]: str(Now.month), ConfVars["Day"]: str(Now.day), ConfVars["Hour"]: str(Now.hour), ConfVars["Minute"]: str(Now.minute), ConfVars["Second"]: str(Now.second), - ConfVars["VarCall"]: Var, - ConfVars["NextNum"]: "NOT ADDED", - ConfVars["NextChar"]: "NOT ADDED", - ConfVars["Parent"]: "NOT ADDED", - ConfVars["OrgFileName"]: "NOT ADDED", - ConfVars["OrgFileType"]: "NOT ADDED", + ConfVars["VarCall"]: Var } - for Key, Value in ParseBook.items(): + for Key, Value in VarValues.items(): if Key in String and Value is not None: String = String.replace(Key, Value) + String = re.sub(r"%([^%]*)%", Replacer, String) + return String @@ -104,13 +139,14 @@ def Init(): LoadConf() if (Conf != None): ConfCheck() - - TextOutput = Parse(ConfLog["Start"]) - LogWrite(TextOutput) - Speak(TextOutput) if (Start): + TextOutput = Parse(ConfLog["Start"]) + LogWrite(TextOutput) + Speak(TextOutput) + Main() + else: TextOutput = Parse(ConfLog["NotStart"]) LogWrite(TextOutput) From ef5b40ffef34a85340285d8c98b50736641def0d Mon Sep 17 00:00:00 2001 From: Sarahtonin Date: Thu, 2 Apr 2026 15:43:43 +0200 Subject: [PATCH 3/3] Fixed ConfCheck so that it doesn't raise nonexistent errors. --- Config.toml | 2 +- SaraSortd.py | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Config.toml b/Config.toml index ad7e0bd..b0914b8 100644 --- a/Config.toml +++ b/Config.toml @@ -27,7 +27,7 @@ VarCall = "%%" # The characters used at the start and end of variable calls. Th RootDir = "Unset" # Parent directory. Folder where all SaraSortd folders live. InputDir = ["%RootDir%"] # If files get put here, they'll be sorted. OutputDir = ["%RootDir%"] # Where sortted files go. -LogDir = ".%RootDir%/Logs" # Where log files from SaraSortd go. +LogDir = "%RootDir%/.Logs" # Where log files from SaraSortd go. FailedDir =".%RootDir%/Failed" # For files that failed to be sorted. [Names] diff --git a/SaraSortd.py b/SaraSortd.py index c5a6544..0921b6a 100644 --- a/SaraSortd.py +++ b/SaraSortd.py @@ -111,26 +111,33 @@ def LogWrite(Text): def ConfCheck(): - for Item in ConfVars: - if (Item == None or Item == "Unset"): - TextOutput = Parse(ConfLog["Unset"], Item) + for Parameter, Value in ConfVars.items(): + if (Value == None or Value == "Unset"): + TextOutput = Parse(ConfLog["Unset"], Parameter) LogWrite(TextOutput) Speak(TextOutput) Error() - for Item in ConfDirs: - if (Item == None or Item == "Unset"): - TextOutput = Parse(ConfLog["Unset"], Item) + for Parameter, Value in ConfDirs.items(): + if (Value == None or Value == "Unset"): + TextOutput = Parse(ConfLog["Unset"], Parameter) LogWrite(TextOutput) Speak(TextOutput) Error() + + for Parameter, Value in ConfNames.items(): + if (Value == None or Value == "Unset"): + TextOutput = Parse(ConfLog["Unset"], Parameter) + LogWrite(TextOutput) + Speak(TextOutput) + Error() - for Item in ConfLog: - if (Item == None or Item == "Unset"): - TextOutput = Parse(ConfLog["Unset"], Item) + for Parameter, Value in ConfLog.items(): + if (Value == None or Value == "Unset"): + TextOutput = Parse(ConfLog["Unset"], Parameter) LogWrite(TextOutput) Speak(TextOutput) - Error() + Error() def Init(): @@ -144,7 +151,7 @@ def Init(): TextOutput = Parse(ConfLog["Start"]) LogWrite(TextOutput) Speak(TextOutput) - + Main() else: