aboutsummaryrefslogtreecommitdiffstats
path: root/core/utils.rb
blob: 843026bc585e481813ded500ded3eecddc5dd633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# -*- coding: utf-8 -*-
=begin rdoc
CHI内部で共通で使われるユーティリティ。
=end

if defined? HYDE
  raise "HYDEが再定義されました。utils.rbを2回Linkしてるんじゃないか?それはいけない。すぐにバグをSHOT AT THE DEVILしろ。"
  # こんなふうにするといいと想います
  require File.expand_path(File.join(File.dirname(__FILE__), 'utils'))
end

require 'thread'
require 'monitor'
require "open-uri"

$atomic = Monitor.new
$log_lock = Mutex.new

# 基本的な単位であり、数学的にも重要なマジックナンバーで、至るところで使われる。
# これが言語仕様に含まれていないRubyは正直気が狂っていると思う。
# http://ja.uncyclopedia.info/wiki/Hyde
HYDE = 156

# Rubyのバージョンを配列で。あると便利。
RUBY_VERSION_ARRAY = RUBY_VERSION.split('.').map{ |i| i.to_i }.freeze

require File.join(File::dirname(__FILE__), 'miquire')

[File::dirname(__FILE__),
 File.join(File::dirname(__FILE__), 'lib'),
 File.join(File::dirname(__FILE__), 'miku')].each{|path|
  $LOAD_PATH.push(File.expand_path(File.join(Dir.pwd, path)))
}

miquire :lib, 'lazy'

# すべてのクラスにメモ化機能を
miquire :lib, 'memoist'
include Memoist

# Environment::CONFROOT内のファイル名を得る。
#   confroot(*path)
# は
#   File::expand_path(File.join(Environment::CONFROOT, *path))
# と等価。
def confroot(*path)
  File::expand_path(File.join(Environment::CONFROOT, *path))
end
miquire :core, 'environment'

# _num_ 番目の引数をそのまま返す関数を返す
def ret_nth(num=0)
  lambda { |*arg| arg[num] } end

# スレッドセーフなカウンタを返す。
# カウンタの初期値は _count_ で、呼び出すたびに値が _increment_ づつ増える。
# なお、カウンタが返す値はインクリメント前の値。
def gen_counter(count=0, increment=1)
  mutex = Mutex.new
  lambda{
    mutex.synchronize{
      result = count
      count += increment
      result } } end

# ファイルの内容を文字列に読み込む
def file_get_contents(fn)
  open(fn, 'rb:utf-8'.freeze){ |input|
    input.read
  }
end

# 文字列をファイルに書き込む
def file_put_contents(fn, body)
  File.open(fn, 'wb'.freeze){ |put|
    put.write body
    body
  }
end

# ファイル _fn_ の内容からオブジェクトを読み込む。
# _fn_ は、object_put_contents() で保存されたファイルでなければならない。
def object_get_contents(fn)
  File.open(fn, 'rb'.freeze){ |input|
    Marshal.load input
  }
end

# UNIXコマンド _cmd_ が存在するか否かを返す。
def command_exist?(cmd)
  system("which #{cmd} > /dev/null")
end

# 存在するかわからないrubyファイル _file_ を読み込む。
# ただし、_file_ が存在しない場合は例外を投げずにfalseを返す。
def require_if_exist(file)
  begin
    require file
    true
  rescue LoadError
    warn "require-if-exist: file not found: #{file}"
    false end end

# _insertion_ を、 _src_ の挿入するべき場所のインデックスを返す。
# _order_ は順番を表す配列で、 _src_ 内のオブジェクトの前後関係を表す。
# _order_ 内に _insertion_ が存在しない場合は一番最後のインデックスを返す
def where_should_insert_it(insertion, src, order)
  if(order.include?(insertion)) then
    src.dup.push(insertion).sort_by{|a|
      order.index(a) or 65536
    }.index(insertion)
  else
    src.size end end

# 一般メッセージを表示する。
def notice(msg)
  log "notice", msg if Mopt.error_level >= 3
end

# 警告メッセージを表示する。
def warn(msg)
  log "warning", msg if Mopt.error_level >= 2
end

# エラーメッセージを表示する。
def error(msg)
  log "error", msg if Mopt.error_level >= 1
  abort if Mopt.error_level >= 4
end

