Skip to content

Commit c4fe932

Browse files
authored
Merge pull request #14 from DSACMS/add-save-parse-methods
Add save parse methods
2 parents 8bd22ce + fb1b740 commit c4fe932

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

codejson_index_generator/parsers.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import os
55

66
from typing import Dict, Optional
7-
from github import Github, Repository, GithubException
7+
from github import Github, Repository, GithubException, Organization
8+
89

910
class IndexGenerator:
1011
def __init__(self, agency: str, verison: str, token: Optional[str] = None,):
@@ -33,6 +34,18 @@ def get_code_json(self, repo: Repository) -> Optional[Dict]:
3334
except (json.JSONDecodeError, ValueError) as e:
3435
print(f"JSON Error: {str(e)}")
3536
return None
37+
38+
def save_code_json(self, repo: Repository, output_path: str) -> Optional[str]:
39+
40+
res = self.get_code_json(repo)
41+
42+
if res:
43+
with open(output_path, 'w') as f:
44+
json.dump(res, f, indent=2)
45+
else:
46+
print(f"Error getting codejson file!")
47+
48+
return res
3649

3750
def update_index(self, index: Dict, code_json: Dict, org_name: str, repo_name: str) -> None:
3851
baseline = {
@@ -44,13 +57,35 @@ def update_index(self, index: Dict, code_json: Dict, org_name: str, repo_name: s
4457

4558
index['releases'].append(baseline)
4659

47-
def process_organization(self, org_name: str) -> None:
60+
def get_org_repos(self, org_name: str) -> list[Organization]:
4861
try:
4962
org = self.github.get_organization(org_name)
5063
print(f"\nProcessing organization: {org_name}")
5164

5265
total_repos = org.public_repos
5366
print(f"Found {total_repos} public repositories")
67+
68+
return total_repos
69+
except GithubException as e:
70+
raise e
71+
72+
def save_organization_files(self, org_name: str, codeJSONPath) -> None:
73+
try:
74+
org = self.github.get_organization(org_name)
75+
total_repos = self.get_org_repos(org_name)
76+
77+
for id, repo in enumerate(org.get_repos(type='public'), 1):
78+
print(f"\n Saving codeJSON for {repo.name} [{id}/{total_repos}]")
79+
80+
repoPath = os.path.join(codeJSONPath, (repo.name + '.json'))
81+
code_json = self.save_code_json(repo,repoPath)
82+
except GithubException as e:
83+
print(f"Error processing organization {org_name}: {str(e)}")
84+
85+
def process_organization(self, org_name: str) -> None:
86+
try:
87+
org = self.github.get_organization(org_name)
88+
total_repos = self.get_org_repos(org_name)
5489

5590
for id, repo in enumerate(org.get_repos(type='public'), 1):
5691
print(f"\nChecking {repo.name} [{id}/{total_repos}]")

0 commit comments

Comments
 (0)