Advertisement
duggabe

Build_block_docs.py

Nov 4th, 2023
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.62 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Build_block_docs.py
  5. # Author: Barry Duggan
  6.  
  7. import os.path
  8. import sys
  9.  
  10. # Files:
  11. #   Inputs: YML blocks list; block names list; "partial_skeleton.mediawiki"
  12. #   Outputs: "<block name>.mediawiki" file for each block
  13.  
  14. # State machine
  15. #   0 - find a YML block in the block names list
  16. #   1 - search for parameters
  17. #   2 - extract parameters
  18. #   3 - search for documentation
  19. #   4 - process documentation
  20. #   5 - finish wiki output file
  21. #   9 - wrap up
  22. state = 0
  23.  
  24. _debug = 1          # set to zero to turn off diagnostics
  25. runFlag = True
  26. foundDocSection = False
  27. no_doc_count = 0
  28. total_files_found = 0
  29. parm_buff = ""
  30. 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'
  31.  
  32. if (len(sys.argv) < 3):
  33.     print ('Usage: python3 Build_block_docs.py <YML blocks list> <block names list>')
  34.     print ('Number of arguments=', len(sys.argv))
  35.     print ('Argument List:', str(sys.argv))
  36.     exit (1)
  37.  
  38. # test if YML blocks file exists
  39. ybl = sys.argv[1]
  40. if (_debug & 4):
  41.     print (ybl)
  42. if not(os.path.exists(ybl)):
  43.     print('The YML blocks file does not exist')
  44.     exit (1)
  45. # open yml_blocks list file
  46. yb_in = open (ybl, 'r')
  47.  
  48. # test if block names file exists
  49. bnf = sys.argv[2]
  50. if (_debug & 4):
  51.     print (bnf)
  52. if not(os.path.exists(bnf)):
  53.     print('The block names file does not exist')
  54.     exit (1)
  55. # open block names file
  56. bn_in = open (bnf, 'r')
  57. name_pool = bn_in.read()    # read entire file
  58.  
  59. while runFlag:
  60.     while (state == 0):
  61.     #   0 - find a YML block in the block names list
  62.         if (_debug & 4):
  63.             print ("state=", state)
  64.         # read line from yml_blocks list
  65.         buff = yb_in.readline()
  66.         b_len = len(buff)
  67.         if b_len == 0:
  68.             print ('End of yml_blocks list')
  69.             state = 9
  70.             break
  71.         bfn = buff.strip()  # get yml file name
  72.         # open yml file
  73.         yf_in = open (bfn, 'r')
  74.         # readline
  75.         line1 = yf_in.readline()    # block id
  76.         # readline
  77.         line2 = yf_in.readline()    # block label
  78.         ll1 = len(line1) - 1
  79.         block_id = line1[4:ll1]
  80.         if (block_id in name_pool):     # found it!
  81.             if (_debug & 4):
  82.                 print (line1)
  83.             ll2 = len(line2) - 1
  84.             temp_name = line2[7:ll2] + ".mediawiki"
  85.             wiki_name = temp_name.replace(" ", "_")
  86.             if (_debug & 1):
  87.                 print (wiki_name)
  88.             f_out = open (wiki_name, 'w')
  89.             f_out.write ("<!-- " + wiki_name + " -->\n")
  90.             f_out.write ("<!-- " + bfn + " -->\n")
  91.             total_files_found += 1
  92.             state = 1
  93.             break
  94.         # close yml file
  95.         yf_in.close()
  96.  
  97.     while (state == 1):
  98. #   1 - search for parameters
  99.         if (_debug & 4):
  100.             print ("state=", state)
  101.         lineN = yf_in.readline()
  102.         N_len = len(lineN)
  103.         if (N_len == 0):
  104.             print ('Unexpected end of file; state=1')
  105.             yf_in.close()
  106.             state = 0
  107.             break
  108.         if ("parameters:" in lineN):
  109.             parm_buff = params
  110.             state = 2
  111.             break
  112.  
  113.     while (state == 2):
  114. #   2 - extract parameters
  115.         if (_debug & 4):
  116.             print ("state=", state)
  117.         lineN = yf_in.readline()
  118.         N_len = len(lineN)
  119.         if (N_len == 0):
  120.             print ('Unexpected end of file; state=2')
  121.             yf_in.close()
  122.             state = 0
  123.             break
  124.         if ("id:" in lineN):
  125.             continue
  126.         if ("label:" in lineN):     # name of param
  127.             lineN1 = lineN.strip()
  128.             lineN2 = lineN1.replace("label: ", "")
  129.             parm_buff += (";" + lineN2 + "\n")
  130.             continue
  131.         # test for end of parameters
  132.         if (("inputs:" in lineN) or ("outputs:" in lineN) or ("templates:" in lineN) or ("file_format:" in lineN)):
  133.             state = 3
  134.             parm_buff += "\n"
  135.             break
  136.         if (N_len < 2):     # blank line
  137.             continue
  138.         # skip these
  139.         if (("dtype:" in lineN) or ("category:" in lineN) or ("advanced:" in lineN) or ("option_attributes:" in lineN) or ("hide:" in lineN)):
  140.             continue
  141.         if ("option_labels:" in lineN):
  142.             lineN1 = lineN.strip()
  143.             lineN2 = lineN1.replace("option_labels: ", "options: ")
  144.             parm_buff += (":" + lineN2 + "\n")
  145.         else:
  146.             parm_buff += (":" + lineN.strip() + "\n")
  147.  
  148.     while (state == 3):
  149. #   3 - search for documentation
  150.         if (_debug & 4):
  151.             print ("state=", state)
  152.         lineN = yf_in.readline()
  153.         N_len = len(lineN)
  154.         if (N_len == 0):
  155.             print ('End of yml file; no doc found\n')
  156.             no_doc_count += 1
  157.             # close yml files
  158.             yf_in.close()
  159.             f_out.write ("Statement of purpose / function<br>\n")
  160.             state = 5
  161.             break
  162.         if ("documentation:" in lineN):
  163.             if ("|-" in lineN):
  164.                 state = 4
  165.                 break
  166.             lineN1 = lineN.strip()
  167.             N1_len = len(lineN1)
  168.             f_out.write (lineN1[16:N1_len] + "\n")
  169.             state = 4
  170.             break
  171.  
  172.     while (state == 4):
  173. #   4 - process documentation
  174.         if (_debug & 4):
  175.             print ("state=", state)
  176.         lineN = yf_in.readline()
  177.         N_len = len(lineN)
  178.         if ((N_len == 0) or ("file_format:" in lineN)):
  179.             print ('End of yml file\n')
  180.             # close yml files
  181.             yf_in.close()
  182.             f_out.write ("\n")
  183.             state = 5
  184.             break
  185.         lineN1 = lineN.strip()
  186.         lineN2 = lineN1.replace("\\n", "<br>")
  187.         N2_len = len(lineN2)
  188.         if ("\\ " in lineN):
  189.             f_out.write (lineN2[2:N2_len] + "\n")
  190.         else:
  191.             f_out.write (lineN2[:N2_len] + "\n")
  192.  
  193.     while (state == 5):
  194. #   5 - finish wiki output file
  195.         if (_debug & 4):
  196.             print ("state=", state)
  197.         f_out.write (parm_buff)
  198.         # copy partial skeleton to output
  199.         ps_in = open ("partial_skeleton.mediawiki")
  200.         skel_buff = ps_in.read()    # read entire file
  201.         f_out.write (skel_buff)
  202.         ps_in.close()
  203.         f_out.close()
  204.         state = 0
  205.  
  206.     while (state == 9):
  207. #   9 - wrap up
  208.         if (_debug & 4):
  209.             print ("state=", state)
  210.         runFlag = 0
  211.         print ("total_files_found:", total_files_found)
  212.         print ("no_doc_count:", no_doc_count)
  213.         break
  214.  
  215. yb_in.close()
  216. bn_in.close()
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement