Skip to content

Instantly share code, notes, and snippets.

@allenap
Created December 5, 2016 16:20
Show Gist options
  • Save allenap/614af046490d2a5bbc005c01b9c25844 to your computer and use it in GitHub Desktop.
Save allenap/614af046490d2a5bbc005c01b9c25844 to your computer and use it in GitHub Desktop.
import string
import requests
maas_url = "http://maas.server:5240/MAAS/"
api_desc = requests.get(maas_url + "api/2.0/describe/").json()
uri_templates = {h["uri"] for h in api_desc["handlers"]}
class FormatKeys:
def __getitem__(self, name):
# Remove hyphens and underscores from the key.
return name.replace("_", "").replace("-", "").upper()
# Convert "{this_style}" placeholders in URIs because the underscores will not
# appear in rendered URLs but will skew the counts.
vformat = string.Formatter().vformat
uris = {vformat(uri, None, FormatKeys()) for uri in uri_templates}
for uri in sorted(uris):
print(uri)
print()
print(" Number of URIs:", len(uris))
print(" underscores:", sum(1 for uri in uris if "_" in uri))
print(" hyphens:", sum(1 for uri in uris if "-" in uri))
print()
@allenap
Copy link
Author

allenap commented Dec 5, 2016

For my server this outputs the list of URIs which it's counting followed by the summary:

  Number of URIs: 79
     underscores: 0
         hyphens: 21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment