-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacs.rb
More file actions
executable file
·236 lines (208 loc) · 7.06 KB
/
pacs.rb
File metadata and controls
executable file
·236 lines (208 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env ruby -wKU
#
# parse PACS2008 file and do things with it
#
# Author: Eike Bernhardt <bernhardt@isn-oldenburg.de>
#
# (c) Copyright 2009 ISN Oldenburg http://www.isn-oldenburg.de/
#
# Released under the MIT License - see LICENSE
#
require 'open-uri'
require "strscan"
# PACSFILE = "http://www.aip.org/pacs/pacs08/ASCII2008FullPacs.txt"
PACSFILE = ARGV[0]
MODE=ARGV[1] || "hash" # see print()
if PACSFILE.nil? or PACSFILE.empty?
puts "Usage: pacs.rb PACSFILE (MODE)"
puts "\tvalid modes: hash, ruby, text"
exit 1
end
class PACSParser
PACS = Struct.new(:id, :desc, :children, :comment)
attr_reader :container
def initialize
@container = Array.new
@current = @parent = nil
end
def load
begin
f = File.open(PACSFILE,'r')
@lines = f.read
f.close
# sort, so that the ids are in the right order
# @lines.sort{|a,b| a.casecmp(b)}.to_s
@lines
rescue Exception => e
puts "Error loading file: #{e}"
end
end
def parse(input)
@input = StringScanner.new(input)
begin
while !@input.eos?
trim_space ||
parse_toplevel ||
parse_secondlevel ||
parse_thirdlevel ||
parse_fourthlevel ||
parse_fifthlevel ||
parse_comment ||
ignore
end
rescue Exception => e
ap @container
puts "Error in line: "+@input[0]
raise e
end
end
def print
@container.each do |i|
if MODE == "ruby"
print_ruby(i) if !i.nil?
elsif MODE == "hash"
print_hash(i) if !i.nil?
else
print_pacs(i) if !i.nil?
end
end
end
def print_pacs(pacs, indent = "")
puts "#{indent}#{pacs.id} #{pacs.desc}" unless pacs.id.nil? or pacs.id.empty?
if !pacs.comment.nil?
pacs.comment.each do |c|
puts "#{indent} ... ... #{c}"
end
end
if !pacs.children.nil?
pacs.children.keys.sort{ |a,b| a.to_s.casecmp(b.to_s) }.each do |k|
print_pacs(pacs.children[k], indent+"\t") if !pacs.children[k].nil?
end
end
end
def print_ruby(pacs, parent = nil)
puts "Pacs.create!(:pid => '#{pacs.id}', :desc => '#{pacs.desc.gsub(/'/, "\\\\'")}', :comment => '#{pacs.comment.nil? ? '' : pacs.comment.join("\n")}', :parent => #{parent.nil? ? 'nil' : "Pacs.find_by_pid('#{parent}')"})" unless pacs.id.nil? or pacs.id.empty?
if !pacs.children.nil?
pacs.children.keys.sort{ |a,b| a.to_s.casecmp(b.to_s) }.each do |k|
print_ruby(pacs.children[k], pacs.id) if !pacs.children[k].nil?
end
end
end
def print_hash(pacs, parent = nil)
puts "pacs['#{pacs.id}'] = %{#{pacs.desc.strip}}" unless pacs.id.nil? or pacs.id.empty?
if !pacs.children.nil?
pacs.children.keys.sort{ |a,b| a.to_s.casecmp(b.to_s) }.each do |k|
print_hash(pacs.children[k], pacs.id) if !pacs.children[k].nil?
end
end
end
private
def parse_toplevel
if @input.scan(/(\d0)\.(\d{2})\.(..) (.*)\n/)
@current_id = "#{@input[1]}.#{@input[2]}.#{@input[3]}"
@desc = @input[4]
@container.push( PACS.new(@current_id, @desc, {}) )
@current = @container.last
elsif @input.scan(/(\d0)\.\s{8}(.*)\n/)
@current_id = "#{@input[1]}."
@desc = @input[2]
@container.push( PACS.new(@current_id, @desc, {}) )
@current = @container.last
else
false
end
end
def parse_secondlevel
if @input.scan(/(\d{2})\.(00)\.(..) (.*)\n/)
@current_id = "#{@input[1]}.#{@input[2]}.#{@input[3]}"
@desc = @input[4]
@parent = @container[@input[1].to_i/10.round]
@current = @parent.children[@current_id] = PACS.new(@current_id, @desc, {})
elsif @input.scan(/(\d{2})\.\s{8}(.*)\n/)
@current_id = "#{@input[1]}."
@desc = @input[2]
@parent = @container[@input[1].to_i/10.round]
@current = @parent.children[@current_id] = PACS.new(@current_id, @desc, {})
else
false
end
end
def parse_thirdlevel
if @input.scan(/(\d{2})\.(\d{2})\.(\+.) (.*)\n/) # normal 3rd level, just add
@current_3rd_lv = @current_id = "#{@input[1]}.#{@input[2]}.#{@input[3]}"
@desc = @input[4]
# this might end a subgroup. so re-set parent
@parent = @container[@input[1].to_i/10.round].children["#{@input[1]}.00.00"] || @container[@input[1].to_i/10.round].children["#{@input[1]}."]
@current = @parent.children[@current_id] = PACS.new(@current_id, @desc, {})
elsif @input.scan(/(\d{2})\.(\d{2})\.(\-.) (.*)\n/) # opening of 4th level group
@current_3rd_lv = @current_id = "#{@input[1]}.#{@input[2]}.#{@input[3]}"
@desc = @input[4]
# errata group
if @input[1] == '99' and @container[9].children['99.00.00'].nil?
@container[9].children['99.00.00'] = PACS.new('','', {})
end
# this might end a subgroup. so re-set parent
@parent = @container[@input[1].to_i/10.round].children["#{@input[1]}.00.00"] || @container[@input[1].to_i/10.round].children["#{@input[1]}."]
@current = @parent = @parent.children[@current_id] = PACS.new(@current_id, @desc, {})
else
false
end
end
def parse_fourthlevel
if @input.scan(/(\d{2})\.(\d{2})\.([A-Z][^-]) (.*)\n/) # normal 4th level, just add
@current_id = "#{@input[1]}.#{@input[2]}.#{@input[3]}"
@desc = @input[4]
# this resets parent to level 4
@parent = (@container[@input[1].to_i/10.round].children["#{@input[1]}.00.00"] || @container[@input[1].to_i/10.round].children["#{@input[1]}."]).children[@current_3rd_lv]
@current = @parent.children[@current_id] = PACS.new(@current_id, @desc, {})
elsif @input.scan(/(\d{2})\.(\d{2})\.([A-Z]\-) (.*)\n/) # opening of 5th level group
@current_id = "#{@input[1]}.#{@input[2]}.#{@input[3]}"
@desc = @input[4]
# this resets parent to level 4
@parent = (@container[@input[1].to_i/10.round].children["#{@input[1]}.00.00"] || @container[@input[1].to_i/10.round].children["#{@input[1]}."]).children[@current_3rd_lv]
@current = @parent = @parent.children[@current_id] = PACS.new(@current_id, @desc, {})
else
false
end
end
def parse_fifthlevel
if @input.scan(/(\d{2})\.(\d{2})\.([a-z][a-z]) (.*)\n/) # normal 5th level, just add
@current_id = "#{@input[1]}.#{@input[2]}.#{@input[3]}"
@desc = @input[4]
@current = @parent.children[@current_id] = PACS.new(@current_id, @desc, {})
else
false
end
end
def parse_comment
if @input.scan(/\.\.\. \.\.\. (.*)\n/)
# found a comment, append this to @current element
comment = @input[1]
@current.comment ||= []
@current.comment.push comment
else
false
end
end
def ignore
if !@input.check(/..\...\... .*\n/) and !@input.check(/ \.{3} \.{3} .*\n/) and !@input.check(/\d{2}\.\s{8}.*\n/) and @input.scan(/(.*)\n/)
STDERR.puts "Ignoring '#{@input[1]}'"
else
false
end
end
def trim_space
@input.scan(/\s+/)
end
def error(message)
if @input.eos?
raise "Unexpected end of input."
else
raise "#{message}: '#{@input.peek(@input.string.length)}'"
end
end
end
p = PACSParser.new
p.parse(p.load)
p.print
# puts p.container.to_yaml