# --debug オプション付きで起動されている場合、インタプリタに入る。
# ==== Args
# [exception] 原因となった例外
# [binding] インタプリタを実行するスコープ
# ==== Return
# インタプリタから復帰したら(インタプリタが起動されたら)true、
# デバッグモードではない、pryがインストールされていない等、起動に失敗したらfalse
def into_debug_mode(exception = nil, bind = binding)
  if Mopt.debug and not Mopt.testing
    require_if_exist 'pry'
    if binding.respond_to?(:pry)
      log "error", exception if exception
      $into_debug_mode_lock.synchronize {
        begin
          $into_debug_mode = Set.new
          bind.pry
        ensure
          threads = $into_debug_mode
          $into_debug_mode = false
          threads.each &:wakeup end }
      return true end end end
$into_debug_mode = false
$into_debug_mode_lock = Monitor.new

# 他のスレッドでinto_debug_modeが呼ばれているなら、それが終わるまでカレントスレッドをスリープさせる
def debugging_wait
  if $into_debug_mode
    $into_debug_mode << Thread.current
    Thread.stop end end

# 引数のチェックをすべてパスした場合のみブロックを実行する
# チェックに引っかかった項目があればwarnを出力してブロックは実行せずにnilを返す。
# チェックはassocできる配列か、Hashで定義する。
#  type_check(value => nil,              # チェックしない(常にパス)
#             value => Module,           # その型とis_a?関係ならパス
#             value => [:method, *args], # value.method(*args)が真を返せばパス
#             value => lambda{ |x| ...}) # xにvalueを渡して実行し、真を返せばパス
# チェックをすべてパスしたかどうかを真偽値で返す。
# ブロックが指定されていれば、それを実行してブロックの実行結果を返す
# メモ: いずれかのタイプに一致するチェックを定義するにはtcorを使う
#   type_check object => tcor(Array, Hash)
def type_check(args, &proc)
  check_function = lambda{ |val, check|
    if not check
      true
    elsif check.is_a? Array
      val.__send__(*check)
    elsif check.is_a? Symbol
      val.respond_to?(check)
    elsif check.is_a?(Class) or check.is_a?(Module)
      val.is_a?(check)
    elsif check.respond_to?(:call)
      check.call(val) end }
  error = args.find{ |a| not(check_function.call(*a)) }
  if(error)
    warn "argument error: #{error[0].inspect} is not passed #{error[1].inspect}"
    warn "in #{caller_util}"
    false
  else
    if proc
      proc.call
    else
      true end end end

# _types_ のうちいずれかとis_a?関係ならtrueを返すProcオブジェクトを返す
def tcor(*types)
  lambda{ |v| types.any?{ |c| v.is_a?(c) } } end

# type_checkと同じだが、チェックをパスしなかった場合にabortする
# type_checkの戻り値を返す
def type_strict(args, &proc)
  result = type_check(args, &proc)
  if not result
    into_debug_mode(binding)
    raise ArgumentError.new end
  result end

# blockの評価結果がチェックをパスしなかった場合にabortする
def result_strict(must, &block)
  result = block.call
  type_strict(result => must)
  result
end

# メインスレッド以外で呼び出されたらThreadErrorを投げる
def mainthread_only
  unless Thread.main == Thread.current
    raise ThreadError.new('The method can calls only main thread. but called by another thread.') end end

# メインスレッドで呼び出されたらThreadErrorを投げる
def no_mainthread
  if Thread.main == Thread.current
    raise ThreadError.new('The method can not calls main thread. but called by main thread.') end end

# utils.rbのメソッドを呼び出した最初のバックトレースを返す
def caller_util
  caller.each{ |result|
    return result unless /utils\.rb/ === result } end

# エラーログに記録する。
# 内部処理用。外部からは呼び出さないこと。
def log(prefix, object)
  debugging_wait
  begin
    msg = "#{prefix}: #{caller_util}: #{object}"
    msg += "\nfrom " + object.backtrace.join("\nfrom ") if object.is_a? Exception
    unless $daemon
      $log_lock.synchronize do
        if msg.is_a? Exception
          __write_stderr(msg.to_s)
          __write_stderr(msg.backtrace.join("\n"))
        else
          __write_stderr(msg) end end
      if logfile
        FileUtils.mkdir_p(File.expand_path(File.dirname(logfile + '_')))
        File.open(File.expand_path("#{logfile}#{Time.now.strftime('%Y-%m-%d')}.log"), 'a'){ |wp|
          wp.write("#{Time.now.to_s}: #{msg}\n") } end end
  rescue Exception => e
    __write_stderr("critical!: #{caller(0)}: #{e.to_s}")
  end
end

