Class: Translator

Inherits:
Object
  • Object
show all
Includes:
Utils_set
Defined in:
lib/ebngen/translate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils_set

#nodes_exist?

Constructor Details

#initialize(node, logger = nil) ⇒ Translator

Returns a new instance of Translator.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ebngen/translate.rb', line 22

def initialize(node, logger = nil)
    @logger = logger 
    unless (logger)
      @logger = Logger.new(STDOUT)
      @logger.level = Logger::INFO
    end
    @data_in = deep_copy(node)
    @data_in_component = deep_copy(node)
    @data_out = Hash.new
    merge_by_add(@data_in)
end

Instance Attribute Details

#data_inObject

Returns the value of attribute data_in.



18
19
20
# File 'lib/ebngen/translate.rb', line 18

def data_in
  @data_in
end

#data_outObject

Returns the value of attribute data_out.



18
19
20
# File 'lib/ebngen/translate.rb', line 18

def data_out
  @data_out
end

#reference_pathObject

Returns the value of attribute reference_path.



18
19
20
# File 'lib/ebngen/translate.rb', line 18

def reference_path
  @reference_path
end

Instance Method Details

#deep_add_merge(struct, subnode, addon) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ebngen/translate.rb', line 38

def deep_add_merge(struct, subnode, addon)
  return if Hash != struct.class
  if struct[addon]['__add__'].nil?
    #we do not want the addon module to change the status
    struct[addon]['attribute'] = ""
    struct[subnode] = struct[subnode].deep_merge(deep_copy(struct[addon]))
    struct[addon]['attribute'] = 'required'
    return
  end
  #if has more addon
  if struct[addon]['__add__'].count != 0     
     struct[addon]['__add__'].each do |submodule|
       deep_add_merge(struct, addon, submodule)
     end
     struct[subnode] = struct[subnode].deep_merge(deep_copy(struct[addon]))
     struct[addon]['attribute'] = 'required'
  else
    struct[addon]['attribute'] = ""
    struct[subnode] = struct[subnode].deep_merge(deep_copy(struct[addon]))
    struct[addon]['attribute'] = 'required'     
  end
end

#deep_copy(o) ⇒ Object



34
35
36
# File 'lib/ebngen/translate.rb', line 34

def deep_copy(o)
  Marshal.load(Marshal.dump(o))
end

#merge_by_add(struct) ⇒ Object

perform merge by “__add__” node only applys to application type



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ebngen/translate.rb', line 62

def merge_by_add(struct)
    #only scan the top level
    return if Hash != struct.class
    struct.each_key do |subnode|
      if struct[subnode]['__add__'] != nil
        struct[subnode]['__add__'].each do |addon|
          next if struct[addon].class != Hash
        begin
          next if struct[subnode]['configuration']['section-type'] != "application"
          if struct[addon]['configuration']['section-type'] != "component"
            @logger.warn "WARNING #{addon} is required as component but has not a component attribute"
          end
        rescue
          @logger.error "error with the merge_by_add with #{subnode} add #{addon}"
        end
          deep_add_merge(struct, subnode, addon)
        end
        #struct[subnode].delete('__add__')
      end
    end
end

#output_path(proj, comp) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ebngen/translate.rb', line 90

def output_path(proj, comp)
  if @data_in[proj].has_key?('outdir')
    board = @data_in[proj]['configuration']['board']
    if @data_out[proj][comp]['type'] == "library"
      if comp == 'uv4'
        @data_out[proj][comp]['outdir'] = File.join(@data_in[proj]['outdir'], proj, "mdk")
      else
        @data_out[proj][comp]['outdir'] = File.join(@data_in[proj]['outdir'], proj, comp)
      end
    else
      if comp == "uv4"
        @data_out[proj][comp]['outdir'] = File.join(board, @data_in[proj]['outdir'], "mdk")
      else
        @data_out[proj][comp]['outdir'] = File.join(board, @data_in[proj]['outdir'], comp)
      end
    end
  end
end

#translateObject



84
85
86
87
88
# File 'lib/ebngen/translate.rb', line 84

def translate
   translate_project()
   return [@data_out, @data_in_component, @data_in]
   #puts @data_out.to_yaml
end