~ubuntu-branches/ubuntu/intrepid/ruby1.8/intrepid-updates

« back to all changes in this revision

Viewing changes to ext/tk/lib/tk/namespace.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
#   tk/namespace.rb : methods to manipulate Tcl/Tk namespace
 
3
#                           by Hidetoshi Nagai <nagai@ai.kyutech.ac.jp>
 
4
#
 
5
require 'tk'
 
6
 
 
7
class TkNamespace < TkObject
 
8
  extend Tk
 
9
 
 
10
  TkCommandNames = [
 
11
    'namespace'.freeze, 
 
12
  ].freeze
 
13
 
 
14
  Tk_Namespace_ID_TBL = TkCore::INTERP.create_table
 
15
  Tk_Namespace_ID = ["ns".freeze, "00000".taint].freeze
 
16
 
 
17
  Tk_NsCode_RetObjID_TBL = TkCore::INTERP.create_table
 
18
 
 
19
  TkCore::INTERP.init_ip_env{
 
20
    Tk_Namespace_ID_TBL.clear
 
21
    Tk_NsCode_RetObjID_TBL.clear
 
22
  }
 
23
 
 
24
  def TkNamespace.id2obj(id)
 
25
    Tk_Namespace_ID_TBL[id]? Tk_Namespace_ID_TBL[id]: id
 
26
  end
 
27
 
 
28
  #####################################
 
29
 
 
30
  class Ensemble < TkObject
 
31
    def __cget_cmd
 
32
      ['namespace', 'ensemble', 'configure', self.path]
 
33
    end
 
34
    private :__cget_cmd
 
35
 
 
36
    def __config_cmd
 
37
      ['namespace', 'ensemble', 'configure', self.path]
 
38
    end
 
39
    private :__config_cmd
 
40
 
 
41
    def __configinfo_struct
 
42
      {:key=>0, :alias=>nil, :db_name=>nil, :db_class=>nil, 
 
43
        :default_value=>nil, :current_value=>2}
 
44
    end
 
45
    private :__configinfo_struct
 
46
 
 
47
    def __boolval_optkeys
 
48
      ['prefixes']
 
49
    end
 
50
    private :__boolval_optkeys
 
51
 
 
52
    def __listval_optkeys
 
53
      ['map', 'subcommands', 'unknown']
 
54
    end
 
55
    private :__listval_optkeys
 
56
 
 
57
    def self.exist?(ensemble)
 
58
      bool(tk_call('namespace', 'ensemble', 'exists', ensemble))
 
59
    end
 
60
 
 
61
    def initialize(keys = {})
 
62
      @ensemble = @path = tk_call('namespace', 'ensemble', 'create', keys)
 
63
    end
 
64
 
 
65
    def cget(slot)
 
66
      if slot == :namespace || slot == 'namespace'
 
67
        ns = super(slot)
 
68
        if TkNamespace::Tk_Namespace_ID_TBL.key?(ns)
 
69
          TkNamespace::Tk_Namespace_ID_TBL[ns]
 
70
        else
 
71
          ns
 
72
        end
 
73
      else
 
74
        super(slot)
 
75
      end
 
76
    end
 
77
 
 
78
    def configinfo(slot = nil)
 
79
      if slot
 
80
        if slot == :namespace || slot == 'namespace'
 
81
          val = super(slot)
 
82
          if TkNamespace::Tk_Namespace_ID_TBL.key?(val)
 
83
            val = TkNamespace::Tk_Namespace_ID_TBL[val]
 
84
          end
 
85
        else
 
86
          val = super(slot)
 
87
        end
 
88
 
 
89
        if TkComm::GET_CONFIGINFO_AS_ARRAY
 
90
          [slot.to_s, val]
 
91
        else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
 
92
          {slot.to_s => val}
 
93
        end
 
94
 
 
95
      else
 
96
        info = super()
 
97
 
 
98
        if TkComm::GET_CONFIGINFO_AS_ARRAY
 
