Netflix-Skunkworks/flagpole

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

flagpole

Flag arg parser to build out a dictionary with optional keys.

Version

Build Status

Coverage Status

Install:

pip install flagpole

Usage:

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.

Flags
 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.

FlagRegistry

The registry has two parts:

The FlagRegistry is specialized for the cause of building out a datastructure (a python dictionary) with an arbitrary number of optional fields.

FlagRegistry decorator:

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:

FlagRegistry build_out:

The registry.build_out(...) method takes the following arguments:

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.

Full example:
 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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.