<commit_before>module Interact

using Reactive

import Base: mimewritable, writemime, parse, recv
import Reactive.signal
export signal, statedict, Widget, InputWidget, register_widget,
       get_widget, parse, recv, update_view

# A widget
abstract Widget <: SignalSource

# A widget that gives out a signal of type T
abstract InputWidget{T}  <: Widget

signal(w::InputWidget) = w.signal

function statedict(w::Widget)
    msg = Dict()
    attrs = names(w)
    for n in attrs
        if n in [:signal, :label]
            continue
        end
        msg[n] = getfield(w, n)
    end
    msg
end

function parse{T}(::InputWidget{T}, msg)
    # Should return a value of type T, by default
    # msg itself is assumed to be the value.
    return convert(T, msg)
end

# default cases

parse{T <: Integer}(::InputWidget{T}, v) = int(v)
parse{T <: FloatingPoint}(::InputWidget{T}, v) = float(v)
parse(::InputWidget{Bool}, v) = bool(v)

function update_view(w)
    # update the view of a widget.
    # child packages need to override.
end

function recv{T}(widget ::InputWidget{T}, value)
    # Hand-off received value to the signal graph
    parsed = parse(widget, value)
    println(STDERR, signal(widget))
    push!(signal(widget), parsed)
    widget.value = parsed
    if value != parsed
        update_view(widget)
    end
end

uuid4() = string(Base.Random.uuid4())

const id_to_widget = Dict{String, InputWidget}()
const widget_to_id = Dict{InputWidget, String}()

function register_widget(w::InputWidget)
    if haskey(widget_to_id, w)
        return widget_to_id[w]
    else
        id = uuid4()
        widget_to_id[w] = id
        id_to_widget[id] = w
        return id
    end
end

function get_widget(id::String)
    if haskey(id_to_widget, id)
        return id_to_widget[id]
    else
        warn("Widget with id $(id) does not exist.")
    end
end

include("widgets.jl")
include("compose.jl")
include("manipulate.jl")
include("html_setup.jl")

if isdefined(Main, :IJulia) && Main.IJulia.inited
    ijuliaver = Pkg.installed("IJulia")
    if ijuliaver === nothing || ijuliaver < v"0.1.3-"
        warn("Interact requires IJulia >= v0.1.3 to work properly.")
    end
    include("IJulia/setup.jl")
end

end # module
<commit_msg>Prevent likely 0.4 deprecation warnings<commit_after>module Interact

using Reactive, Compat

import Base: mimewritable, writemime, parse, recv
import Reactive.signal
export signal, statedict, Widget, InputWidget, register_widget,
       get_widget, parse, recv, update_view

# A widget
abstract Widget <: SignalSource

# A widget that gives out a signal of type T
abstract InputWidget{T}  <: Widget

signal(w::InputWidget) = w.signal

function statedict(w::Widget)
    msg = Dict()
    attrs = names(w)
    for n in attrs
        if n in [:signal, :label]
            continue
        end
        msg[n] = getfield(w, n)
    end
    msg
end

function parse{T}(::InputWidget{T}, msg)
    # Should return a value of type T, by default
    # msg itself is assumed to be the value.
    return convert(T, msg)
end

# default cases

parse{T <: Number}(::InputWidget{T}, v) = cnvt(T, v)

cnvt(::Type{Bool}, v::AbstractString) = parse(Bool, v) # doesn't work, but needed for ambiguity resolution
cnvt(::Type{Bool}, v) = v != 0
cnvt{T}(::Type{T}, v::AbstractString) = parse(T, v)
cnvt{T}(::Type{T}, v) = convert(T, v)

function update_view(w)
    # update the view of a widget.
    # child packages need to override.
end

function recv{T}(widget ::InputWidget{T}, value)
    # Hand-off received value to the signal graph
    parsed = parse(widget, value)
    println(STDERR, signal(widget))
    push!(signal(widget), parsed)
    widget.value = parsed
    if value != parsed
        update_view(widget)
    end
end

uuid4() = string(Base.Random.uuid4())

const id_to_widget = Dict{String, InputWidget}()
const widget_to_id = Dict{InputWidget, String}()

function register_widget(w::InputWidget)
    if haskey(widget_to_id, w)
        return widget_to_id[w]
    else
        id = uuid4()
        widget_to_id[w] = id
        id_to_widget[id] = w
        return id
    end
end

function get_widget(id::String)
    if haskey(id_to_widget, id)
        return id_to_widget[id]
    else
        warn("Widget with id $(id) does not exist.")
    end
end

include("widgets.jl")
include("compose.jl")
include("manipulate.jl")
include("html_setup.jl")

if isdefined(Main, :IJulia) && Main.IJulia.inited
    ijuliaver = Pkg.installed("IJulia")
    if ijuliaver === nothing || ijuliaver < v"0.1.3-"
        warn("Interact requires IJulia >= v0.1.3 to work properly.")
    end
    include("IJulia/setup.jl")
end

end # module

{'fraction_non_alphanumeric': 0.11966364812419146, 'fraction_numerical': 0.004527813712807244, 'mean_word_length': 3.3764150943396225, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 5, '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': '3659107', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1582, 'n_words': 450}