bcl 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bcl/component_methods.rb +239 -0
- data/lib/bcl/{ComponentSpreadsheet.rb → component_spreadsheet.rb} +5 -5
- data/lib/bcl/component_xml.rb +476 -0
- data/lib/bcl/current_taxonomy.json +0 -0
- data/lib/bcl/{MasterTaxonomy.rb → master_taxonomy.rb} +2 -1
- data/lib/bcl/mongo_to_component.rb +114 -0
- data/lib/bcl/{TarBall.rb → tar_ball.rb} +20 -19
- data/lib/bcl/version.rb +1 -1
- data/lib/bcl.rb +6 -6
- metadata +125 -93
- data/lib/bcl/ComponentXml.rb +0 -479
- data/lib/bcl/GatherComponents.rb +0 -94
- data/lib/bcl/MongoToComponent.rb +0 -116
@@ -0,0 +1,476 @@
|
|
1
|
+
######################################################################
|
2
|
+
# Copyright (c) 2008-2013, Alliance for Sustainable Energy.
|
3
|
+
# All rights reserved.
|
4
|
+
#
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License as published by the Free Software Foundation; either
|
8
|
+
# version 2.1 of the License, or (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This library is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
# Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public
|
16
|
+
# License along with this library; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
######################################################################
|
19
|
+
|
20
|
+
# Provides programmatic access to the component.xsd schema needed for
|
21
|
+
# generating the component information that will be uploaded to
|
22
|
+
# the Building Component Library.
|
23
|
+
|
24
|
+
require 'rubygems'
|
25
|
+
require 'pathname'
|
26
|
+
require 'fileutils'
|
27
|
+
require 'csv'
|
28
|
+
|
29
|
+
# required gems
|
30
|
+
require 'builder' #gem install builder (creates xml files)
|
31
|
+
require 'uuid' # gem install uuid
|
32
|
+
require 'bcl/tar_ball'
|
33
|
+
|
34
|
+
|
35
|
+
module BCL
|
36
|
+
|
37
|
+
SCHEMA_LOCATION = "component.xsd"
|
38
|
+
|
39
|
+
ProvStruct = Struct.new(:author, :datetime, :comment)
|
40
|
+
TagsStruct = Struct.new(:descriptor)
|
41
|
+
AttrStruct = Struct.new(:name, :value, :datatype, :units)
|
42
|
+
FileStruct = Struct.new(:version_software_program, :version_id, :fqp_file, :filename, :filetype)
|
43
|
+
#cost_type is an enumeration (not enforced) of installation, material, operations and maintenance,
|
44
|
+
#variable operations and maintenance, salvage
|
45
|
+
CostStruct = Struct.new(:cost_name, :cost_type, :category, :value, :interval,
|
46
|
+
:interval_units, :year, :location, :units, :currency, :source,
|
47
|
+
:reference_component_name, :reference_component_id)
|
48
|
+
ObjectStruct = Struct.new(:obj_type, :obj_instance)
|
49
|
+
|
50
|
+
class Component
|
51
|
+
attr_accessor :name
|
52
|
+
attr_accessor :uid
|
53
|
+
attr_accessor :comp_version_id
|
54
|
+
attr_accessor :description
|
55
|
+
attr_accessor :comment
|
56
|
+
attr_accessor :source_manufacturer
|
57
|
+
attr_accessor :source_model
|
58
|
+
attr_accessor :source_serial_no
|
59
|
+
attr_accessor :source_year
|
60
|
+
attr_accessor :source_url
|
61
|
+
attr_accessor :tags
|
62
|
+
attr_accessor :provenance
|
63
|
+
attr_accessor :attributes
|
64
|
+
attr_accessor :files
|
65
|
+
attr_accessor :costs
|
66
|
+
attr_accessor :objects
|
67
|
+
|
68
|
+
public
|
69
|
+
|
70
|
+
#the save path is where the component will be saved
|
71
|
+
def initialize(save_path)
|
72
|
+
@name = "" #this is also a unique identifier to the component...
|
73
|
+
@uid = UUID.new.generate
|
74
|
+
@comp_version_id = UUID.new.generate
|
75
|
+
@description = ""
|
76
|
+
@comment = ""
|
77
|
+
@source_manufacturer = ""
|
78
|
+
@source_model = ""
|
79
|
+
@source_serial_no = ""
|
80
|
+
@source_year = ""
|
81
|
+
@source_url = ""
|
82
|
+
|
83
|
+
#these items have multiple instances
|
84
|
+
@provenance = []
|
85
|
+
@tags = []
|
86
|
+
@attributes = []
|
87
|
+
@files = []
|
88
|
+
@costs = []
|
89
|
+
@objects = [] #container for saving the idf/osm snippets
|
90
|
+
|
91
|
+
@path = save_path
|
92
|
+
|
93
|
+
#puts "[ComponentXml] " + @path
|
94
|
+
#need to hit a webservice to validate which tags and attributes are
|
95
|
+
#available (including units?)
|
96
|
+
|
97
|
+
#todo: validate against master taxonomy
|
98
|
+
end
|
99
|
+
|
100
|
+
def open_component(filename)
|
101
|
+
read_component_xml(filename)
|
102
|
+
end
|
103
|
+
|
104
|
+
# savefile, save the component xml along with
|
105
|
+
# the files that have been added to the object
|
106
|
+
def save_tar_gz(delete_files = true)
|
107
|
+
current_d = Dir.pwd
|
108
|
+
paths = []
|
109
|
+
|
110
|
+
save_component_xml
|
111
|
+
|
112
|
+
paths << "./component.xml"
|
113
|
+
|
114
|
+
#copy over the files to the directory
|
115
|
+
@files.each do |file|
|
116
|
+
src_path = Pathname.new(file.fqp_file)
|
117
|
+
dest_path = Pathname.new("#{resolve_path}/#{file.filename}")
|
118
|
+
if File.exists?(src_path)
|
119
|
+
if src_path == dest_path
|
120
|
+
#do nothing, file is already where it needs to be
|
121
|
+
else
|
122
|
+
#move the file where it needs to go
|
123
|
+
FileUtils.cp(src_path, dest_path)
|
124
|
+
end
|
125
|
+
else
|
126
|
+
puts "#{src_path} -> File does not exist"
|
127
|
+
end
|
128
|
+
paths << "./#{file.filename}"
|
129
|
+
end
|
130
|
+
|
131
|
+
#take all the files and tar.gz them -- name the file the same as
|
132
|
+
#the directory
|
133
|
+
|
134
|
+
Dir.chdir("#{resolve_path}")
|
135
|
+
destination = "#{@name.gsub(" ","_").gsub(".","").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}.tar.gz"
|
136
|
+
|
137
|
+
File.delete(destination) if File.exists?(destination)
|
138
|
+
|
139
|
+
BCL.tarball(destination, paths)
|
140
|
+
|
141
|
+
Dir.chdir(current_d)
|
142
|
+
|
143
|
+
if (delete_files)
|
144
|
+
@files.each do |file|
|
145
|
+
if File.exists?(File.dirname(file.fqp_file))
|
146
|
+
puts "[ComponentXml] Deleting: #{File.dirname(file.fqp_file)}"
|
147
|
+
FileUtils.rm_rf(File.dirname(file.fqp_file))
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
#puts "[ComponentXml] " + Dir.pwd
|
153
|
+
end
|
154
|
+
|
155
|
+
def add_provenance(author, datetime, comment)
|
156
|
+
prov = ProvStruct.new
|
157
|
+
prov.author = author
|
158
|
+
prov.datetime = datetime
|
159
|
+
prov.comment = comment
|
160
|
+
|
161
|
+
@provenance << prov
|
162
|
+
end
|
163
|
+
|
164
|
+
def add_tag(tag_name)
|
165
|
+
tag = TagsStruct.new
|
166
|
+
tag.descriptor = tag_name
|
167
|
+
|
168
|
+
@tags << tag
|
169
|
+
end
|
170
|
+
|
171
|
+
def add_attribute(name, value, units, datatype = nil)
|
172
|
+
attr = AttrStruct.new
|
173
|
+
attr.name = name
|
174
|
+
attr.value = value
|
175
|
+
|
176
|
+
if !datatype.nil?
|
177
|
+
attr.datatype = datatype
|
178
|
+
else
|
179
|
+
attr.datatype = get_datatype(value)
|
180
|
+
end
|
181
|
+
attr.units = units
|
182
|
+
|
183
|
+
@attributes << attr
|
184
|
+
end
|
185
|
+
|
186
|
+
def add_file(version_sp, version_id, fqp_file, filename, filetype)
|
187
|
+
fs = FileStruct.new
|
188
|
+
fs.version_software_program = version_sp
|
189
|
+
fs.version_id = version_id
|
190
|
+
fs.fqp_file = fqp_file
|
191
|
+
fs.filename = filename
|
192
|
+
fs.filetype = filetype
|
193
|
+
|
194
|
+
@files << fs
|
195
|
+
end
|
196
|
+
|
197
|
+
|
198
|
+
def add_cost(cost_name, cost_type, category, value, units, interval, interval_units, year, location, currency,
|
199
|
+
source, reference_component_name, reference_component_id)
|
200
|
+
cs = CostStruct.new
|
201
|
+
cs.cost_name = cost_name
|
202
|
+
cs.cost_type = cost_type
|
203
|
+
cs.category = category
|
204
|
+
cs.value = value
|
205
|
+
cs.interval = interval
|
206
|
+
cs.interval_units = interval_units
|
207
|
+
cs.year = year
|
208
|
+
cs.location = location
|
209
|
+
cs.units = units
|
210
|
+
cs.currency = currency
|
211
|
+
cs.source = source
|
212
|
+
cs.reference_component_name = reference_component_name
|
213
|
+
cs.reference_component_id = reference_component_id
|
214
|
+
|
215
|
+
@costs << cs
|
216
|
+
end
|
217
|
+
|
218
|
+
def add_object(object_type, object_instance)
|
219
|
+
ob = ObjectStruct.new
|
220
|
+
ob.obj_type = object_type
|
221
|
+
ob.obj_instance = object_instance
|
222
|
+
|
223
|
+
@objects << ob
|
224
|
+
end
|
225
|
+
|
226
|
+
def resolve_path
|
227
|
+
FileUtils.mkdir_p(@path) unless File.directory?(@path)
|
228
|
+
new_path = "#{@path}/#{name.gsub(" ","_").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}"
|
229
|
+
FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
|
230
|
+
result = new_path
|
231
|
+
end
|
232
|
+
|
233
|
+
def osm_resolve_path
|
234
|
+
FileUtils.mkdir_p(@path) unless File.directory?(@path)
|
235
|
+
new_path = "#{@path}/osm_#{name.gsub(" ","_").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}"
|
236
|
+
FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
|
237
|
+
result = new_path
|
238
|
+
end
|
239
|
+
|
240
|
+
def osc_resolve_path
|
241
|
+
FileUtils.mkdir_p(@path) unless File.directory?(@path)
|
242
|
+
new_path = "#{@path}/osc_#{name.gsub(" ","_").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}"
|
243
|
+
FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
|
244
|
+
result = new_path
|
245
|
+
end
|
246
|
+
|
247
|
+
def resolve_component_path(component_type)
|
248
|
+
FileUtils.mkdir_p(@path) unless File.directory?(@path)
|
249
|
+
new_path = @path + '/OpenStudio'
|
250
|
+
FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
|
251
|
+
new_path = new_path + "/#{component_type}"
|
252
|
+
FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
|
253
|
+
return new_path
|
254
|
+
end
|
255
|
+
|
256
|
+
def tmp_resolve_path
|
257
|
+
FileUtils.mkdir_p(@path) unless File.directory?(@path)
|
258
|
+
new_path = "#{@path}/tmp_#{name.gsub(" ","_").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}"
|
259
|
+
FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
|
260
|
+
result = new_path
|
261
|
+
end
|
262
|
+
|
263
|
+
|
264
|
+
def create_os_component(osobj)
|
265
|
+
osobj.getCostLineItems.each do |os|
|
266
|
+
@costs.each do |cost|
|
267
|
+
#figure out costs for constructions
|
268
|
+
os.setMaterialCost(cost.value.to_f) if cost.category == "material"
|
269
|
+
if cost.category == "installation"
|
270
|
+
os.setInstallationCost(cost.value.to_f)
|
271
|
+
os.setExpectedLife(cost.interval.to_i)
|
272
|
+
end
|
273
|
+
os.setFixedOM(cost.value.to_f) if cost.category == "operations and maintenance"
|
274
|
+
os.setVariableOM(cost.value.to_f) if cost.category == "variable operations and maintenance"
|
275
|
+
os.setSalvageCost(cost.value.to_f) if cost.category == "salvage"
|
276
|
+
end
|
277
|
+
end
|
278
|
+
newcomp = osobj.createComponent
|
279
|
+
|
280
|
+
cd = newcomp.componentData
|
281
|
+
cd.setDescription(@description)
|
282
|
+
|
283
|
+
at = newcomp.componentData.componentDataAttributes
|
284
|
+
@attributes.each do |attrib|
|
285
|
+
if (attrib.value.to_s != "") and (attrib.name.to_s != "")
|
286
|
+
if attrib.units != ""
|
287
|
+
at.addAttribute(tc(attrib.name), attrib.value, attrib.units)
|
288
|
+
else
|
289
|
+
at.addAttribute(tc(attrib.name), attrib.value)
|
290
|
+
end
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
tg = newcomp.componentData.componentDataTags
|
295
|
+
comp_tag = ""
|
296
|
+
@tags.each do |tag|
|
297
|
+
tg.addTag(tc(tag.descriptor))
|
298
|
+
if (tag.descriptor != "energyplus") and (tag.descriptor != "construction")
|
299
|
+
#create a map of component tags to directories
|
300
|
+
comp_tag = tag.descriptor
|
301
|
+
if comp_tag == "interior wall"
|
302
|
+
comp_tag = "interiorwalls"
|
303
|
+
elsif comp_tag == "exterior wall"
|
304
|
+
comp_tag = "exteriorwalls"
|
305
|
+
elsif comp_tag == "exterior slab"
|
306
|
+
comp_tag = "exteriorslabs"
|
307
|
+
elsif comp_tag == "exposed floor"
|
308
|
+
comp_tag = "exposedfloors"
|
309
|
+
elsif comp_tag == "attic floor"
|
310
|
+
comp_tag = "atticfloors"
|
311
|
+
elsif comp_tag == "roof"
|
312
|
+
comp_tag = "roofs"
|
313
|
+
elsif comp_tag == "door"
|
314
|
+
comp_tag = "doors"
|
315
|
+
elsif comp_tag == "skylight"
|
316
|
+
comp_tag = "skylights"
|
317
|
+
elsif comp_tag == "window"
|
318
|
+
comp_tag = "windows"
|
319
|
+
end
|
320
|
+
puts comp_tag
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
return newcomp
|
325
|
+
end
|
326
|
+
|
327
|
+
def save_component_xml(dir_path = resolve_path)
|
328
|
+
xmlfile = File.new(dir_path + '/component.xml', 'w')
|
329
|
+
comp_xml = Builder::XmlMarkup.new(:target => xmlfile, :indent=>2)
|
330
|
+
|
331
|
+
#setup the xml file
|
332
|
+
comp_xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
|
333
|
+
comp_xml.component("xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance",
|
334
|
+
"xsi:noNamespaceSchemaLocation"=>"#{SCHEMA_LOCATION}") {
|
335
|
+
comp_xml.name @name
|
336
|
+
comp_xml.uid @uid
|
337
|
+
comp_xml.version_id @comp_version_id
|
338
|
+
comp_xml.description @description if @description != ""
|
339
|
+
comp_xml.comment @comment if @comment != ""
|
340
|
+
|
341
|
+
comp_xml.provenances {
|
342
|
+
@provenance.each do |prov|
|
343
|
+
comp_xml.provenance {
|
344
|
+
comp_xml.author prov.author
|
345
|
+
comp_xml.datetime prov.datetime
|
346
|
+
comp_xml.comment prov.comment
|
347
|
+
}
|
348
|
+
end
|
349
|
+
}
|
350
|
+
|
351
|
+
comp_xml.tags {
|
352
|
+
@tags.each do |tag|
|
353
|
+
comp_xml.tag tag.descriptor
|
354
|
+
end
|
355
|
+
}
|
356
|
+
|
357
|
+
comp_xml.attributes {
|
358
|
+
@attributes.each do |attrib|
|
359
|
+
if (attrib.value.to_s != "") and (attrib.name.to_s != "") then
|
360
|
+
comp_xml.attribute {
|
361
|
+
comp_xml.name attrib.name
|
362
|
+
comp_xml.value attrib.value
|
363
|
+
comp_xml.datatype attrib.datatype
|
364
|
+
comp_xml.units attrib.units if attrib.units != ""
|
365
|
+
}
|
366
|
+
end
|
367
|
+
end
|
368
|
+
}
|
369
|
+
|
370
|
+
comp_xml.source {
|
371
|
+
comp_xml.manufacturer @source_manufacturer if @source_manufacturer != ""
|
372
|
+
comp_xml.model @source_model if @source_model != ""
|
373
|
+
comp_xml.serial_no @source_serial_no if @source_serial_no != ""
|
374
|
+
comp_xml.year @source_year if @source_year != ""
|
375
|
+
comp_xml.url @source_url if @source_url != ""
|
376
|
+
}
|
377
|
+
|
378
|
+
if not @files.nil?
|
379
|
+
comp_xml.files {
|
380
|
+
@files.each do |file|
|
381
|
+
comp_xml.file {
|
382
|
+
comp_xml.version {
|
383
|
+
comp_xml.software_program file.version_software_program
|
384
|
+
comp_xml.identifier file.version_id
|
385
|
+
}
|
386
|
+
|
387
|
+
comp_xml.filename file.filename
|
388
|
+
comp_xml.filetype file.filetype
|
389
|
+
}
|
390
|
+
end
|
391
|
+
}
|
392
|
+
end
|
393
|
+
|
394
|
+
#check if we should write out costs, don't write if all values are 0 or nil
|
395
|
+
#DLM: schema always expects costs
|
396
|
+
write_costs = true
|
397
|
+
#if not @costs.nil?
|
398
|
+
# @costs.each do |cost|
|
399
|
+
# if (cost.value.nil?) && (not cost.value == 0)
|
400
|
+
# write_costs = true
|
401
|
+
# break
|
402
|
+
# end
|
403
|
+
# end
|
404
|
+
#end
|
405
|
+
|
406
|
+
if write_costs
|
407
|
+
comp_xml.costs {
|
408
|
+
@costs.each do |cost|
|
409
|
+
comp_xml.cost {
|
410
|
+
comp_xml.instance_name cost.cost_name
|
411
|
+
comp_xml.cost_type cost.cost_type
|
412
|
+
comp_xml.category cost.category
|
413
|
+
comp_xml.value cost.value
|
414
|
+
comp_xml.units cost.units if cost.units != ""
|
415
|
+
comp_xml.interval cost.interval if cost.interval != ""
|
416
|
+
comp_xml.interval_units cost.interval_units if cost.interval_units != ""
|
417
|
+
comp_xml.year cost.year if cost.year != ""
|
418
|
+
comp_xml.currency cost.currency if cost.currency != ""
|
419
|
+
comp_xml.source cost.source if cost.source != ""
|
420
|
+
comp_xml.reference_component_name cost.reference_component_name if cost.reference_component_name != ""
|
421
|
+
comp_xml.reference_component_id cost.reference_component_id if cost.reference_component_id != ""
|
422
|
+
}
|
423
|
+
end
|
424
|
+
}
|
425
|
+
end
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
xmlfile.close
|
430
|
+
end
|
431
|
+
|
432
|
+
def get_attribute(attribute_name)
|
433
|
+
result = nil
|
434
|
+
@attributes.each do |attr|
|
435
|
+
if attr.name == attribute_name
|
436
|
+
result = attr
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
result
|
441
|
+
|
442
|
+
end
|
443
|
+
|
444
|
+
#return the title case of the string
|
445
|
+
def tc(input)
|
446
|
+
val = input.gsub(/\b\w/){$&.upcase}
|
447
|
+
if val.downcase == "energyplus"
|
448
|
+
val = "EnergyPlus"
|
449
|
+
end
|
450
|
+
return val
|
451
|
+
end
|
452
|
+
|
453
|
+
private
|
454
|
+
|
455
|
+
def get_datatype(input_value)
|
456
|
+
dt = 'undefined'
|
457
|
+
|
458
|
+
# simple method to test if the input_value is a string, float, or integer.
|
459
|
+
# First convert the value back to a string for testing (in case it was passed as a float/integer)
|
460
|
+
test = input_value.to_s
|
461
|
+
input_value = test.match('\.').nil? ? Integer(test) : Float(test) rescue test.to_s
|
462
|
+
|
463
|
+
if input_value.is_a?(Fixnum) || input_value.is_a?(Bignum)
|
464
|
+
dt = "int"
|
465
|
+
elsif input_value.is_a?(Float)
|
466
|
+
dt = "float"
|
467
|
+
else
|
468
|
+
dt = "string"
|
469
|
+
end
|
470
|
+
|
471
|
+
dt
|
472
|
+
end
|
473
|
+
|
474
|
+
end
|
475
|
+
|
476
|
+
end # module BCL
|
Binary file
|
@@ -20,9 +20,10 @@
|
|
20
20
|
require 'rubygems'
|
21
21
|
require 'pathname'
|
22
22
|
require 'fileutils'
|
23
|
-
require 'builder' #gem install builder (creates xml files)
|
24
23
|
require 'rbconfig'
|
25
24
|
|
25
|
+
#require gems
|
26
|
+
require 'builder' #gem install builder (creates xml files)
|
26
27
|
$have_win32ole = false
|
27
28
|
|
28
29
|
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
|
@@ -0,0 +1,114 @@
|
|
1
|
+
######################################################################
|
2
|
+
# Copyright (c) 2008-2013, Alliance for Sustainable Energy.
|
3
|
+
# All rights reserved.
|
4
|
+
#
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License as published by the Free Software Foundation; either
|
8
|
+
# version 2.1 of the License, or (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This library is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
# Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public
|
16
|
+
# License along with this library; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
######################################################################
|
19
|
+
|
20
|
+
#Mongo Record to Component
|
21
|
+
require 'bcl/component_xml'
|
22
|
+
|
23
|
+
module BCL
|
24
|
+
class MongoToComponent
|
25
|
+
attr_accessor :component
|
26
|
+
attr_accessor :error_message
|
27
|
+
|
28
|
+
def initialize(record)
|
29
|
+
@component = nil
|
30
|
+
@error_message = ""
|
31
|
+
|
32
|
+
if record.has_key? "general"
|
33
|
+
@component = BCL::Component.new('tmp')
|
34
|
+
general = record["general"]
|
35
|
+
|
36
|
+
#add uid/vid
|
37
|
+
if record.has_key? "unique_id"
|
38
|
+
component.uid = record["unique_id"]
|
39
|
+
else
|
40
|
+
@error_message = "Invalid Mongo record: no unique_id"
|
41
|
+
end
|
42
|
+
if record.has_key? "version_id"
|
43
|
+
component.comp_version_id = record["version_id"]
|
44
|
+
else
|
45
|
+
@error_message = "Invalid Mongo record: no version_id"
|
46
|
+
end
|
47
|
+
|
48
|
+
#add general info
|
49
|
+
if general.has_key? "name"
|
50
|
+
component.name = general["name"]
|
51
|
+
end
|
52
|
+
if general.has_key? "description"
|
53
|
+
@component.description = general["description"]
|
54
|
+
end
|
55
|
+
if general.has_key? "fidelity_level"
|
56
|
+
@component.fidelity_level = general["fidelity_level"]
|
57
|
+
end
|
58
|
+
if general.has_key? "source_manufacturer"
|
59
|
+
@component.source_manufacturer = general["source_manufacturer"]
|
60
|
+
end
|
61
|
+
if general.has_key? "source_url"
|
62
|
+
@component.source_url = general["source_url"]
|
63
|
+
end
|
64
|
+
|
65
|
+
#add tags
|
66
|
+
if general.has_key? "tags"
|
67
|
+
tags = general["tags"]
|
68
|
+
tags.each do |name,value|
|
69
|
+
@component.add_tag(value)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
#add attributes
|
74
|
+
if general.has_key? "attributes"
|
75
|
+
attribute_container = general["attributes"]
|
76
|
+
attribute_container.each do |a,attributes|
|
77
|
+
#attributes iterator is an array of hashes
|
78
|
+
#NOTE: double check this...could be old messed-up structure?
|
79
|
+
attributes.each do |attribute|
|
80
|
+
name = ""
|
81
|
+
units = ""
|
82
|
+
value = ""
|
83
|
+
datatype = ""
|
84
|
+
if attribute.has_key? "name"
|
85
|
+
name = attribute["name"]
|
86
|
+
end
|
87
|
+
if attribute.has_key? "value"
|
88
|
+
value = attribute["value"]
|
89
|
+
end
|
90
|
+
if attribute.has_key? "units"
|
91
|
+
units = attribute["units"]
|
92
|
+
end
|
93
|
+
@component.add_attribute(name, value, units)
|
94
|
+
|
95
|
+
#TODO: eventually find a way to validate the datatype in record with datatype in component
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
#todo: add provenance
|
101
|
+
|
102
|
+
else
|
103
|
+
@error_message = "Invalid Mongo record: no 'general' section"
|
104
|
+
end
|
105
|
+
#set component to NIL if there were errors
|
106
|
+
if !@error_message == nil
|
107
|
+
@component = nil
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end # module BCL
|
114
|
+
|
@@ -19,34 +19,35 @@
|
|
19
19
|
|
20
20
|
require 'rubygems'
|
21
21
|
|
22
|
+
# require gems
|
22
23
|
require 'zlib' #gem install zliby
|
23
24
|
require 'archive/tar/minitar' #gem install archive-tar-minitar
|
24
25
|
|
25
26
|
module BCL
|
26
27
|
|
27
|
-
module_function
|
28
|
+
module_function
|
28
29
|
|
29
|
-
def tarball(destination, paths)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
30
|
+
def tarball(destination, paths)
|
31
|
+
Zlib::GzipWriter.open(destination) do |gzip|
|
32
|
+
out = Archive::Tar::Minitar::Output.new(gzip)
|
33
|
+
puts "[TarBall] Starting #{destination}"
|
34
|
+
paths.each do |fi|
|
35
|
+
puts "[TarBall] Packing #{fi}"
|
36
|
+
if File.exists?(fi)
|
37
|
+
Archive::Tar::Minitar.pack_file(fi, out)
|
38
|
+
else
|
39
|
+
puts "[TarBall] Could not file file: #{fi}"
|
40
|
+
end
|
39
41
|
end
|
42
|
+
puts "[TarBall] Finished #{destination}"
|
43
|
+
out.close
|
40
44
|
end
|
41
|
-
puts "[TarBall] Finished #{destination}"
|
42
|
-
out.close
|
43
45
|
end
|
44
|
-
end
|
45
46
|
|
46
|
-
def extract_tarball(filename, destination)
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end
|
47
|
+
def extract_tarball(filename, destination)
|
48
|
+
Zlib::GzipReader.open(filename) {|gz|
|
49
|
+
Archive::Tar::Minitar.unpack(gz, destination)
|
50
|
+
}
|
51
|
+
end
|
51
52
|
|
52
53
|
end # module BCL
|
data/lib/bcl/version.rb
CHANGED
data/lib/bcl.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'bcl/
|
2
|
-
require 'bcl/
|
3
|
-
require 'bcl/
|
4
|
-
require 'bcl/
|
5
|
-
require 'bcl/
|
6
|
-
require 'bcl/
|
1
|
+
require 'bcl/component_spreadsheet'
|
2
|
+
require 'bcl/component_xml'
|
3
|
+
require 'bcl/component_methods'
|
4
|
+
require 'bcl/tar_ball'
|
5
|
+
require 'bcl/master_taxonomy'
|
6
|
+
require 'bcl/mongo_to_component'
|
7
7
|
require 'bcl/version'
|