2e17209388951c92cc98b69a787dd6957191d8df
[chise/ruby.git] / chise / management.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2
3 require "pathname"
4 require "fileutils"
5 require "chise/char"
6 require "chise/qp"
7
8 module CHISE
9   module TableAccessModule
10     def to_hash
11       h = {}
12       each {|k, v| h[k] = v }
13       h
14     end
15
16     def dump
17       txt = @name.path.escape.escape_win_filename.to_s+".txt"
18       t = @ds.location+@category+@keyvalue+txt
19       t.open("wb"){|out|
20         to_hash.sort.each {|k, v|
21           out.printf("%s\t%s\n", k, v)
22         }
23       }
24     end
25   end
26
27   class DataBaseManagement
28     def dump_all
29       cd = ChiseDB.instance
30       cd.each_feature {|f|
31         ft = cd.get_feature(f)
32         ft.dump
33         ft.close
34       }
35       cd.each_ccs {|ccs|
36         ct = cd.get_ccs(ccs)
37         ct.dump
38         ct.close
39       }
40     end
41   end
42
43   class DataBaseFileManagement
44
45     # from specs/char-atr.ja.txt
46     OBSOLETE_FEATURES = "
47 cns-radical
48 cns-radical?
49 kangxi-radical
50 daikanwa-radical
51
52 cns-strokes
53 kangxi-strokes
54 daikanwa-strokes
55 shinjigen-1-radical
56 gb-original-radical
57 japanese-strokes
58 jis-strokes-a
59 jisx0208-strokes
60 unicode-strokes
61
62 cns-total-strokes
63
64 non-morohashi
65
66 =>ucs*
67 #=>mojikyo
68 #=mojikyo
69 ->identical
70
71 ancient-ideograph-of
72 ancient-char-of-shinjigen-1
73 original-ideograph-of
74 original-char-of-shinjigen-1
75 vulgar-ideograph-of
76 vulgar-char-of-shinjigen-1
77 ideographic-variants
78 variant-of-shinjigen-1
79
80 iso-10646-comment
81 ".split
82
83     def initialize()
84       # @opt = {:noop=>true, :verbose=>true}
85       @opt = {:verbose=>true}
86     end
87
88     def move_obsolete_files
89       fpath = Config.instance.db_dir.path+"system-char-id"
90       fpath.chdir {
91         opath = "obsolete".path
92         opath.mkdir unless opath.directory?
93
94         OBSOLETE_FEATURES.each {|attr|
95           next if attr.nil?
96           next if /\A#/ =~ attr
97           f = attr.path.escape.escape_win_filename
98           FileUtils.mv(f.to_s, opath.to_s, @opt) if f.exist?
99           f = f.to_s+".txt"
100           FileUtils.mv(f.to_s, opath.to_s, @opt) if f.exist?
101         }
102       }
103     end
104
105     def rename_files
106       path = Config.instance.db_dir.path
107
108       nfpath = path+"character/feature"
109       FileUtils.mkdir_p(nfpath.to_s, @opt) unless nfpath.directory?
110
111       fpath = path+"system-char-id"
112       fpath.each_entry {|f|
113         next if /\A\./ =~ f
114         FileUtils.mv((fpath+f).to_s, nfpath.to_s, @opt)
115       }
116
117       ncpath = path+"character/by_feature"
118       FileUtils.mkdir_p(ncpath.to_s, @opt) unless ncpath.directory?
119
120       path.each_entry {|f|
121         next if /\A\./ =~ f
122         next if f.to_s == "character"
123         d = path + f
124         next unless d.directory?
125         ff = d + "system-char-id"
126         FileUtils.mv(ff.to_s, (ncpath+f).to_s, @opt) if ff.exist?
127       }
128     end
129   end
130 end