You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gencapdefs.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env python3
  2. """
  3. Updates the capability definitions at irc/caps/defs.go
  4. To add a capability, add it to the CAPDEFS list below,
  5. then run `make capdefs` from the project root.
  6. """
  7. import io
  8. import subprocess
  9. import sys
  10. from collections import namedtuple
  11. CapDef = namedtuple("CapDef", ['identifier', 'name', 'url', 'standard'])
  12. CAPDEFS = [
  13. CapDef(
  14. identifier="AccountNotify",
  15. name="account-notify",
  16. url="https://ircv3.net/specs/extensions/account-notify-3.1.html",
  17. standard="IRCv3",
  18. ),
  19. CapDef(
  20. identifier="AccountTag",
  21. name="account-tag",
  22. url="https://ircv3.net/specs/extensions/account-tag-3.2.html",
  23. standard="IRCv3",
  24. ),
  25. CapDef(
  26. identifier="AwayNotify",
  27. name="away-notify",
  28. url="https://ircv3.net/specs/extensions/away-notify-3.1.html",
  29. standard="IRCv3",
  30. ),
  31. CapDef(
  32. identifier="Batch",
  33. name="batch",
  34. url="https://ircv3.net/specs/extensions/batch-3.2.html",
  35. standard="IRCv3",
  36. ),
  37. CapDef(
  38. identifier="CapNotify",
  39. name="cap-notify",
  40. url="https://ircv3.net/specs/extensions/cap-notify-3.2.html",
  41. standard="IRCv3",
  42. ),
  43. CapDef(
  44. identifier="ChgHost",
  45. name="chghost",
  46. url="https://ircv3.net/specs/extensions/chghost-3.2.html",
  47. standard="IRCv3",
  48. ),
  49. CapDef(
  50. identifier="EchoMessage",
  51. name="echo-message",
  52. url="https://ircv3.net/specs/extensions/echo-message-3.2.html",
  53. standard="IRCv3",
  54. ),
  55. CapDef(
  56. identifier="ExtendedJoin",
  57. name="extended-join",
  58. url="https://ircv3.net/specs/extensions/extended-join-3.1.html",
  59. standard="IRCv3",
  60. ),
  61. CapDef(
  62. identifier="InviteNotify",
  63. name="invite-notify",
  64. url="https://ircv3.net/specs/extensions/invite-notify-3.2.html",
  65. standard="IRCv3",
  66. ),
  67. CapDef(
  68. identifier="LabeledResponse",
  69. name="draft/labeled-response",
  70. url="https://ircv3.net/specs/extensions/labeled-response.html",
  71. standard="draft IRCv3",
  72. ),
  73. CapDef(
  74. identifier="Languages",
  75. name="draft/languages",
  76. url="https://gist.github.com/DanielOaks/8126122f74b26012a3de37db80e4e0c6",
  77. standard="proposed IRCv3",
  78. ),
  79. CapDef(
  80. identifier="MaxLine",
  81. name="oragono.io/maxline",
  82. url="https://oragono.io/maxline",
  83. standard="Oragono-specific",
  84. ),
  85. CapDef(
  86. identifier="MessageTags",
  87. name="draft/message-tags-0.2",
  88. url="https://ircv3.net/specs/core/message-tags-3.3.html",
  89. standard="draft IRCv3",
  90. ),
  91. CapDef(
  92. identifier="MultiPrefix",
  93. name="multi-prefix",
  94. url="https://ircv3.net/specs/extensions/multi-prefix-3.1.html",
  95. standard="IRCv3",
  96. ),
  97. CapDef(
  98. identifier="Rename",
  99. name="draft/rename",
  100. url="https://github.com/SaberUK/ircv3-specifications/blob/rename/extensions/rename.md",
  101. standard="proposed IRCv3",
  102. ),
  103. CapDef(
  104. identifier="Resume",
  105. name="draft/resume-0.2",
  106. url="https://github.com/DanielOaks/ircv3-specifications/blob/master+resume/extensions/resume.md",
  107. standard="proposed IRCv3",
  108. ),
  109. CapDef(
  110. identifier="SASL",
  111. name="sasl",
  112. url="https://ircv3.net/specs/extensions/sasl-3.2.html",
  113. standard="IRCv3",
  114. ),
  115. CapDef(
  116. identifier="ServerTime",
  117. name="server-time",
  118. url="https://ircv3.net/specs/extensions/server-time-3.2.html",
  119. standard="IRCv3",
  120. ),
  121. CapDef(
  122. identifier="STS",
  123. name="sts",
  124. url="https://ircv3.net/specs/extensions/sts.html",
  125. standard="IRCv3",
  126. ),
  127. CapDef(
  128. identifier="UserhostInNames",
  129. name="userhost-in-names",
  130. url="https://ircv3.net/specs/extensions/userhost-in-names-3.2.html",
  131. standard="IRCv3",
  132. ),
  133. ]
  134. def validate_defs():
  135. numCaps = len(CAPDEFS)
  136. numNames = len(set(capdef.name for capdef in CAPDEFS))
  137. if numCaps != numNames:
  138. raise Exception("defs must have unique names, but found duplicates")
  139. numIdentifiers = len(set(capdef.identifier for capdef in CAPDEFS))
  140. if numCaps != numIdentifiers:
  141. raise Exception("defs must have unique identifiers, but found duplicates")
  142. def main():
  143. validate_defs()
  144. output = io.StringIO()
  145. print("""
  146. package caps
  147. /*
  148. WARNING: this file is autogenerated by `make capdefs`
  149. DO NOT EDIT MANUALLY.
  150. */
  151. """, file=output)
  152. numCapabs = len(CAPDEFS)
  153. bitsetLen = numCapabs // 64
  154. if numCapabs % 64 > 0:
  155. bitsetLen += 1
  156. print ("""
  157. const (
  158. // number of recognized capabilities:
  159. numCapabs = %d
  160. // length of the uint64 array that represents the bitset:
  161. bitsetLen = %d
  162. )
  163. """ % (numCapabs, bitsetLen), file=output)
  164. print("const (", file=output)
  165. for capdef in CAPDEFS:
  166. print("// %s is the %s capability named \"%s\":" % (capdef.identifier, capdef.standard, capdef.name), file=output)
  167. print("// %s" % (capdef.url,), file=output)
  168. print("%s Capability = iota" % (capdef.identifier,), file=output)
  169. print(file=output)
  170. print(")", file=output)
  171. print("// `capabilityNames[capab]` is the string name of the capability `capab`", file=output)
  172. print("""var ( capabilityNames = [numCapabs]string{""", file=output)
  173. for capdef in CAPDEFS:
  174. print("\"%s\"," % (capdef.name,), file=output)
  175. print("})", file=output)
  176. # run the generated code through `gofmt -s`, which will print it to stdout
  177. gofmt = subprocess.Popen(['gofmt', '-s'], stdin=subprocess.PIPE)
  178. gofmt.communicate(input=output.getvalue().encode('utf-8'))
  179. if gofmt.poll() != 0:
  180. print(output.getvalue())
  181. raise Exception("gofmt failed")
  182. return 0
  183. if __name__ == '__main__':
  184. sys.exit(main())