Name: flagpole
Owner: Netflix-Skunkworks
Description: Flag arg parser to build out a dictionary with optional keys.
Created: 2017-12-12 21:57:31.0
Updated: 2018-01-20 14:23:11.0
Pushed: 2017-12-12 21:58:41.0
Homepage: null
Size: 18
Language: Python
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Flag arg parser to build out a dictionary with optional keys.
pip install flagpole
Flagpole is used in cloudaux to allow users of cloudaux to specify how the library builds out the items.
Flagpole has two classes: Flags
and FlagRegistry
.
flagpole import Flags
S = Flags('BASE', 'LISTENERS', 'RULES')
t(FLAGS)
deredDict([('BASE', 1), ('LISTENERS', 2), ('RULES', 4), ('ALL', 7), ('None', 0), ('NONE', 0)])
t("{0:b}".format(FLAGS.None).zfill(3))
0
t("{0:b}".format(FLAGS.ALL).zfill(3))
1
t("{0:b}".format(FLAGS.BASE).zfill(3))
1
t("{0:b}".format(FLAGS.LISTENERS).zfill(3))
0
t("{0:b}".format(FLAGS.RULES).zfill(3))
0
mbine multiple flags (100 & 010 = 110):
t("{0:b}".format(FLAGS.RULES | FLAGS.LISTENERS).zfill(3))
0
FLAGS.ALL
and FLAGS.None
are automatically added. All others must be added in the constructor.
Note: both NONE
and None
are provided as we found casing to be a common user error.
The registry has two parts:
@registry.register(...)
registry.build_out(...)
The FlagRegistry is specialized for the cause of building out a datastructure (a python dictionary) with an arbitrary number of optional fields.
The decorator is used to wrap methods to indicate which flag will cause the method to be invoked, whether any other flags are a dependency, and under what key the return value should be placed.
Supports wrapping methods with multiple return values. Each return value can have a separate flag and a separate key.
The decorator has the following keyword arguments:
build_out
is invoked with a flag which matches the flag provided here.FLAG.RULES
), or for multiple return values, can be a list or tuple. See the source for an example.dict.update(other_dict)
) with the result dictionary.The registry.build_out(...)
method takes the following arguments:
flags = FLAGS.CORS | FLAGS.WEBSITE
)The build_out
method executes all registry decorated methods having a flag which matches that passed into build_out
.
It will follow any dependency chains to execute methods in the correct order.
The Flags
combined with the ability to recursively follow dependency chains, are in large part the strength of this package. This package will also detect any circular depdenencies in the decorated methods and will raise an appropriate exception.
flagpole import FlagRegistry, Flags
cloudaux.aws.elbv2 import describe_listeners
cloudaux.aws.elbv2 import describe_rules
stry = FlagRegistry()
S = Flags('BASE', 'LISTENERS', 'RULES')
istry.register(flag=FLAGS.LISTENERS, key='listeners')
get_listeners(alb, **conn):
return describe_listeners(load_balancer_arn=alb['Arn'], **conn)
istry.register(flag=FLAGS.RULES, depends_on=FLAGS.LISTENERS, key='rules')
get_rules(alb, **conn):
rules = list()
for listener in alb['listeners']:
rules.append(describe_rules(listener_arn=listener['ListenerArn'], **conn))
return rules
y is not specified here, so the return value is merged (`dict.update(other_dict)`) with the result dictionary.
istry.register(flag=FLAGS.BASE)
get_base(alb, **conn):
return {
'region': conn.get('region'),
'_version': 1
}
And then you can call registry.build_out()
like so:
get_elbv2(alb_arn, flags=FLAGS.ALL, **conn):
alb = dict(Arn=alb_arn)
registry.build_out(flags, start_with=alb, pass_datastructure=True, **conn)
return result
Note: You can build any arbitrary combination of flags such as: flags=FLAGS.RULES | FLAGS.LISTENERS
The result for this example, when called with FLAGS.ALL
would be a dictionary in the following structure:
'Arn': ...,
'region': ...,
'listeners': ['ListenerArn': ...],
'rules': [...],
'_version': ...,
The FlagRegistry class fully documents its use.