diff --git a/lib/extras.py b/lib/extras.py index e14e1c4..bb25015 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -846,6 +846,15 @@ class CompositeCaster(object): else: self.array_typecaster = None + def make(self, attrs): + return self._ctor(*attrs) + + def register(self, where=None): + _ext.register_type(self.typecaster, where) + + if self.array_typecaster is not None: + _ext.register_type(self.array_typecaster, where) + def parse(self, s, curs): if s is None: return None @@ -858,7 +867,8 @@ class CompositeCaster(object): attrs = [ curs.cast(oid, token) for oid, token in zip(self.atttypes, tokens) ] - return self._ctor(*attrs) + + return self.make(attrs) _re_tokenize = regex.compile(r""" \(? ([,)]) # an empty token, representing NULL @@ -894,7 +904,7 @@ class CompositeCaster(object): self._ctor = self.type @classmethod - def _from_db(self, name, conn_or_curs): + def from_db(self, name, conn_or_curs): """Return a `CompositeCaster` instance for the type *name*. Raise `ProgrammingError` if the type is not found. @@ -940,7 +950,7 @@ ORDER BY attnum; array_oid = recs[0][1] type_attrs = [ (r[2], r[3]) for r in recs ] - return CompositeCaster(tname, type_oid, type_attrs, + return self(tname, type_oid, type_attrs, array_oid=array_oid) def register_composite(name, conn_or_curs, globally=False): @@ -960,12 +970,8 @@ def register_composite(name, conn_or_curs, globally=False): added support for array of composite types """ - caster = CompositeCaster._from_db(name, conn_or_curs) - _ext.register_type(caster.typecaster, not globally and conn_or_curs or None) - - if caster.array_typecaster is not None: - _ext.register_type(caster.array_typecaster, not globally and conn_or_curs or None) - + caster = CompositeCaster.from_db(name, conn_or_curs) + caster.register(not globally and conn_or_curs or None) return caster