def __init__(self, gl: gitlab.Gitlab, what: 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_name = self.cls.__name__ self.what = what.replace("-", "_") self.action = action.lower() self.gl = gl self.args = args self.mgr_cls: Union[Type[gitlab.mixins.CreateMixin], Type[gitlab.mixins.DeleteMixin], Type[gitlab.mixins.GetMixin], Type[gitlab.mixins.GetWithoutIdMixin], Type[gitlab.mixins.ListMixin], Type[gitlab.mixins.UpdateMixin], ] = getattr( gitlab.v4.objects, self.cls.__name__ + "Manager") # We could do something smart, like splitting the manager name to find # parents, build the chain of managers to get to the final object. # Instead we do something ugly and efficient: interpolate variables in # the class _path attribute, and replace the value with the result. if TYPE_CHECKING: assert self.mgr_cls._path is not None self.mgr_cls._path = self.mgr_cls._path % self.args self.mgr = self.mgr_cls(gl) if self.mgr_cls._types: for attr_name, type_cls in self.mgr_cls._types.items(): if attr_name in self.args.keys(): obj = type_cls() obj.set_from_cli(self.args[attr_name]) self.args[attr_name] = obj.get()
def run(gl, what, action, args, verbose, *fargs, **kwargs): try: cls = gitlab.v3.objects.__dict__[cli.what_to_cls(what)] except ImportError: cli.die("Unknown object: %s" % what) g_cli = GitlabCLI() method = None what = what.replace('-', '_') action = action.lower().replace('-', '') for test in ["do_%s_%s" % (what, action), "do_%s" % action]: if hasattr(g_cli, test): method = test break if method is None: sys.stderr.write("Don't know how to deal with this!\n") sys.exit(1) ret_val = getattr(g_cli, method)(cls, gl, what, args) if isinstance(ret_val, list): for o in ret_val: if isinstance(o, gitlab.GitlabObject): o.display(verbose) print("") else: print(o) elif isinstance(ret_val, gitlab.base.GitlabObject): ret_val.display(verbose) elif isinstance(ret_val, six.string_types): print(ret_val)
def test_what_to_cls(what, expected_class): def _namespace(): pass ExpectedClass = type(expected_class, (), {}) _namespace.__dict__[expected_class] = ExpectedClass assert cli.what_to_cls(what, _namespace) == ExpectedClass
def __init__(self, gl, what, action, args): self.cls_name = cli.what_to_cls(what) self.cls = gitlab.v4.objects.__dict__[self.cls_name] self.what = what.replace('-', '_') self.action = action.lower() self.gl = gl self.args = args self.mgr_cls = getattr(gitlab.v4.objects, self.cls.__name__ + 'Manager') # We could do something smart, like splitting the manager name to find # parents, build the chain of managers to get to the final object. # Instead we do something ugly and efficient: interpolate variables in # the class _path attribute, and replace the value with the result. self.mgr_cls._path = self.mgr_cls._path % self.args self.mgr = self.mgr_cls(gl)
def __init__(self, gl, what, action, args): self.cls_name = cli.what_to_cls(what) self.cls = gitlab.v4.objects.__dict__[self.cls_name] self.what = what.replace('-', '_') self.action = action.lower() self.gl = gl self.args = args self.mgr_cls = getattr(gitlab.v4.objects, self.cls.__name__ + 'Manager') # We could do something smart, like splitting the manager name to find # parents, build the chain of managers to get to the final object. # Instead we do something ugly and efficient: interpolate variables in # the class _path attribute, and replace the value with the result. self.mgr_cls._path = self.mgr_cls._path % self.args self.mgr = self.mgr_cls(gl)
def __init__(self, gl, what, action, args): self.cls_name = cli.what_to_cls(what) self.cls = gitlab.v4.objects.__dict__[self.cls_name] self.what = what.replace("-", "_") self.action = action.lower() self.gl = gl self.args = args self.mgr_cls = getattr(gitlab.v4.objects, self.cls.__name__ + "Manager") # We could do something smart, like splitting the manager name to find # parents, build the chain of managers to get to the final object. # Instead we do something ugly and efficient: interpolate variables in # the class _path attribute, and replace the value with the result. self.mgr_cls._path = self.mgr_cls._path % self.args self.mgr = self.mgr_cls(gl) if self.mgr_cls._types: for attr_name, type_cls in self.mgr_cls._types.items(): if attr_name in self.args.keys(): obj = type_cls() obj.set_from_cli(self.args[attr_name]) self.args[attr_name] = obj.get()
def __init__(self, gl, what, action, args): self.cls_name = cli.what_to_cls(what) self.cls = gitlab.v4.objects.__dict__[self.cls_name] self.what = what.replace("-", "_") self.action = action.lower() self.gl = gl self.args = args self.mgr_cls = getattr(gitlab.v4.objects, self.cls.__name__ + "Manager") # We could do something smart, like splitting the manager name to find # parents, build the chain of managers to get to the final object. # Instead we do something ugly and efficient: interpolate variables in # the class _path attribute, and replace the value with the result. self.mgr_cls._path = self.mgr_cls._path % self.args self.mgr = self.mgr_cls(gl) types = getattr(self.mgr_cls, "_types", {}) if types: for attr_name, type_cls in types.items(): if attr_name in self.args.keys(): obj = type_cls() obj.set_from_cli(self.args[attr_name]) self.args[attr_name] = obj.get()
def run(gl, what, action, args, verbose, *fargs, **kwargs): try: cls = gitlab.v3.objects.__dict__[cli.what_to_cls(what)] except ImportError: cli.die("Unknown object: %s" % what) g_cli = GitlabCLI() method = None what = what.replace('-', '_') action = action.lower().replace('-', '') for test in ["do_%s_%s" % (what, action), "do_%s" % action]: if hasattr(g_cli, test): method = test break if method is None: sys.stderr.write("Don't know how to deal with this!\n") sys.exit(1) ret_val = getattr(g_cli, method)(cls, gl, what, args) if isinstance(ret_val, list): for o in ret_val: if isinstance(o, gitlab.GitlabObject): o.display(verbose) print("") else: print(o) elif isinstance(ret_val, dict): for k, v in six.iteritems(ret_val): print("{} = {}".format(k, v)) elif isinstance(ret_val, gitlab.base.GitlabObject): ret_val.display(verbose) elif isinstance(ret_val, six.string_types): print(ret_val)
def test_what_to_cls(self): self.assertEqual("Foo", cli.what_to_cls("foo")) self.assertEqual("FooBar", cli.what_to_cls("foo-bar"))
def test_what_to_cls(self): self.assertEqual("Foo", cli.what_to_cls("foo")) self.assertEqual("FooBar", cli.what_to_cls("foo-bar"))
def test_what_to_cls(): assert "Foo" == cli.what_to_cls("foo") assert "FooBar" == cli.what_to_cls("foo-bar")