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

Enable ansible-gendoc for Python 3.13 #26

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@ jobs:
strategy:
matrix:
os: [Ubuntu, macOS]
python-version: ["3.10"]
python-version: ["3.10", "3.11", "3.12", "3.13"]
include:
- os: Ubuntu
image: ubuntu-latest
image: ubuntu-20.04
- os: Ubuntu
image: ubuntu-22.04
- os: Ubuntu
image: ubuntu-24.04
- os: macOS
image: macos-12
- os: macOS
image: macos-13
- os: macOS
image: macos-14
- os: macOS
image: macos-11
image: macos-15
fail-fast: false
defaults:
run:
Expand Down
2 changes: 1 addition & 1 deletion ansible_gendoc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from importlib_metadata import version

__version__ = version('ansible-gendoc')
__version__ = version("ansible-gendoc")
48 changes: 26 additions & 22 deletions ansible_gendoc/gendoc.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
from jinja2 import Environment, PackageLoader, FileSystemLoader
from rich import print
import os
import subprocess
from pathlib import Path

import typer
from git import Repo
from giturlparse import parse
from jinja2 import Environment, FileSystemLoader, PackageLoader
from rich import print

from ansible_gendoc.helpers import (
controlleveltitle,
convert_dict_of_string,
convert_string,
load_yml_file,
load_yml_files,
write_file,
read_files,
controlleveltitle,
convert_dict_of_string,
convert_string
write_file,
)
from git import Repo

import subprocess
import os
import typer
from giturlparse import parse


class Gendoc:
Expand All @@ -39,14 +40,12 @@ def _run_command(self, command):
stdout=subprocess.PIPE,
)
out, err = p.communicate()
return_code = p.returncode
return {
"code": p.returncode,
"out": out,
"err": err,
}


def _make_role_doc(self, role):
# Read ansible galaxy info
if os.path.isfile(os.path.join(role["path"], "meta/.galaxy_install_info")):
Expand All @@ -66,7 +65,7 @@ def _make_role_doc(self, role):
if os.path.isfile(os.path.join(role["path"], "meta/argument_specs.yml")):
specs_vars = load_yml_file(
os.path.join(role["path"], "meta/argument_specs.yml"), self.verbose
)['argument_specs']
)["argument_specs"]
else:
specs_vars = None
# load literaly role defaults/*.yml
Expand All @@ -80,7 +79,9 @@ def _make_role_doc(self, role):
# load template and create templating environment
if os.path.isfile(os.path.join(self.rolespath, "templates/README.j2")):
env = Environment(
loader=FileSystemLoader(searchpath=os.path.join(self.rolespath, "templates")),
loader=FileSystemLoader(
searchpath=os.path.join(self.rolespath, "templates")
),
lstrip_blocks=True,
trim_blocks=True,
)
Expand All @@ -94,11 +95,14 @@ def _make_role_doc(self, role):
string_role_defaults_files = convert_string(defaults_files)
# render role
template = env.get_template("README.j2")
if 'namespace' in meta_vars['galaxy_info'] and 'role_name' in meta_vars['galaxy_info']:
rolename = ("%s.%s" % (meta_vars['galaxy_info']['namespace'],meta_vars['galaxy_info']['role_name'])).lower()
elif 'role_name' in meta_vars['galaxy_info']:
rolename = meta_vars['galaxy_info']['role_name'].lower()
else :
if (
"namespace" in meta_vars["galaxy_info"]
and "role_name" in meta_vars["galaxy_info"]
):
rolename = f"{meta_vars["galaxy_info"]["namespace"]}.{meta_vars["galaxy_info"]["role_name"]}".lower()
elif "role_name" in meta_vars["galaxy_info"]:
rolename = meta_vars["galaxy_info"]["role_name"].lower()
else:
rolename = os.path.basename(role["path"])
# render method accepts the same arguments as the dict constructor
t = template.render(
Expand Down Expand Up @@ -133,7 +137,7 @@ def _make_role_doc(self, role):
)

