def get_specific_gitignore_template(cls, template_name): try: gitignore_templates = cls.get_all_gitignore_templates() except: raise for template in gitignore_templates: if template.lower() == template_name.lower(): # this is to make sure that the template is retreived regardless of whether the user used uppercase or lowercase letters # as the get_gitignore_template() function below only accepts template names which are EXACTLY the write case # eg. get_gitignore_template() accepts 'Python', but not 'python' or 'pyTHOn', etc template_name = template break else: raise Exception( f"Could not retrieve the {template_name} .gitignore template from Github" ) try: gh = Github(timeout=5) return gh.get_gitignore_template(template_name).source except: raise Exception( f"Could not retrieve the {template_name} .gitignore template from Github" )
class GithubVCS(AbstractVCS): """ Github management functions """ def __init__(self, vcs: Vcs): super().__init__() self.vcs = vcs if vcs.token: self.connector = Github(login_or_token=self.vcs.token) else: self.connector = Github(login_or_token=self.vcs.user, password=self.vcs.password) @staticmethod def get_base_url(): return 'https://www.github.com' def get_repository_name(self, project: Project) -> str: return "{}/{}".format(self.vcs.user, project.name) def get_gitignore_template_from_connector(self, template: str) -> str: return self.connector.get_gitignore_template(template).source def create_project(self, project: Project) -> str: repository_name = self.get_repository_name(project) repo = None try: repo = self.connector.get_repo(repository_name) except GithubException: pass user = self.connector.get_user() if repo is None: repo = user.create_repo(project.name) repo.edit( description= "This is a project generated using darkanakin41/project-toolbox", private=True) return repo.ssh_url
from github import Github from github_token import GITHUB_TOKEN, user, password g1 = Github(GITHUB_TOKEN) g2 = Github(user, password) #List all templates available to pass #as an option when creating a repository gitignore_templates = g1.get_gitignore_templates() print(gitignore_templates) for gitignore_template in gitignore_templates: name = gitignore_template print(name) print(g1.get_gitignore_template(name))