base_platform.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import requests,csv,os
  2. from repo_sync.models import Repo
  3. from repo_sync.utils.colors import bcolors
  4. class BasePlatform(object):
  5. """base platform"""
  6. repo_list_path = 'repo_list.csv'
  7. def __init__(self, username:str, token:str ) -> None:
  8. """init"""
  9. self.sess = requests.Session()
  10. self.username = username
  11. self.token = token
  12. self.repos = []
  13. self.sess.headers.update(
  14. {
  15. 'Accept': 'application/json',
  16. 'Content-Type': 'application/json',
  17. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',
  18. 'Authorization': f'token {self.token}',
  19. }
  20. )
  21. if os.path.exists(self.repo_list_path):
  22. with open(self.repo_list_path, 'r', encoding='utf8') as f:
  23. reader = csv.DictReader(f)
  24. for row in reader:
  25. repo = Repo()
  26. repo.__dict__ = row
  27. self.repos.append(repo)
  28. def create_repo(self, repo_name: str):
  29. """create a repo"""
  30. raise NotImplementedError('crawl not implemented')
  31. def delete(self, repo_name: str):
  32. """delete a repo, maybe request a confirm by input"""
  33. raise NotImplementedError('crawl not implemented')
  34. def clone(self, repo_name: str):
  35. """clone a repo"""
  36. raise NotImplementedError('crawl not implemented')
  37. def pull(self, repo_path: str):
  38. """pull a repo"""
  39. raise NotImplementedError('crawl not implemented')
  40. def push(self, repo_path: str):
  41. """push a repo"""
  42. raise NotImplementedError('crawl not implemented')
  43. @classmethod
  44. def suitable(cls, extractor: str) -> bool:
  45. """check if this extractor is suitable for this platform"""
  46. raise NotImplementedError('crawl not implemented')
  47. def save_csv(self):
  48. with open(self.repo_list_path, 'w', newline='') as f:
  49. if len(self.repos) == 0:
  50. print(f"{bcolors.WARNING}repo list is empty, please delete repo_list.csv and try again{bcolors.ENDC}")
  51. return
  52. writer = csv.DictWriter(f, fieldnames=self.repos[0].__dict__.keys(), lineterminator='\n')
  53. writer.writeheader()
  54. for repo in self.repos:
  55. writer.writerow(repo.__dict__)