Skip to content

# Compare

The tool to check the availability or syntax of domain, IP or URL.

::

    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides the configuration comparison interface.

Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks: https://pyfunceble.github.io/#/special-thanks

Contributors: https://pyfunceble.github.io/#/contributors

Project link: https://github.com/funilrys/PyFunceble

Project documentation: https://docs.pyfunceble.com

Project homepage: https://pyfunceble.github.io/

License: ::

    Copyright 2017, 2018, 2019, 2020, 2022, 2023, 2024, 2025 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

## `ConfigComparison`

Provides an interface for comparing 2 configuration.

Source code in `PyFunceble/config/compare.py`

     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399

    class ConfigComparison:
        """
        Provides an interface for comparing 2 configuration.
        """

        DELETED_LINKS: List[str] = [
            "config",
            "dir_structure",
            "iana",
            "ipv4_reputation",
            "mariadb",
            "mysql",
            "psl",
            "repo",
            "requirements",
            "user_agents",
            "api_date_format",
            "api_no_referrer",
        ]
        DELETED_CORE: List[str] = [
            "dns_lookup_over_tcp",
            "generate_json",
            "header_printed",
            "iana_whois_server",
            "idna_conversion",
            "logs",
            "maximal_processes",
            "multiprocess_merging_mode",
            "multiprocess",
            "no_http_codes",
            "outputs",
            "shadow_file",
            "status",
            "store_whois_record",
            "unified",
        ]

        OLD_TO_NEW: dict = {
            "adblock": "cli_decoding.adblock",
            "aggressive": "cli_decoding.adblock_aggressive",
            "auto_continue": "cli_testing.autocontinue",
            "command": "cli_testing.ci.command",
            "command_before_end": "cli_testing.ci.end_command",
            "cooldown_time": "cli_testing.cooldown_time",
            "custom_ip": "cli_testing.hosts_ip",
            "days_between_inactive_db_clean": "cli_testing.days_between.db_clean",
            "days_between_db_retest": "cli_testing.days_between.db_retest",
            "db_type": "cli_testing.db_type",
            "debug": "debug.active",
            "dns_server": "dns.server",
            "filter": "cli_testing.file_filter",
            "generate_complements": "cli_testing.complements",
            "generate_hosts": "cli_testing.file_generation.hosts",
            "hierarchical_sorting": "cli_testing.sorting_mode.hierarchical",
            "inactive_database": "cli_testing.inactive_db",
            "less": "cli_testing.display_mode.less",
            "local": "cli_testing.local_network",
            "mining": "cli_testing.mining",
            "no_files": "cli_testing.file_generation.no_file",
            "plain_list_domain": "cli_testing.file_generation.plain",
            "print_dots": "cli_testing.display_mode.dots",
            "quiet": "cli_testing.display_mode.dots",
            "use_reputation_data": "lookup.reputation",
            "reputation": "lookup.reputation",
            "rpz": "cli_decoding.rpz",
            "show_execution_time": "cli_testing.display_mode.execution_time",
            "show_percentage": "cli_testing.display_mode.percentage",
            "simple": "cli_testing.display_mode.simple",
            "syntax": "cli_testing.testing_mode.syntax",
            "timeout": "lookup.timeout",
            "ci": "cli_testing.ci.active",
            "ci_autosave_commit": "cli_testing.ci.commit_message",
            "ci_autosave_final_commit": "cli_testing.ci.end_commit_message",
            "ci_autosave_minutes": "cli_testing.ci.max_exec_minutes",
            "ci_branch": "cli_testing.ci.branch",
            "ci_distribution_branch": "cli_testing.ci.distribution_branch",
            "whois_database": "cli_testing.whois_db",
            "wildcard": "cli_decoding.wildcard",
            "cli_decoding.adblock_aggressive": "cli_decoding.aggressive",
            "lookup.collection": "lookup.platform",
            "collection.push": "platform.push",
            "collection.preferred_status_origin": "platform.preferred_status_origin",
        }

        OLD_TO_NEW_NEGATE: dict = {
            "no_special": "lookup.special",
            "no_whois": "lookup.whois",
            "split": "cli_testing.file_generation.unified_results",
        }

        DELETE_FLATTEN: List[str] = [
            "platform.url_base",
        ]

        NEW_STATUS_CODES: dict = {
            "up": [102, 207, 208, 226, 429],
            "potentially_down": [451],
            "potentially_up": [
                308,
                403,
                418,
                421,
                422,
                423,
                424,
                426,
                428,
                431,
                506,
                507,
                508,
                510,
                511,
            ],
        }

        _local_config: dict = {}
        _upstream_config: dict = {}

        dict_helper: DictHelper = DictHelper()

        def __init__(
            self,
            *,
            local_config: Optional[dict] = None,
            upstream_config: Optional[dict] = None,
        ) -> None:
            if local_config:
                self.local_config = local_config

            if upstream_config:
                self.upstream_config = upstream_config

        @property
        def local_config(self) -> dict:
            """
            Provides the current state of the :code:`_local_config`.
            """

            return self._local_config

        @local_config.setter
        def local_config(self, value: dict) -> None:
            """
            Sets the local configuration to work with.

            :raise TypeError:
                When :code:`value` is not a :py:class:`dict`.
            """

            if not isinstance(value, dict):
                raise TypeError(f"<value> should be {dict}, {type(value)} given.")

            self._local_config = copy.deepcopy(value)

        def set_local_config(self, value: dict) -> "ConfigComparison":
            """
            Sets the local configuration to work with.
            """

            self.local_config = value

            return self

        @property
        def upstream_config(self) -> dict:
            """
            Provides the current state of the :code:`_upstream_config`.
            """

            return self._upstream_config

        @upstream_config.setter
        def upstream_config(self, value: dict) -> None:
            """
            Sets the upstream configuration to work with.

            :raise TypeError:
                When :code:`value` is not a :py:class:`dict`
            """

            if not isinstance(value, dict):
                raise TypeError(f"<value> should be {dict}, {type(value)} given.")

            self._upstream_config = copy.deepcopy(value)

        def set_upstream_config(self, value: dict) -> "ConfigComparison":
            """
            Sets the upstream configuration to work with.
            """

            self.upstream_config = value

            return self

        def is_local_identical(self) -> bool:
            """
            Checks if the local configuration is identical to the upstream one.
            """

            # pylint: disable=too-many-boolean-expressions,too-many-return-statements
            if (
                not self.dict_helper.set_subject(self.local_config).has_same_keys_as(
                    self.upstream_config
                )
                or "user_agent" not in self.local_config
                or not isinstance(self.local_config["user_agent"], dict)
                or "http_codes" not in self.local_config
                or "active" in self.local_config["http_codes"]
                or "not_found_default" in self.local_config["http_codes"]
                or "self_managed" not in self.local_config["http_codes"]
                or "dns" not in self.local_config
                or "proxy" not in self.local_config
                or "follow_server_order" not in self.local_config["dns"]
                or "trust_server" not in self.local_config["dns"]
                or "platform" not in self.local_config
                or "platform" not in self.local_config["lookup"]
            ):
                return False

            for index in self.local_config:
                if index in self.DELETED_CORE:
                    return False

            for index in self.local_config.get("links", {}):
                if index in self.DELETED_LINKS:
                    return False

            if "self_managed" in self.local_config["http_codes"] and not bool(
                self.local_config["http_codes"]["self_managed"]
            ):
                if "http_codes" not in self.upstream_config:
                    return False

                for index, values in self.local_config["http_codes"]["list"].items():
                    if set(self.upstream_config["http_codes"]["list"][index]) != set(
                        values
                    ):
                        return False

            if (
                "platform" in self.local_config
                and "url_base" in self.local_config["platform"]
            ):
                return False

            return True

        def get_merged(self) -> dict:
            """
            Provides the merged configuration.
            """

            # pylint: disable=too-many-branches

            if self.is_local_identical():
                return self.local_config

            if not self.local_config:
                return self.upstream_config

            original_local = copy.deepcopy(self.local_config)
            original_upstream = copy.deepcopy(self.upstream_config)

            flatten_original = self.dict_helper.set_subject(original_local).flatten()
            flatten_upstream = self.dict_helper.set_subject(original_upstream).flatten()

            for key, value in self.OLD_TO_NEW.items():
                if key not in flatten_original:
                    continue

                if value not in flatten_upstream:  # pragma: no cover ## Safety.
                    raise RuntimeError(f"<value> ({value!r}) not found.")

                if "." not in key:
                    flatten_original[value] = original_local[key]
                else:
                    flatten_original[value] = flatten_original[key]

                del flatten_original[key]

            for key, value in self.OLD_TO_NEW_NEGATE.items():
                if key not in flatten_original:
                    continue

                if value not in flatten_upstream:  # pragma: no cover ## Safety.0
                    raise RuntimeError(f"<value> ({value!r}) not found.")

                if "." not in key:
                    flatten_original[value] = not original_local[key]
                else:
                    flatten_original[value] = not flatten_original[key]

                del flatten_original[key]

            for key in self.DELETE_FLATTEN:
                if key in flatten_original:
                    del flatten_original[key]

            original_local = self.dict_helper.set_subject(flatten_original).unflatten()
            del flatten_original

            merged = Merge(original_local).into(original_upstream)

            if "dns_lookup_over_tcp" in merged and merged["dns_lookup_over_tcp"]:
                merged["dns"]["protocol"] = "TCP"

            for index in self.DELETED_CORE:
                if index in merged:
                    del merged[index]

            for index in self.DELETED_LINKS:
                if index in merged.get("links", {}):
                    del merged["links"][index]

            if "http_codes" in merged and not bool(merged["http_codes"]["self_managed"]):
                for index, values in PyFunceble.storage.STD_HTTP_CODES.list.items():
                    merged["http_codes"]["list"][index] = list(values)

            if merged["cli_testing"]["db_type"] == "json":
                merged["cli_testing"]["db_type"] = "csv"

            if merged["cli_testing"]["cooldown_time"] is None:
                merged["cli_testing"]["cooldown_time"] = self.upstream_config[
                    "cli_testing"
                ]["cooldown_time"]

            if "user_agent" not in self.local_config or not isinstance(
                self.local_config["user_agent"], dict
            ):
                merged["user_agent"] = self.upstream_config["user_agent"]

            if "active" in merged["http_codes"]:
                del merged["http_codes"]["active"]

            if "not_found_default" in merged["http_codes"]:
                del merged["http_codes"]["not_found_default"]

            return merged

### `local_config` `property` `writable`

Provides the current state of the :code:`_local_config`.

### `upstream_config` `property` `writable`

Provides the current state of the :code:`_upstream_config`.

### `get_merged()`

Provides the merged configuration.

Source code in `PyFunceble/config/compare.py`

    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399

    def get_merged(self) -> dict:
        """
        Provides the merged configuration.
        """

        # pylint: disable=too-many-branches

        if self.is_local_identical():
            return self.local_config

        if not self.local_config:
            return self.upstream_config

        original_local = copy.deepcopy(self.local_config)
        original_upstream = copy.deepcopy(self.upstream_config)

        flatten_original = self.dict_helper.set_subject(original_local).flatten()
        flatten_upstream = self.dict_helper.set_subject(original_upstream).flatten()

        for key, value in self.OLD_TO_NEW.items():
            if key not in flatten_original:
                continue

            if value not in flatten_upstream:  # pragma: no cover ## Safety.
                raise RuntimeError(f"<value> ({value!r}) not found.")

            if "." not in key:
                flatten_original[value] = original_local[key]
            else:
                flatten_original[value] = flatten_original[key]

            del flatten_original[key]

        for key, value in self.OLD_TO_NEW_NEGATE.items():
            if key not in flatten_original:
                continue

            if value not in flatten_upstream:  # pragma: no cover ## Safety.0
                raise RuntimeError(f"<value> ({value!r}) not found.")

            if "." not in key:
                flatten_original[value] = not original_local[key]
            else:
                flatten_original[value] = not flatten_original[key]

            del flatten_original[key]

        for key in self.DELETE_FLATTEN:
            if key in flatten_original:
                del flatten_original[key]

        original_local = self.dict_helper.set_subject(flatten_original).unflatten()
        del flatten_original

        merged = Merge(original_local).into(original_upstream)

        if "dns_lookup_over_tcp" in merged and merged["dns_lookup_over_tcp"]:
            merged["dns"]["protocol"] = "TCP"

        for index in self.DELETED_CORE:
            if index in merged:
                del merged[index]

        for index in self.DELETED_LINKS:
            if index in merged.get("links", {}):
                del merged["links"][index]

        if "http_codes" in merged and not bool(merged["http_codes"]["self_managed"]):
            for index, values in PyFunceble.storage.STD_HTTP_CODES.list.items():
                merged["http_codes"]["list"][index] = list(values)

        if merged["cli_testing"]["db_type"] == "json":
            merged["cli_testing"]["db_type"] = "csv"

        if merged["cli_testing"]["cooldown_time"] is None:
            merged["cli_testing"]["cooldown_time"] = self.upstream_config[
                "cli_testing"
            ]["cooldown_time"]

        if "user_agent" not in self.local_config or not isinstance(
            self.local_config["user_agent"], dict
        ):
            merged["user_agent"] = self.upstream_config["user_agent"]

        if "active" in merged["http_codes"]:
            del merged["http_codes"]["active"]

        if "not_found_default" in merged["http_codes"]:
            del merged["http_codes"]["not_found_default"]

        return merged

