Class: MifFile

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/mif_file.rb

Defined Under Namespace

Classes: Helper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_year_to_title(title, dir, filename) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/mif_file.rb', line 86

def append_year_to_title title, dir, filename
  cmd = %Q[cd "#{dir}"; grep -A2 "AttrName \\`CopyrightYear'" '#{filename}' | grep AttrValue]
  values = `#{cmd}`
  if values == ''
    values = get_date(dir, filename)
  end
  year = ""
  if values[/.*(\d\d\d\d).*/]
    title += " #{$1}"
  end
  title
end

.bill_to_paths(paths) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/mif_file.rb', line 17

def bill_to_paths paths
  directories = paths.collect {|x| File.dirname(x)}.uniq

  bills = directories.inject({}) do |hash, dir|
    cmd = %Q[cd "#{dir}"; grep -A12 "ETag \\`Shorttitle'" *.mif | grep String]
    values = `#{cmd}`
    cmd = %Q[cd "#{dir}"; grep -A1 "<AttrName \\`ShortTitle'" *.mif | grep AttrValue]
    values += `#{cmd}`
    cmd = %Q[cd "#{dir}"; grep -A24 "ETag \\`CommitteeShorttitle'" *.mif | grep String]
    values += `#{cmd}`
    parse_bill_titles(values, dir) do |file, title|
      if hash[file]
        hash[file] += title
      else
        hash[file] = title
      end
    end
    hash
  end
  bills
end

.get_date(dir, filename) ⇒ Object


204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/mif_file.rb', line 204

def get_date dir, filename
  values = ""
  cmd = %Q[cd "#{dir}"; grep -A8 "ETag \\`Date.text'" '#{filename}' | grep String]
  values += `#{cmd}`
  if values == ''
    cmd = %Q[cd "#{dir}"; grep -A8 "ETag \\`Day'" '#{filename}' | grep String]
    values += `#{cmd}`
  end
  if values == ''
    cmd = %Q[cd "#{dir}"; grep -A8 "ETag \\`Date'" '#{filename}' | grep String]
    values += `#{cmd}`
  end
  values
end

.get_file_type(dir, filename) ⇒ Object


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
# File 'app/models/mif_file.rb', line 129

def get_file_type dir, filename
  cmd = %Q[cd "#{dir}"; grep -A1 "<PDFBookInfo " '#{filename}']
  values = `#{cmd}`
  unless values == ''
    return "Book File"
  end

  cmd = %Q[cd "#{dir}"; grep -A7 "ETag \\`NoticeOfAmds'" '#{filename}' | grep String]
  values = `#{cmd}`
  if values.downcase.include?('notices of amendments')
    return "Marshalled List"
  end

  cmd = %Q[cd "#{dir}"; grep -A7 "ETag \\`Stageheader'" '#{filename}' | grep String]
  values = `#{cmd}`
  if values.downcase.include?('consideration of bill')
    cmd = %Q[cd "#{dir}"; grep -A7 "ETag \\`Date'" '#{filename}' | grep String]
    values = `#{cmd}`
    if values.downcase.include?('tabled')
      return "Tabled Report"
    end
    cmd = %Q[cd "#{dir}"; grep -A7 "PgfTag \\`Header'" '#{filename}' | grep String]
    values = `#{cmd}`
    if values.downcase.include?('tabled')
      return "Tabled Report"
    end

    return "Report"
  end

  cmd = %Q[cd "#{dir}"; grep -A12 "ETag \\`Stageheader'" '#{filename}' | grep String]
  values = `#{cmd}`
  if values.downcase.include?('committee')
    cmd = %Q[cd "#{dir}"; grep -A7 "ETag \\`Day'" '#{filename}' | grep String]
    values = `#{cmd}`
    if values.downcase.include?('tabled')
      return "Tabled Amendments"
    end

    return "Amendments"
  end

  cmd = %Q[cd "#{dir}"; grep -A1 "ETag \\`WordsOfEnactment'" '#{filename}']
  values = `#{cmd}`
  unless values == ''
    return "Clauses"
  end

  cmd = %Q[cd "#{dir}"; grep -A2 "<ElementBegin" '#{filename}' | grep "ETag \\`Arrangement'"]
  values = `#{cmd}`
  unless values == ''
    return "Arrangement"
  end

  cmd = %Q[cd "#{dir}"; grep -A5 "PgfTag \\`SchedulesTitle'" '#{filename}' | grep ETag]
  values = `#{cmd}`
  unless values == ""
    return "Schedules"
  end

  cmd = %Q[cd "#{dir}"; grep -A1 "ETag \\`Endorse'" '#{filename}']
  values = `#{cmd}`
  unless values == ''
    return "Endorsement"
  end

  cmd = %Q[cd "#{dir}"; grep -A1 "ETag \\`Cover'" '#{filename}']
  values = `#{cmd}`
  unless values == ''
    return "Cover"
  end

  return "Other"
end

.get_print_number(dir, filename) ⇒ Object


219
220
221
222
223
# File 'app/models/mif_file.rb', line 219

def get_print_number dir, filename
  cmd = %Q[cd "#{dir}";  grep -A1 "<AttrName \\`PrintNumber'>" '#{filename}' | grep "<AttrValue"]
  values = `#{cmd}`
  values.gsub("<AttrValue \`", '').gsub("'>", "").gsub("HL ", "").gsub("Bill ", "").strip
end

.get_session_number(dir, filename) ⇒ Object


225
226
227
228
229
# File 'app/models/mif_file.rb', line 225

def get_session_number dir, filename
  cmd = %Q[cd "#{dir}";  grep -A1 "<AttrName \\`SessionNumber'>" '#{filename}' | grep "<AttrValue"]
  values = `#{cmd}`
  values.gsub("<AttrValue \`", '').gsub("'>", "").strip