99
          info.map!{|inf| 
 
100
            if inf[0] == 'namespace' && 
 
101
                TkNamespace::Tk_Namespace_ID_TBL.key?(inf[-1])
 
102
              [inf[0], TkNamespace::Tk_Namespace_ID_TBL[inf[-1]]]
 
103
            else
 
104
              inf
 
105
            end
 
106
          }
 
107
        else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
 
108
          val = info['namespace']
 
109
          if TkNamespace::Tk_Namespace_ID_TBL.key?(val)
 
110
            info['namespace'] = TkNamespace::Tk_Namespace_ID_TBL[val]
 
111
          end
 
112
        end
 
113
 
 
114
        info
 
115
      end
 
116
    end
 
117
 
 
118
    def exists?
 
119
      bool(tk_call('namespace', 'ensemble', 'exists', @path))
 
120
    end
 
121
  end
 
122
 
 
123
  #####################################
 
124
 
 
125
  class ScopeArgs < Array
 
126
    include Tk
 
127
 
 
128
    # alias __tk_call             tk_call
 
129
    # alias __tk_call_without_enc tk_call_without_enc
 
130
    # alias __tk_call_with_enc    tk_call_with_enc
 
131
    def tk_call(*args)
 
132
      #super('namespace', 'eval', @namespace, *args)
 
133
      args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
 
134
      super('namespace', 'eval', @namespace, 
 
135
            TkCore::INTERP._merge_tklist(*args))
 
136
    end
 
137
    def tk_call_without_enc(*args)
 
138
      #super('namespace', 'eval', @namespace, *args)
 
139
      args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
 
140
      super('namespace', 'eval', @namespace, 
 
141
            TkCore::INTERP._merge_tklist(*args))
 
142
    end
 
143
    def tk_call_with_enc(*args)
 
144
      #super('namespace', 'eval', @namespace, *args)
 
145
      args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
 
146
      super('namespace', 'eval', @namespace, 
 
147
            TkCore::INTERP._merge_tklist(*args))
 
148
    end
 
149
 
 
150
    def initialize(namespace, *args)
 
151
      @namespace = namespace
 
152
      super(args.size)
 
153
      self.replace(args)
 
154
    end
 
155
  end
 
156
 
 
157
  #####################################
 
158
 
 
159
  class NsCode < TkObject
 
160
    def initialize(scope, use_obj_id = false)
 
161
      @scope = scope + ' '
 
162
      @use_obj_id = use_obj_id
 
163
    end
 
164
    def path
 
165
      @scope
 
166
    end
 
167
    def to_eval
 
168
      @scope
 
169
    end
 
170
    def call(*args)
 
171
      ret = TkCore::INTERP._eval_without_enc(@scope + array2tk_list(args))
 
172
      if @use_obj_id
 
173
        ret = TkNamespace::Tk_NsCode_RetObjID_TBL.delete(ret.to_i)
 
174
      end
 
175
      ret
 
176
    end
 
177
  end
 
178
 
 
179
  #####################################
 
180
 
 
181
  def install_cmd(cmd)
 
182
    lst = tk_split_simplelist(super(cmd), false, false)
 
183
    if lst[1] =~ /^::/
 
184
      lst[1] = @fullname
 
185
    else
 
186
      lst.insert(1, @fullname)
 
187
    end
 
188
    TkCore::INTERP._merge_tklist(*lst)
 
189
  end
 
190
 
 
191
  alias __tk_call             tk_call
 
192
  alias __tk_call_without_enc tk_call_without_enc
 
193
  alias __tk_call_with_enc    tk_call_with_enc
 
194
  def tk_call(*args)
 
195
    #super('namespace', 'eval', @fullname, *args)
 
196
    args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
 
197
    super('namespace', 'eval', @fullname, 
 
198
          TkCore::INTERP._merge_tklist(*args))
 
199
  end
 
200
  def tk_call_without_enc(*args)
 
201
    #super('namespace', 'eval', @fullname, *args)
 
202
    args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
 
203
    super('namespace', 'eval', @fullname,  
 
204
          TkCore::INTERP._merge_tklist(*args))
 
205
  end
 
206
  def tk_call_with_enc(*args)
 
