Sha256: ef94b47d20e97873060013c4b127b4b9826cb4c34ec053aeb1fea5b066c58b62

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

require 'pycall'

module PyCall
  module Import
    def self.main_object
      @main_object
    end
  end
end

main_object = self
PyCall::Import.class_eval { @main_object = main_object }

module PyCall
  module Import
    def pyimport(mod_name, as: nil)
      case as
      when nil
        as = mod_name
      end

      check_valid_module_variable_name(mod_name, as)

      mod = PyCall.import_module(mod_name)
      raise PyError.fetch unless mod

      define_singleton_method(as) { mod }
    end

    def pyfrom(mod_name, import: nil)
      raise ArgumentError, "missing identifiers to be imported" unless import

      mod = PyCall.import_module(mod_name)
      raise PyError.fetch unless mod

      case import
      when Hash
        import.each do |attr, as|
          val = PyCall.getattr(mod, attr)
          define_name(as, val)
        end
      when Array
        import.each do |attr|
          val = PyCall.getattr(mod, attr)
          define_name(attr, val)
        end
      when Symbol, String
        val = PyCall.getattr(mod, import)
        define_name(import, val)
      end
    end

    private

    def define_name(name, pyobj)
      if constant_name?(name)
        context = self
        context = (self == PyCall::Import.main_object) ? Object : self
        context.module_eval { const_set(name, pyobj) }
      else
        define_singleton_method(name) { pyobj }
      end
    end

    def constant_name?(name)
      name =~ /\A[A-Z]/
    end

    def check_valid_module_variable_name(mod_name, var_name)
      var_name = var_name.to_s if var_name.kind_of? Symbol
      if var_name.include?('.')
        raise ArgumentError, "#{var_name} is not a valid module variable name, use pyimport #{mod_name}, as: <name>"
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
pycall-0.1.0.alpha.20170502 lib/pycall/import.rb
pycall-0.1.0.alpha.20170426 lib/pycall/import.rb
pycall-0.1.0.alpha.20170419b lib/pycall/import.rb
pycall-0.1.0.alpha.20170419a lib/pycall/import.rb
pycall-0.1.0.alpha.20170419 lib/pycall/import.rb
pycall-0.1.0.alpha.20170403 lib/pycall/import.rb
pycall-0.1.0.alpha.20170329 lib/pycall/import.rb
pycall-0.1.0.alpha.20170317 lib/pycall/import.rb
pycall-0.1.0.alpha.20170311 lib/pycall/import.rb
pycall-0.1.0.alpha.20170309 lib/pycall/import.rb
pycall-0.1.0.alpha.20170308 lib/pycall/import.rb
pycall-0.1.0.alpha.20170307 lib/pycall/import.rb
pycall-0.1.0.alpha.20170302 lib/pycall/import.rb
pycall-0.1.0.alpha.20170226 lib/pycall/import.rb
pycall-0.1.0.alpha.20170224b lib/pycall/import.rb