781a3ec3aeddb75837beae50d1a9ce764db42e81
[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   class DataBaseManagement
10     def dump_all
11       cd = ChiseDB.instance
12       cd.each_feature_name {|f|
13         ft = cd.get_feature(f)
14         ft.dump
15         ft.close
16       }
17       cd.each_ccs {|ccs|
18         ct = cd.get_ccs(ccs)
19         ct.dump
20         ct.close
21       }
22     end
23   end
24
25   class DataBaseFileManagement
26
27     # from specs/char-atr.ja.txt
28     OBSOLETE_FEATURES = "
29 cns-radical
30 cns-radical?
31 kangxi-radical
32 daikanwa-radical
33
34 cns-strokes
35 kangxi-strokes
36 daikanwa-strokes
37 shinjigen-1-radical
38 gb-original-radical
39 japanese-strokes
40 jis-strokes-a
41 jisx0208-strokes
42 unicode-strokes
43
44 cns-total-strokes
45
46 non-morohashi
47
48 =>ucs*
49 #=>mojikyo
50 #=mojikyo
51 ->identical
52
53 ancient-ideograph-of
54 ancient-char-of-shinjigen-1
55 original-ideograph-of
56 original-char-of-shinjigen-1
57 vulgar-ideograph-of
58 vulgar-char-of-shinjigen-1
59 ideographic-variants
60 variant-of-shinjigen-1
61
62 iso-10646-comment
63 ".split
64
65     def initialize()
66       # @opt = {:noop=>true, :verbose=>true}
67       @opt = {:verbose=>true}
68     end
69
70     def move_obsolete_files
71       #fpath = Config.instance.db_dir.path+"system-char-id"
72       fpath = Config.instance.db_dir.path+"character/feature"
73       #fpath.chdir {
74       Dir.chdir(fpath.to_s) {
75         opath = "obsolete".path
76         opath.mkdir unless opath.directory?
77
78         OBSOLETE_FEATURES.each {|attr|
79           next if attr.nil?
80           next if /\A#/ =~ attr
81           f = attr.path.escape.escape_win_filename
82           FileUtils.mv(f.to_s, opath.to_s, @opt) if f.exist?
83           f = (f.to_s+".txt").path
84           FileUtils.mv(f.to_s, opath.to_s, @opt) if f.exist?
85         }
86       }
87     end
88
89     def rename_files
90       path = Config.instance.db_dir.path
91
92       nfpath = path+"character/feature"
93       FileUtils.mkdir_p(nfpath.to_s, @opt) unless nfpath.directory?
94
95       fpath = path+"system-char-id"
96       fpath.each_entry {|f|
97         next if /\A\./ =~ f
98         FileUtils.mv((fpath+f).to_s, nfpath.to_s, @opt)
99       }
100
101       ncpath = path+"character/by_feature"
102       FileUtils.mkdir_p(ncpath.to_s, @opt) unless ncpath.directory?
103
104       path.each_entry {|f|
105         next if /\A\./ =~ f
106         next if f.to_s == "character"
107         d = path + f
108         next unless d.directory?
109         ff = d + "system-char-id"
110         FileUtils.mv(ff.to_s, (ncpath+f).to_s, @opt) if ff.exist?
111       }
112     end
113   end
114 end