I have two behaviours: pulling or cloning a git repository. It started as an Enum, but then I had to switch over the enum all the time. I wanted to turn it into some kind of strategy pattern and fiddled around with the declaration of the enum values. I found the following solution, but I have two questions regarding it:
- Is it pythonic? If not, how cursed is it?
- Why do I have to call
action(params)
instead ofaction.value(params)
?
If I do the second, it says that .value
is not defined on a function. However, it is the usual way to access an Enums value, and isn't the function the assigned value?
class GitAction(Enum):
@staticmethod
def clone(project_url: str, to_path: Path):
logger.debug(f'Cloning {project_url} to {to_path}')
Repo.clone_from(project_url, to_path)
@staticmethod
def pull(_, local_repo_path: Path):
logger.debug(f'Pulling repo at {local_repo_path}')
Repo(local_repo_path).remote().pull()
CLONE = clone
PULL = pull
def path_exists(path: Path) -> bool:
return path.exists()
def get_required_git_action(repo_id: str, user_repo_root_directory: Path) -> GitAction:
user_repo_path = user_repo_root_directory / repo_id
if not path_exists(user_repo_path):
return GitAction.CLONE
else:
return GitAction.PULL
def download_repo(username: str, repo_name: str, user_repo_root_directory=user_repository_download_location):
repo_id = f'{username}-{repo_name}'
user_repo_location = user_repo_root_directory / repo_id
action = get_required_git_action(repo_id, user_repo_root_directory)
logger.debug(f'downloading {repo_id} to {user_repo_location}')
action(create_repo_url(username, repo_name), user_repo_location)
logger.debug(f'downloaded {repo_id}')
I've been staring at the code base for some time and may take some things for granted.
Also, feel free to give feedback regarding readability/understandability of the code, I've been trying to give meaningful and intuitive names.