diff --git a/kiauh/core/submodules/simple_config_parser/src/simple_config_parser/simple_config_parser.py b/kiauh/core/submodules/simple_config_parser/src/simple_config_parser/simple_config_parser.py index b3f10c6..b2246bb 100644 --- a/kiauh/core/submodules/simple_config_parser/src/simple_config_parser/simple_config_parser.py +++ b/kiauh/core/submodules/simple_config_parser/src/simple_config_parser/simple_config_parser.py @@ -85,10 +85,46 @@ class DuplicateOptionError(Exception): class SimpleConfigParser: """A customized config parser targeted at handling Klipper style config files""" - _SECTION_RE = re.compile(r"\s*\[(\w+\s?.+)]\s*([#;].*)?$") - _OPTION_RE = re.compile(r"^\s*(\w+)\s*[:=]\s*([^=:].*)\s*([#;].*)?$") - _MLOPTION_RE = re.compile(r"^\s*(\w+)\s*[:=]\s*([#;].*)?$") + # definition of section line: + # - then line MUST start with an opening square bracket - it is the first section marker + # - the section marker MUST be followed by at least one character - it is the section name + # - the section name MUST be followed by a closing square bracket - it is the second section marker + # - the second section marker MAY be followed by any amount of whitespace characters + # - the second section marker MAY be followed by a # or ; - it is the comment marker + # - the inline comment MAY be of any length and character + _SECTION_RE = re.compile(r"\[(.+)]\s*([#;].*)?$") + + # definition of option line: + # - the line MUST start with a word - it is the option name + # - the option name MUST be followed by a colon or an equal sign - it is the separator + # - the separator MUST be followed by a value + # - the separator MAY have any amount of leading or trailing whitespaces + # - the separator MUST NOT be directly followed by a colon or equal sign + # - the value MAY be of any length and character + # - the value MAY contain any amount of trailing whitespaces + # - the value MAY be followed by a # or ; - it is the comment marker + # - the inline comment MAY be of any length and character + _OPTION_RE = re.compile(r"^([^:=\s]+)\s?[:=]\s*([^=:].*)\s*([#;].*)?$") + + # definition of multiline option line: + # - the line MUST start with a word - it is the option name + # - the option name MUST be followed by a colon or an equal sign - it is the separator + # - the separator MUST NOT be followed by a value + # - the separator MAY have any amount of leading or trailing whitespaces + # - the separator MUST NOT be directly followed by a colon or equal sign + # - the separator MAY be followed by a # or ; - it is the comment marker + # - the inline comment MAY be of any length and character + _MLOPTION_RE = re.compile(r"^([^:=\s]+)\s*[:=]\s*([#;].*)?$") + + # definition of comment line: + # - the line MAY start with any amount of whitespace characters + # - the line MUST contain a # or ; - it is the comment marker + # - the comment marker MAY be followed by any amount of whitespace characters + # - the comment MAY be of any length and character _COMMENT_RE = re.compile(r"^\s*([#;].*)?$") + + # definition of empty line: + # - the line MUST contain only whitespace characters _EMPTY_LINE_RE = re.compile(r"^\s*$") BOOLEAN_STATES = { diff --git a/kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/data/case_parse_option.py b/kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/data/case_parse_option.py index fbe9001..df849be 100644 --- a/kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/data/case_parse_option.py +++ b/kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/data/case_parse_option.py @@ -21,4 +21,8 @@ testcases = [ "serial", "/dev/serial/by-id/", ), + ("parameter_temperature_(°C): 155", "parameter_temperature_(°C)", "155"), + ("parameter_humidity_(%_RH): 45", "parameter_humidity_(%_RH)", "45"), + ("parameter_spool_weight_(%): 10", "parameter_spool_weight_(%)", "10"), + ("path: /dev/shm/drying_box.json", "path", "/dev/shm/drying_box.json"), ] diff --git a/kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/test_line_parsing.py b/kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/test_single_line_parsing.py similarity index 99% rename from kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/test_line_parsing.py rename to kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/test_single_line_parsing.py index 96366e3..fd2e4a1 100644 --- a/kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/test_line_parsing.py +++ b/kiauh/core/submodules/simple_config_parser/tests/features/line_parsing/test_single_line_parsing.py @@ -14,7 +14,7 @@ def parser(): return SimpleConfigParser() -class TestLineParsing: +class TestSingleLineParsing: @pytest.mark.parametrize("given, expected", [*case_parse_section]) def test_parse_section(self, parser, given, expected): parser._parse_section(given) diff --git a/kiauh/core/submodules/simple_config_parser/tests/features/line_type_detection/data/case_line_is_multiline_option.py b/kiauh/core/submodules/simple_config_parser/tests/features/line_type_detection/data/case_line_is_multiline_option.py index ed93f87..ddf74d5 100644 --- a/kiauh/core/submodules/simple_config_parser/tests/features/line_type_detection/data/case_line_is_multiline_option.py +++ b/kiauh/core/submodules/simple_config_parser/tests/features/line_type_detection/data/case_line_is_multiline_option.py @@ -14,4 +14,14 @@ testcases = [ ("", False), ("# that's a comment", False), ("; that's a comment", False), + ("parameter_humidity_(%_RH):", True), + ("parameter_spool_weight_(%):", True), + ("parameter_temperature_(°C):", True), + ("parameter_humidity_(%_RH): 18.123", False), + ("parameter_spool_weight_(%): 150", False), + ("parameter_temperature_(°C): 30,5", False), + ("trusted_clients:", True), + ("trusted_clients: 192.168.1.0/24", False), + ("cors_domains:", True), + ("cors_domains: http://*.lan", False), ] diff --git a/kiauh/core/submodules/simple_config_parser/tests/features/line_type_detection/data/case_line_is_option.py b/kiauh/core/submodules/simple_config_parser/tests/features/line_type_detection/data/case_line_is_option.py index 280e851..b33e6a8 100644 --- a/kiauh/core/submodules/simple_config_parser/tests/features/line_type_detection/data/case_line_is_option.py +++ b/kiauh/core/submodules/simple_config_parser/tests/features/line_type_detection/data/case_line_is_option.py @@ -26,5 +26,6 @@ testcases = [ ("description: homing!", True), ("description: inline macro :-)", True), ("path: %GCODES_DIR%", True), + ("path: /dev/shm/drying_box.json", True), ("serial = /dev/serial/by-id/", True), ]