# Path: crowd_anki/github/github_importer.py
# class GitImporter(object):
#     """
#     Provides functionality of cloning a git repository that contains CrowdAnki export data, and importing it into Anki
#     """
# 
#     def __init__(self, collection):
#         self.collection = collection
#         self.notifier = AnkiModalNotifier()
# 
#     @staticmethod
#     def on_git_import_action(collection):
#         GitImporter(collection).import_from_git()
# 
#     def import_from_git(self):
#         repo_url, ok = QInputDialog.getText(None, 'Import git repository', 'URL:', text='')
#         if repo_url and ok:
#             self.clone_repository_and_import(repo_url)
# 
#     def clone_repository_and_import(self, repo_url):
#         repo_url = repo_url.strip()
#         try:
#             repo_local_path = self.get_repo_local_path(repo_url)
#             porcelain.pull(str(repo_local_path), repo_url)
#         except ValueError:
#             return self.notifier.error("URL incorrect", f"URL could not be parsed \"{repo_url}\"")
#         except NotGitRepository:
#             try:
#                 self.clone_repository(repo_url, repo_local_path)
#             except (GitProtocolError, NotGitRepository):
#                 return self.notifier.error("repository not found", f"git repository not found at URL {repo_url}")
# 
#         AnkiJsonImporter.import_deck_from_path(self.collection, repo_local_path)
# 
#     def clone_repository(self, repo_url, repo_path):
#         repo_path.mkdir(parents=True, exist_ok=True)
#         repo_object = porcelain.clone(repo_url, target=str(repo_path), bare=False, checkout=True, errstream=porcelain.NoneStream())
#         repo_object.close()
# 
#     def get_repo_local_path(self, repo_url):
#         repo_name = get_repository_name(repo_url)
#         return ConfigSettings.get_instance().full_snapshot_path.joinpath(repo_name)


from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import MagicMock, patch
from mamba import describe, it, context
from expects import expect, equal
from crowd_anki.github.github_importer import GitImporter



TEST_GIT_REPO = "https://github.com/Stvad/Software_Engineering__git"
CLONE_REPETITIONS = 3

temp_dir = TemporaryDirectory()
repo_local_path = Path(temp_dir.name)

GitImporter.get_repo_local_path = lambda self, x: repo_local_path


# Path: crowd_anki/github/github_importer.py
# class GitImporter(object):
#     """
#     Provides functionality of cloning a git repository that contains CrowdAnki export data, and importing it into Anki
#     """
# 
#     def __init__(self, collection):
#         self.collection = collection
#         self.notifier = AnkiModalNotifier()
# 
#     @staticmethod
#     def on_git_import_action(collection):
#         GitImporter(collection).import_from_git()
# 
#     def import_from_git(self):
#         repo_url, ok = QInputDialog.getText(None, 'Import git repository', 'URL:', text='')
#         if repo_url and ok:
#             self.clone_repository_and_import(repo_url)
# 
#     def clone_repository_and_import(self, repo_url):
#         repo_url = repo_url.strip()
#         try:
#             repo_local_path = self.get_repo_local_path(repo_url)
#             porcelain.pull(str(repo_local_path), repo_url)
#         except ValueError:
#             return self.notifier.error("URL incorrect", f"URL could not be parsed \"{repo_url}\"")
#         except NotGitRepository:
#             try:
#                 self.clone_repository(repo_url, repo_local_path)
#             except (GitProtocolError, NotGitRepository):
#                 return self.notifier.error("repository not found", f"git repository not found at URL {repo_url}")
# 
#         AnkiJsonImporter.import_deck_from_path(self.collection, repo_local_path)
# 
#     def clone_repository(self, repo_url, repo_path):
#         repo_path.mkdir(parents=True, exist_ok=True)
#         repo_object = porcelain.clone(repo_url, target=str(repo_path), bare=False, checkout=True, errstream=porcelain.NoneStream())
#         repo_object.close()
# 
#     def get_repo_local_path(self, repo_url):
#         repo_name = get_repository_name(repo_url)
#         return ConfigSettings.get_instance().full_snapshot_path.joinpath(repo_name)


# Path: test/github/github_importer_spec.py
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import MagicMock, patch
from mamba import describe, it, context
from expects import expect, equal
from crowd_anki.github.github_importer import GitImporter




TEST_GIT_REPO = "https://github.com/Stvad/Software_Engineering__git"
CLONE_REPETITIONS = 3

temp_dir = TemporaryDirectory()
repo_local_path = Path(temp_dir.name)

GitImporter.get_repo_local_path = lambda self, x: repo_local_path


with describe(GitImporter) as self: