From 184ab00a17af0f7c150468258b3cc36bcd60fb39 Mon Sep 17 00:00:00 2001 From: "Sir.MoM" Date: Wed, 4 Nov 2020 23:00:24 +0100 Subject: [PATCH 1/8] Create CodeStyleWorkflow.yml --- .github/workflows/CodeStyleWorkflow.yml | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/workflows/CodeStyleWorkflow.yml diff --git a/.github/workflows/CodeStyleWorkflow.yml b/.github/workflows/CodeStyleWorkflow.yml new file mode 100644 index 0000000..f64416d --- /dev/null +++ b/.github/workflows/CodeStyleWorkflow.yml @@ -0,0 +1,58 @@ +name: Code style workflow + +# Events but only for the master branch +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + format: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install gdtoolkit + - name: Format with gdformat + run: | + gdformat logger.gd + - name: Commit changes from formatting + uses: stefanzweifel/git-auto-commit-action@v4.1.2 + with: + commit_message: Apply formatting changes + branch: ${{ github.head_ref }} + + lint: + needs: [format] + + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install gdtoolkit + - name: Format with gdformat + run: | + gdlint logger.gd From 17c66d3a4319e9da16e723c78dfb9ce946c07155 Mon Sep 17 00:00:00 2001 From: SirMoM Date: Wed, 4 Nov 2020 22:01:12 +0000 Subject: [PATCH 2/8] Apply formatting changes --- logger.gd | 268 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 190 insertions(+), 78 deletions(-) diff --git a/logger.gd b/logger.gd index aba09ed..62fcbf5 100644 --- a/logger.gd +++ b/logger.gd @@ -4,12 +4,13 @@ # # Upstream repo: https://github.com/KOBUGE-Games/godot-logger -extends Node # Needed to work as a singleton +extends Node # Needed to work as a singleton ##================## ## Inner classes ## ##================## + class Logfile: # TODO: Godot doesn't support docstrings for inner classes, GoDoIt (GH-1320) # """Class for log files that can be shared between various modules.""" @@ -37,14 +38,14 @@ class Logfile: func get_write_mode(): if not file.file_exists(path): - return File.WRITE # create + return File.WRITE # create else: - return File.READ_WRITE # append + return File.READ_WRITE # append func validate_path(path): """Validate the path given as argument, making it possible to write to the designated file or folder. Returns whether the path is valid.""" - if !(path.is_abs_path() or path.is_rel_path()): + if ! (path.is_abs_path() or path.is_rel_path()): print("[ERROR] [logger] The given path '%s' is not valid." % path) return false var dir = Directory.new() @@ -53,8 +54,12 @@ class Logfile: # TODO: Move directory creation to the function that will actually *write* var err = dir.make_dir_recursive(base_dir) if err: - print("[ERROR] [logger] Could not create the '%s' directory; exited with error %d." \ - % [base_dir, err]) + print( + ( + "[ERROR] [logger] Could not create the '%s' directory; exited with error %d." + % [base_dir, err] + ) + ) return false else: print("[INFO] [logger] Successfully created the '%s' directory." % base_dir) @@ -63,34 +68,42 @@ class Logfile: func flush_buffer(): """Flush the buffer, i.e. write its contents to the target file.""" if buffer_idx == 0: - return # Nothing to write + return # Nothing to write var err = file.open(path, get_write_mode()) if err: - print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." \ - % [path, err]) + print( + ( + "[ERROR] [logger] Could not open the '%s' log file; exited with error %d." + % [path, err] + ) + ) return file.seek_end() for i in range(buffer_idx): file.store_line(buffer[i]) file.close() - buffer_idx = 0 # We don't clear the memory, we'll just overwrite it + buffer_idx = 0 # We don't clear the memory, we'll just overwrite it func write(output, level): """Write the string at the end of the file (append mode), following the queue mode.""" var queue_action = queue_mode if queue_action == QUEUE_SMART: - if level >= WARN: # Don't queue warnings and errors + if level >= WARN: # Don't queue warnings and errors queue_action = QUEUE_NONE flush_buffer() - else: # Queue the log, not important enough for "smart" + else: # Queue the log, not important enough for "smart" queue_action = QUEUE_ALL if queue_action == QUEUE_NONE: var err = file.open(path, get_write_mode()) if err: - print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." \ - % [path, err]) + print( + ( + "[ERROR] [logger] Could not open the '%s' log file; exited with error %d." + % [path, err] + ) + ) return file.seek_end() file.store_line(output) @@ -103,10 +116,7 @@ class Logfile: flush_buffer() func get_config(): - return { - "path": get_path(), - "queue_mode": get_queue_mode() - } + return {"path": get_path(), "queue_mode": get_queue_mode()} class Module: @@ -120,11 +130,11 @@ class Module: name = _name set_output_level(_output_level) - if typeof(_output_strategies) == TYPE_INT: # Only one strategy, use it for all + if typeof(_output_strategies) == TYPE_INT: # Only one strategy, use it for all for i in range(0, LEVELS.size()): output_strategies.append(_output_strategies) else: - for strategy in _output_strategies: # Need to force deep copy + for strategy in _output_strategies: # Need to force deep copy output_strategies.append(strategy) set_logfile(_logfile) @@ -138,8 +148,12 @@ class Module: on their respective strategies, while levels lower than the given one will be discarded.""" if not level in range(0, LEVELS.size()): - print("[ERROR] [%s] The level must be comprised between 0 and %d." \ - % [PLUGIN_NAME, LEVELS.size() - 1]) + print( + ( + "[ERROR] [%s] The level must be comprised between 0 and %d." + % [PLUGIN_NAME, LEVELS.size() - 1] + ) + ) return output_level = level @@ -149,8 +163,12 @@ class Module: func set_common_output_strategy(output_strategy_mask): """Set the common output strategy mask for all levels of the module.""" if not output_strategy_mask in range(0, MAX_STRATEGY + 1): - print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." \ - % [PLUGIN_NAME, MAX_STRATEGY]) + print( + ( + "[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." + % [PLUGIN_NAME, MAX_STRATEGY] + ) + ) return for i in range(0, LEVELS.size()): output_strategies[i] = output_strategy_mask @@ -159,16 +177,24 @@ class Module: """Set the output strategy for the given level or (by default) all levels of the module.""" if not output_strategy_mask in range(0, MAX_STRATEGY + 1): - print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." \ - % [PLUGIN_NAME, MAX_STRATEGY]) + print( + ( + "[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." + % [PLUGIN_NAME, MAX_STRATEGY] + ) + ) return - if level == -1: # Set for all levels + if level == -1: # Set for all levels for i in range(0, LEVELS.size()): output_strategies[i] = output_strategy_mask else: if not level in range(0, LEVELS.size()): - print("[ERROR] [%s] The level must be comprised between 0 and %d." \ - % [PLUGIN_NAME, LEVELS.size() - 1]) + print( + ( + "[ERROR] [%s] The level must be comprised between 0 and %d." + % [PLUGIN_NAME, LEVELS.size() - 1] + ) + ) return output_strategies[level] = output_strategy_mask @@ -214,14 +240,10 @@ const STRATEGY_PRINT = 1 const STRATEGY_FILE = 2 const STRATEGY_PRINT_AND_FILE = STRATEGY_PRINT | STRATEGY_FILE const STRATEGY_MEMORY = 4 -const MAX_STRATEGY = STRATEGY_MEMORY*2 - 1 +const MAX_STRATEGY = STRATEGY_MEMORY * 2 - 1 # Output format identifiers -const FORMAT_IDS = { - "level": "{LVL}", - "module": "{MOD}", - "message": "{MSG}" -} +const FORMAT_IDS = {"level": "{LVL}", "module": "{MOD}", "message": "{MSG}"} # Queue modes const QUEUE_NONE = 0 @@ -230,7 +252,6 @@ const QUEUE_SMART = 2 const FILE_BUFFER_SIZE = 30 - ##=============## ## Variables ## ##=============## @@ -238,7 +259,9 @@ const FILE_BUFFER_SIZE = 30 # Configuration var default_output_level = INFO # TODO: Find (or implement in Godot) a more clever way to achieve that -var default_output_strategies = [STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT] +var default_output_strategies = [ + STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT +] var default_logfile_path = "user://%s.log" % ProjectSettings.get_setting("application/name") var default_configfile_path = "user://%s.cfg" % PLUGIN_NAME @@ -261,17 +284,17 @@ var invalid_memory_cache = false var logfiles = {} var modules = {} - ##=============## ## Functions ## ##=============## + func put(level, message, module = default_module_name): """Log a message in the given module with the given logging level.""" var module_ref = get_module(module) var output_strategy = module_ref.get_output_strategy(level) if output_strategy == STRATEGY_MUTE or module_ref.get_output_level() > level: - return # Out of scope + return # Out of scope var output = format(output_format, level, module, message) @@ -289,97 +312,128 @@ func put(level, message, module = default_module_name): memory_idx = 0 memory_first_loop = false + # Helper functions for each level # ------------------------------- + func verbose(message, module = default_module_name): """Log a message in the given module with level VERBOSE.""" put(VERBOSE, message, module) + func debug(message, module = default_module_name): """Log a message in the given module with level DEBUG.""" put(DEBUG, message, module) + func info(message, module = default_module_name): """Log a message in the given module with level INFO.""" put(INFO, message, module) + func warn(message, module = default_module_name): """Log a message in the given module with level WARN.""" put(WARN, message, module) + func error(message, module = default_module_name): """Log a message in the given module with level ERROR.""" put(ERROR, message, module) + # Module management # ----------------- -func add_module(name, output_level = default_output_level, \ - output_strategies = default_output_strategies, logfile = null): + +func add_module( + name, + output_level = default_output_level, + output_strategies = default_output_strategies, + logfile = null +): """Add a new module with the given parameter or (by default) the default ones. Returns a reference to the instanced module.""" if modules.has(name): - info("The module '%s' already exists; discarding the call to add it anew." \ - % name, PLUGIN_NAME) + info( + "The module '%s' already exists; discarding the call to add it anew." % name, + PLUGIN_NAME + ) else: if logfile == null: logfile = get_logfile(default_logfile_path) modules[name] = Module.new(name, output_level, output_strategies, logfile) return modules[name] + func get_module(module = default_module_name): """Retrieve the given module if it exists; if not, it will be created.""" if not modules.has(module): - info("The requested module '%s' does not exist. It will be created with default values." \ - % module, PLUGIN_NAME) + info( + ( + "The requested module '%s' does not exist. It will be created with default values." + % module + ), + PLUGIN_NAME + ) add_module(module) return modules[module] + func get_modules(): """Retrieve the dictionary containing all modules.""" return modules + # Logfiles management # ------------------- + func set_default_logfile_path(new_logfile_path, keep_old = false): """Sets the new default logfile path. Unless configured otherwise with the optional keep_old argument, it will replace the logfile for all modules which were configured for the previous logfile path.""" if new_logfile_path == default_logfile_path: - return # Nothing to do + return # Nothing to do var old_logfile = get_logfile(default_logfile_path) var new_logfile = null - if logfiles.has(new_logfile_path): # Already exists + if logfiles.has(new_logfile_path): # Already exists new_logfile = logfiles[new_logfile_path] - else: # Create a new logfile + else: # Create a new logfile new_logfile = add_logfile(new_logfile_path) logfiles[new_logfile_path] = new_logfile - if not keep_old: # Replace the old defaut logfile in all modules that used it + if not keep_old: # Replace the old defaut logfile in all modules that used it for module in modules.values(): if module.get_logfile() == old_logfile: module.set_logfile(new_logfile) logfiles.erase(default_logfile_path) default_logfile_path = new_logfile_path + func get_default_logfile_path(): """Return the default logfile path.""" return default_logfile_path + func add_logfile(logfile_path = default_logfile_path): """Add a new logfile that can then be attached to one or more modules. Returns a reference to the instanced logfile.""" if logfiles.has(logfile_path): - info("A logfile pointing to '%s' already exists; discarding the call to add it anew." \ - % logfile_path, PLUGIN_NAME) + info( + ( + "A logfile pointing to '%s' already exists; discarding the call to add it anew." + % logfile_path + ), + PLUGIN_NAME + ) else: logfiles[logfile_path] = Logfile.new(logfile_path) return logfiles[logfile_path] + func get_logfile(logfile_path): """Retrieve the given logfile if it exists, otherwise returns null.""" if not logfiles.has(logfile_path): @@ -388,38 +442,57 @@ func get_logfile(logfile_path): else: return logfiles[logfile_path] + func get_logfiles(): """Retrieve the dictionary containing all logfiles.""" return logfiles + # Default output configuration # ---------------------------- + func set_default_output_strategy(output_strategy_mask, level = -1): """Set the default output strategy mask of the given level or (by default) all levels for all modules without a custom strategy.""" if not output_strategy_mask in range(0, MAX_STRATEGY + 1): - error("The output strategy mask must be comprised between 0 and %d." \ - % MAX_STRATEGY, PLUGIN_NAME) + error( + "The output strategy mask must be comprised between 0 and %d." % MAX_STRATEGY, + PLUGIN_NAME + ) return - if level == -1: # Set for all levels + if level == -1: # Set for all levels for i in range(0, LEVELS.size()): default_output_strategies[i] = output_strategy_mask - info("The default output strategy mask was set to '%d' for all levels." \ - % [output_strategy_mask], PLUGIN_NAME) + info( + ( + "The default output strategy mask was set to '%d' for all levels." + % [output_strategy_mask] + ), + PLUGIN_NAME + ) else: if not level in range(0, LEVELS.size()): - error("The level must be comprised between 0 and %d." % (LEVELS.size() - 1), PLUGIN_NAME) + error( + "The level must be comprised between 0 and %d." % (LEVELS.size() - 1), PLUGIN_NAME + ) return default_output_strategies[level] = output_strategy_mask - info("The default output strategy mask was set to '%d' for the '%s' level." \ - % [output_strategy_mask, LEVELS[level]], PLUGIN_NAME) + info( + ( + "The default output strategy mask was set to '%d' for the '%s' level." + % [output_strategy_mask, LEVELS[level]] + ), + PLUGIN_NAME + ) + func get_default_output_strategy(level): """Get the default output strategy mask of the given level or (by default) all levels for all modules without a custom strategy.""" return default_output_strategies[level] + func set_default_output_level(level): """Set the default minimal level for the output of all modules without a custom output level. @@ -433,11 +506,13 @@ func set_default_output_level(level): default_output_level = level info("The default output level was set to '%s'." % LEVELS[level], PLUGIN_NAME) + func get_default_output_level(): """Get the default minimal level for the output of all modules without a custom output level.""" return default_output_level + # Output formatting # ----------------- @@ -448,6 +523,7 @@ static func format(template, level, module, message): output = output.replace(FORMAT_IDS.message, message) return output + func set_output_format(new_format): """Set the output string format using the following identifiers: {LVL} for the level, {MOD} for the module, {MSG} for the message. @@ -455,25 +531,35 @@ func set_output_format(new_format): """ for key in FORMAT_IDS: if new_format.find(FORMAT_IDS[key]) == -1: - error("Invalid output string format. It lacks the '%s' identifier." \ - % FORMAT_IDS[key], PLUGIN_NAME) + error( + "Invalid output string format. It lacks the '%s' identifier." % FORMAT_IDS[key], + PLUGIN_NAME + ) return output_format = new_format info("Successfully changed the output format to '%s'." % output_format, PLUGIN_NAME) + func get_output_format(): """Get the output string format.""" return output_format + # Strategy "memory" # ----------------- + func set_max_memory_size(new_size): """Set the maximum amount of messages to be remembered when using the STRATEGY_MEMORY output strategy.""" if new_size <= 0: - error("The maximum amount of remembered messages must be a positive non-null integer. Received %d." \ - % new_size, PLUGIN_NAME) + error( + ( + "The maximum amount of remembered messages must be a positive non-null integer. Received %d." + % new_size + ), + PLUGIN_NAME + ) return var new_buffer = [] @@ -504,19 +590,24 @@ func set_max_memory_size(new_size): memory_idx = new_idx invalid_memory_cache = true max_memory_size = new_size - info("Successfully set the maximum amount of remembered messages to %d." % max_memory_size, PLUGIN_NAME) + info( + "Successfully set the maximum amount of remembered messages to %d." % max_memory_size, + PLUGIN_NAME + ) + func get_max_memory_size(): """Get the maximum amount of messages to be remembered when using the STRATEGY_MEMORY output strategy.""" return max_memory_size + func get_memory(): """Get an array of the messages remembered following STRATEGY_MEMORY. The messages are sorted from the oldest to the newest.""" - if invalid_memory_cache: # Need to recreate the cached ordered array + if invalid_memory_cache: # Need to recreate the cached ordered array memory_cache = [] - if not memory_first_loop: # else those would be uninitialized + if not memory_first_loop: # else those would be uninitialized for i in range(memory_idx, max_memory_size): memory_cache.append(memory_buffer[i]) for i in range(0, memory_idx): @@ -524,6 +615,7 @@ func get_memory(): invalid_memory_cache = false return memory_cache + func clear_memory(): """Clear the buffer or remembered messages.""" memory_buffer.clear() @@ -535,6 +627,7 @@ func clear_memory(): # Configuration loading/saving # ---------------------------- + func save_config(configfile = default_configfile_path): """Save the default configuration as well as the set of modules and their respective configurations. @@ -552,7 +645,7 @@ func save_config(configfile = default_configfile_path): # Logfiles config var logfiles_arr = [] var sorted_keys = logfiles.keys() - sorted_keys.sort() # Sadly doesn't return the array, so we need to split it + sorted_keys.sort() # Sadly doesn't return the array, so we need to split it for logfile in sorted_keys: logfiles_arr.append(logfiles[logfile].get_config()) config.set_value(PLUGIN_NAME, "logfiles", logfiles_arr) @@ -568,12 +661,15 @@ func save_config(configfile = default_configfile_path): # Save and return the corresponding error code var err = config.save(configfile) if err: - error("Could not save the config in '%s'; exited with error %d." \ - % [configfile, err], PLUGIN_NAME) + error( + "Could not save the config in '%s'; exited with error %d." % [configfile, err], + PLUGIN_NAME + ) return err info("Successfully saved the config to '%s'." % configfile, PLUGIN_NAME) return OK + func load_config(configfile = default_configfile_path): """Load the configuration as well as the set of defined modules and their respective configurations. The expect file contents must be those @@ -582,21 +678,31 @@ func load_config(configfile = default_configfile_path): # Look for the file var dir = Directory.new() if not dir.file_exists(configfile): - warn("Could not load the config in '%s', the file does not exist." % configfile, PLUGIN_NAME) + warn( + "Could not load the config in '%s', the file does not exist." % configfile, PLUGIN_NAME + ) return ERR_FILE_NOT_FOUND # Load its contents var config = ConfigFile.new() var err = config.load(configfile) if err: - warn("Could not load the config in '%s'; exited with error %d." \ - % [configfile, err], PLUGIN_NAME) + warn( + "Could not load the config in '%s'; exited with error %d." % [configfile, err], + PLUGIN_NAME + ) return err # Load default config - default_output_level = config.get_value(PLUGIN_NAME, "default_output_level", default_output_level) - default_output_strategies = config.get_value(PLUGIN_NAME, "default_output_strategies", default_output_strategies) - default_logfile_path = config.get_value(PLUGIN_NAME, "default_logfile_path", default_logfile_path) + default_output_level = config.get_value( + PLUGIN_NAME, "default_output_level", default_output_level + ) + default_output_strategies = config.get_value( + PLUGIN_NAME, "default_output_strategies", default_output_strategies + ) + default_logfile_path = config.get_value( + PLUGIN_NAME, "default_logfile_path", default_logfile_path + ) max_memory_size = config.get_value(PLUGIN_NAME, "max_memory_size", max_memory_size) # Load logfiles config and initialize them @@ -608,8 +714,12 @@ func load_config(configfile = default_configfile_path): # Load modules config and initialize them modules = {} for module_cfg in config.get_value(PLUGIN_NAME, "modules"): - var module = Module.new(module_cfg["name"], module_cfg["output_level"], \ - module_cfg["output_strategies"], get_logfile(module_cfg["logfile_path"])) + var module = Module.new( + module_cfg["name"], + module_cfg["output_level"], + module_cfg["output_strategies"], + get_logfile(module_cfg["logfile_path"]) + ) modules[module_cfg["name"]] = module info("Successfully loaded the config from '%s'." % configfile, PLUGIN_NAME) @@ -620,14 +730,16 @@ func load_config(configfile = default_configfile_path): ## Callbacks ## ##=============## + func _init(): # Default logfile add_logfile(default_logfile_path) # Default modules - add_module(PLUGIN_NAME) # needs to be instanced first + add_module(PLUGIN_NAME) # needs to be instanced first add_module(default_module_name) memory_buffer.resize(max_memory_size) + func _exit_tree(): # Flush non-empty buffers var processed_logfiles = [] From aa2ff0485eb0b90ad020aaecfb9c342aa796bafe Mon Sep 17 00:00:00 2001 From: SirMoM Date: Thu, 5 Nov 2020 11:18:13 +0100 Subject: [PATCH 3/8] Extend allowed line lenght to 500 to prevent wierd formatting --- .github/workflows/CodeStyleWorkflow.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/CodeStyleWorkflow.yml b/.github/workflows/CodeStyleWorkflow.yml index f64416d..32bdc2f 100644 --- a/.github/workflows/CodeStyleWorkflow.yml +++ b/.github/workflows/CodeStyleWorkflow.yml @@ -27,7 +27,7 @@ jobs: pip install gdtoolkit - name: Format with gdformat run: | - gdformat logger.gd + gdformat --line-length=500 logger.gd - name: Commit changes from formatting uses: stefanzweifel/git-auto-commit-action@v4.1.2 with: @@ -53,6 +53,6 @@ jobs: run: | python -m pip install --upgrade pip pip install gdtoolkit - - name: Format with gdformat + - name: Lint the code with gdlint run: | gdlint logger.gd From df8d4c93d6b7662dd33b2b7812517be48c4757d1 Mon Sep 17 00:00:00 2001 From: SirMoM Date: Thu, 5 Nov 2020 10:26:39 +0000 Subject: [PATCH 4/8] Apply formatting changes --- logger.gd | 166 ++++++++++-------------------------------------------- 1 file changed, 29 insertions(+), 137 deletions(-) diff --git a/logger.gd b/logger.gd index d49735d..3795708 100644 --- a/logger.gd +++ b/logger.gd @@ -54,12 +54,7 @@ class Logfile: # TODO: Move directory creation to the function that will actually *write* var err = dir.make_dir_recursive(base_dir) if err: - print( - ( - "[ERROR] [logger] Could not create the '%s' directory; exited with error %d." - % [base_dir, err] - ) - ) + print("[ERROR] [logger] Could not create the '%s' directory; exited with error %d." % [base_dir, err]) return false else: print("[INFO] [logger] Successfully created the '%s' directory." % base_dir) @@ -71,12 +66,7 @@ class Logfile: return # Nothing to write var err = file.open(path, get_write_mode()) if err: - print( - ( - "[ERROR] [logger] Could not open the '%s' log file; exited with error %d." - % [path, err] - ) - ) + print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." % [path, err]) return file.seek_end() for i in range(buffer_idx): @@ -98,12 +88,7 @@ class Logfile: if queue_action == QUEUE_NONE: var err = file.open(path, get_write_mode()) if err: - print( - ( - "[ERROR] [logger] Could not open the '%s' log file; exited with error %d." - % [path, err] - ) - ) + print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." % [path, err]) return file.seek_end() file.store_line(output) @@ -148,12 +133,7 @@ class Module: on their respective strategies, while levels lower than the given one will be discarded.""" if not level in range(0, LEVELS.size()): - print( - ( - "[ERROR] [%s] The level must be comprised between 0 and %d." - % [PLUGIN_NAME, LEVELS.size() - 1] - ) - ) + print("[ERROR] [%s] The level must be comprised between 0 and %d." % [PLUGIN_NAME, LEVELS.size() - 1]) return output_level = level @@ -163,12 +143,7 @@ class Module: func set_common_output_strategy(output_strategy_mask): """Set the common output strategy mask for all levels of the module.""" if not output_strategy_mask in range(0, MAX_STRATEGY + 1): - print( - ( - "[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." - % [PLUGIN_NAME, MAX_STRATEGY] - ) - ) + print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." % [PLUGIN_NAME, MAX_STRATEGY]) return for i in range(0, LEVELS.size()): output_strategies[i] = output_strategy_mask @@ -177,24 +152,14 @@ class Module: """Set the output strategy for the given level or (by default) all levels of the module.""" if not output_strategy_mask in range(0, MAX_STRATEGY + 1): - print( - ( - "[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." - % [PLUGIN_NAME, MAX_STRATEGY] - ) - ) + print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." % [PLUGIN_NAME, MAX_STRATEGY]) return if level == -1: # Set for all levels for i in range(0, LEVELS.size()): output_strategies[i] = output_strategy_mask else: if not level in range(0, LEVELS.size()): - print( - ( - "[ERROR] [%s] The level must be comprised between 0 and %d." - % [PLUGIN_NAME, LEVELS.size() - 1] - ) - ) + print("[ERROR] [%s] The level must be comprised between 0 and %d." % [PLUGIN_NAME, LEVELS.size() - 1]) return output_strategies[level] = output_strategy_mask @@ -212,12 +177,7 @@ class Module: return logfile func get_config(): - return { - "name": get_name(), - "output_level": get_output_level(), - "output_strategies": get_output_strategy(), - "logfile_path": get_logfile().get_path() - } + return {"name": get_name(), "output_level": get_output_level(), "output_strategies": get_output_strategy(), "logfile_path": get_logfile().get_path()} ##=============## @@ -266,9 +226,7 @@ const FILE_BUFFER_SIZE = 30 var default_output_level = INFO # TODO: Find (or implement in Godot) a more clever way to achieve that -var default_output_strategies = [ - STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT -] +var default_output_strategies = [STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT, STRATEGY_PRINT] var default_logfile_path = "user://%s.log" % ProjectSettings.get_setting("application/name") var default_configfile_path = "user://%s.cfg" % PLUGIN_NAME @@ -357,20 +315,12 @@ func error(message, module = default_module_name): # ----------------- -func add_module( - name, - output_level = default_output_level, - output_strategies = default_output_strategies, - logfile = null -): +func add_module(name, output_level = default_output_level, output_strategies = default_output_strategies, logfile = null): """Add a new module with the given parameter or (by default) the default ones. Returns a reference to the instanced module.""" if modules.has(name): - info( - "The module '%s' already exists; discarding the call to add it anew." % name, - PLUGIN_NAME - ) + info("The module '%s' already exists; discarding the call to add it anew." % name, PLUGIN_NAME) else: if logfile == null: logfile = get_logfile(default_logfile_path) @@ -381,13 +331,7 @@ func add_module( func get_module(module = default_module_name): """Retrieve the given module if it exists; if not, it will be created.""" if not modules.has(module): - info( - ( - "The requested module '%s' does not exist. It will be created with default values." - % module - ), - PLUGIN_NAME - ) + info("The requested module '%s' does not exist. It will be created with default values." % module, PLUGIN_NAME) add_module(module) return modules[module] @@ -433,13 +377,7 @@ func add_logfile(logfile_path = default_logfile_path): """Add a new logfile that can then be attached to one or more modules. Returns a reference to the instanced logfile.""" if logfiles.has(logfile_path): - info( - ( - "A logfile pointing to '%s' already exists; discarding the call to add it anew." - % logfile_path - ), - PLUGIN_NAME - ) + info("A logfile pointing to '%s' already exists; discarding the call to add it anew." % logfile_path, PLUGIN_NAME) else: logfiles[logfile_path] = Logfile.new(logfile_path) return logfiles[logfile_path] @@ -467,35 +405,18 @@ func set_default_output_strategy(output_strategy_mask, level = -1): """Set the default output strategy mask of the given level or (by default) all levels for all modules without a custom strategy.""" if not output_strategy_mask in range(0, MAX_STRATEGY + 1): - error( - "The output strategy mask must be comprised between 0 and %d." % MAX_STRATEGY, - PLUGIN_NAME - ) + error("The output strategy mask must be comprised between 0 and %d." % MAX_STRATEGY, PLUGIN_NAME) return if level == -1: # Set for all levels for i in range(0, LEVELS.size()): default_output_strategies[i] = output_strategy_mask - info( - ( - "The default output strategy mask was set to '%d' for all levels." - % [output_strategy_mask] - ), - PLUGIN_NAME - ) + info("The default output strategy mask was set to '%d' for all levels." % [output_strategy_mask], PLUGIN_NAME) else: if not level in range(0, LEVELS.size()): - error( - "The level must be comprised between 0 and %d." % (LEVELS.size() - 1), PLUGIN_NAME - ) + error("The level must be comprised between 0 and %d." % (LEVELS.size() - 1), PLUGIN_NAME) return default_output_strategies[level] = output_strategy_mask - info( - ( - "The default output strategy mask was set to '%d' for the '%s' level." - % [output_strategy_mask, LEVELS[level]] - ), - PLUGIN_NAME - ) + info("The default output strategy mask was set to '%d' for the '%s' level." % [output_strategy_mask, LEVELS[level]], PLUGIN_NAME) func get_default_output_strategy(level): @@ -527,6 +448,7 @@ func get_default_output_level(): # Output formatting # ----------------- + # Format the fields: # * YYYY = Year # * MM = Month @@ -545,6 +467,7 @@ func get_formatted_datetime(): result = result.replacen("ss", "%02d" % [datetime.second]) return result + func format(template, level, module, message): var output = template output = output.replace(FORMAT_IDS.level, LEVELS[level]) @@ -561,10 +484,7 @@ func set_output_format(new_format): """ for key in FORMAT_IDS: if new_format.find(FORMAT_IDS[key]) == -1: - error( - "Invalid output string format. It lacks the '%s' identifier." % FORMAT_IDS[key], - PLUGIN_NAME - ) + error("Invalid output string format. It lacks the '%s' identifier." % FORMAT_IDS[key], PLUGIN_NAME) return output_format = new_format info("Successfully changed the output format to '%s'." % output_format, PLUGIN_NAME) @@ -583,13 +503,7 @@ func set_max_memory_size(new_size): """Set the maximum amount of messages to be remembered when using the STRATEGY_MEMORY output strategy.""" if new_size <= 0: - error( - ( - "The maximum amount of remembered messages must be a positive non-null integer. Received %d." - % new_size - ), - PLUGIN_NAME - ) + error("The maximum amount of remembered messages must be a positive non-null integer. Received %d." % new_size, PLUGIN_NAME) return var new_buffer = [] @@ -620,10 +534,7 @@ func set_max_memory_size(new_size): memory_idx = new_idx invalid_memory_cache = true max_memory_size = new_size - info( - "Successfully set the maximum amount of remembered messages to %d." % max_memory_size, - PLUGIN_NAME - ) + info("Successfully set the maximum amount of remembered messages to %d." % max_memory_size, PLUGIN_NAME) func get_max_memory_size(): @@ -691,10 +602,7 @@ func save_config(configfile = default_configfile_path): # Save and return the corresponding error code var err = config.save(configfile) if err: - error( - "Could not save the config in '%s'; exited with error %d." % [configfile, err], - PLUGIN_NAME - ) + error("Could not save the config in '%s'; exited with error %d." % [configfile, err], PLUGIN_NAME) return err info("Successfully saved the config to '%s'." % configfile, PLUGIN_NAME) return OK @@ -708,31 +616,20 @@ func load_config(configfile = default_configfile_path): # Look for the file var dir = Directory.new() if not dir.file_exists(configfile): - warn( - "Could not load the config in '%s', the file does not exist." % configfile, PLUGIN_NAME - ) + warn("Could not load the config in '%s', the file does not exist." % configfile, PLUGIN_NAME) return ERR_FILE_NOT_FOUND # Load its contents var config = ConfigFile.new() var err = config.load(configfile) if err: - warn( - "Could not load the config in '%s'; exited with error %d." % [configfile, err], - PLUGIN_NAME - ) + warn("Could not load the config in '%s'; exited with error %d." % [configfile, err], PLUGIN_NAME) return err # Load default config - default_output_level = config.get_value( - PLUGIN_NAME, "default_output_level", default_output_level - ) - default_output_strategies = config.get_value( - PLUGIN_NAME, "default_output_strategies", default_output_strategies - ) - default_logfile_path = config.get_value( - PLUGIN_NAME, "default_logfile_path", default_logfile_path - ) + default_output_level = config.get_value(PLUGIN_NAME, "default_output_level", default_output_level) + default_output_strategies = config.get_value(PLUGIN_NAME, "default_output_strategies", default_output_strategies) + default_logfile_path = config.get_value(PLUGIN_NAME, "default_logfile_path", default_logfile_path) max_memory_size = config.get_value(PLUGIN_NAME, "max_memory_size", max_memory_size) # Load logfiles config and initialize them @@ -744,12 +641,7 @@ func load_config(configfile = default_configfile_path): # Load modules config and initialize them modules = {} for module_cfg in config.get_value(PLUGIN_NAME, "modules"): - var module = Module.new( - module_cfg["name"], - module_cfg["output_level"], - module_cfg["output_strategies"], - get_logfile(module_cfg["logfile_path"]) - ) + var module = Module.new(module_cfg["name"], module_cfg["output_level"], module_cfg["output_strategies"], get_logfile(module_cfg["logfile_path"])) modules[module_cfg["name"]] = module info("Successfully loaded the config from '%s'." % configfile, PLUGIN_NAME) From 3ce5deaa894de306af979bef2d63e656682c16c7 Mon Sep 17 00:00:00 2001 From: SirMoM Date: Fri, 13 Nov 2020 10:45:19 +0100 Subject: [PATCH 5/8] Change settings for the linter and add settings file --- .github/workflows/.gdlintrc | 40 +++++++++++++++++++++++++ .github/workflows/CodeStyleWorkflow.yml | 5 ++-- 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/.gdlintrc diff --git a/.github/workflows/.gdlintrc b/.github/workflows/.gdlintrc new file mode 100644 index 0000000..64fa66b --- /dev/null +++ b/.github/workflows/.gdlintrc @@ -0,0 +1,40 @@ +class-definitions-order: +- tools +- classnames +- extends +- signals +- enums +- consts +- exports +- pubvars +- prvvars +- onreadypubvars +- onreadyprvvars +- others +class-load-variable-name: (([A-Z][a-z0-9]*)+|_?[a-z][a-z0-9]*(_[a-z0-9]+)*) +class-name: ([A-Z][a-z0-9]*)+ +class-variable-name: _?[a-z][a-z0-9]*(_[a-z0-9]+)* +comparison-with-itself: null +constant-name: '[A-Z][A-Z0-9]*(_[A-Z0-9]+)*' +duplicated-load: null +enum-element-name: '[A-Z][A-Z0-9]*(_[A-Z0-9]+)*' +enum-name: ([A-Z][a-z0-9]*)+ +expression-not-assigned: null +function-argument-name: _?[a-z][a-z0-9]*(_[a-z0-9]+)* +function-arguments-number: 10 +function-load-variable-name: ([A-Z][a-z0-9]*)+ +function-name: (_on_([A-Z][a-z0-9]*)+(_[a-z0-9]+)*|_?[a-z][a-z0-9]*(_[a-z0-9]+)*) +function-variable-name: '[a-z][a-z0-9]*(_[a-z0-9]+)*' +load-constant-name: (([A-Z][a-z0-9]*)+|[A-Z][A-Z0-9]*(_[A-Z0-9]+)*) +loop-variable-name: _?[a-z][a-z0-9]*(_[a-z0-9]+)* +max-file-lines: 1000 +max-line-length: 150 +max-public-methods: 50 +mixed-tabs-and-spaces: null +private-method-call: null +signal-name: '[a-z][a-z0-9]*(_[a-z0-9]+)*' +sub-class-name: _?([A-Z][a-z0-9]*)+ +trailing-whitespace: null +unnecessary-pass: null +unused-argument: null +disable: [class-definitions-order, max-file-lines, max-line-length, max-public-methods] diff --git a/.github/workflows/CodeStyleWorkflow.yml b/.github/workflows/CodeStyleWorkflow.yml index 32bdc2f..532eb7b 100644 --- a/.github/workflows/CodeStyleWorkflow.yml +++ b/.github/workflows/CodeStyleWorkflow.yml @@ -27,7 +27,7 @@ jobs: pip install gdtoolkit - name: Format with gdformat run: | - gdformat --line-length=500 logger.gd + gdformat --line-length=150 logger.gd - name: Commit changes from formatting uses: stefanzweifel/git-auto-commit-action@v4.1.2 with: @@ -55,4 +55,5 @@ jobs: pip install gdtoolkit - name: Lint the code with gdlint run: | - gdlint logger.gd + cp './.github/workflows/.gdlintrc' '.' + gdlint logger.gd \ No newline at end of file From 773700b5254042118746cbe3b78ad1e95469b087 Mon Sep 17 00:00:00 2001 From: SirMoM Date: Fri, 13 Nov 2020 09:49:12 +0000 Subject: [PATCH 6/8] Apply formatting changes --- logger.gd | 104 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/logger.gd b/logger.gd index df695c8..5dfbd08 100644 --- a/logger.gd +++ b/logger.gd @@ -4,12 +4,13 @@ # # Upstream repo: https://github.com/KOBUGE-Games/godot-logger -extends Node # Needed to work as a singleton +extends Node # Needed to work as a singleton ##================## ## Inner classes ## ##================## + class Logfile: # TODO: Godot doesn't support docstrings for inner classes, GoDoIt (GH-1320) # """Class for log files that can be shared between various modules.""" @@ -37,14 +38,14 @@ class Logfile: func get_write_mode(): if not file.file_exists(path): - return File.WRITE # create + return File.WRITE # create else: - return File.READ_WRITE # append + return File.READ_WRITE # append func validate_path(path): """Validate the path given as argument, making it possible to write to the designated file or folder. Returns whether the path is valid.""" - if !(path.is_abs_path() or path.is_rel_path()): + if ! (path.is_abs_path() or path.is_rel_path()): print("[ERROR] [logger] The given path '%s' is not valid." % path) return false var dir = Directory.new() @@ -53,8 +54,7 @@ class Logfile: # TODO: Move directory creation to the function that will actually *write* var err = dir.make_dir_recursive(base_dir) if err: - print("[ERROR] [logger] Could not create the '%s' directory; exited with error %d." \ - % [base_dir, err]) + print("[ERROR] [logger] Could not create the '%s' directory; exited with error %d." % [base_dir, err]) return false else: print("[INFO] [logger] Successfully created the '%s' directory." % base_dir) @@ -63,34 +63,32 @@ class Logfile: func flush_buffer(): """Flush the buffer, i.e. write its contents to the target file.""" if buffer_idx == 0: - return # Nothing to write + return # Nothing to write var err = file.open(path, get_write_mode()) if err: - print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." \ - % [path, err]) + print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." % [path, err]) return file.seek_end() for i in range(buffer_idx): file.store_line(buffer[i]) file.close() - buffer_idx = 0 # We don't clear the memory, we'll just overwrite it + buffer_idx = 0 # We don't clear the memory, we'll just overwrite it func write(output, level): """Write the string at the end of the file (append mode), following the queue mode.""" var queue_action = queue_mode if queue_action == QUEUE_SMART: - if level >= WARN: # Don't queue warnings and errors + if level >= WARN: # Don't queue warnings and errors queue_action = QUEUE_NONE flush_buffer() - else: # Queue the log, not important enough for "smart" + else: # Queue the log, not important enough for "smart" queue_action = QUEUE_ALL if queue_action == QUEUE_NONE: var err = file.open(path, get_write_mode()) if err: - print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." \ - % [path, err]) + print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." % [path, err]) return file.seek_end() file.store_line(output) @@ -103,7 +101,10 @@ class Logfile: flush_buffer() func get_config(): - return {"path": get_path(), "queue_mode": get_queue_mode(),} + return { + "path": get_path(), + "queue_mode": get_queue_mode(), + } class Module: @@ -117,11 +118,11 @@ class Module: name = _name set_output_level(_output_level) - if typeof(_output_strategies) == TYPE_INT: # Only one strategy, use it for all + if typeof(_output_strategies) == TYPE_INT: # Only one strategy, use it for all for i in range(0, LEVELS.size()): output_strategies.append(_output_strategies) else: - for strategy in _output_strategies: # Need to force deep copy + for strategy in _output_strategies: # Need to force deep copy output_strategies.append(strategy) set_logfile(_logfile) @@ -135,8 +136,7 @@ class Module: on their respective strategies, while levels lower than the given one will be discarded.""" if not level in range(0, LEVELS.size()): - print("[ERROR] [%s] The level must be comprised between 0 and %d." \ - % [PLUGIN_NAME, LEVELS.size() - 1]) + print("[ERROR] [%s] The level must be comprised between 0 and %d." % [PLUGIN_NAME, LEVELS.size() - 1]) return output_level = level @@ -146,8 +146,7 @@ class Module: func set_common_output_strategy(output_strategy_mask): """Set the common output strategy mask for all levels of the module.""" if not output_strategy_mask in range(0, MAX_STRATEGY + 1): - print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." \ - % [PLUGIN_NAME, MAX_STRATEGY]) + print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." % [PLUGIN_NAME, MAX_STRATEGY]) return for i in range(0, LEVELS.size()): output_strategies[i] = output_strategy_mask @@ -156,16 +155,14 @@ class Module: """Set the output strategy for the given level or (by default) all levels of the module.""" if not output_strategy_mask in range(0, MAX_STRATEGY + 1): - print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." \ - % [PLUGIN_NAME, MAX_STRATEGY]) + print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." % [PLUGIN_NAME, MAX_STRATEGY]) return - if level == -1: # Set for all levels + if level == -1: # Set for all levels for i in range(0, LEVELS.size()): output_strategies[i] = output_strategy_mask else: if not level in range(0, LEVELS.size()): - print("[ERROR] [%s] The level must be comprised between 0 and %d." \ - % [PLUGIN_NAME, LEVELS.size() - 1]) + print("[ERROR] [%s] The level must be comprised between 0 and %d." % [PLUGIN_NAME, LEVELS.size() - 1]) return output_strategies[level] = output_strategy_mask @@ -183,12 +180,12 @@ class Module: return logfile func get_config(): - return { + return { "name": get_name(), "output_level": get_output_level(), "output_strategies": get_output_strategy(), "logfile_path": get_logfile().get_path(), - } + } ##=============## @@ -322,6 +319,7 @@ var modules = {} ## Functions ## ##=============## + func put(level, message, module = default_module_name, error_code = -1): """Log a message in the given module with the given logging level.""" var module_ref = get_module(module) @@ -345,29 +343,36 @@ func put(level, message, module = default_module_name, error_code = -1): memory_idx = 0 memory_first_loop = false + # Helper functions for each level # ------------------------------- + func verbose(message, module = default_module_name, error_code = -1): """Log a message in the given module with level VERBOSE.""" put(VERBOSE, message, module, error_code) + func debug(message, module = default_module_name, error_code = -1): """Log a message in the given module with level DEBUG.""" put(DEBUG, message, module, error_code) + func info(message, module = default_module_name, error_code = -1): """Log a message in the given module with level INFO.""" put(INFO, message, module, error_code) + func warn(message, module = default_module_name, error_code = -1): """Log a message in the given module with level WARN.""" put(WARN, message, module, error_code) + func error(message, module = default_module_name, error_code = -1): """Log a message in the given module with level ERROR.""" put(ERROR, message, module, error_code) + # Module management # ----------------- @@ -407,17 +412,17 @@ func set_default_logfile_path(new_logfile_path, keep_old = false): the optional keep_old argument, it will replace the logfile for all modules which were configured for the previous logfile path.""" if new_logfile_path == default_logfile_path: - return # Nothing to do + return # Nothing to do var old_logfile = get_logfile(default_logfile_path) var new_logfile = null - if logfiles.has(new_logfile_path): # Already exists + if logfiles.has(new_logfile_path): # Already exists new_logfile = logfiles[new_logfile_path] - else: # Create a new logfile + else: # Create a new logfile new_logfile = add_logfile(new_logfile_path) logfiles[new_logfile_path] = new_logfile - if not keep_old: # Replace the old defaut logfile in all modules that used it + if not keep_old: # Replace the old defaut logfile in all modules that used it for module in modules.values(): if module.get_logfile() == old_logfile: module.set_logfile(new_logfile) @@ -462,21 +467,19 @@ func set_default_output_strategy(output_strategy_mask, level = -1): """Set the default output strategy mask of the given level or (by default) all levels for all modules without a custom strategy.""" if not output_strategy_mask in range(0, MAX_STRATEGY + 1): - error("The output strategy mask must be comprised between 0 and %d." \ - % MAX_STRATEGY, PLUGIN_NAME) + error("The output strategy mask must be comprised between 0 and %d." % MAX_STRATEGY, PLUGIN_NAME) return - if level == -1: # Set for all levels + if level == -1: # Set for all levels for i in range(0, LEVELS.size()): default_output_strategies[i] = output_strategy_mask - info("The default output strategy mask was set to '%d' for all levels." \ - % [output_strategy_mask], PLUGIN_NAME) + info("The default output strategy mask was set to '%d' for all levels." % [output_strategy_mask], PLUGIN_NAME) else: if not level in range(0, LEVELS.size()): error("The level must be comprised between 0 and %d." % (LEVELS.size() - 1), PLUGIN_NAME) return default_output_strategies[level] = output_strategy_mask - info("The default output strategy mask was set to '%d' for the '%s' level." \ - % [output_strategy_mask, LEVELS[level]], PLUGIN_NAME) + info("The default output strategy mask was set to '%d' for the '%s' level." % [output_strategy_mask, LEVELS[level]], PLUGIN_NAME) + func get_default_output_strategy(level): """Get the default output strategy mask of the given level or (by @@ -526,6 +529,7 @@ func get_formatted_datetime(): result = result.replacen("ss", "%02d" % [datetime.second]) return result + func format(template, level, module, message, error_code = -1): var output = template output = output.replace(FORMAT_IDS.level, LEVELS[level]) @@ -550,8 +554,7 @@ func set_output_format(new_format): """ for key in FORMAT_IDS: if new_format.find(FORMAT_IDS[key]) == -1: - error("Invalid output string format. It lacks the '%s' identifier." \ - % FORMAT_IDS[key], PLUGIN_NAME) + error("Invalid output string format. It lacks the '%s' identifier." % FORMAT_IDS[key], PLUGIN_NAME) return output_format = new_format info("Successfully changed the output format to '%s'." % output_format, PLUGIN_NAME) @@ -570,8 +573,7 @@ func set_max_memory_size(new_size): """Set the maximum amount of messages to be remembered when using the STRATEGY_MEMORY output strategy.""" if new_size <= 0: - error("The maximum amount of remembered messages must be a positive non-null integer. Received %d." \ - % new_size, PLUGIN_NAME) + error("The maximum amount of remembered messages must be a positive non-null integer. Received %d." % new_size, PLUGIN_NAME) return var new_buffer = [] @@ -614,9 +616,9 @@ func get_max_memory_size(): func get_memory(): """Get an array of the messages remembered following STRATEGY_MEMORY. The messages are sorted from the oldest to the newest.""" - if invalid_memory_cache: # Need to recreate the cached ordered array + if invalid_memory_cache: # Need to recreate the cached ordered array memory_cache = [] - if not memory_first_loop: # else those would be uninitialized + if not memory_first_loop: # else those would be uninitialized for i in range(memory_idx, max_memory_size): memory_cache.append(memory_buffer[i]) for i in range(0, memory_idx): @@ -654,7 +656,7 @@ func save_config(configfile = default_configfile_path): # Logfiles config var logfiles_arr = [] var sorted_keys = logfiles.keys() - sorted_keys.sort() # Sadly doesn't return the array, so we need to split it + sorted_keys.sort() # Sadly doesn't return the array, so we need to split it for logfile in sorted_keys: logfiles_arr.append(logfiles[logfile].get_config()) config.set_value(PLUGIN_NAME, "logfiles", logfiles_arr) @@ -670,8 +672,7 @@ func save_config(configfile = default_configfile_path): # Save and return the corresponding error code var err = config.save(configfile) if err: - error("Could not save the config in '%s'; exited with error %d." \ - % [configfile, err], PLUGIN_NAME) + error("Could not save the config in '%s'; exited with error %d." % [configfile, err], PLUGIN_NAME) return err info("Successfully saved the config to '%s'." % configfile, PLUGIN_NAME) return OK @@ -710,8 +711,9 @@ func load_config(configfile = default_configfile_path): # Load modules config and initialize them modules = {} for module_cfg in config.get_value(PLUGIN_NAME, "modules"): - var module = Module.new(module_cfg["name"], module_cfg["output_level"], \ - module_cfg["output_strategies"], get_logfile(module_cfg["logfile_path"])) + var module = Module.new( + module_cfg["name"], module_cfg["output_level"], module_cfg["output_strategies"], get_logfile(module_cfg["logfile_path"]) + ) modules[module_cfg["name"]] = module info("Successfully loaded the config from '%s'." % configfile, PLUGIN_NAME) @@ -727,7 +729,7 @@ func _init(): # Default logfile add_logfile(default_logfile_path) # Default modules - add_module(PLUGIN_NAME) # needs to be instanced first + add_module(PLUGIN_NAME) # needs to be instanced first add_module(default_module_name) memory_buffer.resize(max_memory_size) From 17d2344ec222073383eeeb168129d13a38194666 Mon Sep 17 00:00:00 2001 From: "Sir.MoM" Date: Fri, 13 Nov 2020 10:54:19 +0100 Subject: [PATCH 7/8] Update CodeStyleWorkflow.yml --- .github/workflows/CodeStyleWorkflow.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/CodeStyleWorkflow.yml b/.github/workflows/CodeStyleWorkflow.yml index 532eb7b..34a4c87 100644 --- a/.github/workflows/CodeStyleWorkflow.yml +++ b/.github/workflows/CodeStyleWorkflow.yml @@ -56,4 +56,5 @@ jobs: - name: Lint the code with gdlint run: | cp './.github/workflows/.gdlintrc' '.' - gdlint logger.gd \ No newline at end of file + gdlint logger.gd + echo "Done!" From 88bf95e1a12a53ebdad7e7872ff0756dc406dc90 Mon Sep 17 00:00:00 2001 From: SirMoM Date: Fri, 13 Nov 2020 11:22:14 +0100 Subject: [PATCH 8/8] More pythonic not --- logger.gd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logger.gd b/logger.gd index 5dfbd08..cb21311 100644 --- a/logger.gd +++ b/logger.gd @@ -45,7 +45,7 @@ class Logfile: func validate_path(path): """Validate the path given as argument, making it possible to write to the designated file or folder. Returns whether the path is valid.""" - if ! (path.is_abs_path() or path.is_rel_path()): + if not (path.is_abs_path() or path.is_rel_path()): print("[ERROR] [logger] The given path '%s' is not valid." % path) return false var dir = Directory.new()