Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
13168352
Commit
13168352
authored
Mar 23, 2015
by
Toshio Kuratomi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add some comments to explain how the property code for Attributes works
parent
8a0b8629
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
7 deletions
+32
-7
v2/ansible/playbook/base.py
+32
-7
No files found.
v2/ansible/playbook/base.py
View file @
13168352
...
@@ -54,21 +54,40 @@ class Base:
...
@@ -54,21 +54,40 @@ class Base:
# and initialize the base attributes
# and initialize the base attributes
self
.
_initialize_base_attributes
()
self
.
_initialize_base_attributes
()
# The following three functions are used to programatically define data
# descriptors (aka properties) for the Attributes of all of the playbook
# objects (tasks, blocks, plays, etc).
#
# The function signature is a little strange because of how we define
# them. We use partial to give each method the name of the Attribute that
# it is for. Since partial prefills the positional arguments at the
# beginning of the function we end up with the first positional argument
# being allocated to the name instead of to the class instance (self) as
# normal. To deal with that we make the property name field the first
# positional argument and self the second arg.
#
# Because these methods are defined inside of the class, they get bound to
# the instance when the object is created. After we run partial on them
# and put the result back into the class as a property, they get bound
# a second time. This leads to self being placed in the arguments twice.
# To work around that, we mark the functions as @staticmethod so that the
# first binding to the instance doesn't happen.
@staticmethod
@staticmethod
def
_generic_g
(
key
,
self
):
def
_generic_g
(
prop_name
,
self
):
method
=
"_get_attr_
%
s"
%
key
method
=
"_get_attr_
%
s"
%
prop_name
if
method
in
dir
(
self
):
if
method
in
dir
(
self
):
return
getattr
(
self
,
method
)()
return
getattr
(
self
,
method
)()
return
self
.
_attributes
[
key
]
return
self
.
_attributes
[
prop_name
]
@staticmethod
@staticmethod
def
_generic_s
(
key
,
self
,
value
):
def
_generic_s
(
prop_name
,
self
,
value
):
self
.
_attributes
[
key
]
=
value
self
.
_attributes
[
prop_name
]
=
value
@staticmethod
@staticmethod
def
_generic_d
(
key
,
self
):
def
_generic_d
(
prop_name
,
self
):
del
self
.
_attributes
[
key
]
del
self
.
_attributes
[
prop_name
]
def
_get_base_attributes
(
self
):
def
_get_base_attributes
(
self
):
'''
'''
...
@@ -91,7 +110,13 @@ class Base:
...
@@ -91,7 +110,13 @@ class Base:
getter
=
partial
(
self
.
_generic_g
,
name
)
getter
=
partial
(
self
.
_generic_g
,
name
)
setter
=
partial
(
self
.
_generic_s
,
name
)
setter
=
partial
(
self
.
_generic_s
,
name
)
deleter
=
partial
(
self
.
_generic_d
,
name
)
deleter
=
partial
(
self
.
_generic_d
,
name
)
# Place the property into the class so that cls.name is the
# property functions.
setattr
(
Base
,
name
,
property
(
getter
,
setter
,
deleter
))
setattr
(
Base
,
name
,
property
(
getter
,
setter
,
deleter
))
# Place the value into the instance so that the property can
# process and hold that value/
setattr
(
self
,
name
,
value
.
default
)
setattr
(
self
,
name
,
value
.
default
)
def
preprocess_data
(
self
,
ds
):
def
preprocess_data
(
self
,
ds
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment