Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # -*- coding: utf-8 -*-
- # Build_block_docs.py
- # Author: Barry Duggan
- import os.path
- import sys
- # Files:
- # Inputs: YML blocks list; block names list; "partial_skeleton.mediawiki"
- # Outputs: "<block name>.mediawiki" file for each block
- # State machine
- # 0 - find a YML block in the block names list
- # 1 - search for parameters
- # 2 - extract parameters
- # 3 - search for documentation
- # 4 - process documentation
- # 5 - finish wiki output file
- # 9 - wrap up
- state = 0
- _debug = 1 # set to zero to turn off diagnostics
- runFlag = True
- foundDocSection = False
- no_doc_count = 0
- total_files_found = 0
- parm_buff = ""
- params = '== Parameters ==\n<b>(R):</b> <span class="plainlinks">[https://wiki.gnuradio.org/index.php/GNURadioCompanion#Variable_Controls <b>Run-time adjustable</b>]</span><br>\n'
- if (len(sys.argv) < 3):
- print ('Usage: python3 Build_block_docs.py <YML blocks list> <block names list>')
- print ('Number of arguments=', len(sys.argv))
- print ('Argument List:', str(sys.argv))
- exit (1)
- # test if YML blocks file exists
- ybl = sys.argv[1]
- if (_debug & 4):
- print (ybl)
- if not(os.path.exists(ybl)):
- print('The YML blocks file does not exist')
- exit (1)
- # open yml_blocks list file
- yb_in = open (ybl, 'r')
- # test if block names file exists
- bnf = sys.argv[2]
- if (_debug & 4):
- print (bnf)
- if not(os.path.exists(bnf)):
- print('The block names file does not exist')
- exit (1)
- # open block names file
- bn_in = open (bnf, 'r')
- name_pool = bn_in.read() # read entire file
- while runFlag:
- while (state == 0):
- # 0 - find a YML block in the block names list
- if (_debug & 4):
- print ("state=", state)
- # read line from yml_blocks list
- buff = yb_in.readline()
- b_len = len(buff)
- if b_len == 0:
- print ('End of yml_blocks list')
- state = 9
- break
- bfn = buff.strip() # get yml file name
- # open yml file
- yf_in = open (bfn, 'r')
- # readline
- line1 = yf_in.readline() # block id
- # readline
- line2 = yf_in.readline() # block label
- ll1 = len(line1) - 1
- block_id = line1[4:ll1]
- if (block_id in name_pool): # found it!
- if (_debug & 4):
- print (line1)
- ll2 = len(line2) - 1
- temp_name = line2[7:ll2] + ".mediawiki"
- wiki_name = temp_name.replace(" ", "_")
- if (_debug & 1):
- print (wiki_name)
- f_out = open (wiki_name, 'w')
- f_out.write ("<!-- " + wiki_name + " -->\n")
- f_out.write ("<!-- " + bfn + " -->\n")
- total_files_found += 1
- state = 1
- break
- # close yml file
- yf_in.close()
- while (state == 1):
- # 1 - search for parameters
- if (_debug & 4):
- print ("state=", state)
- lineN = yf_in.readline()
- N_len = len(lineN)
- if (N_len == 0):
- print ('Unexpected end of file; state=1')
- yf_in.close()
- state = 0
- break
- if ("parameters:" in lineN):
- parm_buff = params
- state = 2
- break
- while (state == 2):
- # 2 - extract parameters
- if (_debug & 4):
- print ("state=", state)
- lineN = yf_in.readline()
- N_len = len(lineN)
- if (N_len == 0):
- print ('Unexpected end of file; state=2')
- yf_in.close()
- state = 0
- break
- if ("id:" in lineN):
- continue
- if ("label:" in lineN): # name of param
- lineN1 = lineN.strip()
- lineN2 = lineN1.replace("label: ", "")
- parm_buff += (";" + lineN2 + "\n")
- continue
- # test for end of parameters
- if (("inputs:" in lineN) or ("outputs:" in lineN) or ("templates:" in lineN) or ("file_format:" in lineN)):
- state = 3
- parm_buff += "\n"
- break
- if (N_len < 2): # blank line
- continue
- # skip these
- if (("dtype:" in lineN) or ("category:" in lineN) or ("advanced:" in lineN) or ("option_attributes:" in lineN) or ("hide:" in lineN)):
- continue
- if ("option_labels:" in lineN):
- lineN1 = lineN.strip()
- lineN2 = lineN1.replace("option_labels: ", "options: ")
- parm_buff += (":" + lineN2 + "\n")
- else:
- parm_buff += (":" + lineN.strip() + "\n")
- while (state == 3):
- # 3 - search for documentation
- if (_debug & 4):
- print ("state=", state)
- lineN = yf_in.readline()
- N_len = len(lineN)
- if (N_len == 0):
- print ('End of yml file; no doc found\n')
- no_doc_count += 1
- # close yml files
- yf_in.close()
- f_out.write ("Statement of purpose / function<br>\n")
- state = 5
- break
- if ("documentation:" in lineN):
- if ("|-" in lineN):
- state = 4
- break
- lineN1 = lineN.strip()
- N1_len = len(lineN1)
- f_out.write (lineN1[16:N1_len] + "\n")
- state = 4
- break
- while (state == 4):
- # 4 - process documentation
- if (_debug & 4):
- print ("state=", state)
- lineN = yf_in.readline()
- N_len = len(lineN)
- if ((N_len == 0) or ("file_format:" in lineN)):
- print ('End of yml file\n')
- # close yml files
- yf_in.close()
- f_out.write ("\n")
- state = 5
- break
- lineN1 = lineN.strip()
- lineN2 = lineN1.replace("\\n", "<br>")
- N2_len = len(lineN2)
- if ("\\ " in lineN):
- f_out.write (lineN2[2:N2_len] + "\n")
- else:
- f_out.write (lineN2[:N2_len] + "\n")
- while (state == 5):
- # 5 - finish wiki output file
- if (_debug & 4):
- print ("state=", state)
- f_out.write (parm_buff)
- # copy partial skeleton to output
- ps_in = open ("partial_skeleton.mediawiki")
- skel_buff = ps_in.read() # read entire file
- f_out.write (skel_buff)
- ps_in.close()
- f_out.close()
- state = 0
- while (state == 9):
- # 9 - wrap up
- if (_debug & 4):
- print ("state=", state)
- runFlag = 0
- print ("total_files_found:", total_files_found)
- print ("no_doc_count:", no_doc_count)
- break
- yb_in.close()
- bn_in.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement