Skip to content

Commit

Permalink
Merge pull request #534 from JrGoodle/repo-check
Browse files Browse the repository at this point in the history
Refactor utilities
  • Loading branch information
JrGoodle committed May 25, 2020
2 parents 9af6728 + 3c52a7f commit 07b1e9b
Show file tree
Hide file tree
Showing 27 changed files with 331 additions and 313 deletions.
15 changes: 13 additions & 2 deletions src/clowder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from clowder.error import ClowderExit
from clowder.util.formatting import ERROR

from clowder.git.util import existing_git_repository

# Set up logging #

Expand Down Expand Up @@ -47,11 +47,22 @@ def LOG_DEBUG(message: str, exception: Optional[Exception] = None): # noqa
CLOWDER_YAML: Optional[Path] = None


def existing_clowder_repo(directory: Path) -> bool:
"""Check if directory is a clowder repository
:param Path directory: Path to check
:return: True, if it looks like it's a clowder repository
:rtype: bool
"""

return directory.is_dir() and existing_git_repository(directory)


# Walk up directory tree to find possible clowder repo (.clowder directory) and set global variable
path = Path.cwd()
while str(path) != path.root:
clowder_repo_dir = path / '.clowder'
if clowder_repo_dir.is_dir():
if existing_clowder_repo(clowder_repo_dir):
CLOWDER_DIR = path
CLOWDER_REPO_DIR = clowder_repo_dir
break
Expand Down
2 changes: 2 additions & 0 deletions src/clowder/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
from clowder.cli.stash import add_stash_parser
from clowder.cli.status import add_status_parser
from clowder.cli.yaml import add_yaml_parser

from clowder.cli.util import add_parser_arguments
7 changes: 3 additions & 4 deletions src/clowder/cli/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
import clowder.util.formatting as fmt
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.util.clowder_utils import (
add_parser_arguments,
filter_projects
)
from clowder.model.util import filter_projects
from clowder.util.decorators import (
print_clowder_name,
print_clowder_repo_status,
valid_clowder_yaml_required
)

from .util import add_parser_arguments


def add_branch_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
7 changes: 3 additions & 4 deletions src/clowder/cli/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
import clowder.util.formatting as fmt
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.util.clowder_utils import (
add_parser_arguments,
filter_projects
)
from clowder.model.util import filter_projects
from clowder.util.decorators import (
print_clowder_name,
print_clowder_repo_status,
valid_clowder_yaml_required
)

from .util import add_parser_arguments


def add_checkout_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
7 changes: 3 additions & 4 deletions src/clowder/cli/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.model import Project
from clowder.util.clowder_utils import (
add_parser_arguments,
filter_projects
)
from clowder.model.util import filter_projects
from clowder.util.decorators import (
print_clowder_name,
print_clowder_repo_status,
valid_clowder_yaml_required
)

from .util import add_parser_arguments


def add_clean_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
3 changes: 2 additions & 1 deletion src/clowder/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
valid_clowder_yaml_required
)
from clowder.config import Config, ClowderConfigType
from clowder.util.clowder_utils import add_parser_arguments

from .util import add_parser_arguments


def add_config_parser(subparsers: argparse._SubParsersAction) -> None: # noqa
Expand Down
7 changes: 3 additions & 4 deletions src/clowder/cli/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
import clowder.util.formatting as fmt
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.util.clowder_utils import (
add_parser_arguments,
filter_projects
)
from clowder.model.util import filter_projects
from clowder.util.decorators import (
print_clowder_name,
print_clowder_repo_status,
valid_clowder_yaml_required
)

from .util import add_parser_arguments


def add_diff_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
7 changes: 3 additions & 4 deletions src/clowder/cli/forall.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
import clowder.util.formatting as fmt
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.util.clowder_utils import (
add_parser_arguments,
filter_projects
)
from clowder.model.util import filter_projects
from clowder.util.decorators import (
print_clowder_name,
print_clowder_repo_status,
valid_clowder_yaml_required
)
from clowder.util.parallel import forall_parallel

from .util import add_parser_arguments


def add_forall_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
5 changes: 3 additions & 2 deletions src/clowder/cli/herd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import clowder.util.formatting as fmt
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.util.clowder_utils import (
add_parser_arguments,
from clowder.model.util import (
filter_projects,
validate_project_statuses
)
Expand All @@ -24,6 +23,8 @@
)
from clowder.util.parallel import herd_parallel

from .util import add_parser_arguments


def add_herd_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
8 changes: 4 additions & 4 deletions src/clowder/cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from termcolor import colored, cprint

import clowder.clowder_repo as clowder_repo
from clowder import CURRENT_DIR
from clowder import CURRENT_DIR, existing_clowder_repo
from clowder.error import ClowderExit
from clowder.git.util import existing_git_repository
from clowder.util.clowder_utils import add_parser_arguments
from clowder.util.connectivity import network_connection_required

from .util import add_parser_arguments


