<commit_before># -*- coding: utf-8 -*-
#==============================================================================
# Copyright:    Hybrid Labs
# Licence:      Private
#==============================================================================

import ast
import inspect
import json
import sys
import types

from .. import client, model


class JSCode(object):
    def __init__(self, code):
        pass


class YieldPoint(object):
    def __init__(self):
        self.count = 0

    def create(self):
        self.count += 1
        return self.count


class YieldSearch(ast.NodeVisitor):
    def visit_Yield(self, node):
        self.found_yield = True

    def visit_FunctionDef(self, node):
        pass


class JSCompiler(ast.NodeVisitor):
    KEYWORDS = ['default', 'switch', 'throw']

    BOOL_OP = {
        ast.And: '&&',
        ast.Or: '||',
    }

    BIN_OP = {
        ast.Add: '+',
        ast.Sub: '-',
        ast.Mult: '*',
        ast.Div: '/',
        ast.Mod: '%',
        ast.LShift: '<<',
        ast.RShift: '>>',
        ast.BitOr: '|',
        ast.BitXor: '^',
        ast.BitAnd: '&',
    }

    UNARY_OP = {
        ast.Invert: '~',
        ast.Not: '!',
        ast.UAdd: '+',
        ast.USub: '-',
    }

    COMPARE_OP = {
        ast.Eq: '==',
        ast.NotEq: '!=',
        ast.Lt: '<',
        ast.LtE: '<=',
        ast.Gt: '>',
        ast.GtE: '>=',
        ast.Is: '===',
        ast.IsNot: '!==',
    }

    def __init__(self, obj):
        self.obj = obj
        self.node_chain = [None]
        if isinstance(obj, types.ModuleType):
            self.module = obj
        else:
            self.module = sys.modules.get(getattr(obj, '__module__', None))

    def visit(self, node, context=None, inherit=True):
        node.parent = self.node_chain[-1]
        node.context = getattr(node, 'context', context)
        if inherit and node.parent:
            node.yield_point = getattr(node.parent, 'yield_point', None)
            node.loop_point = getattr(node.parent, 'loop_point', None)
            node.break_point = getattr(node.parent, 'break_point', None)
            node.context = node.context or node.parent.context

        self.node_chain.append(node)
        ret = super(JSCompiler, self).visit(node)
        self.node_chain.pop()
        return ret

    def lookup(self, name):
        if name == 'object':
            return 'Object'

        value = getattr(self.module, name, None)
        if value is None:
            return None
        elif value is client.session:
            return '_session'
        elif value is model.model:
            return 'avalon.model'
        return self.safe_name(name)

    def safe_name(self, name):
        if name in JSCompiler.KEYWORDS:
            return name + '_'
        return name

    def generic_visit(self, node):
        raise NotImplementedError(node)

    # Module(stmt* body)
    def visit_Module(self, node):
        tpl = []
        for child in node.body:
            extend(tpl, self.visit(child))
        return '\n'.join(tpl)<commit_msg>Compiler: lookup built in function and types<commit_after># -*- coding: utf-8 -*-
#==============================================================================
# Copyright:    Hybrid Labs
# Licence:      Private
#==============================================================================

import ast
import inspect
import json
import sys
import types

from .. import client, model


class JSCode(object):
    def __init__(self, code):
        pass


class YieldPoint(object):
    def __init__(self):
        self.count = 0

    def create(self):
        self.count += 1
        return self.count


class YieldSearch(ast.NodeVisitor):
    def visit_Yield(self, node):
        self.found_yield = True

    def visit_FunctionDef(self, node):
        pass


class JSCompiler(ast.NodeVisitor):
    KEYWORDS = ['default', 'switch', 'throw']

    BOOL_OP = {
        ast.And: '&&',
        ast.Or: '||',
    }

    BIN_OP = {
        ast.Add: '+',
        ast.Sub: '-',
        ast.Mult: '*',
        ast.Div: '/',
        ast.Mod: '%',
        ast.LShift: '<<',
        ast.RShift: '>>',
        ast.BitOr: '|',
        ast.BitXor: '^',
        ast.BitAnd: '&',
    }

    UNARY_OP = {
        ast.Invert: '~',
        ast.Not: '!',
        ast.UAdd: '+',
        ast.USub: '-',
    }

    COMPARE_OP = {
        ast.Eq: '==',
        ast.NotEq: '!=',
        ast.Lt: '<',
        ast.LtE: '<=',
        ast.Gt: '>',
        ast.GtE: '>=',
        ast.Is: '===',
        ast.IsNot: '!==',
    }

    def __init__(self, obj):
        self.obj = obj
        self.node_chain = [None]
        if isinstance(obj, types.ModuleType):
            self.module = obj
        else:
            self.module = sys.modules.get(getattr(obj, '__module__', None))

    def visit(self, node, context=None, inherit=True):
        node.parent = self.node_chain[-1]
        node.context = getattr(node, 'context', context)
        if inherit and node.parent:
            node.yield_point = getattr(node.parent, 'yield_point', None)
            node.loop_point = getattr(node.parent, 'loop_point', None)
            node.break_point = getattr(node.parent, 'break_point', None)
            node.context = node.context or node.parent.context

        self.node_chain.append(node)
        ret = super(JSCompiler, self).visit(node)
        self.node_chain.pop()
        return ret

    def lookup(self, name):
        from . import type, built_ins
        if name == 'object':
            return 'Object'

        value = (getattr(type, name, None) or getattr(built_ins, name, None) or
                 getattr(self.module, name, None))

        if value is None:
            return None
        elif value is client.session:
            return '_session'
        elif value is model.model:
            return 'avalon.model'
        return self.safe_name(name)

    def safe_name(self, name):
        if name in JSCompiler.KEYWORDS:
            return name + '_'
        return name

    def generic_visit(self, node):
        raise NotImplementedError(node)

    # Module(stmt* body)
    def visit_Module(self, node):
        tpl = []
        for child in node.body:
            extend(tpl, self.visit(child))
        return '\n'.join(tpl)
{'fraction_non_alphanumeric': 0.18275971956660292, 'fraction_numerical': 0.0012746972594008922, 'mean_word_length': 1.9876249405045217, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2458910', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1903, 'n_words': 421}