<commit_before>"""
Wrapper for _liblegato_py.so C Extension.
"""

from _liblegato_py import ffi, lib
from enum import IntEnum, Enum
import inspect
import sys
import types
from functools import wraps

__copyright__ = 'Copyright (C) Sierra Wireless Inc.'

def convert_to_py(obj):
    """
    Convert a ffi CData object to a python representation
    """
    if not isinstance(obj, ffi.CData):
        return obj
    type=ffi.typeof(obj)
    if type.kind == 'primitive':
        if 'char' in type.cname: # includes wchar, etc
            return ffi.string(obj)
        return int(obj)
    elif type.kind == 'pointer':
        if type.item.cname == 'char':
            return ffi.string(obj)
        else:
            # if it's not a char*, then it's an opaque reference that we shouldn't touch
            return obj
    elif type.kind == 'array':
            return [ convert_to_py(obj[i]) for i in range(type.length) ]
    else:
        print("convert_to_py() can't convert: ")
        print(type.kind)
        print(type)
        print(type.cname)
        return obj

def convert_to_c(obj):
    """
    Convert a python object to ffi CData
    """
    if isinstance(obj, ffi.CData):
        return obj
    if obj is None:
        return ffi.NULL
    if isinstance(obj, basestring):
        return ffi.new("char[]", obj)
    if obj is list or obj is tuple:
        return [ convert_to_c[x] for x in obj ]
    if isinstance(obj, Enum):
        return obj.value
    return obj

# -aliases to hide ffi methods-
# I have decided not to include this in the automatic conversion function,
# because C pointers MUST be kept alive, or else the data they point to
# is freed.
# For example, if a dict was implicitly converted to a pointer in something like
# le_foo_SetContext({'a':1})
# it will crash when deferencing later in a callback, because the pointer created
# would only be valid for the SetContext function call. As soon as SetContext
# returns, the C handle is freed.
# For more info see https://cffi.readthedocs.io/en/latest/using.html#working<commit_msg>[python] Impossible to use C char type<commit_after>"""
Wrapper for _liblegato_py.so C Extension.
"""

from _liblegato_py import ffi, lib
from enum import IntEnum, Enum
import inspect
import sys
import types
from functools import wraps

__copyright__ = 'Copyright (C) Sierra Wireless Inc.'

def convert_to_py(obj):
    """
    Convert a ffi CData object to a python representation
    """
    if not isinstance(obj, ffi.CData):
        return obj
    type=ffi.typeof(obj)
    if type.kind == 'primitive':
        if 'char' in type.cname: # includes wchar, etc
            return ffi.string(obj)
        return int(obj)
    elif type.kind == 'pointer':
        if type.item.cname == 'char':
            return ffi.string(obj)
        else:
            # if it's not a char*, then it's an opaque reference that we shouldn't touch
            return obj
    elif type.kind == 'array':
            return [ convert_to_py(obj[i]) for i in range(type.length) ]
    else:
        print("convert_to_py() can't convert: ")
        print(type.kind)
        print(type)
        print(type.cname)
        return obj

def convert_to_c(obj):
    """
    Convert a python object to ffi CData
    """
    if isinstance(obj, ffi.CData):
        return obj
    if obj is None:
        return ffi.NULL
    if obj is list or obj is tuple:
        return [ convert_to_c[x] for x in obj ]
    if isinstance(obj, Enum):
        return obj.value
    return obj

# -aliases to hide ffi methods-
# I have decided not to include this in the automatic conversion function,
# because C pointers MUST be kept alive, or else the data they point to
# is freed.
# For example, if a dict was implicitly converted to a pointer in something like
# le_foo_SetContext({'a':1})
# it will crash when deferencing later in a callback, because the pointer created
# would only be valid for the SetContext function call. As soon as SetContext
# returns, the C handle is freed.
# For more info see https://cffi.readthedocs.io/en/latest/using.html#working
{'fraction_non_alphanumeric': 0.08802557167445291, 'fraction_numerical': 0.0004917629702483403, 'mean_word_length': 2.7049180327868854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '217419', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1206, 'n_words': 477}