Refactor describe func in model

This commit is contained in:
sfigato 2020-08-29 20:14:20 +02:00
parent b979002f78
commit 1b97d6d7ca
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F

View File

@ -101,6 +101,9 @@ class Entry(object):
def __repr__(self): def __repr__(self):
return f"<{call_if_callable(self, 'name')} {self.dn}>" return f"<{call_if_callable(self, 'name')} {self.dn}>"
def __dict__(self):
return self._dict
def __aiter__(self): def __aiter__(self):
return self return self
@ -120,9 +123,23 @@ class Entry(object):
def dn(self): def dn(self):
return "{},{}".format(inheritance(self, "qualified_name", Entry), self.base_dn) return "{},{}".format(inheritance(self, "qualified_name", Entry), self.base_dn)
@property
def attributes(self):
raise NotImplemented()
async def describe(self): async def describe(self):
async with self.client.connect(is_async=True) as conn: async with self.client.connect(is_async=True) as conn:
# This returns a list of dicts. It should always contain only one item:
# the one we are interested.
res = await conn.search(self.dn, 0) res = await conn.search(self.dn, 0)
if len(res) == 0:
return
elif len(res) > 1:
raise PhiUnexpectedRuntimeValue(
"return value should be no more than one", res
)
res = res[0]
res.update({"dn": self.dn, "name": self.name})
return res return res
@ -143,6 +160,7 @@ class Hackers(Entry):
""" """
kind = "ou" kind = "ou"
_name = "Hackers"
_instances: T.Dict[str, Entry] = dict() _instances: T.Dict[str, Entry] = dict()
def __new__(cls, client, *args, **kwargs): def __new__(cls, client, *args, **kwargs):
@ -170,6 +188,10 @@ class Hackers(Entry):
res = await self.get_by_attr("uid", uid) res = await self.get_by_attr("uid", uid)
return res[0] return res[0]
@property
def attributes(self):
return {"kind": self.kind}
class Robots(Entry): class Robots(Entry):
""" """
@ -338,9 +360,10 @@ class Service(Robots):
def __new__(cls, client, name, *args, **kwargs): def __new__(cls, client, name, *args, **kwargs):
return singletonize(cls, f"{name}-{id(client)}") return singletonize(cls, f"{name}-{id(client)}")
def __init__(self, name, *args, **kwargs): def __init__(self, client, name, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(client, *args, **kwargs)
self._name = name self._name = name
self._entry = LDAPEntry(self.dn)
def __repr__(self): def __repr__(self):
return f"<{get_class(self).__name__}({self.name}) {self.dn}>" return f"<{get_class(self).__name__}({self.name}) {self.dn}>"
@ -397,9 +420,10 @@ class Group(Congregations):
def __new__(cls, client, name, *args, **kwargs): def __new__(cls, client, name, *args, **kwargs):
return singletonize(cls, f"{name}-{id(client)}") return singletonize(cls, f"{name}-{id(client)}")
def __init__(self, name, *args, **kwargs): def __init__(self, client, name, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(client, *args, **kwargs)
self._name = name self._name = name
self._entry = LDAPEntry(self.dn)
def __repr__(self): def __repr__(self):
return f"<{get_class(self).__name__}({self.name}) {self.dn}>" return f"<{get_class(self).__name__}({self.name}) {self.dn}>"