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