Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 773c8168 authored by Jérémie Gaidamour's avatar Jérémie Gaidamour
Browse files

[dev] Split input_loader file. Moved hash functionalities to lib/hash/hash.rb

parent fdc21ae7
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/ruby # Monkey patching Ruby's Hash class
require 'pp'
require 'pathname'
require 'yaml'
require 'json'
# Merge a and b. If a and b are hashes, they are recursively merged. # Merge a and b. If a and b are hashes, they are recursively merged.
# - a and b might be strings or nil. # - a and b might be strings or nil.
...@@ -23,8 +18,8 @@ class ::Hash ...@@ -23,8 +18,8 @@ class ::Hash
# Returns a new hash containing the contents of other_hash and the contents of hash. The value for entries with duplicate keys will be that of other_hash: # Returns a new hash containing the contents of other_hash and the contents of hash. The value for entries with duplicate keys will be that of other_hash:
# a = {"key": "value_a"} # a = {"key": "value_a"}
# b = {"key": "value_b"} # b = {"key": "value_b"}
# pp a.deep_merge(b) => {:key=>"value_b"} # a.deep_merge(b) -> {:key=>"value_b"}
# pp b.deep_merge(a) => {:key=>"value_a"} # b.deep_merge(a) -> {:key=>"value_a"}
def deep_merge(other_hash) def deep_merge(other_hash)
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
self.merge(other_hash, &merger) self.merge(other_hash, &merger)
...@@ -91,39 +86,5 @@ class ::Hash ...@@ -91,39 +86,5 @@ class ::Hash
def self.from_array(array, value) def self.from_array(array, value)
return array.reverse.inject(value) { |a, n| { n => a } } return array.reverse.inject(value) { |a, n| { n => a } }
end end
end
global_hash = {} # the global data structure
Dir.chdir("../input/grid5000/")
# Recursively list the .yaml files.
# The order in which the results are returned depends on the system (http://ruby-doc.org/core-2.2.3/Dir.html).
# => List deepest files first as they have lowest priority when hash keys are duplicated.
list_of_yaml_files = Dir['**/*.y*ml'].sort_by { |x| -x.count('/') }
list_of_yaml_files.each { |filename|
# Load YAML
file_hash = YAML::load_file(filename)
if not file_hash
puts "Error loading '#{filename}'"
next
end
# Expand the hash
file_hash.expand_angle_brackets()
# Inject the file content into the global_hash, at the right place
path_hierarchy = File.dirname(filename).split('/') # Split the file path (path relative to input/)
file_hash = Hash.from_array(path_hierarchy, file_hash) # Build the nested hash hierarchy according to the file path
global_hash = global_hash.deep_merge(file_hash) # Merge global_hash and file_hash. The value for entries with duplicate keys will be that of file_hash
}
#pp global_hash
#pp data
#puts JSON.generate(data)
end
# Load a hierarchy of YAML file into a Ruby hash
require 'yaml'
require '../lib/hash/hash.rb'
def load_yaml_file_hierarchy(directory)
global_hash = {} # the global data structure
Dir.chdir(directory) {
# Recursively list the .yaml files.
# The order in which the results are returned depends on the system (http://ruby-doc.org/core-2.2.3/Dir.html).
# => List deepest files first as they have lowest priority when hash keys are duplicated.
list_of_yaml_files = Dir['**/*.y*ml'].sort_by { |x| -x.count('/') }
list_of_yaml_files.each { |filename|
# Load YAML
file_hash = YAML::load_file(filename)
if not file_hash
puts "Error loading '#{filename}'"
next
end
# Expand the hash
file_hash.expand_angle_brackets()
# Inject the file content into the global_hash, at the right place
path_hierarchy = File.dirname(filename).split('/') # Split the file path (path relative to input/)
file_hash = Hash.from_array(path_hierarchy, file_hash) # Build the nested hash hierarchy according to the file path
global_hash = global_hash.deep_merge(file_hash) # Merge global_hash and file_hash. The value for entries with duplicate keys will be that of file_hash
}
}
return global_hash
end
#!/usr/bin/ruby
require_relative "input_loader" require_relative "input_loader"
require "test/unit" require "test/unit"
require "hashdiff" require "hashdiff"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment