dir-to-xml 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dir-to-xml.rb +24 -22
- metadata +2 -2
data/lib/dir-to-xml.rb
CHANGED
@@ -2,12 +2,10 @@
|
|
2
2
|
|
3
3
|
# file: dir-to-xml.rb
|
4
4
|
|
5
|
-
require '
|
5
|
+
require 'rexle'
|
6
6
|
require 'dynarex'
|
7
7
|
|
8
8
|
class DirToXML
|
9
|
-
#include PrettyXML
|
10
|
-
include REXML
|
11
9
|
|
12
10
|
attr_reader :status
|
13
11
|
|
@@ -18,7 +16,7 @@ class DirToXML
|
|
18
16
|
|
19
17
|
a = Dir.glob("*").sort
|
20
18
|
command = a.include?('dir.xml') ? 'run' : 'new_run'
|
21
|
-
|
19
|
+
|
22
20
|
@doc, @status = self.send command, a
|
23
21
|
Dir.chdir old_path #File.expand_path('~')
|
24
22
|
end
|
@@ -34,13 +32,13 @@ class DirToXML
|
|
34
32
|
private
|
35
33
|
|
36
34
|
def new_element(name, text=nil)
|
37
|
-
new_node = Element.new(name)
|
35
|
+
new_node = Rexle::Element.new(name)
|
38
36
|
new_node.text = text if text
|
39
37
|
new_node
|
40
38
|
end
|
41
39
|
|
42
40
|
def new_file(name, type, ext, ctime, mtime, atime)
|
43
|
-
node = Element.new('file')
|
41
|
+
node = Rexle::Element.new('file')
|
44
42
|
node.add_element new_element('name', name)
|
45
43
|
node.add_element new_element('type', type)
|
46
44
|
node.add_element new_element('ext', ext)
|
@@ -59,31 +57,38 @@ class DirToXML
|
|
59
57
|
[x, File.extname(x), File::ftype(x), File::ctime(x), File::mtime(x), File::atime(x)]
|
60
58
|
end
|
61
59
|
|
60
|
+
records = doc.root.element('records')
|
61
|
+
i = '0'
|
62
62
|
dir_files.each do |name, ext, type, ctime, mtime, atime|
|
63
|
-
|
63
|
+
records.add_element new_file(name, type, ext, ctime, mtime, atime)
|
64
|
+
i.succ!
|
64
65
|
end
|
65
66
|
end
|
66
67
|
|
67
68
|
def new_run(a)
|
68
69
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
70
|
+
summary = "
|
71
|
+
<summary>
|
72
|
+
<title>Index of #{File.basename(Dir.pwd)}</title>
|
73
|
+
<file_path>#{File.dirname(Dir.pwd)}</file_path>
|
74
|
+
<recordx_type>dynarex</recordx_type>
|
75
|
+
<schema>directory[title,file_path]/file(name, type, ext, created, last_modified, last_accessed, description, owner, group, permissions)</schema>
|
76
|
+
</summary>"
|
77
|
+
|
78
|
+
buffer = "<directory>%s<records/></directory>" % summary
|
79
|
+
doc = Rexle.new(buffer)
|
73
80
|
add_files(doc, a)
|
74
81
|
|
75
|
-
doc.
|
82
|
+
doc.element('records').add_element new_file(name='dir.xml', type=nil, ext='.xml', *[Time.now] * 3)
|
76
83
|
|
77
|
-
|
78
|
-
File.open('dir.xml','w'){|f| doc.write f}
|
84
|
+
File.open('dir.xml','w'){|f| f.write doc.xml pretty: false}
|
79
85
|
[doc, "created"]
|
80
86
|
end
|
81
87
|
|
82
88
|
def run(a)
|
83
89
|
|
84
|
-
doc =
|
85
|
-
|
86
|
-
a_dir = XPath.match(doc.root, 'records/file/name/text()').map {|x| x.to_s}.sort
|
90
|
+
doc = Rexle.new(File.open('dir.xml','r').read)
|
91
|
+
a_dir = doc.xpath('records/file/name/text()').sort
|
87
92
|
|
88
93
|
return [doc, "nothing new"] if a == a_dir
|
89
94
|
|
@@ -94,14 +99,11 @@ class DirToXML
|
|
94
99
|
# files to delete
|
95
100
|
files_to_delete = a_dir - a
|
96
101
|
files_to_delete.each do |filename|
|
97
|
-
node = doc.
|
98
|
-
node.
|
102
|
+
node = doc.element("records/file[name='#{filename}']")
|
103
|
+
node.delete
|
99
104
|
end
|
100
105
|
|
101
|
-
#File.open('dir.xml','w'){|f| f.write pretty_xml(doc)}
|
102
106
|
File.open('dir.xml','w'){|f| doc.write f}
|
103
107
|
[doc, "updated"]
|
104
108
|
end
|
105
109
|
end
|
106
|
-
|
107
|
-
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dir-to-xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors: []
|
7
7
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2011-02-24 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|