Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: renamewhat togitlab_resource#2053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nejch merged 2 commits intomainfromjlvillal/resource
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletionsgitlab/cli.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -91,14 +91,16 @@ def die(msg: str, e: Optional[Exception] = None) -> None:
sys.exit(1)


def what_to_cls(what: str, namespace: ModuleType) -> Type[RESTObject]:
def gitlab_resource_to_cls(
gitlab_resource: str, namespace: ModuleType
) -> Type[RESTObject]:
classes = CaseInsensitiveDict(namespace.__dict__)
lowercase_class =what.replace("-", "")
lowercase_class =gitlab_resource.replace("-", "")

return classes[lowercase_class]


defcls_to_what(cls: RESTObject) -> str:
defcls_to_gitlab_resource(cls: RESTObject) -> str:
dasherized_uppercase = camel_upperlower_regex.sub(r"\1-\2", cls.__name__)
dasherized_lowercase = camel_lowerupper_regex.sub(r"\1-\2", dasherized_uppercase)
return dasherized_lowercase.lower()
Expand DownExpand Up@@ -322,7 +324,7 @@ def main() -> None:
fields = [x.strip() for x in args.fields.split(",")]
debug = args.debug
action = args.whaction
what = args.what
gitlab_resource = args.gitlab_resource

args_dict = vars(args)
# Remove CLI behavior-related args
Expand All@@ -331,7 +333,7 @@ def main() -> None:
"config_file",
"verbose",
"debug",
"what",
"gitlab_resource",
"whaction",
"version",
"output",
Expand DownExpand Up@@ -359,4 +361,4 @@ def main() -> None:
if debug:
gl.enable_debug()

gitlab.v4.cli.run(gl,what, action, args_dict, verbose, output, fields)
gitlab.v4.cli.run(gl,gitlab_resource, action, args_dict, verbose, output, fields)
22 changes: 11 additions & 11 deletionsgitlab/v4/cli.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,13 +29,13 @@

class GitlabCLI:
def __init__(
self, gl: gitlab.Gitlab,what: str, action: str, args: Dict[str, str]
self, gl: gitlab.Gitlab,gitlab_resource: str, action: str, args: Dict[str, str]
) -> None:
self.cls: Type[gitlab.base.RESTObject] = cli.what_to_cls(
what, namespace=gitlab.v4.objects
self.cls: Type[gitlab.base.RESTObject] = cli.gitlab_resource_to_cls(
gitlab_resource, namespace=gitlab.v4.objects
)
self.cls_name = self.cls.__name__
self.what =what.replace("-", "_")
self.gitlab_resource =gitlab_resource.replace("-", "_")
self.action = action.lower()
self.gl = gl
self.args = args
Expand DownExpand Up@@ -79,9 +79,9 @@ def _process_from_parent_attrs(self) -> None:
# If we don't delete it then it will be added to the URL as a query-string
del self.args[key]

def__call__(self) -> Any:
defrun(self) -> Any:
# Check for a method that matches object + action
method = f"do_{self.what}_{self.action}"
method = f"do_{self.gitlab_resource}_{self.action}"
if hasattr(self, method):
return getattr(self, method)()

Expand DownExpand Up@@ -333,7 +333,7 @@ def _populate_sub_parser_by_class(

def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
subparsers = parser.add_subparsers(
title="object", dest="what", help="Object to manipulate."
title="object", dest="gitlab_resource", help="Object to manipulate."
)
subparsers.required = True

Expand All@@ -347,7 +347,7 @@ def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
classes.add(cls._obj_cls)

for cls in sorted(classes, key=operator.attrgetter("__name__")):
arg_name = cli.cls_to_what(cls)
arg_name = cli.cls_to_gitlab_resource(cls)
object_group = subparsers.add_parser(arg_name)

object_subparsers = object_group.add_subparsers(
Expand DownExpand Up@@ -497,15 +497,15 @@ def display_list(

def run(
gl: gitlab.Gitlab,
what: str,
gitlab_resource: str,
action: str,
args: Dict[str, Any],
verbose: bool,
output: str,
fields: List[str],
) -> None:
g_cli = GitlabCLI(gl=gl,what=what, action=action, args=args)
data = g_cli()
g_cli = GitlabCLI(gl=gl,gitlab_resource=gitlab_resource, action=action, args=args)
data = g_cli.run()

printer: Union[JSONPrinter, LegacyPrinter, YAMLPrinter] = PRINTERS[output]()

Expand Down
14 changes: 7 additions & 7 deletionstests/unit/test_cli.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,7 +29,7 @@


@pytest.mark.parametrize(
"what,expected_class",
"gitlab_resource,expected_class",
[
("class", "Class"),
("test-class", "TestClass"),
Expand All@@ -39,18 +39,18 @@
("ldap-group", "LDAPGroup"),
],
)
deftest_what_to_cls(what, expected_class):
deftest_gitlab_resource_to_cls(gitlab_resource, expected_class):
def _namespace():
pass

ExpectedClass = type(expected_class, (), {})
_namespace.__dict__[expected_class] = ExpectedClass

assert cli.what_to_cls(what, _namespace) == ExpectedClass
assert cli.gitlab_resource_to_cls(gitlab_resource, _namespace) == ExpectedClass


@pytest.mark.parametrize(
"class_name,expected_what",
"class_name,expected_gitlab_resource",
[
("Class", "class"),
("TestClass", "test-class"),
Expand All@@ -61,10 +61,10 @@ def _namespace():
("LDAPGroup", "ldap-group"),
],
)
deftest_cls_to_what(class_name,expected_what):
deftest_cls_to_gitlab_resource(class_name,expected_gitlab_resource):
TestClass = type(class_name, (), {})

assert cli.cls_to_what(TestClass) ==expected_what
assert cli.cls_to_gitlab_resource(TestClass) ==expected_gitlab_resource


@pytest.mark.parametrize(
Expand DownExpand Up@@ -125,7 +125,7 @@ def test_base_parser():
def test_v4_parse_args():
parser = cli._get_parser()
args = parser.parse_args(["project", "list"])
assert args.what == "project"
assert args.gitlab_resource == "project"
assert args.whaction == "list"


Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp