ykutils 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ module Ykutils
2
+ class FilePermision
3
+ attr_reader :owner, :group, :other
4
+
5
+ class PermisionEntry
6
+ def initialize( str )
7
+ @read = str[0].chr
8
+ @write = str[1].chr
9
+ @exec = str[2].chr
10
+ end
11
+
12
+ def to_s
13
+ @read + @write + @exec
14
+ end
15
+
16
+ def to_hash
17
+ { "read" => @read , "write" => @write , "exec" => @exec }
18
+ end
19
+ end
20
+
21
+ def initialize( str )
22
+ @owner = PermisionEntry.new( str[0..2] )
23
+ @group = PermisionEntry.new( str[3..5] )
24
+ @other = PermisionEntry.new( str[6..8] )
25
+ end
26
+
27
+ def to_s
28
+ @owner.to_s + @group.to_s + @other.to_s
29
+ end
30
+
31
+ def to_hash
32
+ h = { "owner" => @owner.to_hash , "group" => @group.to_hash , "other" => @other.to_hash }
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,67 @@
1
+ module Ykutils
2
+ class Hasharray < Hash
3
+
4
+ def initialize( *args )
5
+ super( *args )
6
+ @ary = super.keys
7
+ # @ary ||= []
8
+ unless @ary
9
+ @ary = []
10
+ end
11
+ end
12
+
13
+ def []=(key,value)
14
+ super(key,value)
15
+ unless @ary.index(key)
16
+ @ary << key
17
+ end
18
+ end
19
+
20
+ def each(&block)
21
+ @ary.each do |it|
22
+ block.call( it, self[it] )
23
+ end
24
+ end
25
+
26
+ def keys
27
+ @ary
28
+ end
29
+
30
+ def values
31
+ @ary.collect{ |it| self[it] }
32
+ end
33
+
34
+ def clear
35
+ super
36
+ @ary = []
37
+ end
38
+
39
+ def replace( *args )
40
+ super( *args )
41
+ @ary = super.keys
42
+ unless @ary
43
+ @ary = []
44
+ end
45
+ end
46
+
47
+ def delete(ind , &block)
48
+ @ary.delete(ind)
49
+
50
+ super
51
+ end
52
+
53
+ def delete_if(&block)
54
+ @ary.each do |it|
55
+ if block.call( it , self[it] )
56
+ @ary.delete(it)
57
+ end
58
+ end
59
+
60
+ super
61
+ end
62
+
63
+ def reject(&block)
64
+ super
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,154 @@
1
+ module Ykutils
2
+ class BasicLines
3
+ def initialize( line_ary )
4
+ @line_ary = line_ary
5
+ end
6
+
7
+ def get_line
8
+ if @line_ary.size > -1
9
+ @line_ary.shift
10
+ end
11
+ end
12
+ end
13
+
14
+ class Lines
15
+ def initialize( line_ary )
16
+ @lines = BasicLines.new( line_ary )
17
+ @line_stack = []
18
+ @status = nil
19
+
20
+ setup
21
+ end
22
+
23
+ def setup
24
+ end
25
+
26
+ def get_line
27
+ if @line_stack.size > -1
28
+ @line_stack.shift
29
+ end
30
+ end
31
+
32
+ def output_f( fname )
33
+ if fname
34
+ File.open( fname , 'w' ){ |file|
35
+ @line_stack.each do |it|
36
+ file.write(it.to_s)
37
+ end
38
+ }
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ class FtpOpLines < BasicLines
45
+ end
46
+
47
+ #class AccountLines < BasicLines
48
+ class AccountLines < Lines
49
+ HOST_ACCOUNT_START = 10
50
+ HOST_ACCOUNT = 11
51
+ HOST_ACCOUNT_END = 12
52
+ DOMAIN_ACCOUNT_START = 20
53
+ DOMAIN_ACCOUNT = 21
54
+ DOMAIN_ACCOUNT_END = 22
55
+ SEPARATOR = 30
56
+ ETC = 100
57
+
58
+ def setup
59
+ while line = @lines.get_line
60
+ next if line.strip == ""
61
+ if line =~ /^\-\-\-/
62
+ case @status
63
+ when HOST_ACCOUNT_START , HOST_ACCOUNT
64
+ @line_stack.push( { "STATUS" => HOST_ACCOUNT_END ,
65
+ "CONTENT" => nil })
66
+ when DOMAIN_ACCOUNT_START , DOMAIN_ACCOUNT
67
+ @line_stack.push( { "STATUS" => DOMAIN_ACCOUNT_END ,
68
+ "CONTENT" => nil })
69
+ else
70
+ end
71
+
72
+ @line_stack.push( { "STATUS" => SEPARATOR ,
73
+ "CONTENT" => line })
74
+ @status = SEPARATOR
75
+
76
+ elsif line =~ /^\s/
77
+ case @status
78
+ when HOST_ACCOUNT_START , HOST_ACCOUNT
79
+ @line_stack.push( { "STATUS" => HOST_ACCOUNT ,
80
+ "CONTENT" => line })
81
+ @status = HOST_ACCOUNT
82
+ when HOST_ACCOUNT_END
83
+ puts( "error!" )
84
+ puts( line )
85
+ @line_stack.push( { "STATUS" => HOST_ACCOUNT ,
86
+ "CONTENT" => line })
87
+ @status = HOST_ACCOUNT
88
+ when DOMAIN_ACCOUNT_START , DOMAIN_ACCOUNT
89
+ @line_stack.push( { "STATUS" => DOMAIN_ACCOUNT ,
90
+ "CONTENT" => line })
91
+ @status = DOMAIN_ACCOUNT
92
+ when DOMAIN_ACCOUNT_END
93
+ puts( "error!" )
94
+ puts( line )
95
+ @line_stack.push( { "STATUS" => DOMAIN_ACCOUNT ,
96
+ "CONTENT" => line })
97
+ @status = DOMAIN_ACCOUNT
98
+ else
99
+ @line_stack.push( { "STATUS" => ETC ,
100
+ "CONTENT" => line })
101
+ @status = ETC
102
+ end
103
+ else
104
+ if line =~ /^==/
105
+ case @status
106
+ when HOST_ACCOUNT_START , HOST_ACCOUNT
107
+ @line_stack.push( { "STATUS" => HOST_ACCOUNT_END ,
108
+ "CONTENT" => nil })
109
+ when DOMAIN_ACCOUNT_START , DOMAIN_ACCOUNT
110
+ @line_stack.push( { "STATUS" => DOMAIN_ACCOUNT_END ,
111
+ "CONTENT" => nil })
112
+ else
113
+ end
114
+ @line_stack.push( { "STATUS" => DOMAIN_ACCOUNT_START ,
115
+ "CONTENT" => line })
116
+ @status = DOMAIN_ACCOUNT_START
117
+ else
118
+ case @status
119
+ when HOST_ACCOUNT_START , HOST_ACCOUNT
120
+ @line_stack.push( { "STATUS" => HOST_ACCOUNT_END ,
121
+ "CONTENT" => nil })
122
+ when DOMAIN_ACCOUNT_START , DOMAIN_ACCOUNT
123
+ @line_stack.push( { "STATUS" => DOMAIN_ACCOUNT_END ,
124
+ "CONTENT" => nil })
125
+ else
126
+ end
127
+ @line_stack.push( { "STATUS" => HOST_ACCOUNT_START ,
128
+ "CONTENT" => line })
129
+ @status = HOST_ACCOUNT_START
130
+ end
131
+ end
132
+ end
133
+
134
+ i = @line_stack.size - 1
135
+ seeking = true
136
+ while i >= 0 and seeking
137
+ case @line_stack[i]["STATUS"]
138
+ when DOMAIN_ACCOUNT_START , DOMAIN_ACCOUNT
139
+ @line_stack.push( { "STATUS" => DOMAIN_ACCOUNT_END ,
140
+ "CONTENT" => nil })
141
+ seeking = false
142
+ when HOST_ACCOUNT_START , HOST_ACCOUNT
143
+ @line_stack.push( { "STATUS" => HOST_ACCOUNT_END ,
144
+ "CONTENT" => nil })
145
+ seeking = false
146
+ when ETC
147
+ else
148
+ seeking = false
149
+ end
150
+ i -= 1
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,72 @@
1
+ require 'ykutils/filepermision'
2
+ require 'pathname'
3
+
4
+ module Ykytils
5
+ class DirEntryItem
6
+ attr_accessor :name , :user , :group , :size , :month, :day, :time , :year, :path, :type , :parent_dir , :valid
7
+
8
+ def initialize
9
+ end
10
+
11
+ def parse( str , parent_dir , valid = true )
12
+ ary = str.split(/\s+/)
13
+ perm = ary[0]
14
+ if perm[0].chr == 'd'
15
+ @type = :DIRECTORY
16
+ else
17
+ @type = :FILE
18
+ end
19
+ @perm = FilePermision.new( ary[0][1..9] )
20
+ @value = ary[1]
21
+ @user = ary[2]
22
+ @group = ary[3]
23
+ @size = ary[4]
24
+ @month = ary[5]
25
+ @day = ary[6]
26
+ str = ary[7]
27
+ if str =~ /:/
28
+ @year = Time.now.year
29
+ @time = str
30
+ else
31
+ @year = str
32
+ @time = "00:00:00"
33
+ end
34
+
35
+ @time = ary[7]
36
+ @name = ary[8]
37
+ @path = File.join( parent_dir, @name )
38
+ @parent_dir = parent_dir
39
+ @valid = valid
40
+ end
41
+
42
+ def to_hash
43
+ { "type" => @type, "perm" => @perm.to_hash, "value" => @value, "user" => @user , "group" => @group, "size" => @size, "month" => @month, "day" => @day, "year" => @year , "time" => @time, "name" => @name, "path" => @path, "parent_dir" => @parent_dir , "valid" => @valid }
44
+ end
45
+
46
+ def to_csv
47
+ "#{@type},#{@perm.to_s},#{@value},#{@user},#{@group},#{@size},#{@year},#{@month},#{@day},#{@time},#{@name},#{@parent_dir},#{@valid}"
48
+ end
49
+
50
+ def directory?
51
+ @type === :DIRECTORY
52
+ end
53
+
54
+ def file?
55
+ @type === :FILE
56
+ end
57
+
58
+ def owner_perm
59
+ @perm.owner
60
+ end
61
+
62
+ def group_perm
63
+ @perm.group
64
+ end
65
+
66
+ def otherr_perm
67
+ @perm.other
68
+ end
69
+ end
70
+
71
+
72
+ end
@@ -0,0 +1,155 @@
1
+ # -*- coding utf-8 -*-
2
+
3
+ require 'nkf'
4
+
5
+ module Ykutils
6
+ module NKFUTIL
7
+ CODE_TO_NAME = Hash.new("ASCII")
8
+ CODE_TO_NAME[NKF::JIS] = "JIS"
9
+ CODE_TO_NAME[NKF::EUC] = "EUC"
10
+ CODE_TO_NAME[NKF::SJIS] = "SJIS"
11
+ CODE_TO_NAME[NKF::BINARY] = "BINARY"
12
+ CODE_TO_NAME[NKF::UTF8] = "UTF8" if NKF.const_defined?(:UTF8)
13
+
14
+ def NKFUTIL.guess_encoding(str)
15
+ CODE_TO_NAME[NKF.guess(str)]
16
+ end
17
+
18
+ class Assoc
19
+ @@hs = {}
20
+ @@config = nil
21
+
22
+ def Assoc.set( key, value )
23
+ if value
24
+ @@hs[key] = Assoc.convert( value )
25
+ else
26
+ @@hs[key] = value
27
+ end
28
+ end
29
+
30
+ def Assoc.get( key )
31
+ @@hs[key]
32
+ end
33
+
34
+ def Assoc.to_nkf_encoding_format( encoding )
35
+ case encoding
36
+ when "UTF8"
37
+ "w"
38
+ else
39
+ encoding[0,1]
40
+ end
41
+ end
42
+
43
+
44
+ def Assoc.config( src_encoding, dest_encoding , misc_option = nil )
45
+ @@config = "-#{(dest_encoding.to_s)[0,1].downcase}#{(src_encoding.to_s)[0,1].upcase}"
46
+ if misc_option != nil
47
+ @@config += " #{misc_option}"
48
+ end
49
+ end
50
+
51
+ def Assoc.auto_config_to_inner( str , misc_option = nil )
52
+ if str
53
+ src_encoding = Assoc.to_nkf_encoding_format( NKFUTIL.guess_encoding( str ) )
54
+ else
55
+ src_encoding = "A"
56
+ end
57
+
58
+ inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
59
+ if inner_encoding != "A"
60
+ @@config = "-#{inner_encoding.downcase}#{src_encoding.upcase}"
61
+ if misc_option != nil
62
+ @@config += " #{misc_option}"
63
+ end
64
+ end
65
+ src_encoding
66
+ end
67
+
68
+ def Assoc.auto_config_from_inner( dest_enc , misc_option = nil )
69
+ dest_encoding = Assoc.to_nkf_encoding_format( dest_enc )
70
+ inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
71
+ if inner_encoding != "A"
72
+ @@config = "-#{dest_encoding.downcase}#{inner_encoding.upcase}"
73
+ if misc_option != nil
74
+ @@config += " #{misc_option}"
75
+ end
76
+ end
77
+ end
78
+
79
+ def Assoc.convert( str )
80
+ nstr = nil
81
+ if str != nil
82
+ if @@config != nil
83
+ begin
84
+ nstr = NKF.nkf( @@config , str )
85
+ rescue => ex
86
+ puts ex
87
+ puts "========="
88
+ pp caller(0)
89
+ end
90
+ else
91
+ nstr = str
92
+ end
93
+ end
94
+ nstr
95
+ end
96
+
97
+ def Assoc.get_inner_encoding
98
+ @@inner_encoding = ($KCODE != "NONE") ? $KCODE : "ASCII"
99
+ end
100
+ end
101
+
102
+ def NKFUTIL.set( key, value )
103
+ Assoc.set( key , value )
104
+ end
105
+
106
+ def NKFUTIL.get( key )
107
+ Assoc.get( key )
108
+ end
109
+
110
+ def NKFUTIL.convert( str )
111
+ if str != nil
112
+ Assoc.convert( str )
113
+ else
114
+ ""
115
+ end
116
+ end
117
+
118
+ def NKFUTIL.assoc_equal( target , key )
119
+ target == key || target == Assoc.get( key )
120
+ end
121
+
122
+ def NKFUTIL.config( src_encoding, dest_encoding , misc_option = nil )
123
+ Assoc.config( src_encoding, dest_encoding , misc_option )
124
+ end
125
+
126
+ def NKFUTIL.auto_config_to_inner( str, misc_option = nil )
127
+ Assoc.auto_config_to_inner( str , misc_option )
128
+ end
129
+
130
+ def NKFUTIL.auto_config_from_inner( dest_encoding , misc_option = nil )
131
+ Assoc.auto_config_to_inner( dest_encoding , misc_option )
132
+ end
133
+
134
+ def puts_sj( line )
135
+ puts NKF.nkf( "-Ws -m0" , line )
136
+ end
137
+
138
+ def puts_u( line )
139
+ puts NKF.nkf( "-Sw -m0" , line )
140
+ end
141
+
142
+ def nkf_utf8_file( infname , outfname )
143
+ File.open( outfname ){|outf|
144
+ File.open( infname ){|file|
145
+ while line = file.gets
146
+ line.chomp!
147
+ oline = NKF.nkf( "-w -m0" , line )
148
+ outf.printf("%s\n" , oline )
149
+ end
150
+ }
151
+ }
152
+ end
153
+ end
154
+
155
+ end