FOLLOW_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..'))
def __write_stderr (msg)
  $stderr.write(msg.gsub(FOLLOW_DIR, '{MIKUTTER_DIR}')+"\n")
end

# 環境や設定の不備で終了する。msgには、何が原因かを文字列で渡す。このメソッドは
# 処理を返さずにアボートする。
def chi_fatal_alert(msg)
  require_if_exist 'gtk2'
  if defined?(Gtk::MessageDialog)
    dialog = Gtk::MessageDialog.new(nil,
                                    Gtk::Dialog::DESTROY_WITH_PARENT,
                                    Gtk::MessageDialog::ERROR,
                                    Gtk::MessageDialog::BUTTONS_CLOSE,
                                    "#{Environment::NAME} エラー")
    dialog.secondary_text = msg.to_s
    dialog.run
    dialog.destroy end
  puts msg.to_s
  abort end

#ログファイルを取得設定
def logfile(fn = nil)
  if(fn) then
    $logfile = fn
  end
  $logfile or nil
end

# 共通のMutexで処理を保護して実行する。
# atomicブロックで囲まれたコードは、別々のスレッドで同時に実行されない。
def atomic
  $atomic.synchronize{ yield }
end

# コマンドをバックグラウンドで起動することを覗いては system() と同じ
def bg_system(*args)
  Process.detach(spawn(*args))
end

class Module
  # ハッシュ用のアクセサ。最初から空の連想配列が入っている
  # 引数なしで呼び出すとハッシュ自身を返し、1つ引数を与えると、引数をキーにハッシュの値を返す
  def attr_hash_accessor(*names)
    names.each { |name|
      hash = {}
      define_method(name){ |*args|
        case args.size
        when 0
          hash
        when 1
          hash[args[0]]
        when 2
          hash[args[0]] = args[1] end } } end end

class Object

  # freezeできるならtrueを返す
  def freezable?
    true end

  # freezeできる場合はfreezeする。selfを返す
  def freeze_ifn
    freeze if freezable?
    self end

  # freezeされていない同じ内容のオブジェクトを作って返す。
  # メルト 溶けてしまいそう (実装が)dupだなんて 絶対に 言えない
  def melt
    if frozen? then dup else self end end end

#
# Numeric
#

class Numeric
  def freezable?
    false end end

#
# Array
#
class Array
  include Comparable

  def symbolize
    result = []
    each { |val|
      result << if val.respond_to?(:symbolize) then val.symbolize else val end }
    result
  end

end

class Hash
  # キーの名前を変換する。
  def convert_key(rule = nil)
    result = {}
    self.each_pair { |key, val|
      if rule[key]
        result[rule[key]] = val
      else
        result[key.to_sym] = val end }
    result end

  # キーを全てto_symしたhashを新たにつくる
  def symbolize
    result = {}
    each_pair { |key, val|
      result[key.to_sym] = if val.respond_to?(:symbolize) then val.symbolize else val end }
    result
  end
end

#
# String
#

class String
  def match_regexp(str)
    if(str.is_a? String)
      match(Regexp.new(str))
    else
      match(str) end end

  def matches(regexp)
    warn "String#matches is obsolete method. use String#scan"
    result = []
    each_matches(regexp){ |m, pos|
      result << m.to_s }
    result
  end

  def each_matches(regexp, &proc) # :yield: match, byte_index, char_intex
    warn "String#each_matches is obsolete method. use String#scan"
    pos = 0
    str = self
    while(match = regexp.match(str))
      if(proc.arity == 1)
        proc.call(match)
      elsif(proc.arity == 2)
        proc.call(match, pos + match.begin(0))
      else
        proc.call(match, pos + match.begin(0), pos + match.begin(0)) end
      str = match.post_match
      pos += match.end(0) end end

  # _byte_ バイト目が何文字目にあたるかを返す
  def get_index_from_byte(byte)
    result = 0
    split(//u).each{ |c|
      byte -= c.to_enum(:each_byte).to_a.size
      return result if(byte < 0)
      result += 1 }
    result end

  def inspect
    '"'+to_s+'"'
  end

end

class Symbol
  def freezable?
    false end end

class TrueClass
  def freezable?
    false end end

class FalseClass
  def freezable?
    false end end

class NilClass
  def freezable?
    false end end

class Regexp
  def to_json(*a)
    {
      'json_class'   => self.class.name,
      'data'         => to_s
    }.to_json(*a)
  end

  def self.json_create(o)
    new(o['data'])
  end
end