-
-
Notifications
You must be signed in to change notification settings - Fork 0
Components
Components are one-file scripts with various misc. functions. They are rarely used during model importing, although they mostly provide the program functionality that can be moved out of main to prevent cluttering of code. If need arises, a component will be added.
Update Checker [check_updates.py]
Checks for updates by returning a hardcoded within itself VERSION_CURRENT
string as well as a string pulled from VERSION file downloaded from the github repo.
The file may be replaced by github releases api in the future but for now it works like that.
The main, and the only function CheckUpdates()
takes no arguments and returns a dict with 'current'
and 'fetched'
keys. Current is the VERSION_CURRENT
value, fetched is the one pulled from the VERSION file.
Note: VERSION_CURRENT
is used as a version marker for the whole program.
There's also a secrety-secret file handle that disables update checking by making it always return "no new version" without doing anything. Normally it would be done through settings, but Kozmadeus retains no settings. It's a hack that prevents VERSION
file from getting overwritten or deleted during development.
All you need to do is create an empty file named UPDATER_DISABLE
.
Logger [logger.py]
Excerpt from the code:
Why a custom logger? Simply to prevent circular imports. The whole framework is designed according to one-way descending imports tree. In order to use "logging" module I'd have to make modules import logging functs somehow
I'm not proud of this implementation, but it's better than nothing at the moment, I need a quick implementation allowing verbose output and tracebacks logging.
This makeshift logging implementation is based on this stackoverflow answer. It works by mirroring stdout into a file and adding some misc stuff such as system info and timestamps.
It's non-standard and hacky as hell, but it works. Maybe there would be a way to implement "Logging" module but for now there's little to no benefit even considering the downsides.
Note: This section is written for the sake of documentation, there's no need to reimplement the logger within modules as it is initiated globally in main.py
from components.logger import LoggerInit
try:
logger_filename = 'kozmadeus.log'
logger = LoggerInit(logger_filename)
... # Do some stuff
logger.close()
except:
logger.log_exception()
logger.close()
Virtually that's all that needs to be done to ensure the logger works, from initiating the logger until calling logger.close()
every print()
will get logged. In order to log exception tracebacks logger.log_exception()
needs to be called under except
block. Afterwards you can do whatever you want with the exception, either re-raise it or handle it in your own way.
Note: logger.close()
has to be called no matter what in order to stop duplicating the stdout and close the log file properly!
The nasty thing about it, is that although the logger adds timestamps by itself, it cannot do so for log levels and other log info, hence it needs to be added manually within print()
calls.
The way it's done in Kozmadeus is simple:
print('[MAIN][INFO]: Finished extracting the model data.')
The first segment states the name of the logged layer (refer to the Program Hierarchy graph). The second segment is the log level (INFO/WARNING/ERROR). Important to note that "ERROR" level usually skips the logged layer segment as it handles exceptions within the logging module without knowing where it had occured.
Files Restore [restore_files.py]
Made to restore files necessary for Kozmadeus to function, namely templates. The only function RestoreFiles()
, that takes no arguments, downloads and unzips restore.zip downloaded directly from the repo's assets
folder. As simple as that, no shenanigans this time.
XML Template Writer [xml_write.py]
Based on Bootshuze extensively.
The only function here is ExportXML()
, which takes 3 arguments:
-
file_name
: String containing input file name -
template
: String containing chosen template name -
args
: Dict containing geometry data
It reads the selected template file, line by line. In each line, it looks for strings in format {{ foo }}
, takes the string from the brackets, checks if it's is present in the args
dict keys and if it is, using regex it substitutes the whole {{ foo }}
with the key's value.
It has its own way of fixing up a new file name if it turns out that the output name already exists. Moreso, even though it's implemented in main.py, should the template files ever be corrupted or missing, RestoreFiles()
will be called in attempt to restore them.