module TkResourceDB - tk-0.2.0 Documentation

# module TkResourceDB

### Constants

CmdClassID
RAND_BASE_CHAR
RAND_BASE_CNT
RAND_BASE_HEAD
TkCommandNames

### Public Class Methods

add(pat, value, pri=None) click to toggle source

    # File lib/tk/optiondb.rb, line 25
    def add(pat, value, pri=None)
      tk_call('option', 'add', pat, value, pri)
    end

clear() click to toggle source

    # File lib/tk/optiondb.rb, line 28
    def clear
      tk_call_without_enc('option', 'clear')
    end

eval_under_random_base(parent = nil, &b) click to toggle source

    # File lib/tk/optiondb.rb, line 354
    def eval_under_random_base(parent = nil, &b)
      new_klass = __create_new_class(__get_random_basename(),
                                     [], 4, false, parent)
      ret = new_klass.class_eval(&b) if block_given?
      __remove_methods_of_proc_class(new_klass)
      new_klass.freeze
      ret
    end

get(win, name, klass) click to toggle source

    # File lib/tk/optiondb.rb, line 31
    def get(win, name, klass)
      tk_call('option', 'get', win ,name, klass)
    end

new_proc_class(klass, func, safe = 4, add = false, parent = nil, &b) click to toggle source

define new proc class : If you want to modify the new class or create a new subclass, you must do such operation in the block parameter. Because the created class is frozen after evaluating the block.

    # File lib/tk/optiondb.rb, line 345
    def new_proc_class(klass, func, safe = 4, add = false, parent = nil, &b)
      new_klass = __create_new_class(klass, func, safe, add, parent)
      new_klass.class_eval(&b) if block_given?
      __remove_methods_of_proc_class(new_klass)
      new_klass.freeze
      new_klass
    end

new_proc_class_random(klass, func, safe = 4, add = false, &b) click to toggle source

    # File lib/tk/optiondb.rb, line 364
    def new_proc_class_random(klass, func, safe = 4, add = false, &b)
      eval_under_random_base(){
        TkOptionDB.new_proc_class(klass, func, safe, add, self, &b)
      }
    end

