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