Class: MODL::Parser::FileImporter

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/modl/parser/file_importer.rb

Overview

This class handled file loading from local or remote file systems.

Constant Summary collapse

CACHE_DISABLED =
false

Instance Method Summary collapse

Constructor Details

#initializeFileImporter

Returns a new instance of FileImporter.



38
39
40
# File 'lib/modl/parser/file_importer.rb', line 38

def initialize
  @cache = ObjectCache.new
end

Instance Method Details

#import_files(files, global) ⇒ Object

Supply a single file name as a string or an array of file names.



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
# File 'lib/modl/parser/file_importer.rb', line 43

def import_files(files, global)
  file_names = []
  file_names += files if files.is_a? Array
  file_names << files if files.is_a? String

  file_names.each do |file_name|
    force = file_name.end_with?('!')
    file_name = Sutil.head(file_name) if force
    file_name << '.modl' unless file_name.end_with?('.txt', '.modl')
    file_name, _new_val = RefProcessor.deref file_name, global if file_name.include?('%')
    if force
      # Don't use the cache if we're forcing a reload.
      @cache.evict(file_name)
      parsed = nil
    else
      # Do we have a cached version?
      parsed = @cache.get(file_name)
    end

    # Did we hit the cache?
    if parsed.nil? || CACHE_DISABLED
      # No.

      begin
        if file_name.start_with?('http')
          uri = URI(file_name)
          txt = Net::HTTP.get(uri)
        else
          txt = File.readlines(file_name).join
        end
      rescue StandardError => e
        # Force load from the cache if possible
        parsed = @cache.force_get(file_name)
        if parsed.nil?
          raise InterpreterError, 'File not found: ' + file_name + ', error: ' + e.message
        end
      end
      global.loaded_file(file_name)

      # Parse the downloaded file ands extract the classes
      new_parse_context = GlobalParseContext.new
      new_parse_context.merge_pairs(global)
      parsed = MODL::Parser::Parser.parse txt, new_parse_context
      # Save it for next time
      @cache.put(file_name, parsed) unless CACHE_DISABLED
    else
      global.loaded_file(file_name)
    end
    # Extract the JSON content and add the classes and pairs to the existing GlobalParseContext hashes.
    parsed.extract_hash
    global.merge_classes(parsed.global)
    global.merge_pairs(parsed.global)
    global.merge_loaded_files(parsed.global)
    global.structures.concat(parsed.structures)
  end
end