if self.verbose:
print("Role '%s' ...done\n" % role["name"])
print(f"Role '{role["name"]}' ...done\n")

def render(self):
"""
Expand Down
23 changes: 13 additions & 10 deletions ansible_gendoc/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from pathlib import Path
from rich import print
import re
import os
import fnmatch
import os
import re
from pathlib import Path

import yaml
from rich import print


def load_yml_file(fpath, verbose):
Expand Down Expand Up @@ -111,6 +112,7 @@ def controlleveltitle(mdfiles):
file["content"] = re.sub(r"((?:[#]+))", r"#\1", file["content"])
return mdfiles


def _convert_dict_of_string(d):

if isinstance(d, dict):
Expand All @@ -131,8 +133,8 @@ def convert_dict_of_string(dict):
output = []
for file in dict:
temp = {}
temp["filename"] = file['filename']
temp['content'] = _convert_dict_of_string(file['content'])
temp["filename"] = file["filename"]
temp["content"] = _convert_dict_of_string(file["content"])
output.append(temp)

return output
Expand All @@ -143,17 +145,18 @@ def convert_string(dict):
output = []
for file in dict:
temp = {}
temp["filename"] = file['filename']
value = yaml.dump(file['content'],Dumper=MyDumper)
temp['content'] = re.sub(r'^(.)$',r' \1', value)
temp["filename"] = file["filename"]
value = yaml.dump(file["content"], Dumper=MyDumper)
temp["content"] = re.sub(r"^(.)$", r" \1", value)
output.append(temp)

return output


import yaml


class MyDumper(yaml.Dumper):

def increase_indent(self, flow=False, indentless=False):
return super(MyDumper, self).increase_indent(flow, False)
return super(MyDumper, self).increase_indent(flow, False)
26 changes: 15 additions & 11 deletions ansible_gendoc/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import os
import shutil
import sys
from pathlib import Path
from re import template
from ansible_gendoc import __version__
from ansible_gendoc.gendoc import Gendoc
from typing import Optional

import typer
import os
import sys
import shutil

from ansible_gendoc import __version__
from ansible_gendoc.gendoc import Gendoc

app = typer.Typer()


def _version_callback(value: bool) -> None:
if value:
versionText = typer.style(
version_text = typer.style(
f"ansible-gendoc v{__version__}", fg=typer.colors.RED, bg=typer.colors.WHITE
)
typer.echo(versionText)
typer.echo(version_text)
raise typer.Exit()


Expand All @@ -27,7 +27,10 @@ def init(
default="./", help="The path of your role.", exists=True
),
force: Optional[bool] = typer.Option(
False, "--force", "-f", help="Replace existing Template README.j2 in templates folder."
False,
"--force",
"-f",
help="Replace existing Template README.j2 in templates folder.",
),
):
"""
Expand All @@ -41,7 +44,7 @@ def init(
if not os.path.isfile(os.path.join(path, "templates")):
Path(os.path.dirname(template_path)).mkdir(parents=True, exist_ok=True)
if not os.path.isfile(os.path.join(template_path, "README.j2")) or force:
pkgdir = sys.modules['ansible_gendoc'].__path__[0]
pkgdir = sys.modules["ansible_gendoc"].__path__[0]
fullpath = os.path.join(pkgdir, "templates/README.j2")
shutil.copy(fullpath, template_path)
else:
Expand All @@ -50,7 +53,6 @@ def init(
raise typer.Exit(code=1)



@app.command()
def render(
path: Path = typer.Argument(
Expand All @@ -76,6 +78,7 @@ def render(
)
gendoc.render()


@app.callback()
def main(
version: Optional[bool] = typer.Option(
Expand All @@ -89,5 +92,6 @@ def main(
) -> None:
return


if __name__ == "__main__":
app()