207
    #super('namespace', 'eval', @fullname, *args)
 
208
    args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
 
209
    super('namespace', 'eval', @fullname, 
 
210
          TkCore::INTERP._merge_tklist(*args))
 
211
  end
 
212
  alias ns_tk_call             tk_call
 
213
  alias ns_tk_call_without_enc tk_call_without_enc
 
214
  alias ns_tk_call_with_enc    tk_call_with_enc
 
215
 
 
216
  def initialize(name = nil, parent = nil)
 
217
    unless name
 
218
      # name = Tk_Namespace_ID.join('')
 
219
      name = Tk_Namespace_ID.join(TkCore::INTERP._ip_id_)
 
220
      Tk_Namespace_ID[1].succ!
 
221
    end
 
222
    name = __tk_call('namespace', 'current') if name == ''
 
223
    if parent
 
224
      if parent =~ /^::/
 
225
        if name =~ /^::/
 
226
          @fullname = parent + name
 
227
        else
 
228
          @fullname = parent +'::'+ name
 
229
        end
 
230
      else
 
231
        ancestor = __tk_call('namespace', 'current')
 
232
        ancestor = '' if ancestor == '::'
 
233
        if name =~ /^::/
 
234
          @fullname = ancestor + '::' + parent + name
 
235
        else
 
236
          @fullname = ancestor + '::'+ parent +'::'+ name
 
237
        end
 
238
      end
 
239
    else # parent == nil
 
240
      ancestor = __tk_call('namespace', 'current')
 
241
      ancestor = '' if ancestor == '::'
 
242
      if name =~ /^::/
 
243
        @fullname = name
 
244
      else
 
245
        @fullname = ancestor + '::' + name
 
246
      end
 
247
    end
 
248
    @path = @fullname
 
249
    @parent = __tk_call('namespace', 'qualifiers', @fullname)
 
250
    @name = __tk_call('namespace', 'tail', @fullname)
 
251
 
 
252
    # create namespace
 
253
    __tk_call('namespace', 'eval', @fullname, '')
 
254
 
 
255
    Tk_Namespace_ID_TBL[@fullname] = self
 
256
  end
 
257
 
 
258
  def self.children(*args)
 
259
    # args ::= [<namespace>] [<pattern>]
 
260
    # <pattern> must be glob-style pattern
 
261
    tk_split_simplelist(tk_call('namespace', 'children', *args)).collect{|ns|
 
262
      # ns is fullname
 
263
      if Tk_Namespace_ID_TBL.key?(ns)
 
264
        Tk_Namespace_ID_TBL[ns]
 
265
      else
 
266
        ns
 
267
      end
 
268
    }
 
269
  end
 
270
  def children(pattern=None)
 
271
    TkNamespace.children(@fullname, pattern)
 
272
  end
 
273
 
 
274
  def self.code(script = Proc.new)
 
275
    TkNamespace.new('').code(script)
 
276
  end
 
277
=begin
 
278
  def code(script = Proc.new)
 
279
    if script.kind_of?(String)
 
280
      cmd = proc{|*args| ScopeArgs.new(@fullname,*args).instance_eval(script)}
 
281
    elsif script.kind_of?(Proc)
 
282
      cmd = proc{|*args| ScopeArgs.new(@fullname,*args).instance_eval(&script)}
 
283
    else
 
284
      fail ArgumentError, "String or Proc is expected"
 
285
    end
 
286
    TkNamespace::NsCode.new(tk_call_without_enc('namespace', 'code', 
 
287
                                                _get_eval_string(cmd, false)))
 
288
  end
 
289
=end
 
290
  def code(script = Proc.new)
 
291
    if script.kind_of?(String)
 
292
      cmd = proc{|*args|
 
293
        ret = ScopeArgs.new(@fullname,*args).instance_eval(script)
 
294
        id = ret.object_id
 
295
        TkNamespace::Tk_NsCode_RetObjID_TBL[id] = ret
 
296
        id
 
297
      }
 
298
    elsif script.kind_of?(Proc)
 
