plist4r 0.1.0 → 0.1.1
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.
- data/README.rdoc +30 -4
- data/VERSION +1 -1
- data/lib/plist4r.rb +7 -6
- data/lib/plist4r/backend.rb +47 -36
- data/lib/plist4r/backend/example.rb +1 -1
- data/lib/plist4r/backend/haml.rb +3 -3
- data/lib/plist4r/backend/libxml4r.rb +4 -4
- data/lib/plist4r/backend/plutil.rb +2 -0
- data/lib/plist4r/backend/ruby_cocoa.rb +3 -3
- data/lib/plist4r/backend_base.rb +3 -0
- data/lib/plist4r/config.rb +15 -12
- data/lib/plist4r/mixin/data_methods.rb +49 -47
- data/lib/plist4r/mixin/popen4.rb +167 -161
- data/lib/plist4r/mixin/ruby_stdlib.rb +2 -0
- data/lib/plist4r/plist.rb +151 -144
- data/lib/plist4r/plist_cache.rb +54 -50
- data/lib/plist4r/plist_type.rb +44 -43
- data/lib/plist4r/plist_type/info.rb +7 -0
- data/lib/plist4r/plist_type/launchd.rb +0 -2
- data/lib/plist4r/plist_type/plist.rb +7 -0
- data/plist4r.gemspec +12 -3
- data/plists/Picture of Today.plist +75 -0
- data/plists/com.adobe.PDFAdminSettings.plist +0 -0
- data/plists/com.apple.SoftwareUpdate.plist +12 -0
- data/plists/foofoo.xml +53 -0
- data/plists/test.plist +0 -0
- data/spec/plist4r/plist_spec.rb +42 -0
- data/spec/plist4r_spec.rb +56 -3
- data/spec/spec.opts +2 -0
- data/test.rb +14 -0
- metadata +11 -2
data/lib/plist4r/plist.rb
CHANGED
@@ -1,88 +1,92 @@
|
|
1
1
|
|
2
2
|
require 'plist4r/mixin/ordered_hash'
|
3
|
+
require 'plist4r/mixin/ruby_stdlib'
|
4
|
+
require 'plist4r/plist_cache'
|
3
5
|
require 'plist4r/plist_type'
|
6
|
+
Dir.glob(File.dirname(__FILE__) + "/plist_type/**/*.rb").each {|t| require File.expand_path t}
|
4
7
|
require 'plist4r/backend'
|
5
8
|
|
6
|
-
|
7
|
-
class
|
8
|
-
|
9
|
-
|
9
|
+
module Plist4r
|
10
|
+
class Plist
|
11
|
+
PlistOptionsHash = %w[from_string filename path file_format plist_type unsupported_keys backends]
|
12
|
+
FileFormats = %w[binary xml next_step]
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
def initialize *args, &blk
|
15
|
+
@hash = ::ActiveSupport::OrderedHash.new
|
16
|
+
# @plist_type = plist_type PlistType::Plist
|
17
|
+
@unsupported_keys = Config[:unsupported_keys]
|
18
|
+
@backends = Config[:backends]
|
19
|
+
@from_string = nil
|
20
|
+
@filename = nil
|
21
|
+
@file_format = nil
|
22
|
+
@path = Config[:default_path]
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
+
case args.first
|
25
|
+
when Hash
|
26
|
+
parse_opts args.first
|
24
27
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
when String, Symbol
|
29
|
+
@filename = args.first.to_s
|
30
|
+
when nil
|
31
|
+
else
|
32
|
+
raise "Unrecognized first argument: #{args.first.inspect}"
|
33
|
+
end
|
34
|
+
|
35
|
+
@plist_cache = PlistCache.new self
|
30
36
|
end
|
31
|
-
end
|
32
37
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
38
|
+
def from_string string=nil
|
39
|
+
case string
|
40
|
+
when String
|
41
|
+
plist_format = ::Plist4r.string_detect_format string
|
42
|
+
if plist_format
|
43
|
+
eval "@plist_cache.from_#{format}, string"
|
44
|
+
else
|
45
|
+
raise "Unknown plist format for string: #{string}"
|
46
|
+
end
|
47
|
+
@from_string = string
|
48
|
+
when nil
|
49
|
+
@from_string
|
39
50
|
else
|
40
|
-
raise "
|
51
|
+
raise "Please specify a string of plist data"
|
41
52
|
end
|
42
|
-
@from_string = string
|
43
|
-
when nil
|
44
|
-
@from_string
|
45
|
-
else
|
46
|
-
raise "Please specify a string of plist data"
|
47
53
|
end
|
48
|
-
end
|
49
54
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
def filename filename=nil
|
56
|
+
case filename
|
57
|
+
when String
|
58
|
+
@filename = filename
|
59
|
+
when nil
|
60
|
+
@filename
|
61
|
+
else
|
62
|
+
raise "Please specify a filename"
|
63
|
+
end
|
58
64
|
end
|
59
|
-
end
|
60
65
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
def path path=nil
|
67
|
+
case path
|
68
|
+
when String
|
69
|
+
@path = path
|
70
|
+
when nil
|
71
|
+
@path
|
72
|
+
else
|
73
|
+
raise "Please specify a directory"
|
74
|
+
end
|
69
75
|
end
|
70
|
-
end
|
71
76
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
77
|
+
def filename_path filename_path=nil
|
78
|
+
case path
|
79
|
+
when String
|
80
|
+
@filename = File.basename filename_path
|
81
|
+
@path = File.dirname filename_path
|
82
|
+
when nil
|
83
|
+
File.expand_path @filename, @path
|
84
|
+
else
|
85
|
+
raise "Please specify directory + filename"
|
86
|
+
end
|
81
87
|
end
|
82
|
-
end
|
83
88
|
|
84
|
-
|
85
|
-
begin
|
89
|
+
def file_format file_format=nil
|
86
90
|
case file_format
|
87
91
|
when Symbol, String
|
88
92
|
if FileFormats.include? file_format.to_s.snake_case
|
@@ -95,111 +99,114 @@ class Plist4r::Plist
|
|
95
99
|
else
|
96
100
|
raise "Please specify a valid plist file format, #{FileFormats.inspect}"
|
97
101
|
end
|
98
|
-
rescue
|
99
|
-
raise "Please specify a valid plist file format, #{FileFormats.inspect}"
|
100
102
|
end
|
101
|
-
end
|
102
103
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
104
|
+
def plist_type plist_type=nil
|
105
|
+
begin
|
106
|
+
case plist_type
|
107
|
+
when Class
|
108
|
+
@plist_type = PlistType::Plist.new :hash => @hash
|
109
|
+
when Symbol, String
|
110
|
+
eval "pt_klass = PlistType::#{plist_type.to_s.camelcase}"
|
111
|
+
@plist_type = pt_klass.new :hash => @hash
|
112
|
+
when nil
|
113
|
+
@plist_type
|
114
|
+
else
|
115
|
+
raise "Please specify a valid plist class name, eg ::Plist4r::PlistType::ClassName, \"class_name\" or :class_name"
|
116
|
+
end
|
117
|
+
rescue
|
118
|
+
raise "Please specify a valid plist class name, eg ::Plist4r::PlistType::ClassName, \"class_name\" or :class_name"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def unsupported_keys bool
|
123
|
+
case bool
|
124
|
+
when true,false
|
125
|
+
@unsupported_keys = bool
|
111
126
|
when nil
|
112
|
-
@
|
127
|
+
@unsupported_keys
|
113
128
|
else
|
114
|
-
raise "Please specify
|
129
|
+
raise "Please specify true or false to enable / disable this option"
|
115
130
|
end
|
116
|
-
rescue
|
117
|
-
raise "Please specify a valid plist class name, eg ::Plist4r::PlistType::ClassName, \"class_name\" or :class_name"
|
118
131
|
end
|
119
|
-
end
|
120
132
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
133
|
+
def backends backends=nil
|
134
|
+
case backends
|
135
|
+
when Array
|
136
|
+
@backends = backends
|
137
|
+
when nil
|
138
|
+
@backends
|
139
|
+
else
|
140
|
+
raise "Please specify an array of valid Plist4r Backends"
|
141
|
+
end
|
129
142
|
end
|
130
|
-
end
|
131
143
|
|
132
|
-
|
133
|
-
|
134
|
-
|
144
|
+
def parse_opts opts
|
145
|
+
PlistOptionsHash.each do |opt|
|
146
|
+
eval "@#{opt} = #{opts[opt.to_sym]}" if opts[opt.to_sym]
|
147
|
+
end
|
135
148
|
end
|
136
|
-
end
|
137
149
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
150
|
+
def open filename=nil
|
151
|
+
@filename = filename if filename
|
152
|
+
raise "No filename specified" unless @filename
|
153
|
+
# @hash = @plist_cache.open
|
154
|
+
@plist_cache.open
|
155
|
+
end
|
143
156
|
|
144
|
-
|
145
|
-
|
146
|
-
|
157
|
+
def << *args, &blk
|
158
|
+
edit *args, &blk
|
159
|
+
end
|
147
160
|
|
148
|
-
|
149
|
-
|
150
|
-
|
161
|
+
def edit *args, &blk
|
162
|
+
instance_eval &blk
|
163
|
+
end
|
151
164
|
|
152
|
-
|
153
|
-
|
154
|
-
|
165
|
+
def method_missing method_sym, *args, &blk
|
166
|
+
@plist_type.send method_sym, *args, &blk
|
167
|
+
end
|
155
168
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
169
|
+
def import_hash hash=nil
|
170
|
+
case hash
|
171
|
+
when ::ActiveSupport::OrderedHash
|
172
|
+
@hash = hash
|
173
|
+
when nil
|
174
|
+
@hash = ::ActiveSupport::OrderedHash.new
|
175
|
+
else
|
176
|
+
raise "Please use ::ActiveSupport::OrderedHash.new for your hashes"
|
177
|
+
end
|
164
178
|
end
|
165
|
-
end
|
166
179
|
|
167
|
-
|
168
|
-
|
169
|
-
|
180
|
+
def to_hash
|
181
|
+
@hash
|
182
|
+
end
|
170
183
|
|
171
|
-
|
172
|
-
|
173
|
-
|
184
|
+
def to_xml
|
185
|
+
@plist_cache.to_xml
|
186
|
+
end
|
174
187
|
|
175
|
-
|
176
|
-
|
177
|
-
|
188
|
+
def to_binary
|
189
|
+
@plist_cache.to_binary
|
190
|
+
end
|
178
191
|
|
179
|
-
|
180
|
-
|
181
|
-
|
192
|
+
def to_next_step
|
193
|
+
@plist_cache.to_next_step
|
194
|
+
end
|
182
195
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
196
|
+
def save
|
197
|
+
raise "No filename specified" unless @filename
|
198
|
+
@plist_cache.save
|
199
|
+
end
|
187
200
|
|
188
|
-
|
189
|
-
|
190
|
-
|
201
|
+
def save_as filename
|
202
|
+
@filename = filename
|
203
|
+
save
|
204
|
+
end
|
191
205
|
end
|
192
206
|
end
|
193
207
|
|
194
|
-
|
195
|
-
|
196
|
-
# plutil -convert xml1 @filename
|
197
|
-
# plutil -convert binary1 @filename
|
198
|
-
|
199
|
-
|
200
208
|
module Plist4r
|
201
|
-
class
|
202
|
-
include ::Plist4r::Popen4
|
209
|
+
class OldPlist
|
203
210
|
|
204
211
|
def initialize path_prefix, plist_str, &blk
|
205
212
|
plist_str << ".plist" unless plist_str =~ /\.plist$/
|
data/lib/plist4r/plist_cache.rb
CHANGED
@@ -1,66 +1,70 @@
|
|
1
1
|
|
2
2
|
require 'plist4r/backend'
|
3
|
-
|
4
|
-
class
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Plist4r
|
4
|
+
class PlistCache
|
5
|
+
def initialize plist, *args, &blk
|
6
|
+
@plist = plist
|
7
|
+
@backend = Backend.new plist, *args, &blk
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def checksum
|
11
|
+
@plist.to_hash.hash
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def last_checksum
|
15
|
+
@checksum
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
def update_checksum
|
19
|
+
@checksum = @plist.to_hash.hash
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def needs_update
|
23
|
+
checksum != last_checksum
|
24
|
+
end
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
def to_xml
|
27
|
+
puts "in to_xml"
|
28
|
+
# if needs_update
|
29
|
+
puts "needs update"
|
30
|
+
update_checksum
|
31
|
+
@xml = @backend.call :to_xml
|
32
|
+
# else
|
33
|
+
# @xml
|
34
|
+
# end
|
32
35
|
end
|
33
|
-
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
def to_binary
|
38
|
+
if needs_update
|
39
|
+
update_checksum
|
40
|
+
@binary = @backend.call :to_binary
|
41
|
+
else
|
42
|
+
@binary
|
43
|
+
end
|
41
44
|
end
|
42
|
-
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
def to_next_step
|
47
|
+
if needs_update
|
48
|
+
update_checksum
|
49
|
+
@next_step = @backend.call :to_next_step
|
50
|
+
else
|
51
|
+
@next_step
|
52
|
+
end
|
50
53
|
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def open
|
54
|
-
@backend.call :load
|
55
|
-
update_checksum
|
56
|
-
end
|
57
54
|
|
58
|
-
|
59
|
-
|
55
|
+
def open
|
56
|
+
@backend.call :open
|
60
57
|
update_checksum
|
61
|
-
@
|
62
|
-
|
63
|
-
|
58
|
+
@plist
|
59
|
+
end
|
60
|
+
|
61
|
+
def save
|
62
|
+
if needs_update
|
63
|
+
update_checksum
|
64
|
+
@backend.call :save
|
65
|
+
else
|
66
|
+
true
|
67
|
+
end
|
64
68
|
end
|
65
69
|
end
|
66
|
-
end
|
70
|
+
end
|