Class: AIPP::Parser
Overview
AIP parser infrastructure
Instance Attribute Summary collapse
-
#aixm ⇒ AIXM::Document
readonly
Target document.
-
#borders ⇒ Hash
readonly
Map from border names to border objects.
-
#cache ⇒ OpenStruct
readonly
Object cache.
-
#config ⇒ Hash
readonly
Configuration read from config.yml.
-
#fixtures ⇒ Hash
readonly
Map from AIP name to fixtures.
-
#options ⇒ Hash
readonly
Passed command line arguments.
Instance Method Summary collapse
-
#initialize(options:) ⇒ Parser
constructor
A new instance of Parser.
-
#parse_aip ⇒ Object
Parse AIP by invoking the parser classes for the current region.
-
#read_config ⇒ Object
Read the configuration from config.yml.
-
#read_region ⇒ Object
Read the region directory and build the dependency list.
-
#validate_aixm ⇒ Object
Validate the AIXM document.
-
#write_aixm ⇒ Object
Write the AIXM document.
-
#write_config ⇒ Object
Write the configuration to config.yml.
Constructor Details
#initialize(options:) ⇒ Parser
Returns a new instance of Parser.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/aipp/parser.rb', line 24 def initialize(options:) @options = @options[:storage] = [:storage].join([:region]) @options[:storage].mkpath unless @options[:storage].exist? @config = {} @aixm = AIXM.document(region: @options[:region], effective_at: @options[:airac].date) @dependencies = THash.new @fixtures = {} @borders = {} @cache = OpenStruct.new AIXM.send("#{[:schema]}!") end |
Instance Attribute Details
#aixm ⇒ AIXM::Document (readonly)
Returns target document.
13 14 15 |
# File 'lib/aipp/parser.rb', line 13 def aixm @aixm end |
#borders ⇒ Hash (readonly)
Returns map from border names to border objects.
19 20 21 |
# File 'lib/aipp/parser.rb', line 19 def borders @borders end |
#cache ⇒ OpenStruct (readonly)
Returns object cache.
22 23 24 |
# File 'lib/aipp/parser.rb', line 22 def cache @cache end |
#config ⇒ Hash (readonly)
Returns configuration read from config.yml.
10 11 12 |
# File 'lib/aipp/parser.rb', line 10 def config @config end |
#fixtures ⇒ Hash (readonly)
Returns map from AIP name to fixtures.
16 17 18 |
# File 'lib/aipp/parser.rb', line 16 def fixtures @fixtures end |
#options ⇒ Hash (readonly)
Returns passed command line arguments.
7 8 9 |
# File 'lib/aipp/parser.rb', line 7 def @options end |
Instance Method Details
#parse_aip ⇒ Object
Parse AIP by invoking the parser classes for the current region.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/aipp/parser.rb', line 77 def parse_aip info("AIRAC #{[:airac].id} effective #{[:airac].date}", color: :green) AIPP::Downloader.new(storage: [:storage], archive: [:airac].date.xmlschema) do |downloader| @dependencies.tsort([:aip]).each do |aip| info("Parsing #{aip}") ("AIPP::%s::%s" % [[:region], aip.remove(/\W/).classify]).constantize.new( aip: aip, downloader: downloader, fixture: @fixtures[aip], parser: self ).attach_patches.tap(&:parse).detach_patches end end end |
#read_config ⇒ Object
Read the configuration from config.yml.
38 39 40 41 42 43 |
# File 'lib/aipp/parser.rb', line 38 def read_config info("Reading config.yml") @config = YAML.load_file(config_file, fallback: {}).transform_keys(&:to_sym) if config_file.exist? @config[:namespace] ||= SecureRandom.uuid @aixm.namespace = @config[:namespace] end |
#read_region ⇒ Object
Read the region directory and build the dependency list.
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 |
# File 'lib/aipp/parser.rb', line 46 def read_region info("Reading region #{[:region]}") dir = Pathname(__FILE__).dirname.join('regions', [:region]) fail("unknown region `#{[:region]}'") unless dir.exist? # Fixtures dir.glob('fixtures/*.yml').each do |file| verbose_info "Reading fixture fixtures/#{file.basename}" fixture = YAML.load_file(file) @fixtures[file.basename('.yml').to_s] = fixture end # Borders dir.glob('borders/*.geojson').each do |file| verbose_info "Reading border borders/#{file.basename}" border = AIPP::Border.new(file) @borders[border.name] = border end # Helpers dir.glob('helpers/*.rb').each do |file| verbose_info "Reading helper helpers/#{file.basename}" require file end # Parsers dir.glob('*.rb').each do |file| verbose_info "Requiring #{file.basename}" require file aip = file.basename('.*').to_s @dependencies[aip] = ("AIPP::%s::%s::DEPENDS" % [[:region], aip.remove(/\W/).classify]).constantize end end |
#validate_aixm ⇒ Object
Validate the AIXM document.
95 96 97 98 99 100 101 |
# File 'lib/aipp/parser.rb', line 95 def validate_aixm info("Validating #{[:schema].upcase}") unless aixm.valid? = "invalid #{[:schema].upcase} document:\n" + aixm.errors.map(&:message).join("\n") @options[:force] ? warn(, pry: binding) : fail() end end |
#write_aixm ⇒ Object
Write the AIXM document.
104 105 106 107 108 |
# File 'lib/aipp/parser.rb', line 104 def write_aixm file = "#{[:region]}_#{[:airac].date.xmlschema}.#{[:schema]}" info("Writing #{file}") File.write(file, aixm.to_xml) end |
#write_config ⇒ Object
Write the configuration to config.yml.
111 112 113 114 |
# File 'lib/aipp/parser.rb', line 111 def write_config info("Writing config.yml") File.write(config_file, config.to_yaml) end |