rwnx / pynonymizer

A universal tool for translating sensitive production database dumps into anonymized copies.
https://pypi.org/project/pynonymizer/
MIT License
102 stars 38 forks source link

locale from strategyfile will always be overwritten #78

Closed derfryday closed 3 years ago

derfryday commented 3 years ago

Describe the bug You can't set the locale via the strategyfile because it will always be overwritten by the fallback locale

To Reproduce Steps to reproduce the behavior:

  1. set the locale in a strategyfile to some locale with some exclusive faker modules i.e. de_DE
  2. use a faker module exclusive to that locale i.e. state
  3. run pynonymizer without the --fake-locale argument but enable verbose to see that the locale will fall back to en_GB

Expected behavior settings the locale in the strategyfile results in that locale being used by pynonmizer.

Additional context The issue seemingly lies in pynonymizer/pynonymize.py:47

    if fake_locale is None:
        fake_locale = "en_GB"

fake_locale will never be None beyond this point, it's either a value specified by the CLI argument/environment variable or the fallback en_GB

the parse_config method relies on local_override to be None to use the value set in the strategyfile.

pynonymizer/strategy/parser.py:162

    def parse_config(self, raw_config, locale_override=None):
        """
        parse a configuration dict into a DatabaseStrategy.
        :param raw_config:
        :return:
        """
        config = StrategyParser.__normalize_table_list(deepcopy(raw_config))

        locale = config.get("locale", None)
        if locale_override:
            locale = locale_override

I'd suggest to put the fallback to en_GB into the line where the locale is being read from the strategyfile instead of the fallback logic from pynonymizer/pynonymize.py:47.

    def parse_config(self, raw_config, locale_override=None):
        """
        parse a configuration dict into a DatabaseStrategy.
        :param raw_config:
        :return:
        """
        config = StrategyParser.__normalize_table_list(deepcopy(raw_config))

---        locale = config.get("locale", None)
+++        locale = config.get("locale", "en_GB")
        if locale_override:
            locale = locale_override

I've created a pull request with the suggested changes: https://github.com/jerometwell/pynonymizer/pull/79

rwnx commented 3 years ago

Thank you for this report (and fix!) this is a case of building for one set of defaults and then setting a different bunch of values in the CLI, and not really accounting for either 😅 oops.

There's definitely other places this can happen that are due an update but locale is the first one to actually get overriden, so it makes sense! I'll pick up on your PR.

rwnx commented 3 years ago

seeing as this patches some broken behaviour i'm going to package as patchv1.21.1, and ship over the next day or so! I'll update here.

derfryday commented 3 years ago

lovely, thanks!

rwnx commented 3 years ago

I've released v1.21.1 which should contain this fix. If you could take a look and report/close this issue if it's resolved, that would be fantastic 😇