ykutils 0.1.7 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,31 +1,29 @@
1
1
  module Ykutils
2
- require 'find'
3
2
  class Nginxconfigfiles
4
- def get_file_list(start_dir, re)
3
+ def get_file_list(start_dir_pn, reg)
5
4
  @file_list = []
6
5
 
7
- Find.find(start_dir) { |x|
8
- if x =~ re
9
- # puts x
10
- @file_list << x
11
- Find.prune
12
- end
6
+ start_dir_pn.find { |x|
7
+ next unless x.to_s =~ reg
8
+
9
+ # puts x
10
+ @file_list << x
13
11
  }
14
12
  @file_list
15
13
  end
16
14
 
17
15
  def output(file_list)
18
- file_list.map { |fname|
16
+ file_list.map do |fname|
19
17
  parent_dir_pn = Pathname.new(fname).cleanpath.parent
20
18
  vdomain = parent_dir_pn.basename
21
19
  output_fname = "#{vdomain}.conf"
22
20
  cli = Nginxconfig.new(fname)
23
21
  scope = nil
24
- File.open(output_fname, "w") { |f|
22
+ File.open(output_fname, "w") do |f|
25
23
  x = cli.extract(scope)
26
24
  f.write(x)
27
- }
28
- }
25
+ end
26
+ end
29
27
  end
30
28
  end
31
29
  end
@@ -16,19 +16,19 @@ module Ykutils
16
16
  end
17
17
 
18
18
  class Assoc
19
- @@hs = {}
20
- @@config = nil
19
+ @hs = {}
20
+ @config = nil
21
21
 
22
22
  def self.set(key, value)
23
- @@hs[key] = if value
24
- Assoc.convert(value)
25
- else
26
- value
27
- end
23
+ @hs[key] = if value
24
+ Assoc.convert(value)
25
+ else
26
+ value
27
+ end
28
28
  end
29
29
 
30
30
  def self.get(key)
31
- @@hs[key]
31
+ @hs[key]
32
32
  end
33
33
 
34
34
  def self.to_nkf_encoding_format(encoding)
@@ -41,8 +41,8 @@ module Ykutils
41
41
  end
42
42
 
43
43
  def self.config(src_encoding, dest_encoding, misc_option = nil)
44
- @@config = "-#{dest_encoding.to_s[0, 1].downcase}#{src_encoding.to_s[0, 1].upcase}"
45
- @@config += " #{misc_option}" unless misc_option.nil?
44
+ @config = "-#{dest_encoding.to_s[0, 1].downcase}#{src_encoding.to_s[0, 1].upcase}"
45
+ @config += " #{misc_option}" unless misc_option.nil?
46
46
  end
47
47
 
48
48
  def self.auto_config_to_inner(str, misc_option = nil)
@@ -55,8 +55,8 @@ module Ykutils
55
55
 
56
56
  inner_encoding = Assoc.to_nkf_encoding_format(Assoc.get_inner_encoding)
57
57
  if inner_encoding != "A"
58
- @@config = "-#{inner_encoding.downcase}#{src_encoding.upcase}"
59
- @@config += " #{misc_option}" unless misc_option.nil?
58
+ @config = "-#{inner_encoding.downcase}#{src_encoding.upcase}"
59
+ @config += " #{misc_option}" unless misc_option.nil?
60
60
  end
61
61
  src_encoding
62
62
  end
@@ -64,20 +64,20 @@ module Ykutils
64
64
  def self.auto_config_from_inner(dest_enc, misc_option = nil)
65
65
  dest_encoding = Assoc.to_nkf_encoding_format(dest_enc)
66
66
  inner_encoding = Assoc.to_nkf_encoding_format(Assoc.get_inner_encoding)
67
- if inner_encoding != "A"
68
- @@config = "-#{dest_encoding.downcase}#{inner_encoding.upcase}"
69
- @@config += " #{misc_option}" unless misc_option.nil?
70
- end
67
+ return unless inner_encoding != "A"
68
+
69
+ @config = "-#{dest_encoding.downcase}#{inner_encoding.upcase}"
70
+ @config += " #{misc_option}" unless misc_option.nil?
71
71
  end
72
72
 
73
73
  def self.convert(str)
74
74
  nstr = nil
75
75
  unless str.nil?
76
- if @@config.nil?
76
+ if @config.nil?
77
77
  nstr = str
78
78
  else
79
79
  begin
80
- nstr = NKF.nkf(@@config, str)
80
+ nstr = NKF.nkf(@config, str)
81
81
  rescue StandardError => e
82
82
  puts e
83
83
  puts "========="
@@ -88,8 +88,8 @@ module Ykutils
88
88
  nstr
89
89
  end
90
90
 
91
- def self.get_inner_encoding
92
- @@inner_encoding = $KCODE == "NONE" ? "ASCII" : $KCODE
91
+ def self.inner_encoding
92
+ @inner_encoding = $KCODE == "NONE" ? "ASCII" : $KCODE
93
93
  end
94
94
  end
95
95
 
@@ -22,7 +22,7 @@ module Ykutils
22
22
 
23
23
  begin
24
24
  NAME_TO_ENCODING
25
- rescue => ex
25
+ rescue StandardError
26
26
  NAME_TO_ENCODING = Hash.new("US-ASCII")
27
27
  NAME_TO_ENCODING["UTF8"] = "UTF-8"
28
28
  NAME_TO_ENCODING["ASCII"] = "US-ASCII"
@@ -38,19 +38,19 @@ module Ykutils
38
38
  end
39
39
 
40
40
  class Assoc
41
- @@hs = {}
42
- @@config = nil
41
+ @hs = {}
42
+ @config = nil
43
43
 
44
44
  def self.set(key, value)
45
- @@hs[key] = if value
46
- Assoc.convert(value)
47
- else
48
- value
49
- end
45
+ @hs[key] = if value
46
+ Assoc.convert(value)
47
+ # else
48
+ # value
49
+ end
50
50
  end
51
51
 
52
52
  def self.get(key)
53
- @@hs[key]
53
+ @hs[key]
54
54
  end
55
55
 
56
56
  def self.to_nkf_encoding_format(encoding)
@@ -58,7 +58,7 @@ module Ykutils
58
58
  end
59
59
 
60
60
  def self.config(_src_encoding, dest_encoding, _misc_option = nil)
61
- @@config = dest_encoding
61
+ @config = dest_encoding
62
62
  end
63
63
 
64
64
  def self.auto_config_to_inner(str, _misc_option = nil)
@@ -70,8 +70,8 @@ module Ykutils
70
70
 
71
71
  # inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
72
72
  # if inner_encoding != "US-ASCII"
73
- # @@config = inner_encoding
74
- @@config = nil
73
+ # @config = inner_encoding
74
+ @config = nil
75
75
  # end
76
76
  src_encoding
77
77
  end
@@ -80,20 +80,20 @@ module Ykutils
80
80
  dest_encoding = Assoc.to_nkf_encoding_format(dest_enc)
81
81
  # inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
82
82
  # if inner_encoding != "US-ASCII"
83
- # @@config = dest_encoding.downcase
84
- @@config = dest_encoding
83
+ # @config = dest_encoding.downcase
84
+ @config = dest_encoding
85
85
  # end
86
86
  end
87
87
 
88
88
  def self.convert(str)
89
89
  nstr = nil
90
90
  unless str.nil?
91
- if @@config.nil?
91
+ if @config.nil?
92
92
  nstr = str
93
93
  else
94
94
  begin
95
95
  # nstr = NKF.nkf( @@config , str )
96
- nstr = str.encode(@@config)
96
+ nstr = str.encode(@config)
97
97
  rescue StandardError => e
98
98
  puts e
99
99
  puts "========="
@@ -0,0 +1,167 @@
1
+ # -*- coding utf-8 -*-
2
+
3
+ require "nkf"
4
+
5
+ module Ykutils
6
+ module NKFUTIL
7
+ # ASCII-8BIT
8
+ # US-ASCII
9
+ unless CODE_TO_NAME
10
+ CODE_TO_NAME = Hash.new("ASCII")
11
+ CODE_TO_NAME["US-ASCII"] = "ASCII"
12
+ CODE_TO_NAME["stateless-ISO-2022-JP"] = "JIS"
13
+ CODE_TO_NAME["ISO-2022-JP"] = "JIS"
14
+ CODE_TO_NAME["ISO-2022-JP-2"] = "JIS"
15
+ CODE_TO_NAME["EUC-JP"] = "EUC"
16
+ CODE_TO_NAME["eucJP-ms"] = "EUC"
17
+ CODE_TO_NAME["Shift_JIS"] = "SJIS"
18
+ CODE_TO_NAME["Windows-31J"] = "SJIS"
19
+ CODE_TO_NAME["ASCII-8BIT"] = "BINARY"
20
+ CODE_TO_NAME["UTF-8"] = "UTF8"
21
+ end
22
+
23
+ begin
24
+ NAME_TO_ENCODING
25
+ rescue StandardError
26
+ NAME_TO_ENCODING = Hash.new("US-ASCII")
27
+ NAME_TO_ENCODING["UTF8"] = "UTF-8"
28
+ NAME_TO_ENCODING["ASCII"] = "US-ASCII"
29
+ NAME_TO_ENCODING["JIS"] = "ISO-2022-JP"
30
+ NAME_TO_ENCODING["EUC"] = "EUC-JP"
31
+ NAME_TO_ENCODING["SJIS"] = "Winodws-31J"
32
+ NAME_TO_ENCODING["BINARY"] = "ASCII-8BIT"
33
+ end
34
+
35
+ def self.guess_encoding(str)
36
+ puts "**#{str.encoding}"
37
+ CODE_TO_NAME[str.encoding.to_s]
38
+ end
39
+
40
+ class Assoc
41
+ @hs = {}
42
+ @config = nil
43
+
44
+ def self.set(key, value)
45
+ @hs[key] = if value
46
+ Assoc.convert(value)
47
+ # else
48
+ # value
49
+ end
50
+ end
51
+
52
+ def self.get(key)
53
+ @hs[key]
54
+ end
55
+
56
+ def self.to_nkf_encoding_format(encoding)
57
+ NAME_TO_ENCODING[encoding]
58
+ end
59
+
60
+ def self.config(_src_encoding, dest_encoding, _misc_option = nil)
61
+ @config = dest_encoding
62
+ end
63
+
64
+ def self.auto_config_to_inner(str, _misc_option = nil)
65
+ src_encoding = if str
66
+ Assoc.to_nkf_encoding_format(NKFUTIL.guess_encoding(str))
67
+ else
68
+ "US-ASCII"
69
+ end
70
+
71
+ # inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
72
+ # if inner_encoding != "US-ASCII"
73
+ # @config = inner_encoding
74
+ @config = nil
75
+ # end
76
+ src_encoding
77
+ end
78
+
79
+ def self.auto_config_from_inner(dest_enc, _misc_option = nil)
80
+ dest_encoding = Assoc.to_nkf_encoding_format(dest_enc)
81
+ # inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
82
+ # if inner_encoding != "US-ASCII"
83
+ # @config = dest_encoding.downcase
84
+ @config = dest_encoding
85
+ # end
86
+ end
87
+
88
+ def self.convert(str)
89
+ nstr = nil
90
+ unless str.nil?
91
+ if @config.nil?
92
+ nstr = str
93
+ else
94
+ begin
95
+ # nstr = NKF.nkf( @@config , str )
96
+ nstr = str.encode(@config)
97
+ rescue StandardError => e
98
+ puts e
99
+ puts "========="
100
+ pp caller(0)
101
+ end
102
+ end
103
+ end
104
+ nstr
105
+ end
106
+
107
+ # def Assoc.get_inner_encoding
108
+ # @@inner_encoding = ($KCODE != "NONE") ? $KCODE : "ASCII"
109
+ # end
110
+ end
111
+
112
+ def self.set(key, value)
113
+ Assoc.set(key, value)
114
+ end
115
+
116
+ def self.get(key)
117
+ Assoc.get(key)
118
+ end
119
+
120
+ def self.convert(str)
121
+ if str.nil?
122
+ ""
123
+ else
124
+ Assoc.convert(str)
125
+ end
126
+ end
127
+
128
+ def self.assoc_equal(target, key)
129
+ target == key || target == Assoc.get(key)
130
+ end
131
+
132
+ def self.config(src_encoding, dest_encoding, misc_option = nil)
133
+ Assoc.config(src_encoding, dest_encoding, misc_option)
134
+ end
135
+
136
+ def self.auto_config_to_inner(str, misc_option = nil)
137
+ Assoc.auto_config_to_inner(str, misc_option)
138
+ end
139
+
140
+ def self.auto_config_from_inner(dest_encoding, misc_option = nil)
141
+ Assoc.auto_config_to_inner(dest_encoding, misc_option)
142
+ end
143
+
144
+ def puts_sj(line)
145
+ puts line.encod(NAME_TO_ENCODING["SJIS"])
146
+ # puts NKF.nkf( "-Ws -m0" , line )
147
+ end
148
+
149
+ def puts_u(line)
150
+ puts line.encod(NAME_TO_ENCODING["UTF8"])
151
+ puts NKF.nkf("-Sw -m0", line)
152
+ end
153
+
154
+ def nkf_utf8_file(infname, outfname)
155
+ File.open(outfname) do |outf|
156
+ File.open(infname) do |file|
157
+ while (line = file.gets)
158
+ line.chomp!
159
+ # oline = NKF.nkf( "-w -m0" , line )
160
+ oline = line.encode(NAME_TO_ENCODING["UTF8"])
161
+ outf.printf("%s\n", oline)
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -2,15 +2,15 @@ require "pathname"
2
2
 
3
3
  module Ykutils
4
4
  class OSUtil
5
- @@os = nil
5
+ @os = nil
6
6
 
7
7
  def self.runtime
8
- @@os ||= if Pathname.pwd.to_s =~ %r{^/cygdrive}
9
- :CYGWIN
10
- else
11
- :ELSE
12
- end
13
- @@os
8
+ @os ||= if Pathname.pwd.to_s =~ %r{^/cygdrive}
9
+ :CYGWIN
10
+ else
11
+ :ELSE
12
+ end
13
+ @os
14
14
  end
15
15
  end
16
16
  end
@@ -6,7 +6,7 @@ module Ykutils
6
6
  one_path = Pathname.new(fname).expand_path
7
7
  dir_path = one_path.dirname
8
8
  base_path = one_path.basename(".*")
9
- append_name = "-2" unless append_name.empty? or extname.empty?
9
+ append_name = "-2" unless append_name.empty? || extname.empty?
10
10
  extname ||= one_path.extname
11
11
 
12
12
  [one_path, dir_path.join(base_path.to_s + append_name + extname)]
@@ -19,10 +19,11 @@ module Ykutils
19
19
  def determine_fname_for_update2(fname)
20
20
  begin
21
21
  ctime = File.ctime(fname)
22
- rescue StandardError => e
22
+ rescue YkutilsError => e
23
+ puts e
23
24
  end
24
25
  ctime ||= Time.now
25
- ary = ctime.to_s.split(" ")
26
+ ary = ctime.to_s.split
26
27
  extname = File.extname(fname)
27
28
  append = ["", ary[0], ary[1].gsub(":", "-")].join("-")
28
29
  get_buddy_path(fname, append, extname)
@@ -1,6 +1,7 @@
1
1
  module Ykutils
2
2
  class RetCode
3
- attr_reader :val, :mes, :ret, :bool
3
+ attr_reader :val
4
+ attr_accessor :bool, :mes, :ret
4
5
 
5
6
  def initialize(obj)
6
7
  @val = obj
@@ -16,22 +17,6 @@ module Ykutils
16
17
  def to_s
17
18
  @bool
18
19
  end
19
-
20
- def set_bool(val)
21
- @bool = val
22
- end
23
-
24
- def get_bool
25
- @bool
26
- end
27
-
28
- def set_mes(val)
29
- @mes = val
30
- end
31
-
32
- def set_ret(val)
33
- @ret = val
34
- end
35
20
  end
36
21
 
37
22
  class RetCode2 < RetCode
@@ -1,14 +1,14 @@
1
1
  # -*- coding utf-8 -*-
2
2
 
3
- require "pp"
4
3
  require "yaml"
5
4
  require "csv"
6
5
  require "ykutils/treemanagera"
7
- if RUBY_VERSION >= "1.8.7"
8
- require "ykutils/nkfutil19"
9
- else
10
- require "ykutils/nkfutil"
11
- end
6
+ #if RUBY_VERSION >= "1.8.7"
7
+ # require "ykutils/nkfutil19"
8
+ #else
9
+ # require "ykutils/nkfutil"
10
+ #end
11
+ require "ykutils/nkfutil20"
12
12
  require "ykutils/debugutils"
13
13
 
14
14
  module Ykutils
@@ -63,7 +63,7 @@ module Ykutils
63
63
 
64
64
  def parse_yaml_file(fname)
65
65
  begin
66
- @data = YAML.parse(File.read(fname))
66
+ @data = YAML.parse_file(fname)
67
67
  rescue StandardError => e
68
68
  pp e
69
69
  pp e.backtrace
@@ -82,7 +82,7 @@ module Ykutils
82
82
  v.scan(re).flatten.each do |it|
83
83
  if it
84
84
  tm.add(it, k)
85
- tm.addTag(k, it)
85
+ tm.tag(k, it)
86
86
  else
87
87
  tm.add(nil, k)
88
88
  end
@@ -92,10 +92,9 @@ module Ykutils
92
92
  tm.tsort.reverse.each do |k|
93
93
  next unless data[k]
94
94
 
95
- tag = tm.getTag(k)
95
+ tag = tm.tag(k)
96
96
  next unless tag
97
97
 
98
- i = 0
99
98
  tag.each do |it|
100
99
  ntag = Regexp.new(separator + it + separator)
101
100
  data[k] = data[k].sub(ntag, data[it]) if data[it]
@@ -107,12 +106,12 @@ module Ykutils
107
106
 
108
107
  ary = v.scan(re).flatten
109
108
  i = 0
110
- if ary and ary.size > 0
109
+ if ary && !ary.empty?
111
110
  except_ary.each do |it|
112
111
  i += 1 unless ary.index(it)
113
112
  end
114
113
  end
115
- if i > 0
114
+ if i.positive?
116
115
  puts "#{k} fails to exapnd data. value is #{v}"
117
116
  @valid = false
118
117
  end
@@ -123,7 +122,7 @@ module Ykutils
123
122
 
124
123
  def make_data_complement(item_ary, data, common)
125
124
  item_ary.each do |it|
126
- data[it] = common[it] unless data[it] and data[it].strip != ""
125
+ data[it] = common[it] unless data[it] && (data[it].strip != "")
127
126
  end
128
127
  end
129
128
 
@@ -182,9 +181,9 @@ module Ykutils
182
181
 
183
182
  begin
184
183
  ary0 = File.readlines(fname)
185
- senc = NKFUTIL.auto_config_to_inner(ary0.join)
184
+ NKFUTIL.auto_config_to_inner(ary0.join)
186
185
 
187
- ary = ary0.select { |x| !x.nil? }.collect do |x|
186
+ ary = ary0.compact.collect do |x|
188
187
  if x.nil?
189
188
  ""
190
189
  else