lib/aipp/parser.rb in aipp-0.2.4 vs lib/aipp/parser.rb in aipp-0.2.5
- old
+ new
@@ -1,10 +1,12 @@
module AIPP
# AIP parser infrastructure
class Parser
+ using AIXM::Refinements
+
# @return [Hash] passed command line arguments
attr_reader :options
# @return [Hash] configuration read from config.yml
attr_reader :config
@@ -22,11 +24,11 @@
attr_reader :cache
def initialize(options:)
@options = options
@options[:storage] = options[:storage].join(options[:region])
- @options[:storage].mkpath unless @options[:storage].exist?
+ @options[:storage].mkpath
@config = {}
@aixm = AIXM.document(region: @options[:region], effective_at: @options[:airac].date)
@dependencies = THash.new
@fixtures = {}
@borders = {}
@@ -74,11 +76,11 @@
end
# Parse AIP by invoking the parser classes for the current region.
def parse_aip
info("AIRAC #{options[:airac].id} effective #{options[:airac].date}", color: :green)
- AIPP::Downloader.new(storage: options[:storage], archive: options[:airac].date.xmlschema) do |downloader|
+ AIPP::Downloader.new(storage: options[:storage], source: options[:airac].date.xmlschema) do |downloader|
@dependencies.tsort(options[:aip]).each do |aip|
info("Parsing #{aip}")
("AIPP::%s::%s" % [options[:region], aip.remove(/\W/).classify]).constantize.new(
aip: aip,
downloader: downloader,
@@ -100,21 +102,72 @@
end
end
# Write the AIXM document.
def write_aixm
- file = "#{options[:region]}_#{options[:airac].date.xmlschema}.#{options[:schema]}"
- info("Writing #{file}")
- File.write(file, aixm.to_xml)
+ info("Writing #{aixm_file}")
+ AIXM.config.mid_region = options[:region] if options[:mid]
+ File.write(aixm_file, aixm.to_xml)
end
+ # Write the AIXM document and context information.
+ def write_build
+ info("Writing build")
+ builds_path.mkpath
+ build_file = builds_path.join("#{@options[:airac].date.xmlschema}.zip")
+ build_file.delete if build_file.exist?
+ Dir.mktmpdir do |tmp_dir|
+ tmp_dir = Pathname(tmp_dir)
+ AIXM.config.mid_region = options[:region]
+ # AIXM/OFMX file
+ File.write(tmp_dir.join(aixm_file), aixm.to_xml)
+ # Build details
+ File.write(
+ tmp_dir.join('build.yaml'), {
+ version: AIPP::VERSION,
+ config: @config,
+ options: @options
+ }.to_yaml
+ )
+ # Manifest
+ manifest, buffer, feature, uid, comment = [], '', '', '', ''
+ File.open(tmp_dir.join(aixm_file)).each do |line|
+ buffer << line
+ case line
+ when /^ {2}<(\w{3}) / then buffer, feature = line, $1
+ when /^ {4}<#{feature}Uid mid="(.*?)"/ then uid = $1
+ when /^ {2}<!-- (.*) -->/ then comment = $1
+ when /^ {2}<\/#{feature}>/
+ manifest << [feature, uid[0,8], buffer.payload_hash(region: options[:region])[0,8], comment].to_csv
+ feature, uid = '', ''
+ end
+ end
+ manifest = manifest.sort.prepend "Feature,Short Uid Hash,Short Feature Hash,Comment\n"
+ File.write(tmp_dir.join('manifest.csv'), manifest.join)
+ # Zip it
+ Zip::File.open(build_file, Zip::File::CREATE) do |zip|
+ tmp_dir.children.each do |entry|
+ zip.add(entry.basename.to_s, entry) unless entry.basename.to_s[0] == '.'
+ end
+ end
+ end
+ end
+
# Write the configuration to config.yml.
def write_config
info("Writing config.yml")
File.write(config_file, config.to_yaml)
end
private
+
+ def aixm_file
+ "#{options[:region]}_#{options[:airac].date.xmlschema}.#{options[:schema]}"
+ end
+
+ def builds_path
+ options[:storage].join('builds')
+ end
def config_file
options[:storage].join('config.yml')
end
end