299
      cmd = proc{|*args|
 
300
        ret = ScopeArgs.new(@fullname,*args).instance_eval(&script)
 
301
        id = ret.object_id
 
302
        TkNamespace::Tk_NsCode_RetObjID_TBL[id] = ret
 
303
        id
 
304
      }
 
305
    else
 
306
      fail ArgumentError, "String or Proc is expected"
 
307
    end
 
308
    TkNamespace::NsCode.new(tk_call_without_enc('namespace', 'code', 
 
309
                                                _get_eval_string(cmd, false)), 
 
310
                            true)
 
311
  end
 
312
 
 
313
  def self.current_path
 
314
    tk_call('namespace', 'current')
 
315
  end
 
316
  def current_path
 
317
    @fullname
 
318
  end
 
319
 
 
320
  def self.current
 
321
    ns = self.current_path
 
322
    if Tk_Namespace_ID_TBL.key?(ns)
 
323
      Tk_Namespace_ID_TBL[ns]
 
324
    else
 
325
      ns
 
326
    end
 
327
  end
 
328
  def current_namespace
 
329
    # ns_tk_call('namespace', 'current')
 
330
    # @fullname
 
331
    self
 
332
  end
 
333
  alias current current_namespace
 
334
 
 
335
  def self.delete(*ns_list)
 
336
    tk_call('namespace', 'delete', *ns_list)
 
337
    ns_list.each{|ns|
 
338
      if ns.kind_of?(TkNamespace)
 
339
        Tk_Namespace_ID_TBL.delete(ns.path)
 
340
      else
 
341
        Tk_Namespace_ID_TBL.delete(ns.to_s)
 
342
      end
 
343
    }
 
344
  end
 
345
  def delete
 
346
    TkNamespece.delete(@fullname)
 
347
  end
 
348
 
 
349
  def self.ensemble_create(*keys)
 
350
    tk_call('namespace', 'ensemble', 'create', *hash_kv(keys))
 
351
  end
 
352
  def self.ensemble_configure(cmd, slot, value=None)
 
353
    if slot.kind_of?(Hash)
 
354
      tk_call('namespace', 'ensemble', 'configure', cmd, *hash_kv(slot))
 
355
    else
 
356
      tk_call('namespace', 'ensemble', 'configure', cmd, '-'+slot.to_s, value)
 
357
    end
 
358
  end
 
359
  def self.ensemble_configinfo(cmd, slot = nil)
 
360
    if slot
 
361
      tk_call('namespace', 'ensemble', 'configure', cmd, '-' + slot.to_s)
 
362
    else
 
363
      inf = {}
 
364
      Hash(*tk_split_simplelist(tk_call('namespace', 'ensemble', 'configure', cmd))).each{|k, v| inf[k[1..-1]] = v}
 
365
      inf
 
366
    end
 
367
  end
 
368
  def self.ensemble_exist?(cmd)
 
369
    bool(tk_call('namespace', 'ensemble', 'exists', cmd))
 
370
  end
 
371
 
 
372
  def self.eval(namespace, cmd = Proc.new, *args)
 
373
    #tk_call('namespace', 'eval', namespace, cmd, *args)
 
374
    TkNamespace.new(namespece).eval(cmd, *args)
 
375
  end
 
376
=begin
 
377
  def eval(cmd = Proc.new, *args)
 
378
    #TkNamespace.eval(@fullname, cmd, *args)
 
379
    #ns_tk_call(cmd, *args)
 
380
    code_obj = code(cmd)
 
381
    ret = code_obj.call(*args)
 
382
    # uninstall_cmd(TkCore::INTERP._split_tklist(code_obj.path)[-1])
 
383
    uninstall_cmd(_fromUTF8(TkCore::INTERP._split_tklist(_toUTF8(code_obj.path))[-1]))
 
384
    tk_tcl2ruby(ret)
 
385
  end
 
386
=end
 
387
  def eval(cmd = Proc.new, *args)
 
388
    code_obj = code(cmd)
 
389
    ret = code_obj.call(*args)
 
390
    uninstall_cmd(_fromUTF8(TkCore::INTERP._split_tklist(_toUTF8(code_obj.path))[-1]))
 
