ykutils 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ # -*- coding utf-8 -*-
2
+
3
+ require 'nkf'
4
+
5
+ module Ykutils
6
+ module NKFUTIL
7
+ #ASCII-8BIT
8
+ #US-ASCII
9
+ CODE_TO_NAME = Hash.new("ASCII")
10
+ CODE_TO_NAME["US-ASCII"] = "ASCII"
11
+ CODE_TO_NAME["stateless-ISO-2022-JP"] = "JIS"
12
+ CODE_TO_NAME["ISO-2022-JP"] = "JIS"
13
+ CODE_TO_NAME["ISO-2022-JP-2"] = "JIS"
14
+ CODE_TO_NAME["EUC-JP"] = "EUC"
15
+ CODE_TO_NAME["eucJP-ms"] = "EUC"
16
+ CODE_TO_NAME["Shift_JIS"] = "SJIS"
17
+ CODE_TO_NAME["Windows-31J"] = "SJIS"
18
+ CODE_TO_NAME["ASCII-8BIT"] = "BINARY"
19
+ CODE_TO_NAME["UTF-8"] = "UTF8"
20
+
21
+ NAME_TO_ENCODING = Hash.new("US-ASCII")
22
+ NAME_TO_ENCODING["UTF8"] = "UTF-8"
23
+ NAME_TO_ENCODING["ASCII"] = "US-ASCII"
24
+ NAME_TO_ENCODING["JIS"] = "ISO-2022-JP"
25
+ NAME_TO_ENCODING["EUC"] = "EUC-JP"
26
+ NAME_TO_ENCODING["SJIS"] = "Winodws-31J"
27
+ NAME_TO_ENCODING["BINARY"] = "ASCII-8BIT"
28
+
29
+ def NKFUTIL.guess_encoding(str)
30
+ puts "**#{str.encoding}"
31
+ CODE_TO_NAME[ str.encoding.to_s ]
32
+ end
33
+
34
+ def NKFUTIL.config( src_encoding, dest_encoding , misc_option = nil )
35
+ end
36
+
37
+ class Assoc
38
+ @@hs = {}
39
+ @@config = nil
40
+
41
+ def Assoc.set( key, value )
42
+ if value
43
+ @@hs[key] = Assoc.convert( value )
44
+ else
45
+ @@hs[key] = value
46
+ end
47
+ end
48
+
49
+ def Assoc.get( key )
50
+ @@hs[key]
51
+ end
52
+
53
+ def Assoc.to_nkf_encoding_format( encoding )
54
+ NAME_TO_ENCODING[ encoding ]
55
+ end
56
+
57
+
58
+ def Assoc.config( src_encoding, dest_encoding , misc_option = nil )
59
+ @@config = dest_encoding
60
+ end
61
+
62
+ def Assoc.auto_config_to_inner( str , misc_option = nil )
63
+ if str
64
+ src_encoding = Assoc.to_nkf_encoding_format( NKFUTIL.guess_encoding( str ) )
65
+ else
66
+ src_encoding = "US-ASCII"
67
+ end
68
+
69
+ # inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
70
+ # if inner_encoding != "US-ASCII"
71
+ # @@config = inner_encoding
72
+ @@config = nil
73
+ # end
74
+ src_encoding
75
+ end
76
+
77
+ def Assoc.auto_config_from_inner( dest_enc , misc_option = nil )
78
+ dest_encoding = Assoc.to_nkf_encoding_format( dest_enc )
79
+ # inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
80
+ # if inner_encoding != "US-ASCII"
81
+ # @@config = dest_encoding.downcase
82
+ @@config = dest_encoding
83
+ # end
84
+ end
85
+
86
+ def Assoc.convert( str )
87
+ nstr = nil
88
+ if str != nil
89
+ if @@config != nil
90
+ begin
91
+ # nstr = NKF.nkf( @@config , str )
92
+ nstr = str.encode( @@config )
93
+ rescue => ex
94
+ puts ex
95
+ puts "========="
96
+ pp caller(0)
97
+ end
98
+ else
99
+ nstr = str
100
+ end
101
+ end
102
+ nstr
103
+ end
104
+
105
+ # def Assoc.get_inner_encoding
106
+ # @@inner_encoding = ($KCODE != "NONE") ? $KCODE : "ASCII"
107
+ # end
108
+ end
109
+
110
+ def NKFUTIL.set( key, value )
111
+ Assoc.set( key , value )
112
+ end
113
+
114
+ def NKFUTIL.get( key )
115
+ Assoc.get( key )
116
+ end
117
+
118
+ def NKFUTIL.convert( str )
119
+ if str != nil
120
+ Assoc.convert( str )
121
+ else
122
+ ""
123
+ end
124
+ end
125
+
126
+ def NKFUTIL.assoc_equal( target , key )
127
+ target == key || target == Assoc.get( key )
128
+ end
129
+
130
+ def NKFUTIL.config( src_encoding, dest_encoding , misc_option = nil )
131
+ Assoc.config( src_encoding, dest_encoding , misc_option )
132
+ end
133
+
134
+ def NKFUTIL.auto_config_to_inner( str, misc_option = nil )
135
+ Assoc.auto_config_to_inner( str , misc_option )
136
+ end
137
+
138
+ def NKFUTIL.auto_config_from_inner( dest_encoding , misc_option = nil )
139
+ Assoc.auto_config_to_inner( dest_encoding , misc_option )
140
+ end
141
+
142
+ def puts_sj( line )
143
+ puts line.encod( NAME_TO_ENCODING["SJIS"] )
144
+ # puts NKF.nkf( "-Ws -m0" , line )
145
+ end
146
+
147
+ def puts_u( line )
148
+ puts line.encod( NAME_TO_ENCODING["UTF8"] )
149
+ puts NKF.nkf( "-Sw -m0" , line )
150
+ end
151
+
152
+ def nkf_utf8_file( infname , outfname )
153
+ File.open( outfname ){|outf|
154
+ File.open( infname ){|file|
155
+ while line = file.gets
156
+ line.chomp!
157
+ # oline = NKF.nkf( "-w -m0" , line )
158
+ oline = line.encode( NAME_TO_ENCODING["UTF8"] )
159
+ outf.printf("%s\n" , oline )
160
+ end
161
+ }
162
+ }
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,19 @@
1
+ require 'pathname'
2
+
3
+ module Ykutils
4
+ class OSUtil
5
+ @@os = nil
6
+
7
+ def OSUtil.runtime
8
+ unless @@os
9
+ if Pathname.pwd.to_s =~ /^\/cygdrive/
10
+ @@os=:CYGWIN
11
+ else
12
+ @@os=:ELSE
13
+ end
14
+ end
15
+ @@os
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,37 @@
1
+ require 'pathname'
2
+
3
+ module Ykutils
4
+ module PathOp
5
+ def get_buddy_path( fname , append_name = "" , extname = "" )
6
+ one_path = Pathname.new( fname ).expand_path
7
+ dir_path = one_path.dirname
8
+ base_path = one_path.basename(".*")
9
+ append_name = "-2" unless (append_name.empty? or extname.empty?)
10
+ extname ||= one_path.extname
11
+
12
+ [one_path , dir_path.join( base_path.to_s + append_name + extname )]
13
+ end
14
+
15
+ def determine_fname_for_update( fname , ext = ".bak")
16
+ get_buddy_path( fname , "" , ext )
17
+ end
18
+
19
+ def determine_fname_for_update2( fname )
20
+ begin
21
+ ctime = File.ctime( fname )
22
+ rescue => ex
23
+ end
24
+ ctime = Time.now unless ctime
25
+ ary = ctime.to_s.split(" ")
26
+ extname = File.extname( fname )
27
+ append = ["", ary[0] , ary[1].gsub(":","-")].join("-")
28
+ get_buddy_path( fname , append , extname )
29
+ end
30
+
31
+ def file_ensure( fname )
32
+ File.open( fname , "w" ).close unless File.exist?( fname )
33
+ end
34
+
35
+ end
36
+ end
37
+
@@ -0,0 +1,50 @@
1
+ module Ykutils
2
+
3
+ class RetCode
4
+ attr_reader :val , :mes, :ret , :bool
5
+
6
+ def initialize(obj)
7
+ @val = obj
8
+ @mes = obj["mes"]
9
+ @ret = obj["ret"]
10
+ @bool = obj["bool"]
11
+ end
12
+
13
+ def [](key)
14
+ @val[key]
15
+ end
16
+
17
+ def to_s
18
+ @bool
19
+ end
20
+
21
+ def set_bool( val )
22
+ @bool = val
23
+ end
24
+
25
+ def get_bool
26
+ @bool
27
+ end
28
+
29
+ def set_mes( val )
30
+ @mes = val
31
+ end
32
+
33
+ def set_ret( val )
34
+ @ret = val
35
+ end
36
+ end
37
+
38
+ class RetCode2 < RetCode
39
+ attr_reader :val
40
+ attr_accessor :mes, :ret , :bool
41
+
42
+ def initialize( ret , bool, mes )
43
+ @val = { "ret" => ret , "bool" => bool, "mes" => mes }
44
+ @ret = ret
45
+ @bool = bool
46
+ @mes = mes
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,213 @@
1
+ # -*- coding utf-8 -*-
2
+
3
+ require 'pp'
4
+ require 'yaml'
5
+ require 'csv'
6
+ require 'ykutils/treemanagera'
7
+ if RUBY_VERSION >= "1.8.7"
8
+ require 'ykutils/nkfutil19'
9
+ else
10
+ require 'ykutils/nkfutil'
11
+ end
12
+ require 'ykutils/debugutils'
13
+
14
+ module Ykutils
15
+
16
+ module SpecFileOp
17
+ include DebugUtils
18
+ include NKFUTIL
19
+
20
+ def valid?
21
+ @valid
22
+ end
23
+
24
+ def open_for_write( fname )
25
+ begin
26
+ fileobj = File.open( fname , "w")
27
+ rescue => ex
28
+ pp ex
29
+ pp ex.backtrace
30
+ @valid = false
31
+ end
32
+ fileobj
33
+ end
34
+
35
+ def dump_yaml_fileobj( obj , fileobj )
36
+ begin
37
+ YAML.dump( obj , fileobj)
38
+ rescue => ex
39
+ pp ex
40
+ pp ex.backtrace
41
+ @valid = false
42
+ end
43
+ end
44
+
45
+ def save_yaml_file( obj , fname )
46
+ begin
47
+ File.open( fname , "w"){ |fileobj|
48
+ YAML.dump( obj , fileobj )
49
+ }
50
+ rescue => ex
51
+ pp ex
52
+ pp ex.backtrace
53
+ @valid = false
54
+ end
55
+ end
56
+
57
+ def load_yaml_file( fname )
58
+ @data = nil
59
+ begin
60
+ @data = YAML.load_file(fname)
61
+ rescue => ex
62
+ pp ex
63
+ pp ex.backtrace
64
+ @valid = false
65
+ end
66
+ @data
67
+ end
68
+
69
+ def parse_yaml_file( fname )
70
+ begin
71
+ @data = YAML.parse( File.read(fname) )
72
+ rescue => ex
73
+ pp ex
74
+ pp ex.backtrace
75
+ @valid = false
76
+ end
77
+ @data
78
+ end
79
+
80
+ def expand_data( data , separator , except_ary = [] )
81
+ tm = TreeManager.new
82
+ re = Regexp.new( separator + "([^#{separator[0,1]}]+)" + separator )
83
+
84
+ data.each do |k,v|
85
+ next unless v
86
+ v.scan(re).flatten.each do |it|
87
+ if it
88
+ tm.add( it , k )
89
+ tm.addTag( k , it )
90
+ else
91
+ tm.add( nil , k )
92
+ end
93
+ end
94
+ end
95
+
96
+ tm.tsort.reverse.each do |k|
97
+ next unless data[k]
98
+
99
+ tag = tm.getTag(k)
100
+ if tag
101
+ i=0
102
+ tag.each do |it|
103
+ ntag = Regexp.new( separator + it + separator )
104
+ if data[it]
105
+ data[k] = data[k].sub( ntag , data[it] )
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ data.each do |k,v|
112
+ next unless v
113
+ ary = v.scan(re).flatten
114
+ i = 0
115
+ if ary and ary.size > 0
116
+ except_ary.each do |it|
117
+ unless ary.index(it)
118
+ i += 1
119
+ end
120
+ end
121
+ end
122
+ if i > 0
123
+ puts "#{k} fails to exapnd data. value is #{v}"
124
+ @valid = false
125
+ end
126
+ end
127
+
128
+ data
129
+ end
130
+
131
+ def make_data_complement(item_ary, data , common )
132
+ item_ary.each do |it|
133
+ unless data[it] and data[it].strip != ""
134
+ data[it] = common[it]
135
+ end
136
+ end
137
+ end
138
+
139
+ def check_data_complement( item_ary , data )
140
+ mes_ary = []
141
+ item_ary.each do |k|
142
+ unless @data[k]
143
+ mes_ary << "Specify #{k} "
144
+ @valid = false
145
+ end
146
+ end
147
+ mes_ary
148
+ end
149
+
150
+ def check_data_complement_print_message( item_ary , data )
151
+ mes_ary = check_data_complement( item_ary , data )
152
+ mes_ary.each do |mes|
153
+ puts mes
154
+ end
155
+ end
156
+
157
+ def load_csv_file( fname )
158
+ ary = []
159
+ begin
160
+ CSV.open( fname , 'r') do |row|
161
+ next unless row
162
+
163
+ ary << row[0]
164
+ end
165
+ rescue => ex
166
+ pp ex
167
+ pp ex.backtrace
168
+ @valid = false
169
+ end
170
+ ary
171
+ end
172
+
173
+ def load_csv_file_ex( fname )
174
+ ary = []
175
+ begin
176
+ CSV.open( fname , 'r') do |row|
177
+ next unless row
178
+
179
+ ary.concat( row )
180
+ end
181
+ rescue => ex
182
+ pp ex
183
+ pp ex.backtrace
184
+ @valid = false
185
+ end
186
+ ary
187
+ end
188
+
189
+ def load_plain_text_file( fname )
190
+ ary = []
191
+
192
+ begin
193
+ ary0 = File.readlines(fname)
194
+ senc = NKFUTIL.auto_config_to_inner( ary0.join )
195
+
196
+ ary = ary0.select{ |x| x != nil }.collect{ |x|
197
+ if x != nil then
198
+ NKFUTIL.convert( x )
199
+ else
200
+ ""
201
+ end
202
+ }
203
+ rescue => ex
204
+ pp ex
205
+ pp ex.backtrace
206
+ @valid = false
207
+
208
+ ary = []
209
+ end
210
+ ary
211
+ end
212
+ end
213
+ end