Skip to content

Commit

Permalink
Generate GEMSEO code in package layout mode (#206)
Browse files Browse the repository at this point in the history
* Manage gemseo pull in pkg mode in the generator

* Adjust the gemseo templates to use pkg_prefix
  • Loading branch information
relf authored Aug 28, 2024
1 parent a61fdb9 commit 7843e98
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def new
with_egmdo: with_egmdo, with_runops: with_runops, with_unittests: with_unittests)
send_data content, filename: filename
when "gemseo", "gemseo_pkg"
ggen = WhatsOpt::GemseoGenerator.new(mda)
ggen = WhatsOpt::GemseoGenerator.new(mda, pkg_format: (format == "gemseo_pkg"))
begin
content, filename = ggen.generate(user_agent: user_agent, with_run: with_run, with_server: with_server,
with_egmdo: with_egmdo, with_runops: with_runops, with_unittests: with_unittests)
Expand Down
40 changes: 34 additions & 6 deletions app/lib/whats_opt/gemseo_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,45 @@ class DisciplineNotFoundException < StandardError
class NotYetImplementedError < StandardError
end

def initialize(mda)
super(mda)
def initialize(mda, pkg_format: false)
super(mda, pkg_format: pkg_format)
@prefix = "gemseo"
@framework = "gemseo"
end

# sqlite_filename: nil, with_run: true, with_server: true, with_runops: true
def _generate_code(gendir, options = {})
opts = { with_server: false, with_run: true, with_unittests: false }.merge(options)
opts = { with_server: false, with_run: true, with_unittests: false, with_src_dir: false }.merge(options)
if opts[:with_src_dir]
src_dir = File.join(gendir, "src")
Dir.mkdir(src_dir) unless Dir.exist?(src_dir)
else
src_dir = gendir
end
pkg_dir = package_dir? ? File.join(src_dir, @impl.py_modulename) : src_dir
Dir.mkdir(pkg_dir) unless Dir.exist?(pkg_dir)

@mda.disciplines.nodes.each do |disc|
if disc.has_sub_analysis?
raise NotYetImplementedError.new("Cannot generate code for sub_analysis #{disc.name}")
_generate_sub_analysis(disc, gendir, opts)
_generate_sub_analysis(disc, pkg_dir, opts)
else
_generate_discipline(disc, gendir, opts)
_generate_discipline(disc, pkg_dir, opts)
raise NotYetImplementedError.new("Cannot generate code for unit test #{disc.name}") if opts[:with_unittests]
end
end
_generate_main(gendir, opts)
_generate_main(pkg_dir, opts)
_generate_run_scripts(gendir, opts)
if opts[:with_server] || (!@check_only && @mda.has_remote_discipline?(@remote_ip))
raise NotYetImplementedError.new("Cannot generate code for server")
end
if opts[:with_egmdo] || @driver_name =~ /egmdo|egdoe/
raise NotYetImplementedError.new("Cannot generate code for egmdo")
end
if package_dir? && @framework == "gemseo"
_generate_package_files(gendir)
end

@genfiles
end

Expand Down Expand Up @@ -70,5 +86,17 @@ def _generate_run_scripts(gendir, options = {})
end
end
end

def _generate_package_files(gendir)
if @mda.packaged?
# Package is attached, use it!
@genfiles |= WhatsOpt::PackageExtractor.new(@mda).extract(gendir)
else
# no package => generate package skeleton
_generate(".gitignore", "package/gitignore.erb", gendir, no_comment: true)
_generate("README.md", "package/README.md.erb", gendir, no_comment: true)
_generate("pyproject.toml", "package/pyproject.toml.erb", gendir, no_comment: true)
end
end
end
end
2 changes: 1 addition & 1 deletion app/lib/whats_opt/templates/gemseo/gemseo_analysis.py.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from <%= @impl.py_full_modulename %>_base import <%= @impl.py_classname %>MDABase, <%= @impl.py_classname %>DOEScenarioBase, <%= @impl.py_classname %>MDOScenarioBase
from <%= @pkg_prefix %><%= @impl.py_full_modulename %>_base import <%= @impl.py_classname %>MDABase, <%= @impl.py_classname %>DOEScenarioBase, <%= @impl.py_classname %>MDOScenarioBase

class <%= @impl.py_classname %>MDA(<%= @impl.py_classname %>MDABase):
""" A GEMSEO MDA to encapsulate <%= @impl.py_classname %> analysis """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import numpy as np
from numpy import nan

<% @mda.all_plain_disciplines.each do |disc| -%>
from <%= disc.impl.py_full_modulename %> import <%= disc.impl.py_classname %>
from <%= @pkg_prefix %><%= disc.impl.py_full_modulename %> import <%= disc.impl.py_classname %>
<% end -%>

from gemseo.mda.jacobi import MDAJacobi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from <%= @discipline.impl.py_full_modulename %>_base import <%= @discipline.impl.py_classname %>Base
from <%= @pkg_prefix %><%= @discipline.impl.py_full_modulename %>_base import <%= @discipline.impl.py_classname %>Base

class <%= @discipline.impl.py_classname %>(<%= @discipline.impl.py_classname %>Base):
""" A class to encapsulate <%= @discipline.impl.py_classname %> discipline """
Expand Down
2 changes: 1 addition & 1 deletion app/lib/whats_opt/templates/gemseo/run_doe.py.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from optparse import OptionParser
from gemseo import configure_logger
from <%= @impl.py_modulename %> import <%= @impl.py_classname %>DOEScenario
from <%= @pkg_prefix %><%= @impl.py_modulename %> import <%= @impl.py_classname %>DOEScenario

parser = OptionParser()
parser.add_option("-n", "--ncases", type="int",
Expand Down
2 changes: 1 addition & 1 deletion app/lib/whats_opt/templates/gemseo/run_mda.py.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from <%= @impl.py_modulename %> import <%= @impl.py_classname %>MDA
from <%= @pkg_prefix %><%= @impl.py_modulename %> import <%= @impl.py_classname %>MDA
from mda_init import initialize

mda = <%= @impl.py_classname %>MDA()
Expand Down
2 changes: 1 addition & 1 deletion app/lib/whats_opt/templates/gemseo/run_mdo.py.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from gemseo import configure_logger
from <%= @impl.py_modulename %> import <%= @impl.py_classname %>MDOScenario
from <%= @pkg_prefix %><%= @impl.py_modulename %> import <%= @impl.py_classname %>MDOScenario

configure_logger()

Expand Down
2 changes: 1 addition & 1 deletion app/lib/whats_opt/templates/package/pyproject.toml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [
]
description = "Package <%= @mda.name %> Analysis"
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.9"
classifiers = [
"Programming Language :: Python :: 3",
]
Expand Down

0 comments on commit 7843e98

Please sign in to comment.