Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it flexible to load macros in SSG #12816

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

marcusburghardt
Copy link
Member

@marcusburghardt marcusburghardt commented Jan 13, 2025

Description:

The CaC/content project uses Jinja2 macros during building time in order to make the content more flexible or easier to be managed in a context with several products. Macros are used in multiple files and should be properly processed in order to return the expected data.

Until recently the SSG library was not expected to be used externally and therefore the path to load macros was hard-coded. This PR brings more flexibility to process yaml files with macros by allowing the macros to be load also from an informed directory.

Rationale:

Allow Jinja2 macros to be consumed when SSG library is called outside the project.

Review Hints:

Here is an example to simulate the issue in a virtual environment:

mkdir ~/tmp
cd ~/tmp
virtualenv ssg_tests
cd ssg_tests
source source bin/activate
pip install git+https://github.com/ComplianceAsCode/content.git

Now you can use the example script from #12797 and you will notice many exceptions similar to what was reported in #12797 (comment).

Once the issue is reproduced, you can try again with the fixes:

pip uninstall ssg
pip install git+https://github.com/marcusburghardt/content.git@ssg_external_macros

No issues are expected now.

Unit tests can be trying with:

python -m pytest tests/unit/ssg-module/test_jinja.py
python -m pytest tests/unit/ssg-module/test_variables.py
python -m pytest tests/unit/ssg-module/test_yaml.py

The project defines the JINJA_MACROS_DIRECTORY contant based on the
assumption that SSG library is only executed internally. Once the
library was exported and is expected to be used externally, this
flexibility is necessary. It was created another function that allows
the content directory to be informed.

Signed-off-by: Marcus Burghardt <[email protected]>
@marcusburghardt marcusburghardt added the Infrastructure Our content build system label Jan 13, 2025
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Used by openshift-ci bot. label Jan 13, 2025
Copy link

openshift-ci bot commented Jan 13, 2025

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

Copy link

Start a new ephemeral environment with changes proposed in this pull request:

Fedora Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

@marcusburghardt marcusburghardt marked this pull request as ready for review January 13, 2025 15:16
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Used by openshift-ci bot. label Jan 13, 2025
@marcusburghardt marcusburghardt marked this pull request as draft January 13, 2025 15:18
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Used by openshift-ci bot. label Jan 13, 2025
This function was created to use the flexibility provided by
load_macros_from_content_dir in jinja.py.

Signed-off-by: Marcus Burghardt <[email protected]>
Allow to expand macros used in variables when the variable functions
are called outside the project.

Signed-off-by: Marcus Burghardt <[email protected]>
@marcusburghardt marcusburghardt marked this pull request as ready for review January 13, 2025 20:42
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Used by openshift-ci bot. label Jan 13, 2025
@marcusburghardt marcusburghardt added this to the 0.1.76 milestone Jan 13, 2025
@marcusburghardt marcusburghardt added the enhancement General enhancements to the project. label Jan 13, 2025
@Mab879
Copy link
Member

Mab879 commented Jan 13, 2025

While I'm all in favor of adding type hints, the style guide still states all build system code needs to be Python 2 compatible. Last I checked the only hold up was OL7. I have asked in #12046 to get more details on this.

@marcusburghardt
Copy link
Member Author

While I'm all in favor of adding type hints, the style guide still states all build system code needs to be Python 2 compatible. Last I checked the only hold up was OL7. I have asked in #12046 to get more details on this.

Where the type hints were included they are also compatible with Python 2.

@jan-cerny
Copy link
Collaborator

But for Python 2 the type hints would have to be in comments, right?
https://peps.python.org/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code

ssg/jinja.py Outdated Show resolved Hide resolved
@marcusburghardt
Copy link
Member Author

But for Python 2 the type hints would have to be in comments, right? https://peps.python.org/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code

It is a recommendation, but not mandatory. According to the notes on this link, if moved to type comment, type annotation could not be used together. So would prefer to remove both than keep as type comment considering the intention to drop Python2 support as soon as we can. We also have type hints in other files, so is it worth to remove it on this PR? Let me know what do you think so I can quickly update the PR if necessary. Thanks

@jan-cerny
Copy link
Collaborator

But the code won't work with Python 2 if you keep the type hints. The type hint syntax is a new thing in Python 3.5 and wasn't present in Python 2. If you want to have the code compatible with Python 2, you have to put the type hints to comments.

[root@vm-10-0-186-245 ~]# python --version
Python 2.7.5
[root@vm-10-0-186-245 ~]# cat run.py 
#/usr/bin/python

def greeting(name: str) -> str:
    return 'Hello ' + name

greeting("John")
[root@vm-10-0-186-245 ~]# python run.py
  File "run.py", line 3
    def greeting(name: str) -> str:
                     ^
SyntaxError: invalid syntax

@marcusburghardt
Copy link
Member Author

But the code won't work with Python 2 if you keep the type hints. The type hint syntax is a new thing in Python 3.5 and wasn't present in Python 2. If you want to have the code compatible with Python 2, you have to put the type hints to comments.

[root@vm-10-0-186-245 ~]# python --version
Python 2.7.5
[root@vm-10-0-186-245 ~]# cat run.py 
#/usr/bin/python

def greeting(name: str) -> str:
    return 'Hello ' + name

greeting("John")
[root@vm-10-0-186-245 ~]# python run.py
  File "run.py", line 3
    def greeting(name: str) -> str:
                     ^
SyntaxError: invalid syntax

Ok. Let me quickly remove in this specific PR so we can move forward. I will comment in the discussion to confirm if we are already good to officially remove Python 2 support. The informed date there was 2014/12.

@jan-cerny
Copy link
Collaborator

yeah, it would be great to drop the python2 support soon, the type hints feature is a nice reason

@marcusburghardt
Copy link
Member Author

yeah, it would be great to drop the python2 support soon, the type hints feature is a nice reason

Type hints removed from functions changed in this PR @jan-cerny .

Copy link

codeclimate bot commented Jan 15, 2025

Code Climate has analyzed commit 371915d and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 61.9% (0.1% change).

View more on Code Climate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement General enhancements to the project. Infrastructure Our content build system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants