1818# distutils: sources = grnpy/grnpy_ctx.c
1919
2020from grnpy.grn_ctx cimport grn_ctx
21+ from grnpy.grn_encoding cimport grn_encoding
2122from grnpy.grn_error cimport grn_rc
2223from grnpy.grn_id cimport grn_id
2324from grnpy.grn_obj cimport grn_obj
2425
26+ cimport grnpy.grn_encoding
27+
2528from grnpy.object cimport build_object
2629
2730from .error import Error
@@ -39,6 +42,7 @@ cdef extern from "groonga.h":
3942cdef extern from " grnpy_ctx.h" :
4043 grn_rc grnpy_ctx_get_rc(grn_ctx * ctx)
4144 const char * grnpy_ctx_get_error_message(grn_ctx * ctx)
45+ grn_encoding grnpy_ctx_get_encoding(grn_ctx * ctx)
4246
4347cdef class Context:
4448 def __cinit__ (self , flags = 0 ):
@@ -56,6 +60,23 @@ cdef class Context:
5660 cdef grn_ctx * unwrap(self ):
5761 return self ._ctx
5862
63+ cdef encoding_name(self ):
64+ # https://docs.python.org/3/library/codecs.html
65+ cdef grn_encoding encoding = grnpy_ctx_get_encoding(self ._ctx)
66+ if encoding == grnpy.grn_encoding.UTF8:
67+ return " utf_8"
68+ elif encoding == grnpy.grn_encoding.EUC_JP:
69+ return " euc_jp"
70+ elif encoding == grnpy.grn_encoding.SJIS:
71+ # CP932 has better coverage of special characters.
72+ return " cp932"
73+ elif encoding == grnpy.grn_encoding.LATIN1:
74+ return " latin_1"
75+ elif encoding == grnpy.grn_encoding.KOI8R:
76+ return " koi8_r"
77+ else :
78+ raise NotImplementedError (f" unsupported encoding: <{encoding}>" )
79+
5980 def rc (self ):
6081 return grnpy_ctx_get_rc(self ._ctx)
6182
0 commit comments