parser.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import re
  2. from constants import *
  3. match_parenthesis = r'\(.*'
  4. match_base_weight = r'\(((\d+_{0,1})+)'
  5. re_match_base_weight = re.compile(match_base_weight)
  6. match_db_ops_reads = r'DbWeight::get\(\).reads\((\d+) as Weight\)'
  7. match_db_ops_writes = r'DbWeight::get\(\).writes\((\d+) as Weight\)'
  8. re_match_db_ops_reads = re.compile(match_db_ops_reads)
  9. re_match_db_ops_writes = re.compile(match_db_ops_writes)
  10. match_scaling_var = r'\((\D) as Weight\)'
  11. re_match_scaling_var = re.compile(match_scaling_var)
  12. def parse_weights(weight_file):
  13. weights = {}
  14. with open(weight_file) as f:
  15. start_reading = False
  16. reading_func = False
  17. function_name = ""
  18. weight = 0
  19. db_reads_base = 0
  20. db_reads = []
  21. db_writes_base = 0
  22. db_writes = []
  23. variables = []
  24. pallet_name = ""
  25. for line in f:
  26. words = line.strip().split(" ")
  27. if words[0] == "impl":
  28. start_reading = True
  29. pallet_name = words[1].split("::")[0]
  30. if reading_func:
  31. if reading_func and "}" in words:
  32. reading_func = False
  33. weights[function_name] = {
  34. BASE_WEIGHT: weight,
  35. DB_READS: {
  36. BASE_DB: db_reads_base,
  37. DB_VARS: db_reads
  38. },
  39. DB_WRITES: {
  40. BASE_DB: db_writes_base,
  41. DB_VARS: db_writes,
  42. },
  43. VARS: variables
  44. }
  45. weight = 0
  46. db_reads_base = 0
  47. db_writes_base = 0
  48. variables = []
  49. db_reads = []
  50. db_writes = []
  51. if "DbWeight::get()" in line:
  52. if "reads" in line:
  53. if re.search(re_match_scaling_var, line):
  54. var = re.search(
  55. re_match_scaling_var, line).group(1)
  56. weight_factor = re.search(
  57. re_match_base_weight, line).group(1)
  58. db_reads.append((var, int(weight_factor)))
  59. else:
  60. db_reads_base = int(
  61. re.search(re_match_db_ops_reads, line).group(1))
  62. if "writes" in line:
  63. if re.search(re_match_scaling_var, line):
  64. var = re.search(
  65. re_match_scaling_var, line).group(1)
  66. weight_factor = re.search(
  67. re_match_base_weight, line).group(1)
  68. db_writes.append((var, int(weight_factor)))
  69. else:
  70. db_writes_base = int(
  71. re.search(re_match_db_ops_writes, line).group(1))
  72. else:
  73. if re.match(re_match_base_weight, words[0]) is not None:
  74. match = re.match(re_match_base_weight, words[0])
  75. weight = int(match.group(1))
  76. if re.search(re_match_scaling_var, line):
  77. var = re.search(
  78. re_match_scaling_var, line).group(1)
  79. weight_factor = re.search(
  80. re_match_base_weight, line).group(1)
  81. variables.append((var, int(weight_factor)))
  82. if start_reading and words[0] == "fn":
  83. reading_func = True
  84. function_name = re.sub(match_parenthesis, '', words[1])
  85. function_name = pallet_name + "::" + function_name
  86. return weights