44#
55
66require 'json'
7+ require 'fileutils'
78
8- if ARGV . empty?
9+ IguanaGroupNum = 30000
10+ CommonBanks = [
11+ 'REC::Particle' ,
12+ 'RUN::config' ,
13+ 'REC::Calorimeter' ,
14+ 'REC::Traj' ,
15+ ] # FIXME: add more, or take inspiration from DST schema
16+
17+ unless ARGV . size == 2
918 puts "" "
10- USAGE: #{ $0} [DIRECTORY ]
19+ USAGE: #{ $0} [INPUT_JSON_DIR] [OUTPUT_DIR ]
1120
12- Dumps the bank names and ID information for all JSON files in [DIRECTORY]
21+ Dumps the bank names and ID information for all JSON files in [INPUT_JSON_DIR]
22+ Output files will appear in [OUTPUT_DIR]
1323 " ""
1424 exit 2
1525end
16- SpecDir = ARGV [ 0 ]
26+ InputJsonDir = ARGV [ 0 ]
27+ OutputDir = ARGV [ 1 ]
1728
1829# parse the JSON files
19- specs = Dir . glob ( File . join SpecDir , '*.json' ) . map do |spec_file_name |
30+ specs = Dir . glob ( File . join InputJsonDir , '*.json' ) . map do |spec_file_name |
2031 JSON . parse File . read ( spec_file_name )
2132end . flatten
2233
4354# then sort each group's item IDs
4455specs_fully_sorted = Hash . new
4556specs_grouped_sorted . each do |group_id , spec_list |
57+ raise "do not define a bank with group ID #{ IguanaGroupNum } , since that is reserved for Iguana" if group_id ==IguanaGroupNum
4658 specs_fully_sorted [ group_id ] = spec_list . sort do |spec_a , spec_b |
4759 spec_a [ 'item' ] . to_i <=> spec_b [ 'item' ] . to_i
4860 end
4961end
5062
51- # dump a table
52- puts "" "# Bank Group and Item IDs
63+ # functions to give bank details markdown file name and link
64+ BanksSubDir = 'banks'
65+ def bank_md_name ( name )
66+ File . join ( BanksSubDir , name . gsub ( /::/ , '_' ) ) + '.md'
67+ end
68+ def bank_md_link ( name )
69+ "[`#{ name } `](#{ bank_md_name name } )"
70+ end
71+
72+ # data type hash
73+ TypeHash = {
74+ 'B' => 'byte' ,
75+ 'D' => 'double' ,
76+ 'F' => 'float' ,
77+ 'I' => 'int' ,
78+ 'L' => 'long' ,
79+ 'S' => 'short' ,
80+ }
81+
82+ # output tables
83+ FileUtils . mkdir_p OutputDir
84+ outMain = File . open File . join ( OutputDir , "banks.md" ) , 'w'
85+ outMain . puts "" "# HIPO Banks
86+
87+ ## Common Banks
88+
89+ " ""
90+ CommonBanks . each do |name |
91+ outMain . puts "- #{ bank_md_link name } "
92+ end
93+
94+ outMain . puts "" "
95+ ## Full List of Banks
5396
54- > **NOTE:** Iguana banks, which are defined in the Iguana repository, use group number 30000.
97+ Organized by group and item ID
5598
99+ > **NOTE:** Iguana banks, which are defined in the Iguana repository, use group number #{ IguanaGroupNum } .
56100" ""
57101
58- def row ( cols )
59- puts "| #{ cols . join ' | ' } |"
102+ def table_row ( out , cols )
103+ out . puts "| #{ cols . join ' | ' } |"
60104end
61105specs_fully_sorted . each do |group_id , spec_list |
62- puts "\n ## Group #{ group_id } \n \n "
63- row [ 'Item ID' , 'Name' , 'Description' ]
64- row [ '---' , '---' , '---' ]
106+ outMain . puts "\n ## Group #{ group_id } \n \n "
107+ table_row outMain , [ 'Item ID' , 'Name' , 'Description' ]
108+ table_row outMain , [ '---' , '---' , '---' ]
65109 spec_list . each do |spec |
110+
111+ # clean up description
66112 desc = spec [ 'info' ] . split . map do |word |
67113 if word . include? '::'
68114 "`#{ word } `"
@@ -72,10 +118,37 @@ def row(cols)
72118 word
73119 end
74120 end . join ( ' ' )
75- row [
121+
122+ # output main table row
123+ table_row outMain , [
76124 spec [ 'item' ] ,
77- '`' + spec [ 'name' ] + '`' ,
125+ bank_md_link ( spec [ 'name' ] ) ,
78126 desc ,
79127 ]
128+
129+ # generate detailed table
130+ outBank = File . open File . join ( OutputDir , bank_md_name ( spec [ 'name' ] ) ) , 'w'
131+ outBank . puts "" "# `#{ spec [ 'name' ] } ` Bank Details
132+
133+ #{ desc }
134+
135+ [Return to main tables](../banks.md)
136+
137+ " ""
138+ table_row outBank , [ 'Item Name' , 'Type' , 'Description' ]
139+ table_row outBank , [ '---' , '---' , '---' ]
140+ spec [ 'entries' ] . each do |entry |
141+ datatype = TypeHash [ entry [ 'type' ] ]
142+ raise "unknown datatype '#{ datatype } '" if datatype . nil?
143+ table_row outBank , [
144+ "`#{ entry [ 'name' ] } `" ,
145+ "`#{ datatype } `" ,
146+ entry [ 'info' ]
147+ ]
148+ end
149+ outBank . close
80150 end
81151end
152+ outMain . close
153+
154+ puts "OUTPUT FILES WRITTEN TO #{ OutputDir } "
0 commit comments