391
    ret
 
392
  end
 
393
 
 
394
  def self.exist?(ns)
 
395
    bool(tk_call('namespace', 'exists', ns))
 
396
  end
 
397
  def exist?
 
398
    TkNamespece.exist?(@fullname)
 
399
  end
 
400
 
 
401
  def self.export(*patterns)
 
402
    tk_call('namespace', 'export', *patterns)
 
403
  end
 
404
  def self.export_with_clear(*patterns)
 
405
    tk_call('namespace', 'export', '-clear', *patterns)
 
406
  end
 
407
  def export
 
408
    TkNamespace.export(@fullname)
 
409
  end
 
410
  def export_with_clear
 
411
    TkNamespace.export_with_clear(@fullname)
 
412
  end
 
413
 
 
414
  def self.forget(*patterns)
 
415
    tk_call('namespace', 'forget', *patterns)
 
416
  end
 
417
  def forget
 
418
    TkNamespace.forget(@fullname)
 
419
  end
 
420
 
 
421
  def self.import(*patterns)
 
422
    tk_call('namespace', 'import', *patterns)
 
423
  end
 
424
  def self.force_import(*patterns)
 
425
    tk_call('namespace', 'import', '-force', *patterns)
 
426
  end
 
427
  def import
 
428
    TkNamespace.import(@fullname)
 
429
  end
 
430
  def force_import
 
431
    TkNamespace.force_import(@fullname)
 
432
  end
 
433
 
 
434
  def self.inscope(namespace, script, *args)
 
435
    tk_call('namespace', 'inscope', namespace, script, *args)
 
436
  end
 
437
  def inscope(script, *args)
 
438
    TkNamespace.inscope(@fullname, script, *args)
 
439
  end
 
440
 
 
441
  def self.origin(cmd)
 
442
    tk_call('namespace', 'origin', cmd)
 
443
  end
 
444
 
 
445
  def self.parent(namespace=None)
 
446
    ns = tk_call('namespace', 'parent', namespace)
 
447
    if Tk_Namespace_ID_TBL.key?(ns)
 
448
      Tk_Namespace_ID_TBL[ns]
 
449
    else
 
450
      ns
 
451
    end
 
452
  end
 
453
  def parent
 
454
    tk_call('namespace', 'parent', @fullname)
 
455
  end
 
456
 
 
457
  def self.get_path
 
458
    tk_call('namespace', 'path')
 
459
  end
 
460
  def self.set_path(*namespace_list)
 
461
    tk_call('namespace', 'path', array2tk_list(namespace_list))
 
462
  end
 
463
  def set_path
 
464
    tk_call('namespace', 'path', @fullname)
 
465
  end
 
466
 
 
467
  def self.qualifiers(str)
 
468
    tk_call('namespace', 'qualifiers', str)
 
469
  end
 
470
 
 
471
  def self.tail(str)
 
472
    tk_call('namespace', 'tail', str)
 
473
  end
 
474
 
 
475
  def self.upvar(namespace, *var_pairs)
 
476
    tk_call('namespace', 'upvar', namespace, *(var_pairs.flatten))
 
477
  end
 
478
  def upvar(*var_pairs)
 
479
    TkNamespace.inscope(@fullname, *(var_pairs.flatten))
 
480
  end
 
481
 
 
482
  def self.get_unknown_handler
 
483
    tk_tcl2ruby(tk_call('namespace', 'unknown'))
 
484
  end
 
485
  def self.set_unknown_handler(cmd = Proc.new)
 
486
    tk_call('namespace', 'unknown', cmd)
 
487
  end
 
488
 
 
489
  def self.which(name)
 
490
    tk_call('namespace', 'which', name)
 
491
  end
 
492
  def self.which_command(name)
 
493
    tk_call('namespace', 'which', '-command', name)
 
494
  end
 
495
  def self.which_variable(name)
 
496
    tk_call('namespace', 'which', '-variable', name)
 
497
  end
 
498
end
 
499
 
 
500
TkNamespace::Global = TkNamespace.new('::')