Gitlab:文件读取
1 import gitlab 2 3 """ 4 pip install python-gitlab 5 """ 6 7 8 class Gitlab(object): 9 def __init__(self, url: str, private_token: str, project_id: int): 10 self.git = gitlab.Gitlab(url, private_token, ssl_verify=False, per_page=2) 11 self.project = self.git.projects.get(project_id) 12 13 def search(self, scope='blobs', search='router bgp', **kwargs): 14 """ 15 :param scope: 16 :param search: 17 :return: a list iterator 18 """ 19 return self.project.search(scope, search, as_list=False, **kwargs) 20 21 def project_file(self, file_path: str = '', ref: str = 'master', decode: bool = False): 22 """ 23 :param file_path: 'config_backup/CiscoIOS-Switch-10.41.0.174-USRAL8001F13560_01.txt' 24 :param ref: master 25 :param decode 26 :return: ProjectFile object 27 """ 28 try: 29 obj = self.project.files.get(file_path, ref) 30 if decode: 31 obj = obj.decode() 32 except Exception as e: 33 raise e 34 return obj 35 36 37 if __name__ == '__main__': 38 gl = Gitlab(url='http://gitlab.xpaas.lenovo.com', private_token='private_token', project_id=19857) 39 text = gl.project_file('config_backup/CiscoIOS-Switch-10.41.0.174-USRAL8001F13560_01.txt', decode=True) 40 print(text)