<commit_before>#
# Warning: this is an alpha module and might be removed/renamed in
# later pyjamas versions
#
from __pyjamas__ import wnd, doc, JS, setCompilerOptions
from __javascript__ import ActiveXObject, XMLHttpRequest
from pyjamas import DOM
from __pyjamas__ import debugger
import sys

setCompilerOptions("noSourceTracking", "noLineTracking", "noStoreSource")

class AjaxError(RuntimeError):
    pass

def createHttpRequest():
    if JS("""typeof $wnd.XMLHttpRequest != 'undefined'"""):
        # IE7+, Mozilla, Safari, ...
       return JS("""new XMLHttpRequest()""")

    # Check for IE6/ActiveX
    try:
        res = JS("""new ActiveXObject("Msxml2.XMLHTTP")""")
        return res
    except:
        pass
    return None

#
#  load(url)
#
#  @param url                   URL to load
#  @param onreadystatechange    function to be used for onreadystatechange
#  @param on_load_fn            function to be called on succes, with parameters event, request
#  @param async                 request mode
#  @returns                     async == False: request object, async == True: None
#

def load(url, onreadystatechange=None, on_load_fn=None, async=False):
    setCompilerOptions("noDebug")
    wnd().status = ('Loading ' + url)
    req = createHttpRequest()

    if onreadystatechange is None:
        def onreadystatechange(evnt):
            if req.readyState==4 and (req.status == 200 or req.status == 0):
                str = req.responseText
                wnd().status = ('Loaded ' + url)
                if not on_load_fn is None:
                    on_load_fn(evnt, req)

    # next line is in JS() for IE6
    JS("req.onreadystatechange = onreadystatechange")
    req.open("GET", url , async)
    try:
        req.send(None)
        if async:
            return None
        while (req.status == 0 and req.responseText == ""):
            if (    req.status == 200
                 or (req.status == 0 and req.responseText)
               ):
                if not on_load_fn is None:
                    on_load_fn(None, req)
                return req
    except:
        pass
    raise AjaxError("Synchronous error", req.status)

def inject(values, namespace = None, names=None):
    if namespace is None:
        from __pyjamas__ import JS
        namespace = JS("$pyjs.global_namespace")
    values = dict(values)
    if names is None:
        for k in values:
            v = values[k]
            JS("""namespace[k] = v;""")
    else:
        for k in names:
            v = values[k]
            JS("""namespace[k] = v;""")

#
#  activate_css(str)
# 
#  looks for any < link > in the input and sets up a corresponding link node
#  in the main document.
# 

def activate_css(targetnode):
    scriptnodes = list(targetnode.getElementsByTagName('link'))
    for LC in range(len(scriptnodes)):
        sn = scriptnodes[LC]
        sn.parentNode.removeChild(sn)<commit_msg>Fix dynamic loading bug introduced in fixing -d --strict (r1793)<commit_after>#
# Warning: this is an alpha module and might be removed/renamed in
# later pyjamas versions
#
from __pyjamas__ import wnd, doc, JS, setCompilerOptions
from __javascript__ import ActiveXObject, XMLHttpRequest
from pyjamas import DOM
from __pyjamas__ import debugger
import sys

setCompilerOptions("noSourceTracking", "noLineTracking", "noStoreSource")

class AjaxError(RuntimeError):
    pass

def createHttpRequest():
    if JS("""typeof $wnd.XMLHttpRequest != 'undefined'"""):
        # IE7+, Mozilla, Safari, ...
       return JS("""new XMLHttpRequest()""")

    # Check for IE6/ActiveX
    try:
        res = JS("""new ActiveXObject("Msxml2.XMLHTTP")""")
        return res
    except:
        pass
    return None

#
#  load(url)
#
#  @param url                   URL to load
#  @param onreadystatechange    function to be used for onreadystatechange
#  @param on_load_fn            function to be called on succes, with parameters event, request
#  @param async                 request mode
#  @returns                     async == False: request object, async == True: None
#

def load(url, onreadystatechange=None, on_load_fn=None, async=False):
    setCompilerOptions("noDebug")
    wnd().status = ('Loading ' + url)
    req = createHttpRequest()

    if onreadystatechange is None:
        def onreadystatechange(evnt):
            if req.readyState==4 and (req.status == 200 or req.status == 0):
                str = req.responseText
                wnd().status = ('Loaded ' + url)
                if not on_load_fn is None:
                    on_load_fn(evnt, req)

    # next line is in JS() for IE6
    JS("req.onreadystatechange = onreadystatechange")
    req.open("GET", url , async)
    try:
        req.send(None)
        if async:
            return None
        while True:
            if (    req.status == 200
                 or (req.status == 0 and req.responseText)
               ):
                if not on_load_fn is None:
                    on_load_fn(None, req)
                return req
            if req.status != 0 or req.responseText != "":
                break
    except:
        pass
    raise AjaxError("Synchronous error", req.status)

def inject(values, namespace = None, names=None):
    if namespace is None:
        from __pyjamas__ import JS
        namespace = JS("$pyjs.global_namespace")
    values = dict(values)
    if names is None:
        for k in values:
            v = values[k]
            JS("""namespace[k] = v;""")
    else:
        for k in names:
            v = values[k]
            JS("""namespace[k] = v;""")

#
#  activate_css(str)
# 
#  looks for any < link > in the input and sets up a corresponding link node
#  in the main document.
# 

def activate_css(targetnode):
    scriptnodes = list(targetnode.getElementsByTagName('link'))
    for LC in range(len(scriptnodes)):
        sn = scriptnodes[LC]
        sn.parentNode.removeChild(sn)
{'fraction_non_alphanumeric': 0.10313825275657337, 'fraction_numerical': 0.00542832909245123, 'mean_word_length': 2.2574585635359115, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '872914', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1712, 'n_words': 525}