Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pincushion reserved names #281

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion maas/client/bones/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import typing

from keyword import iskeyword

from collections import namedtuple
from collections.abc import Iterable
import json
Expand Down Expand Up @@ -177,7 +179,7 @@ def __populate(self):
self.__doc__ = self.__handler["doc"]
actions = self.__handler["actions"]
for action in actions:
setattr(self, action["name"], ActionAPI(action, self))
_setattr(self, action["name"], ActionAPI(action, self))

@property
def name(self):
Expand Down Expand Up @@ -520,3 +522,15 @@ def _prefer_json(headers):
if not any(header.lower() == "accept" for header in headers):
headers["Accept"] = "application/json,*/*;q=0.9"
return headers


def _setattr(obj, name, value):
"""Classic setattr(), also pincushions name if it's a reserved keyword.

Although keywords can be used as object names, they cause a syntax error
upon call. This renders ActionAPIs with names like 'import' unusable, thus
we pincushion the names.
"""
if iskeyword(name):
name = name + "_"
setattr(obj, name, value)