read_entries(file, f_enc=nil) click to toggle source

    # File lib/tk/optiondb.rb, line 40
    def read_entries(file, f_enc=nil)
      if TkCore::INTERP.safe?
        fail SecurityError,
          "can't call 'TkOptionDB.read_entries' on a safe interpreter"
      end

      i_enc = ((Tk.encoding)? Tk.encoding : Tk.encoding_system)

      unless f_enc
        f_enc = i_enc
      end

      ent = []
      cline = ''
      open(file, 'r') {|f|
        while line = f.gets
          #cline += line.chomp!
          cline.concat(line.chomp!)
          case cline
          when /\\$/    # continue
            cline.chop!
            next
          when /^\s*(!|#)/     # comment
            cline = ''
            next
          when /^([^:]+):(.*)$/
            pat = $1.strip
            val = $2.lstrip
            p "ResourceDB: #{[pat, val].inspect}" if $DEBUG
            pat = TkCore::INTERP._toUTF8(pat, f_enc)
            pat = TkCore::INTERP._fromUTF8(pat, i_enc)
            val = TkCore::INTERP._toUTF8(val, f_enc)
            val = TkCore::INTERP._fromUTF8(val, i_enc)
            ent << [pat, val]
            cline = ''
          else          # unknown --> ignore
            cline = ''
            next
          end
        end
      }
      ent
    end

read_file(file, pri=None)
Alias for: readfile
read_with_encoding(file, f_enc=nil, pri=None) click to toggle source

    # File lib/tk/optiondb.rb, line 85
      def read_with_encoding(file, f_enc=nil, pri=None)
        # try to read the file as an OptionDB file
        read_entries(file, f_enc).each{|pat, val|
          add(pat, val, pri)
        }

    =begin
        i_enc = Tk.encoding()

        unless f_enc
          f_enc = i_enc
        end

        cline = ''
        open(file, 'r') {|f|
          while line = f.gets
            cline += line.chomp!
            case cline
            when /\\$/    # continue
              cline.chop!
              next
            when /^\s*!/     # comment
              cline = ''
              next
            when /^([^:]+):\s(.*)$/
              pat = $1
              val = $2
              p "ResourceDB: #{[pat, val].inspect}" if $DEBUG
              pat = TkCore::INTERP._toUTF8(pat, f_enc)
              pat = TkCore::INTERP._fromUTF8(pat, i_enc)
              val = TkCore::INTERP._toUTF8(val, f_enc)
              val = TkCore::INTERP._fromUTF8(val, i_enc)
              add(pat, val, pri)
              cline = ''
            else          # unknown --> ignore
              cline = ''
              next
            end
          end
        }
    =end
      end

readfile(file, pri=None) click to toggle source

    # File lib/tk/optiondb.rb, line 34
    def readfile(file, pri=None)
      tk_call('option', 'readfile', file, pri)
    end

Also aliased as: read_file

### Private Class Methods

__create_new_class(klass, func, safe = 4, add = false, parent = nil) click to toggle source

# begin¶ ↑

    def __create_new_class(klass, func, safe = 4, add = false, parent = nil)
      klass = klass.to_s if klass.kind_of? Symbol
      unless (?A..?Z) === klass[0]
        fail ArgumentError, "bad string '#{klass}' for class name"
      end
      unless func.kind_of? Array
        fail ArgumentError, "method-list must be Array"
      end
      func_str = func.join(' ')
      if parent == nil
        install_win(parent)
      elsif parent <= @@resource_proc_class
        install_win(parent::CARRIER)
      else
        fail ArgumentError, "parent must be Resource-Proc class"
      end
      carrier = Tk.tk_call_without_enc('frame', @path, '-class', klass)

      body = <<-"EOD"
        class #{klass} < TkOptionDB.module_eval('@@resource_proc_class')
          CARRIER    = '#{carrier}'.freeze
          METHOD_TBL = TkCore::INTERP.create_table
          ADD_METHOD = #{add}
          SAFE_MODE  = #{safe}
          %w(#{func_str}).each{|f| METHOD_TBL[f.intern] = nil }
        end
      EOD

      if parent.kind_of?(Class) && parent <= @@resource_proc_class
        parent.class_eval(body)
        eval(parent.name + '::' + klass)
      else
        eval(body)
        eval('TkOptionDB::' + klass)
      end
    end

# end¶ ↑

    # File lib/tk/optiondb.rb, line 249
      def __create_new_class(klass, func, safe = 4, add = false, parent = nil)
        if klass.kind_of?(TkWindow)
          carrier = klass.path
          CmdClassID.mutex.synchronize{
            klass = CmdClassID.join(TkCore::INTERP._ip_id_)
            CmdClassID[1].succ!
          }
          parent = nil # ignore parent
        else
          klass = klass.to_s if klass.kind_of?(Symbol)
          unless (?A..?Z) === klass[0]
            fail ArgumentError, "bad string '#{klass}' for class name"
          end
          if parent == nil
            install_win(nil)
          elsif parent.kind_of?(TkWindow)
            install_win(parent.path)
          elsif parent <= @@resource_proc_class
            install_win(parent::CARRIER)
          else
            fail ArgumentError, "parent must be Resource-Proc class"
          end
          carrier = Tk.tk_call_without_enc('frame', @path, '-class', klass)
        end

        unless func.kind_of?(Array)
          fail ArgumentError, "method-list must be Array"
        end
        func_str = func.join(' ')

        if parent.kind_of?(Class) && parent <= @@resource_proc_class
          cmd_klass = Class.new(parent)
        else
          cmd_klass = Class.new(TkOptionDB.module_eval('@@resource_proc_class'))
        end
        cmd_klass.const_set(:CARRIER, carrier.dup.freeze)

        cmd_klass.instance_variable_set('@method_tbl', TkCore::INTERP.create_table)
        cmd_klass.instance_variable_set('@add_method', add)
        cmd_klass.instance_variable_set('@safe_mode', safe)
        func.each{|f|
          cmd_klass.instance_variable_get('@method_tbl')[f.to_s.intern] = nil
        }
    =begin
        cmd_klass.const_set(:METHOD_TBL, TkCore::INTERP.create_table)
        cmd_klass.const_set(:ADD_METHOD, add)
        cmd_klass.const_set(:SAFE_MODE, safe)
        func.each{|f| cmd_klass::METHOD_TBL[f.to_s.intern] = nil }
    =end

        cmd_klass
      end

__get_random_basename() click to toggle source

    # File lib/tk/optiondb.rb, line 328
    def __get_random_basename
      name = '%s%03d' % [RAND_BASE_HEAD[rand(RAND_BASE_HEAD.size),1],
                         RAND_BASE_CNT[0]]
      len = RAND_BASE_CHAR.size
      (6+rand(10)).times{
        name << RAND_BASE_CHAR[rand(len),1]
      }
      RAND_BASE_CNT[0] = RAND_BASE_CNT[0] + 1
      name
    end

__remove_methods_of_proc_class(klass) click to toggle source

    # File lib/tk/optiondb.rb, line 304
    def __remove_methods_of_proc_class(klass)
      # for security, make these methods invalid
      class << klass
        def __null_method(*args); nil; end
        [ :class_eval, :name, :superclass, :clone, :dup, :autoload, :autoload?,
          :ancestors, :const_defined?, :const_get, :const_set, :const_missing,
          :class_variables, :constants, :included_modules, :instance_methods,
          :method_defined?, :module_eval, :private_instance_methods,
          :protected_instance_methods, :public_instance_methods,
          :singleton_methods, :remove_const, :remove_method, :undef_method,
          :to_s, :inspect, :display, :method, :methods, :respond_to?,
          :instance_variable_get, :instance_variable_set, :instance_method,
          :instance_eval, :instance_exec, :instance_variables, :kind_of?, :is_a?,
          :private_methods, :protected_methods, :public_methods ].each{|m|
          alias_method(m, :__null_method)
        }
      end
    end

### Public Instance Methods

mutex() click to toggle source

    # File lib/tk/optiondb.rb, line 14
    def mutex; @mutex; end

### Private Instance Methods

__create_new_class(klass, func, safe = 4, add = false, parent = nil) click to toggle source

# begin¶ ↑

    def __create_new_class(klass, func, safe = 4, add = false, parent = nil)
      klass = klass.to_s if klass.kind_of? Symbol
      unless (?A..?Z) === klass[0]
        fail ArgumentError, "bad string '#{klass}' for class name"
      end
      unless func.kind_of? Array
        fail ArgumentError, "method-list must be Array"
      end
      func_str = func.join(' ')
      if parent == nil
        install_win(parent)
      elsif parent <= @@resource_proc_class
        install_win(parent::CARRIER)
      else
        fail ArgumentError, "parent must be Resource-Proc class"
      end
      carrier = Tk.tk_call_without_enc('frame', @path, '-class', klass)

      body = <<-"EOD"
        class #{klass} < TkOptionDB.module_eval('@@resource_proc_class')
          CARRIER    = '#{carrier}'.freeze
          METHOD_TBL = TkCore::INTERP.create_table
          ADD_METHOD = #{add}
          SAFE_MODE  = #{safe}
          %w(#{func_str}).each{|f| METHOD_TBL[f.intern] = nil }
        end
      EOD

      if parent.kind_of?(Class) && parent <= @@resource_proc_class
        parent.class_eval(body)
        eval(parent.name + '::' + klass)
      else
        eval(body)
        eval('TkOptionDB::' + klass)
      end
    end

# end¶ ↑

    # File lib/tk/optiondb.rb, line 249
      def __create_new_class(klass, func, safe = 4, add = false, parent = nil)
        if klass.kind_of?(TkWindow)
          carrier = klass.path
          CmdClassID.mutex.synchronize{
            klass = CmdClassID.join(TkCore::INTERP._ip_id_)
            CmdClassID[1].succ!
          }
          parent = nil # ignore parent
        else
          klass = klass.to_s if klass.kind_of?(Symbol)
          unless (?A..?Z) === klass[0]
            fail ArgumentError, "bad string '#{klass}' for class name"
          end
          if parent == nil
            install_win(nil)
          elsif parent.kind_of?(TkWindow)
            install_win(parent.path)
          elsif parent <= @@resource_proc_class
            install_win(parent::CARRIER)
          else
            fail ArgumentError, "parent must be Resource-Proc class"
          end
          carrier = Tk.tk_call_without_enc('frame', @path, '-class', klass)
        end

        unless func.kind_of?(Array)
          fail ArgumentError, "method-list must be Array"
        end
        func_str = func.join(' ')

        if parent.kind_of?(Class) && parent <= @@resource_proc_class
          cmd_klass = Class.new(parent)
        else
          cmd_klass = Class.new(TkOptionDB.module_eval('@@resource_proc_class'))
        end
        cmd_klass.const_set(:CARRIER, carrier.dup.freeze)

        cmd_klass.instance_variable_set('@method_tbl', TkCore::INTERP.create_table)
        cmd_klass.instance_variable_set('@add_method', add)
        cmd_klass.instance_variable_set('@safe_mode', safe)
        func.each{|f|
          cmd_klass.instance_variable_get('@method_tbl')[f.to_s.intern] = nil
        }
    =begin
        cmd_klass.const_set(:METHOD_TBL, TkCore::INTERP.create_table)
        cmd_klass.const_set(:ADD_METHOD, add)
        cmd_klass.const_set(:SAFE_MODE, safe)
        func.each{|f| cmd_klass::METHOD_TBL[f.to_s.intern] = nil }
    =end

        cmd_klass
      end

__get_random_basename() click to toggle source

    # File lib/tk/optiondb.rb, line 328
    def __get_random_basename
      name = '%s%03d' % [RAND_BASE_HEAD[rand(RAND_BASE_HEAD.size),1],
                         RAND_BASE_CNT[0]]
      len = RAND_BASE_CHAR.size
      (6+rand(10)).times{
        name << RAND_BASE_CHAR[rand(len),1]
      }
      RAND_BASE_CNT[0] = RAND_BASE_CNT[0] + 1
      name
    end

__remove_methods_of_proc_class(klass) click to toggle source

    # File lib/tk/optiondb.rb, line 304
    def __remove_methods_of_proc_class(klass)
      # for security, make these methods invalid
      class << klass
        def __null_method(*args); nil; end
        [ :class_eval, :name, :superclass, :clone, :dup, :autoload, :autoload?,
          :ancestors, :const_defined?, :const_get, :const_set, :const_missing,
          :class_variables, :constants, :included_modules, :instance_methods,
          :method_defined?, :module_eval, :private_instance_methods,
          :protected_instance_methods, :public_instance_methods,
          :singleton_methods, :remove_const, :remove_method, :undef_method,
          :to_s, :inspect, :display, :method, :methods, :respond_to?,
          :instance_variable_get, :instance_variable_set, :instance_method,
          :instance_eval, :instance_exec, :instance_variables, :kind_of?, :is_a?,
          :private_methods, :protected_methods, :public_methods ].each{|m|
          alias_method(m, :__null_method)
        }
      end
    end

add(pat, value, pri=None) click to toggle source

    # File lib/tk/optiondb.rb, line 25
    def add(pat, value, pri=None)
      tk_call('option', 'add', pat, value, pri)
    end

clear() click to toggle source

    # File lib/tk/optiondb.rb, line 28
    def clear
      tk_call_without_enc('option', 'clear')
    end

eval_under_random_base(parent = nil, &b) click to toggle source

    # File lib/tk/optiondb.rb, line 354
    def eval_under_random_base(parent = nil, &b)
      new_klass = __create_new_class(__get_random_basename(),
                                     [], 4, false, parent)
      ret = new_klass.class_eval(&b) if block_given?
      __remove_methods_of_proc_class(new_klass)
      new_klass.freeze
      ret
    end

get(win, name, klass) click to toggle source

    # File lib/tk/optiondb.rb, line 31
    def get(win, name, klass)
      tk_call('option', 'get', win ,name, klass)
    end

new_proc_class(klass, func, safe = 4, add = false, parent = nil, &b) click to toggle source

define new proc class : If you want to modify the new class or create a new subclass, you must do such operation in the block parameter. Because the created class is frozen after evaluating the block.

    # File lib/tk/optiondb.rb, line 345
    def new_proc_class(klass, func, safe = 4, add = false, parent = nil, &b)
      new_klass = __create_new_class(klass, func, safe, add, parent)
      new_klass.class_eval(&b) if block_given?
      __remove_methods_of_proc_class(new_klass)
      new_klass.freeze
      new_klass
    end

new_proc_class_random(klass, func, safe = 4, add = false, &b) click to toggle source

    # File lib/tk/optiondb.rb, line 364
    def new_proc_class_random(klass, func, safe = 4, add = false, &b)
      eval_under_random_base(){
        TkOptionDB.new_proc_class(klass, func, safe, add, self, &b)
      }
    end

read_entries(file, f_enc=nil) click to toggle source

    # File lib/tk/optiondb.rb, line 40
    def read_entries(file, f_enc=nil)
      if TkCore::INTERP.safe?
        fail SecurityError,
          "can't call 'TkOptionDB.read_entries' on a safe interpreter"
      end

      i_enc = ((Tk.encoding)? Tk.encoding : Tk.encoding_system)

      unless f_enc
        f_enc = i_enc
      end

      ent = []
      cline = ''
      open(file, 'r') {|f|
        while line = f.gets
          #cline += line.chomp!
          cline.concat(line.chomp!)
          case cline
          when /\\$/    # continue
            cline.chop!
            next
          when /^\s*(!|#)/     # comment
            cline = ''
            next
          when /^([^:]+):(.*)$/
            pat = $1.strip
            val = $2.lstrip
            p "ResourceDB: #{[pat, val].inspect}" if $DEBUG
            pat = TkCore::INTERP._toUTF8(pat, f_enc)
            pat = TkCore::INTERP._fromUTF8(pat, i_enc)
            val = TkCore::INTERP._toUTF8(val, f_enc)
            val = TkCore::INTERP._fromUTF8(val, i_enc)
            ent << [pat, val]
            cline = ''
          else          # unknown --> ignore
            cline = ''
            next
          end
        end
      }
      ent
    end

read_file(file, pri=None)
Alias for: readfile
read_with_encoding(file, f_enc=nil, pri=None) click to toggle source

    # File lib/tk/optiondb.rb, line 85
      def read_with_encoding(file, f_enc=nil, pri=None)
        # try to read the file as an OptionDB file
        read_entries(file, f_enc).each{|pat, val|
          add(pat, val, pri)
        }

    =begin
        i_enc = Tk.encoding()

        unless f_enc
          f_enc = i_enc
        end

        cline = ''
        open(file, 'r') {|f|
          while line = f.gets
            cline += line.chomp!
            case cline
            when /\\$/    # continue
              cline.chop!
              next
            when /^\s*!/     # comment
              cline = ''
              next
            when /^([^:]+):\s(.*)$/
              pat = $1
              val = $2
              p "ResourceDB: #{[pat, val].inspect}" if $DEBUG
              pat = TkCore::INTERP._toUTF8(pat, f_enc)
              pat = TkCore::INTERP._fromUTF8(pat, i_enc)
              val = TkCore::INTERP._toUTF8(val, f_enc)
              val = TkCore::INTERP._fromUTF8(val, i_enc)
              add(pat, val, pri)
              cline = ''
            else          # unknown --> ignore
              cline = ''
              next
            end
          end
        }
    =end
      end

readfile(file, pri=None) click to toggle source

    # File lib/tk/optiondb.rb, line 34
    def readfile(file, pri=None)
      tk_call('option', 'readfile', file, pri)
    end

Also aliased as: read_file