Sha256: 5d2209be05a55186a10d37761ea982a2bd2324917a23a3a2255e7e5e858f50ac

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

require 'open3'
require 'optparse'

require_relative 'tree_tagger/version'
require_relative 'tree_tagger/cli'

module Opener
  class TreeTagger
    attr_reader :options, :args

    ##
    # Hash containing the default options to use.
    #
    # @return [Hash]
    #
    DEFAULT_OPTIONS = {
      :args => []
    }.freeze

    ##
    # @param [Hash] options
    #
    # @option options [Array] :args Collection of arbitrary arguments to pass
    #  to the underlying kernel.
    #
    def initialize(options = {})
      @args    = options.delete(:args) || []
      @options = DEFAULT_OPTIONS.merge(options)
    end

    def run(input)
      stdout, stderr, process = capture(input)

      if process.success?
        STDERR.puts(stderr) unless stderr.empty?
      else
        abort stderr
      end

      return stdout, stderr, process
    end

    def capture(input)
      Open3.capture3(*command.split(" "), :stdin_data=>input)
    end

    def command
      return "#{adjust_python_path} python -E -OO #{kernel} #{args.join(' ')}"
    end

    protected

    ##
    # @return [String]
    #
    def adjust_python_path
      site_packages =  File.join(core_dir, 'site-packages')
      "env PYTHONPATH=#{site_packages}:$PYTHONPATH"
    end

    def core_dir
      File.expand_path("../../core", File.dirname(__FILE__))
    end

    def kernel
      File.join(core_dir,'/tt_from_kaf_to_kaf.py')
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
opener-tree-tagger-3.0.1 lib/opener/tree_tagger.rb
opener-tree-tagger-2.0.2 lib/opener/tree_tagger.rb
opener-tree-tagger-2.0.1 lib/opener/tree_tagger.rb
opener-tree-tagger-2.0.0 lib/opener/tree_tagger.rb