def add_init_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand All @@ -37,7 +37,7 @@ def init(args) -> None:
"""

clowder_repo_dir = CURRENT_DIR / '.clowder'
if existing_git_repository(clowder_repo_dir):
if existing_clowder_repo(clowder_repo_dir):
cprint('Clowder already initialized in this directory\n', 'red')
raise ClowderExit(1)

Expand Down
7 changes: 4 additions & 3 deletions src/clowder/cli/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

import clowder.util.formatting as fmt
from clowder import CLOWDER_DIR
from clowder.util.clowder_utils import (
add_parser_arguments,
get_saved_version_names,
from clowder.clowder_repo import get_saved_version_names
from clowder.util.yaml import (
link_clowder_yaml_default,
link_clowder_yaml_version
)
Expand All @@ -21,6 +20,8 @@
print_clowder_repo_status
)

from .util import add_parser_arguments


def add_link_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
5 changes: 3 additions & 2 deletions src/clowder/cli/prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
from clowder.config import Config
from clowder.error import ClowderError
from clowder.model import Project
from clowder.util.clowder_utils import (
add_parser_arguments,
from clowder.model.util import (
existing_branch_projects,
filter_projects,
validate_project_statuses
Expand All @@ -28,6 +27,8 @@
valid_clowder_yaml_required
)

from .util import add_parser_arguments


def add_prune_parser(subparsers: argparse._SubParsersAction): # noqa

Expand Down
3 changes: 2 additions & 1 deletion src/clowder/cli/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import argparse

import clowder.clowder_repo as clowder_repo
from clowder.util.clowder_utils import add_parser_arguments
from clowder.util.connectivity import network_connection_required
from clowder.util.decorators import (
print_clowder_name,
Expand All @@ -17,6 +16,8 @@
print_clowder_repo_status_fetch
)

from .util import add_parser_arguments


def add_repo_parser(subparsers: argparse._SubParsersAction) -> None: # noqa
"""Clowder repo command controller"""
Expand Down
5 changes: 3 additions & 2 deletions src/clowder/cli/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import clowder.util.formatting as fmt
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.util.clowder_utils import (
add_parser_arguments,
from clowder.model.util import (
filter_projects,
validate_project_statuses
)
Expand All @@ -25,6 +24,8 @@
)
from clowder.util.parallel import reset_parallel

from .util import add_parser_arguments


def add_reset_parser(subparsers: argparse._SubParsersAction): # noqa

Expand Down
7 changes: 3 additions & 4 deletions src/clowder/cli/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
from clowder import CLOWDER_REPO_DIR
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.error import ClowderExit
from clowder.util.clowder_utils import (
add_parser_arguments,
validate_project_statuses
)
from clowder.model.util import validate_project_statuses
from clowder.util.decorators import (
print_clowder_name,
valid_clowder_yaml_required
)
from clowder.util.file_system import make_dir
from clowder.util.yaml import save_yaml

from .util import add_parser_arguments


def add_save_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
11 changes: 6 additions & 5 deletions src/clowder/cli/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
import clowder.util.formatting as fmt
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.model.util import (
filter_projects,
validate_project_statuses
)
from clowder.util.connectivity import network_connection_required
from clowder.util.decorators import (
print_clowder_name,
print_clowder_repo_status,
valid_clowder_yaml_required
)
from clowder.util.clowder_utils import (
add_parser_arguments,
filter_projects,
validate_project_statuses
)

from .util import add_parser_arguments


def add_start_parser(subparsers: argparse._SubParsersAction) -> None: # noqa
Expand Down
7 changes: 3 additions & 4 deletions src/clowder/cli/stash.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
import clowder.util.formatting as fmt
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.util.clowder_utils import (
add_parser_arguments,
filter_projects
)
from clowder.model.util import filter_projects
from clowder.util.decorators import (
print_clowder_name,
print_clowder_repo_status,
valid_clowder_yaml_required
)

from .util import add_parser_arguments


def add_stash_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
7 changes: 3 additions & 4 deletions src/clowder/cli/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.config import Config
from clowder.model import Project
from clowder.util.clowder_utils import (
add_parser_arguments,
filter_projects
)
from clowder.model.util import filter_projects
from clowder.util.connectivity import network_connection_required
from clowder.util.decorators import (
print_clowder_name,
valid_clowder_yaml_required
)

from .util import add_parser_arguments


def add_status_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down
25 changes: 25 additions & 0 deletions src/clowder/cli/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""Clowder model utilities
.. codeauthor:: Joe Decapo <joe@polka.cat>
"""

import argparse

from typing import List, Tuple, Union


Parser = Union[argparse.ArgumentParser, argparse._MutuallyExclusiveGroup, argparse._ArgumentGroup] # noqa
Arguments = List[Tuple[list, dict]]


def add_parser_arguments(parser: Parser, arguments: Arguments) -> None:
"""Add arguments to parser
:param Parser parser: Parser to add arguments to
:param Arguments arguments: Arguments to add to parser
"""

for argument in arguments:
parser.add_argument(*argument[0], **argument[1])
3 changes: 2 additions & 1 deletion src/clowder/cli/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

import clowder.util.formatting as fmt
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.util.clowder_utils import add_parser_arguments
from clowder.util.decorators import (
print_clowder_name,
print_clowder_repo_status,
valid_clowder_yaml_required
)
from clowder.util.yaml import print_yaml

from .util import add_parser_arguments


def add_yaml_parser(subparsers: argparse._SubParsersAction) -> None: # noqa

Expand Down

0 comments on commit 07b1e9b

Please sign in to comment.