Skip to content

Commit

Permalink
Merge pull request #95 from fivegrant/fix/intertype-path
Browse files Browse the repository at this point in the history
Fix path and naming issues with intertype codegen
  • Loading branch information
olynch authored Dec 19, 2023
2 parents 0b5c038 + e8d4db4 commit 751a01c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
15 changes: 14 additions & 1 deletion src/intertypes/InterTypes.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module InterTypes
export InterType, InterTypeDecl, Binary, intertype, @intertypes
export InterType, InterTypeDecl, Binary, LanguageTarget, SerializationTarget, generate_module, intertype, @intertypes

using MLStyle
using OrderedCollections
Expand Down Expand Up @@ -100,6 +100,19 @@ end

function intertype end

abstract type ExportTarget end
abstract type LanguageTarget <: ExportTarget end
abstract type SerializationTarget <: ExportTarget end

"""
generate_module(mod::Module, target::Type{<:ExportTarget}, path="."; target_specific_args...)
Generate files that define the intertypes for the specified target.
"""
function generate_module(mod::Module, target::Type{<:ExportTarget}, path="."; target_specific_args...)
generate_module(mod.Meta, target, path; target_specific_args...)
end

include("json.jl")
include("sexp.jl")
include("julia.jl")
Expand Down
21 changes: 13 additions & 8 deletions src/intertypes/json.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export tojsonschema, jsonwrite, jsonread, generate_jsonschema_module
export JSONTarget, tojsonschema, jsonwrite, jsonread

using OrderedCollections
using Base64
Expand Down Expand Up @@ -358,8 +358,16 @@ function acsettype(spec)
)
end

function generate_jsonschema_module(
mod::InterTypeModule, path="."
"""
JSONTarget
Specifies a serialization target of JSON Schema when
generating a module.
"""
struct JSONTarget <: SerializationTarget end

function generate_module(
mod::InterTypeModule, ::Type{JSONTarget}, path
;ac=JSON3.AlignmentContext(indent=2)
)
defs = Pair{String, Object}[]
Expand All @@ -384,11 +392,8 @@ function generate_jsonschema_module(
"\$schema" => "http://json-schema.org/draft-07/schema#",
"\$defs" => Object(defs)
)
open(string(mod.name) * "_schema.json", "w") do io
schema_filepath = joinpath(path, string(mod.name)*"_schema.json")
open(schema_filepath, "w") do io
JSON3.pretty(io, schema, ac)
end
end

function generate_jsonschema_module(mod::Module, path=".")
generate_jsonschema_module(mod.Meta, path)
end
2 changes: 1 addition & 1 deletion src/intertypes/julia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ end

function include_intertypes(into::Module, file::String, imports::AbstractVector)
endswith(file, ".it") || error("expected a file ending in \".it\"")
name = Symbol(chop(file; tail=3))
name = Symbol(basename(chop(file; tail=3)))
mod = InterTypeModule(name, OrderedDict{Symbol, InterTypeModule}(imports))
into.include(as_intertypes(mod), file)
# recompute the hash
Expand Down
19 changes: 15 additions & 4 deletions src/intertypes/python.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export generate_python_module
export PydanticTarget

function topy(intertype::InterType; forward_ref=true)
@match intertype begin
Expand Down Expand Up @@ -141,6 +141,7 @@ from intertypes import SafeInt, InterTypeBase
from acsets import Ob, Hom, Attr, AttrType, Schema, ACSet
"""

# TODO: Expose this to the user? Write automatically for the user?
INTERTYPE_PYTHON_MODULE = """
from typing import Annotated
Expand All @@ -156,9 +157,19 @@ class InterTypeBase(BaseModel):
return super().model_dump_json(*args, **kwargs, by_alias=True)
"""

function generate_python_module(jmod::Module, outdir)
mod = jmod.Meta
outfile = outdir * "/" * string(mod.name) * ".py"

"""
PydanticTarget
Targets the creation of `.py` files that use the Pydantic library
which enables integration with the Python language (specifically
when (de)serializing JSON).
"""
struct PydanticTarget <: LanguageTarget end


function generate_module(mod::InterTypeModule, ::Type{PydanticTarget}, path)
outfile = joinpath(path, string(mod.name) * ".py")
open(outfile, "w") do io
print(io, PYTHON_PREAMBLE)
for (name, importedmod) in mod.imports
Expand Down
10 changes: 5 additions & 5 deletions test/intertypes/InterTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ s = jsonwrite(t)

@test jsonread(s, Term) == t

generate_jsonschema_module(simpleast, ".")
generate_module(simpleast, JSONTarget)

simpleast_schema = JSONSchema.Schema(read("simpleast_schema.json", String))

Expand Down Expand Up @@ -71,7 +71,7 @@ add_part!(g, :E, src=1, tgt=2, weight=EdgeData(:mass_ave, 42))

@test testjson(m)

generate_jsonschema_module(wgraph, ".")
generate_module(wgraph, JSONTarget)

wgraph_schema = JSONSchema.Schema(read("wgraph_schema.json", String))

Expand All @@ -86,9 +86,9 @@ wgraph_schema = JSONSchema.Schema(read("wgraph_schema.json", String))

dir = @__DIR__
write(dir * "/intertypes.py", InterTypes.INTERTYPE_PYTHON_MODULE)
generate_python_module(simpleast, dir)
generate_python_module(model, dir)
generate_python_module(wgraph, dir)
generate_module(simpleast, PydanticTarget, dir)
generate_module(model, PydanticTarget, dir)
generate_module(wgraph, PydanticTarget, dir)

pushfirst!(PyList(pyimport("sys")."path"), Py(dir))

Expand Down

0 comments on commit 751a01c

Please sign in to comment.