### `is_local_identical()`

Checks if the local configuration is identical to the upstream one.

Source code in `PyFunceble/config/compare.py`

    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307

    def is_local_identical(self) -> bool:
        """
        Checks if the local configuration is identical to the upstream one.
        """

        # pylint: disable=too-many-boolean-expressions,too-many-return-statements
        if (
            not self.dict_helper.set_subject(self.local_config).has_same_keys_as(
                self.upstream_config
            )
            or "user_agent" not in self.local_config
            or not isinstance(self.local_config["user_agent"], dict)
            or "http_codes" not in self.local_config
            or "active" in self.local_config["http_codes"]
            or "not_found_default" in self.local_config["http_codes"]
            or "self_managed" not in self.local_config["http_codes"]
            or "dns" not in self.local_config
            or "proxy" not in self.local_config
            or "follow_server_order" not in self.local_config["dns"]
            or "trust_server" not in self.local_config["dns"]
            or "platform" not in self.local_config
            or "platform" not in self.local_config["lookup"]
        ):
            return False

        for index in self.local_config:
            if index in self.DELETED_CORE:
                return False

        for index in self.local_config.get("links", {}):
            if index in self.DELETED_LINKS:
                return False

        if "self_managed" in self.local_config["http_codes"] and not bool(
            self.local_config["http_codes"]["self_managed"]
        ):
            if "http_codes" not in self.upstream_config:
                return False

            for index, values in self.local_config["http_codes"]["list"].items():
                if set(self.upstream_config["http_codes"]["list"][index]) != set(
                    values
                ):
                    return False

        if (
            "platform" in self.local_config
            and "url_base" in self.local_config["platform"]
        ):
            return False

        return True

### `set_local_config(value)`

Sets the local configuration to work with.

Source code in `PyFunceble/config/compare.py`

    216
    217
    218
    219
    220
    221
    222
    223

    def set_local_config(self, value: dict) -> "ConfigComparison":
        """
        Sets the local configuration to work with.
        """

        self.local_config = value

        return self

### `set_upstream_config(value)`

Sets the upstream configuration to work with.

Source code in `PyFunceble/config/compare.py`

    247
    248
    249
    250
    251
    252
    253
    254

    def set_upstream_config(self, value: dict) -> "ConfigComparison":
        """
        Sets the upstream configuration to work with.
        """

        self.upstream_config = value

        return self