<commit_before># frozen_string_literal: true

require "zip"
require "open3"
require "pathname"

module WhatsOpt
  class CodeGenerator
    PYTHON = APP_CONFIG["python_cmd"] || "python"
    DEFAULT_DOE_DRIVER = :smt_doe_lhs
    DEFAULT_OPTIMIZATION_DRIVER = :scipy_optimizer_slsqp

    attr_accessor :genfiles, :prefix

    def initialize(mda)
      @prefix = "code"
      @comment_delimiters = { begin: '"""', end: '"""' }
      @mda = mda
      @template_dir = File.join(File.dirname(__FILE__), "templates")
      @genfiles = []
      @server_module = "server"
      @egmdo_module = "egmdo"
      @server_host = "localhost"
      @server_port = 31400
      @generator = self
    end

    # options: with_run: true, with_server: false, with_runops: true, user_agent: nil, sqlite_filename: nil
    def generate(options = {})
      zip_filename = nil
      stringio = nil
      @genfiles = []
      Dir.mktmpdir("#{prefix}_#{@mda.basename}_") do |dir|
        # dir='/tmp'
        zip_rootpath = Pathname.new(dir)
        zip_filename = File.basename(dir) + ".zip"
        _generate_code(dir, options)
        stringio = Zip::OutputStream.write_buffer do |zio|
          @genfiles.each do |filename|
            entry = Pathname.new(filename).relative_path_from(zip_rootpath)
            zio.put_next_entry(entry)
            File.open(filename) do |f|
              zio.write f.read
            end
          end
        end
      end
      stringio.rewind
      return stringio.read, zip_filename
    end

    def render_partial(file)
      ERB.new(File.read(File.join(@template_dir, file))).result(binding)
    end

    def _generate(filename, template_filename, gendir)
      template = File.join(@template_dir, template_filename)
      Rails.logger.info "Creating #{filename} from #{File.basename(template)} in #{gendir}"
      filepath = File.join(gendir, filename) if gendir
      result = _comment_header(filepath)
      result += _run_template(template)
      fh = File.open(filepath, "w:utf-8")
      fh.print result
      fh.close
      @genfiles << filepath if !@genfiles.include?(filepath)
      filepath
    end

    def _run_template(name)
      erb = ERB.new(File.open(name, "rb:utf-8").read, nil, "-")
      erb.result(binding)
    end

    def _comment_header(filepath)
      <<HEADER
#{@comment_delimiters[:begin]}
  #{File.basename(filepath)} generated by WhatsOpt #{WhatsOpt::Version::VERSION}
#{@comment_delimiters[:end]}
HEADER
    end
  end
end
<commit_msg>Format generated code with black<commit_after># frozen_string_literal: true

require "zip"
require "open3"
require "pathname"

module WhatsOpt
  class CodeGenerator
    PYTHON = APP_CONFIG["python_cmd"] || "python"
    DEFAULT_DOE_DRIVER = :smt_doe_lhs
    DEFAULT_OPTIMIZATION_DRIVER = :scipy_optimizer_slsqp

    attr_accessor :genfiles, :prefix

    def initialize(mda)
      @prefix = "code"
      @comment_delimiters = { begin: '"""', end: '"""' }
      @mda = mda
      @template_dir = File.join(File.dirname(__FILE__), "templates")
      @genfiles = []
      @server_module = "server"
      @egmdo_module = "egmdo"
      @server_host = "localhost"
      @server_port = 31400
      @generator = self
    end

    # options: with_run: true, with_server: false, with_runops: true, user_agent: nil, sqlite_filename: nil
    def generate(options = {})
      zip_filename = nil
      stringio = nil
      @genfiles = []
      Dir.mktmpdir("#{prefix}_#{@mda.basename}_") do |dir|
        # dir='/tmp'
        zip_rootpath = Pathname.new(dir)
        zip_filename = File.basename(dir) + ".zip"
        _generate_code(dir, options)
        _format_code(dir)
        stringio = Zip::OutputStream.write_buffer do |zio|
          @genfiles.each do |filename|
            entry = Pathname.new(filename).relative_path_from(zip_rootpath)
            zio.put_next_entry(entry)
            File.open(filename) do |f|
              zio.write f.read
            end
          end
        end
      end
      stringio.rewind
      return stringio.read, zip_filename
    end

    def render_partial(file)
      ERB.new(File.read(File.join(@template_dir, file))).result(binding)
    end

    def _generate(filename, template_filename, gendir)
      template = File.join(@template_dir, template_filename)
      Rails.logger.info "Creating #{filename} from #{File.basename(template)} in #{gendir}"
      filepath = File.join(gendir, filename) if gendir
      result = _comment_header(filepath)
      result += _run_template(template)
      fh = File.open(filepath, "w:utf-8")
      fh.print result
      fh.close
      @genfiles << filepath if !@genfiles.include?(filepath)
      filepath
    end

    def _run_template(name)
      erb = ERB.new(File.open(name, "rb:utf-8").read, nil, "-")
      erb.result(binding)
    end

    def _comment_header(filepath)
      <<HEADER
#{@comment_delimiters[:begin]}
  #{File.basename(filepath)} generated by WhatsOpt #{WhatsOpt::Version::VERSION}
#{@comment_delimiters[:end]}
HEADER
    end

    def _format_code(dir)
      cmd="#{PYTHON} -m black --line-length 100 #{dir}"
      Rails.logger.info cmd
      stdouterr, status = Open3.capture2e(cmd)
      unless status.success?
        Rails.logger.warn "Black formatting failed!"
        Rails.logger.warn "#{stdouterr}"
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11962264150943397, 'fraction_numerical': 0.003962264150943396, 'mean_word_length': 2.8274368231046934, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '6700741', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1747, 'n_words': 352}