end

.load(paths) ⇒ Object


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
# File 'app/models/mif_file.rb', line 39

def load paths
  bills = bill_to_paths paths

  paths.collect do |path|
    file = find_or_create_by_path(path)
    if file.file_type.nil?
      parts = path.split("/")
      filename = parts.pop
      filedir = parts.join("/")
      file_type = get_file_type(filedir, filename)
      file.set_date_or_bill_number(filedir, filename)
    end

    bill_name = bills[path]
    if file_type == 'Endorsement'
      set_bill_version(filedir, filename, bill_name)
    end
    if path.include?('Finance_Clauses.xml')
      bill_name = 'Finance Bill 2009'
      file_type = 'Clauses'
    end

    file.set_bill_title(bill_name) if file.bill_id.nil? && bill_name
    file.set_file_type(file_type) if file.file_type.nil? && file_type

    file
  end
end

.parse_bill_titles(lines, dir) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/mif_file.rb', line 68

def parse_bill_titles lines, dir
  lines.each_line do |line|
    return if line.blank?
    parts = line.split('.mif')
    file = "#{dir}/#{parts[0].strip}.mif"
    title = parts[1][/(String|AttrValue) `([^']+)'/, 2].chomp(', as amended')
    title = title.split.collect {|x| x.capitalize}.join(' ') if title[/^[A-Z ]+$/]
    title.sub!(/\sbill/i, ' Bill')
    if title[0..0][/[a-z]/]
      title = title[0..0].upcase + title[1..(title.length-1)]
    end
    if title == "Finance Bill"
      title = append_year_to_title(title, dir, "#{parts[0].strip}.mif")
    end
    yield [file, title]
  end
end

.set_bill_version(dir, filename, bill_name) ⇒ Object


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
# File 'app/models/mif_file.rb', line 99

def set_bill_version(dir, filename, bill_name)
  cmd = %Q[cd "#{dir}";  grep -A30 "<String \\`to be Printed'>" '#{filename}' | grep "<String"]
  values = `#{cmd}`
  values = values.split("\r\n")
  values.each do |value|
    value = value.strip!.gsub!("<String \`","").gsub!("'>","")
  end
  values.reverse!.pop
  printed_date = values.reverse!.join.gsub(", ","")

  bill_number = MifFile.get_print_number(dir, filename)
  session_number = MifFile.get_session_number(dir, filename)

  cmd = %Q[cd "#{dir}";  grep -A1 "<AttrName \\`House'>" '#{filename}' | grep "<AttrValue"]
  values = `#{cmd}`
  house = values.gsub("<AttrValue \`", '').gsub("'>", "").strip

  bill_name = bill_name.chomp(' [HL]')
  if bill_name[/Bill$/] or bill_name[/Bill \d\d\d\d$/]
    bill = Bill.from_name bill_name
    version = Version.create!(
      :printed_date => printed_date,
      :bill_number => bill_number,
      :session_number => session_number,
      :house => house,
      :bill_id => bill.id
    )
  end
end

Instance Method Details

#clauses_fileObject


275
276
277
278
279
280
281
282
283
284
285
286
# File 'app/models/mif_file.rb', line 275

def clauses_file
  if bill
    file = bill.clauses_file
    if file.blank? || file == self
      nil
    else
      file.path
    end
  else
    path
  end
end

#convert_to_haml(options = {}) ⇒ Object

options :interleave_notes => true (defaults to false) :force => true (defaults to false)


265
266
267
268
# File 'app/models/mif_file.rb', line 265

def convert_to_haml options={}
  do_convert_to_haml(options) if !haml_template_exists?(options) || options[:force]
  haml_template(options)
end

#convert_to_textObject


270
271
272
273
# File 'app/models/mif_file.rb', line 270

def convert_to_text
  xml = MifParser.new.parse path
  result = MifToHtmlParser.new.parse_xml xml, :format => :text, :body_only => true
end

#escaped_pathObject


12
13
14
# File 'app/models/mif_file.rb', line 12

def escaped_path
  URI.escape(self.path)
end

#haml_template_exists?(options = {}) ⇒ Boolean


296
297
298
# File 'app/models/mif_file.rb', line 296

def haml_template_exists? options={}
  File.exist?(haml_template(options)) && html_page_title
end

#has_effects?Boolean


292
293
294
# File 'app/models/mif_file.rb', line 292

def has_effects?
  bill && bill.has_effects?
end

#has_explanatory_notes?Boolean


288
289
290
# File 'app/models/mif_file.rb', line 288

def has_explanatory_notes?
  bill && bill.has_explanatory_notes?
end

#set_bill_title(text) ⇒ Object


252
253
254
255
256
257
258
259
260
# File 'app/models/mif_file.rb', line 252

def set_bill_title text
  text = text.chomp(' [HL]')
  if text[/Bill$/] or text[/Bill \d\d\d\d$/]
    bill = Bill.from_name text
    logger.info "  setting bill: #{text}"
    self.bill_id = bill.id
    self.save
  end
end

#set_date_or_bill_number(dir, filename) ⇒ Object


238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'app/models/mif_file.rb', line 238

def set_date_or_bill_number dir, filename
  printed_date = MifFile.get_date(dir, filename)
  if printed_date == ''
    session_number = MifFile.get_session_number(dir, filename)
    print_number = MifFile.get_print_number(dir, filename)
    self.session_number = session_number unless session_number == ""
    self.bill_number = print_number unless print_number == ""
    self.save unless print_number == "" and session_number == ""
  else
    self.printed_date = printed_date
    self.save
  end
end

#set_file_type(text) ⇒ Object


233
234
235
236
# File 'app/models/mif_file.rb', line 233

def set_file_type text
  self.file_type = text
  self.save
end