Class: AIPP::Parser

Inherits:
Object show all
Defined in:
lib/aipp/parser.rb

Overview

AIP parser infrastructure

Instance Attribute Summary collapse

Instance Method Summary collapse

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
  @options[:storage] = options[:storage].join(options[: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("#{options[:schema]}!")
end

Instance Attribute Details

#aixmAIXM::Document (readonly)

Returns target document.

Returns:

  • (AIXM::Document)

    target document



13
14
15
# File 'lib/aipp/parser.rb', line 13

def aixm
  @aixm
end

#bordersHash (readonly)

Returns map from border names to border objects.

Returns:

  • (Hash)

    map from border names to border objects



19
20
21
# File 'lib/aipp/parser.rb', line 19

def borders
  @borders
end

#cacheOpenStruct (readonly)

Returns object cache.

Returns:

  • (OpenStruct)

    object cache



22
23
24
# File 'lib/aipp/parser.rb', line 22

def cache
  @cache
end

#configHash (readonly)

Returns configuration read from config.yml.

Returns:

  • (Hash)

    configuration read from config.yml



10
11
12
# File 'lib/aipp/parser.rb', line 10

def config
  @config
end

#fixturesHash (readonly)

Returns map from AIP name to fixtures.

Returns:

  • (Hash)

    map from AIP name to fixtures



16
17
18
# File 'lib/aipp/parser.rb', line 16

def fixtures
  @fixtures
end

#optionsHash (readonly)

Returns passed command line arguments.

Returns:

  • (Hash)

    passed command line arguments



7
8
9
# File 'lib/aipp/parser.rb', line 7

def options
  @options
end

Instance Method Details

#parse_aipObject

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 #{options[:airac].id} effective #{options[:airac].date}", color: :green)
  AIPP::Downloader.new(storage: options[:storage], archive: 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,
        fixture: @fixtures[aip],
        parser: self
      ).attach_patches.tap(&:parse).detach_patches
    end
  end
end

#read_configObject

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_regionObject

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 #{options[:region]}")
  dir = Pathname(__FILE__).dirname.join('regions', options[:region])
  fail("unknown region `#{options[: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" % [options[:region], aip.remove(/\W/).classify]).constantize
  end
end

#validate_aixmObject

Validate the AIXM document.

Raises:

  • (RuntimeError)

    if the document is not valid



95
96
97
98
99
100
101
# File 'lib/aipp/parser.rb', line 95

def validate_aixm
  info("Validating #{options[:schema].upcase}")
  unless aixm.valid?
    message = "invalid #{options[:schema].upcase} document:\n" + aixm.errors.map(&:message).join("\n")
    @options[:force] ? warn(message, pry: binding) : fail(message)
  end
end

#write_aixmObject

Write the AIXM document.



104
105
106
107
108
# File 'lib/aipp/parser.rb', line 104

def write_aixm
  file = "#{options[:region]}_#{options[:airac].date.xmlschema}.#{options[:schema]}"
  info("Writing #{file}")
  File.write(file, aixm.to_xml)
end

#write_configObject

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