<commit_before># Import flags

Flags can be imported from a main module by using the special
`$import` attribute of the operation flags.

We'll use the sample project `flags`:

    >>> project_dir = sample("projects/flags")

For our tests, we'll use a helper function to print flag attributes.

    >>> def flag_info(gf, op_name, flag_name):
    ...     op = gf.models["test"].get_operation(op_name)
    ...     flag = op.get_flagdef(flag_name)
    ...     print("description: {}".format(flag.description))
    ...     if flag.choices:
    ...         print("choices: {}".format([c.value for c in flag.choices]))
    ...     print("default: {}".format(flag.default))

When `$import` is used, any referenced main modules must be available
on the system path. If it's not, the Guild file will still load, but a
warning message is logged.

NOTE: We need to preemptively load the flags plugin to register its
logger prior to capturing logs:

    >>> from guild import plugin
    >>> plugin.for_name("flags")
    <guild.plugins.flags.FlagsPlugin object ...>

Let's load the sample project:

    >>> from guild import guildfile
    >>> with LogCapture() as log:
    ...     gf = guildfile.from_dir(project_dir)
    ...     log.print_all()
    WARNING: cannot import flags from main: No module named main
    WARNING: cannot import flags from main2: No module named main2

In this case, we have a loaded Guild file:

    >>> gf
    <guild.guildfile.Guildfile '.../samples/projects/flags/guild.yml'>

The flags for the `import-1` operation are empty - the referenced
flags could not be imported:

    >>> gf.models["test"].get_operation("import-1").flags
    []

Let's now include the project directory in the system path:

    >>> import sys
    >>> sys_path_save = sys.path
    >>> sys.path = [project_dir] + sys.path

And load the Guild file again:

    >>> gf = guildfile.from_dir(project_dir, no_cache=True)

We won't load anymore Guild files so we can restore the system path:

    >>> sys.path = sys_path_save

The flags for `import-1` are now:<commit_msg>Fix tests<commit_after># Import flags

Flags can be imported from a main module by using the special
`$import` attribute of the operation flags.

We'll use the sample project `flags`:

    >>> project_dir = sample("projects/flags")

For our tests, we'll use a helper function to print flag attributes.

    >>> def flag_info(gf, op_name, flag_name):
    ...     op = gf.models["test"].get_operation(op_name)
    ...     flag = op.get_flagdef(flag_name)
    ...     print("description: {}".format(flag.description))
    ...     if flag.choices:
    ...         print("choices: {}".format([c.value for c in flag.choices]))
    ...     print("default: {}".format(flag.default))

When `$import` is used, any referenced main modules must be available
on the system path. If it's not, the Guild file will still load, but a
warning message is logged.

NOTE: We need to preemptively load the flags plugin to register its
logger prior to capturing logs:

    >>> from guild import plugin
    >>> plugin.for_name("flags")
    <guild.plugins.flags.FlagsPlugin object ...>

Let's load the sample project:

    >>> from guild import guildfile
    >>> with LogCapture() as log:
    ...     gf = guildfile.from_dir(project_dir)
    ...     log.print_all()
    WARNING: cannot import flags from main: No module named main
    WARNING: cannot import flags from main2: No module named main2

In this case, we have a loaded Guild file:

    >>> gf
    <guild.guildfile.Guildfile '.../samples/projects/flags/guild.yml'>

The flags for the `import-1` operation are empty - the referenced
flags could not be imported:

    >>> gf.models["test"].get_operation("import-1").flags
    []

Let's now include the project directory in the system path:

    >>> import sys
    >>> sys_path_save = sys.path
    >>> sys.path = [project_dir] + sys.path

And load the Guild file again (we use LogCapture to squelch and
progress messages to the user):

    >>> with LogCapture() as log:
    ...     gf = guildfile.from_dir(project_dir, no_cache=True)

We won't load anymore Guild files so we can restore the system path:

    >>> sys.path = sys_path_save

The flags for `import-1` are now:
{'fraction_non_alphanumeric': 0.11594202898550725, 'fraction_numerical': 0.0023758612497030173, 'mean_word_length': 3.172447968285431, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6315209', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1215, 'n_words': 471}