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

Dynamically Create Plugs and Plug Placeholders #430

Merged
merged 13 commits into from
Sep 7, 2016
Prev Previous commit
Next Next commit
added metaclass for baseplug to enable equivalence and hashing based …
…on plug class
  • Loading branch information
kdsudac authored and fahhem committed Sep 1, 2016
commit da7501b9c7e7dafbe3ac4a7cafd8f352d852b619
15 changes: 14 additions & 1 deletion openhtf/plugs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ class DuplicatePlugError(Exception):
class InvalidPlugError(Exception):
"""Raised when a plug declaration or requested name is invalid."""

class BasePlugMetaclass(type):
"""Metaclass for Baseplug to allow us to define plug class equivalance"""
def __hash__(cls):
# TODO(kdsudac): revisit before release, need to think through and verify
# how exactly to specify plug class equivalance
return hash((cls.__module__, cls.__name__))

def __eq__(cls, other):
if type(cls) != type(other):
return False

return hash(cls) == hash(other)

class BasePlug(object):
"""All plug types must subclass this type.
Expand All @@ -137,6 +149,7 @@ class BasePlug(object):
doesn't appear here), and is the same logger as passed into test
phases via TestApi.
"""
__metaclass__ = BasePlugMetaclass
# Override this to True in subclasses to support remote Plug access.
enable_remote = False
# Allow explicitly disabling remote access to specific attributes.
Expand All @@ -157,7 +170,7 @@ def create_subclass(cls, subclass_name, *args, **kwargs):
def subclass__init__(subclass_self):
cls.__init__(subclass_self, *args, **kwargs)

name = '_'.join(cls.__name, subclass_name)
name = '_'.join([cls.__name__, subclass_name])
subclass = type(name, (cls, ), {'__init__': subclass__init__})
return subclass

Expand Down