Last active
August 28, 2024 11:06
-
-
Save diegoquintanav/4038cab09d387d278488d2e2728eb968 to your computer and use it in GitHub Desktop.
test de prova
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from pathlib import Path | |
import argparse | |
from typing import Dict, List, Optional | |
import json | |
def parse_doc_comments(readme_content: str) -> List[str]: | |
"""Parse DOC comments in the form <!-- [DOC-*] --> from the README.md content.""" | |
pattern = r"<!--\s*\[\s*(.*?)\s*\]\s*-->" | |
matches = re.findall(pattern, readme_content) | |
return matches | |
def parse_doc_comments_from_file(file_path: Path) -> List[str]: | |
"""Parse DOC comments in the form <!-- [DOC-*] --> from a file.""" | |
with open(str(file_path), "r") as file: | |
content = file.read() | |
parsed_contents = parse_doc_comments(content) | |
return parsed_contents | |
def parse_and_save_extended_json( | |
file_path: Path, | |
repo_name: str, | |
last_updated: str, | |
last_commit_sha: str, | |
readme_url: str, | |
save_to_path: Optional[Path] = None, | |
) -> None: | |
"""Parse DOC comments from a file and save them to a JSON file.""" | |
parsed_contents = parse_doc_comments_from_file(file_path) | |
final_json_output = craft_json_output( | |
repo_name=repo_name, | |
doc_comments=parsed_contents, | |
last_updated=last_updated, | |
last_commit_sha=last_commit_sha, | |
readme_url=readme_url, | |
) | |
if save_to_path: | |
print(f"Saving to {save_to_path}") | |
save_to_path.write_text(json.dumps(final_json_output)) | |
return final_json_output | |
def craft_json_output( | |
repo_name: str, | |
doc_comments: List[str], | |
last_updated: str, | |
last_commit_sha: str, | |
readme_url: str, | |
) -> Dict: | |
"""Craft a JSON output with the repository metadata.""" | |
base_doc_keys = [ | |
"DOC-PROJECT-SECRETS", | |
"DOC-DEV-HOW-TO", | |
"DOC-DEPLOY-HOW-TO", | |
"DOC-HOW-TO-CONTRIBUTE", | |
"DOC-BUSINESS-RULES", | |
"DOC-PROCESS-HOW-TO", | |
"DOC-PROJECT-SETTINGS", | |
"DOC-DEV-TOOLS", | |
"DOC-ARCH-FUNC", | |
"DOC-API-DOCS", | |
"DOC-TRANSLATE-HOW-TO", | |
"DOC-ADR", | |
"DOC-USER-GUIDE", | |
"DOC-USER-TUTORIAL", | |
] | |
doc_comments_dict = { | |
key: True if key in doc_comments else False for key in base_doc_keys | |
} | |
json_output = { | |
"repository_name": repo_name, | |
"doc_comments": doc_comments_dict, | |
"last_updated": last_updated, | |
"last_commit_sha": last_commit_sha, | |
"readme_url": readme_url, | |
} | |
return json_output | |
def parse_args(): | |
"""Parse arg.sys inputs | |
# Example | |
python parse_readme.py --input README_no_docs.md --output outputdb.json --repo-name "somenergia-exemple-documentacio" --last-updated "2024-08-12T10:53:57.000-04:00" --last-commit-sha "c9f82dcbe2e6c01806096ca18b711b461f039421" --readme-url "https://gitlab.somenergia.coop/projects/94/repository/files/README.md/raw" | |
parser = argparse.ArgumentParser( | |
description="Parse DOC comments from a README.md file" | |
) | |
parser.add_argument( | |
"--input", | |
type=Path, | |
help="The input file path", | |
required=True, | |
) | |
parser.add_argument( | |
"--output", | |
type=Path, | |
help="The output file path. If not passed, the output will be printed to the console.", | |
required=False, | |
) | |
parser.add_argument( | |
"--repo-name", | |
type=str, | |
help="The repository name", | |
required=False, | |
) | |
parser.add_argument( | |
"--last-updated", | |
type=str, | |
help="The last updated date", | |
required=False, | |
) | |
parser.add_argument( | |
"--last-commit-sha", | |
type=str, | |
help="The last commit SHA", | |
required=False, | |
) | |
parser.add_argument( | |
"--readme-url", | |
type=str, | |
help="The README.md URL", | |
required=False, | |
) | |
args = parser.parse_args() | |
return args | |
def main(): | |
args = parse_args() | |
file_input = args.input | |
file_output = args.output | |
repo_name = args.repo_name | |
last_updated = args.last_updated | |
last_commit_sha = args.last_commit_sha | |
readme_url = args.readme_url | |
doc_comments = parse_and_save_extended_json( | |
file_path=file_input, | |
save_to_path=file_output, | |
repo_name=repo_name, | |
last_updated=last_updated, | |
last_commit_sha=last_commit_sha, | |
readme_url=readme_url, | |
) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"repository_name": "somenergia-exemple-documentacio", | |
"doc_comments": { | |
"DOC-PROJECT-SECRETS": true, | |
"DOC-DEV-HOW-TO": true, | |
"DOC-DEPLOY-HOW-TO": true, | |
"DOC-HOW-TO-CONTRIBUTE": true, | |
"DOC-BUSINESS-RULES": true, | |
"DOC-PROCESS-HOW-TO": true, | |
"DOC-PROJECT-SETTINGS": true, | |
"DOC-DEV-TOOLS": true, | |
"DOC-ARCH-FUNC": true, | |
"DOC-API-DOCS": true, | |
"DOC-TRANSLATE-HOW-TO": true, | |
"DOC-ADR": true, | |
"DOC-USER-GUIDE": true, | |
"DOC-USER-TUTORIAL": true, | |
}, | |
"last_updated": "2024-08-12T10:53:57.000-04:00", | |
"last_commit_sha": "c9f82dcbe2e6c01806096ca18b711b461f039421", | |
"readme_url": "https://gitlab.somenergia.coop/projects/94/repository/files/README.md/raw", | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment