Use tag informations
====================

When we set tags for a variable, it will add :term:`Tiramisu` properties and informations.

We can filter those variables easily with tags.

First of all, create our structural file:

.. code-block:: yaml
   :caption: the :file:`dist/00-base.yml` file content

    %YAML 1.2
    ---
    version: 1.1

    infra_name: my infra   # Name of this infrastructure

    server1:  # the first server

      internal_domain:
        description: Server domaine name
        type: domainname
        tags:
          - internal

      external_domain:
        description: Domain name to access to this server for Internet
        type: domainname
        tags:
          - external

    server2:  # the second server

      address:
        description: Server domaine name
        type: domainname
        tags:
          - internal
    ...

Exclude variables with a specific tag
-------------------------------------

To exclude variables with a specific tag is very easy. When the variable has tags, properties with same name are automaticly create.
So exclude a tag means exclude variable with a particular property.

In this example we exclude variable with "internal" tag and display result of "server1" family:

.. code-block:: python
    :caption: the :file:`script.py` file content

    from rougail import Rougail, RougailConfig
    from pprint import pprint

    RougailConfig['main_namespace'] = None
    RougailConfig['main_structural_directories'] = ['dist']
    rougail = Rougail()
    config = rougail.run()
    print("without filter:")
    pprint(config.option("server1").value.get())
    config.property.add('internal')
    print("with filter:")
    pprint(config.option("server1").value.get())

Let's execute `script.py`:

.. code-block:: bash

    $ python3 script.py 
    without filter:
    {<TiramisuOption path="server1.external_domain">: None,
     <TiramisuOption path="server1.internal_domain">: None}
    with filter:
    {<TiramisuOption path="server1.external_domain">: None}

Only variable with a specific tag
---------------------------------

It's more difficult to see only variable with a specific tag.

We have to walk through the configuration and retrieve variable with the selected tag.
Tags are in properties but, are in information too.

Here is a smal script that walk that the configuration and "print" option with "internal" tag:

.. code-block:: python
    :caption: the :file:`script.py` file content

    from rougail import Rougail, RougailConfig
    
    RougailConfig['main_structural_directories'] = ['dist']
    rougail = Rougail()
    config = rougail.run()
    
    def walk(config):
        for option in config:
            if option.isoptiondescription():
                walk(option)
            elif "internal" in option.information.get('tags', []):
                print(option.description())

    if __name__ == '__main__':
        walk(config)

Let's execute `script.py`:

.. code-block:: bash

    $ python3 script.py 
    server1.internal_domain (Server domaine name)
    server2.address (Server domaine name)
