Skip to content

Instantly share code, notes, and snippets.

@otherwiseguy
Last active August 17, 2016 03:15
Show Gist options
  • Save otherwiseguy/3bdc5566f3896bfffb9d5ab34d7e2736 to your computer and use it in GitHub Desktop.
Save otherwiseguy/3bdc5566f3896bfffb9d5ab34d7e2736 to your computer and use it in GitHub Desktop.
class FakeSchemaHelper(object):
def register_table(self, table):
print "register_table(%s)" % table
def register_columns(self, table, columns, read_only=None):
print "register_column(%s, %s, %s)" % (table, str(columns),
str(read_only))
def register_all(self):
print "register_all"
class SchemaHelperCommand(object):
def __init__(self, method, *args, **kwargs):
self.method = method
self.args = args
self.kwargs = kwargs
def execute(self, helper):
return self.method(helper, *self.args, **self.kwargs)
class Connection(object):
def __init__(self):
self.schema_filter = []
def register_table(self, table, *args, **kwargs):
self.schema_filter.append(
SchemaHelperCommand(FakeSchemaHelper.register_table, table,
*args, **kwargs))
def register_columns(self, table, columns, *args, **kwargs):
self.schema_filter.append(
SchemaHelperCommand(FakeSchemaHelper.register_columns, table,
columns, *args, **kwargs))
def get_schema_helper(self):
return FakeSchemaHelper()
def update_schema_helper(self, helper):
if not self.schema_filter:
helper.register_all()
else:
for sfilter in self.schema_filter:
sfilter.execute(helper)
def start(self, table_name_list=None):
"""Start the OVSDB connection
:param table_name_list: limit schema to these tables (deprecated)
"""
helper = self.get_schema_helper()
for table in table_name_list or []:
self.register_table(table)
self.update_schema_helper(helper)
if __name__ == '__main__':
c = Connection()
c.start() # Should call register_all()
c.register_table("Table1")
c.register_columns("Table2", ['col1', 'col2', 'col3'])
c.start()
@omeranson
Copy link

Looks good.

If a subclass wants to override both get_schema_helper, and make sure it isn't updated, it will have to override update_schema_helper as well, but I don't think that's a problem.

In start, I think the table_name_list parameter, if given, should override the _schema_filter array, and have it applied via update_schema_helper - let the parameter be the final override, but still keep the paradigm that get_schema_helper retrieves the helper, and update_schema_helper registers the tables/columns/all.

I think we can also make _schema_filter public in this case. This will allow developers to externally add features without the api needing to be immediately updated, and allow developers to manipulate it externally (e.g. empty the filter list, or generate the list using their own methods).

New functions can be easily added. And if we will want to support lists and dictionaries as in the currently proposed API, it will be easy to write a generator that does that.

I'll try and incorporate it to the code tomorrow. (I'll also add you as co-author. I only thought about it much later after uploading the patch.)

@otherwiseguy
Copy link
Author

I think we can also make _schema_filter public in this case. This will allow developers to externally add features without the api needing to be immediately updated, and allow developers to manipulate it externally (e.g. empty the filter list, or generate the list using their own methods).

_schema_filter seems like an odd structure to work with directly (a list of functions taking a helper object on which arbitrary methods will be called). I mean, I guess we could make it a little more documented by removing the lambda and using the Command pattern already pretty common to the OVSDB native lib. Something like what I pushed in v5?

@omeranson
Copy link

omeranson commented Aug 17, 2016

I am not sure that using a Command object is clearer, but I don't have a lot of experience with the OVSDB native lib.

It doesn't have to be a lambda - it can be any callable that accepts the helper, e.g. a (client) function, or a class implementing call(self, helper).

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