<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
from __future__ import unicode_literals
import re
import inspect
from os.path import abspath, dirname, join

path = dirname(abspath(__file__))
root = abspath(join(path, "..", ".."))

from thrift.Thrift import TType
from thriftgen.pyload import ttypes
from thriftgen.pyload import Pyload

from pyload import info

type_map = {
    TType.BOOL: 'bool',
    TType.DOUBLE: 'float',
    TType.I16: 'int',
    TType.I32: 'int',
    TType.I64: 'int',
    TType.STRING: 'str',
    TType.MAP: 'dict',
    TType.LIST: 'list',
    TType.SET: 'set',
    TType.VOID: 'None',
    TType.STRUCT: 'BaseObject',
    TType.UTF8: 'unicode',
}

def get_spec(spec, optional=False):
    """ analyze the generated spec file and writes information into file """
    if spec[1] == TType.STRUCT:
        return spec[3][0].__name__
    elif spec[1]  == TType.LIST:
        if spec[3][0] == TType.STRUCT:
            ttype = spec[3][1][0].__name__
        else:
            ttype = type_map[spec[3][0]]
        return "(list, {})".format(ttype)
    elif spec[1] == TType.MAP:
        if spec[3][2] == TType.STRUCT:
            ttype = spec[3][3][0].__name__
        else:
            ttype = type_map[spec[3][2]]

        return "(dict, {}, {})".format(type_map[spec[3][0]], ttype)
    else:
        return type_map[spec[1]]

optional_re = "{:d}: +optional +[a-z0-9<>_-]+ +{}"

def main():

    enums = []
    classes = []
    tf = open(join(path, "pyload.thrift"), "rb").read()

    print("generating apitypes.py")

    for name in dir(ttypes):
        klass = getattr(ttypes, name)

        if name in ("TBase", "TExceptionBase") or name.startswith("_") or not (issubclass(klass, ttypes.TBase) or issubclass(klass, ttypes.TExceptionBase)):
            continue

        if hasattr(klass, "thrift_spec"):
            classes.append(klass)
        else:
            enums.append(klass)


    f = open(join(path, "apitypes.py"), "wb")
    f.write(
        """# -*- coding: utf-8 -*-
# Autogenerated by pyload
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING


class BaseObject(object):
\t__version__ = {0}
\t__slots__ = []

\tdef __str__(self):
\t\treturn "<{} {}>".format(self.__class__.__name__, ", ".join("{}={}".format(k, getattr(self,k)) for k in self.__slots__))


class ExceptionObject(Exception):
\t__version__ = {0}
\t__slots__ = []

""".format(info().version))

    dev = open(join(path, "apitypes_debug.py"), "wb")
    dev.write("""# -*- coding: utf-8 -*-
# Autogenerated by pyload
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n
from apitypes import *\n
""")

    dev.write("enums = [\n")

    ## generate enums
    for enum in enums:
        name = enum.__name__<commit_msg>unicode -> str in create_apitypes.py<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
from __future__ import unicode_literals
import re
import inspect
from os.path import abspath, dirname, join

path = dirname(abspath(__file__))
root = abspath(join(path, "..", ".."))

from thrift.Thrift import TType
from thriftgen.pyload import ttypes
from thriftgen.pyload import Pyload

from pyload import info

type_map = {
    TType.BOOL: 'bool',
    TType.DOUBLE: 'float',
    TType.I16: 'int',
    TType.I32: 'int',
    TType.I64: 'int',
    TType.STRING: 'str',
    TType.MAP: 'dict',
    TType.LIST: 'list',
    TType.SET: 'set',
    TType.VOID: 'None',
    TType.STRUCT: 'BaseObject',
    TType.UTF8: 'str',
}

def get_spec(spec, optional=False):
    """ analyze the generated spec file and writes information into file """
    if spec[1] == TType.STRUCT:
        return spec[3][0].__name__
    elif spec[1]  == TType.LIST:
        if spec[3][0] == TType.STRUCT:
            ttype = spec[3][1][0].__name__
        else:
            ttype = type_map[spec[3][0]]
        return "(list, {})".format(ttype)
    elif spec[1] == TType.MAP:
        if spec[3][2] == TType.STRUCT:
            ttype = spec[3][3][0].__name__
        else:
            ttype = type_map[spec[3][2]]

        return "(dict, {}, {})".format(type_map[spec[3][0]], ttype)
    else:
        return type_map[spec[1]]

optional_re = "{:d}: +optional +[a-z0-9<>_-]+ +{}"

def main():

    enums = []
    classes = []
    tf = open(join(path, "pyload.thrift"), "rb").read()

    print("generating apitypes.py")

    for name in dir(ttypes):
        klass = getattr(ttypes, name)

        if name in ("TBase", "TExceptionBase") or name.startswith("_") or not (issubclass(klass, ttypes.TBase) or issubclass(klass, ttypes.TExceptionBase)):
            continue

        if hasattr(klass, "thrift_spec"):
            classes.append(klass)
        else:
            enums.append(klass)


    f = open(join(path, "apitypes.py"), "wb")
    f.write(
        """# -*- coding: utf-8 -*-
# Autogenerated by pyload
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING


class BaseObject(object):
\t__version__ = {0}
\t__slots__ = []

\tdef __str__(self):
\t\treturn "<{} {}>".format(self.__class__.__name__, ", ".join("{}={}".format(k, getattr(self, k)) for k in self.__slots__))


class ExceptionObject(Exception):
\t__version__ = {0}
\t__slots__ = []

""".format(info().version))

    dev = open(join(path, "apitypes_debug.py"), "wb")
    dev.write("""# -*- coding: utf-8 -*-
# Autogenerated by pyload
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n
from apitypes import *\n
""")

    dev.write("enums = [\n")

    ## generate enums
    for enum in enums:
        name = enum.__name__
{'fraction_non_alphanumeric': 0.1659536541889483, 'fraction_numerical': 0.012834224598930482, 'mean_word_length': 2.9019471488178024, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, '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': '5264662', 'n_tokens_mistral': 2039, 'n_tokens_neox': 2024, 'n_words': 487}