Skip to content

Commit

Permalink
Merge pull request #214 from mottosso/master
Browse files Browse the repository at this point in the history
1.1.4
  • Loading branch information
mottosso committed Aug 4, 2015
2 parents 9681496 + b771160 commit f7a9d39
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 72 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ deploy:
on:
tags: true
all_branches: true
python: 2.6
python: 2.6
sudo: false
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ pyblish Changelog

This contains all major version changes between pyblish releases.

Version 1.1.4
-------------

- Feature: Added support for `"MyInstance" in context`
- Enhancement: Removing the need for @pyblish.api.log (#213)
- Bugfix: Negative collectors (#210)

Version 1.1.3
-------------

Expand Down
4 changes: 2 additions & 2 deletions pyblish/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'LPT1', 'LPT2', 'LPT3', 'PRN', 'NUL')


def inrange(number, base, offset=0):
def inrange(number, base, offset=0.5):
r"""Evaluate whether `number` is within `base` +- `offset`
Lower bound is *included* whereas upper bound is *excluded*
Expand Down Expand Up @@ -39,7 +39,7 @@ def inrange(number, base, offset=0):
"""

return base - offset <= number < base + offset
return (base - offset) <= number < (base + offset)


class MessageHandler(logging.Handler):
Expand Down
143 changes: 102 additions & 41 deletions pyblish/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@


class Provider(object):
"""Dependency provider"""
"""Dependency provider
This object is given a series of "services" that it then distributes
to a passed function based on the function's argument signature.
For example, the function func:`myfunc(a, b)` is given the services
called "a" and "b", given they have previously been added to the provider.
"""

def __init__(self):
self._services = dict()
Expand Down Expand Up @@ -145,53 +153,84 @@ def load(self):
return True


class MetaPlugin(type):
"""Rewrite plug-ins written prior to 1.1
def evaluate_pre11(plugin):
"""Determine whether the plug-in is pre-1.1"""
plugin.__pre11__ = False

..warning:: In case of plug-ins written prior to 1.1,
that also process both instance and context,
only the instance process will remain available.
if hasattr(plugin, "process_context"):
plugin.__pre11__ = True
plugin.process = plugin.process_context
del(plugin.process_context)

"""
if hasattr(plugin, "process_instance"):
plugin.__pre11__ = True
plugin.process = plugin.process_instance
del(plugin.process_instance)

# Repair is deprecated
if hasattr(plugin, "repair_context"):
plugin.__pre11__ = True
plugin.repair = plugin.repair_context
del(plugin.repair_context)

if hasattr(plugin, "repair_instance"):
plugin.__pre11__ = True
plugin.repair = plugin.repair_instance
del(plugin.repair_instance)


def evaluate_enabledness(plugin):
"""Deterimine whether the plug-in supports Context/Instance"""
plugin.__contextEnabled__ = False
plugin.__instanceEnabled__ = False

args_ = inspect.getargspec(plugin.process).args

if "instance" in args_:
plugin.__instanceEnabled__ = True

if "context" in args_:
plugin.__contextEnabled__ = True

# Forwards-compatibility with asset
if "asset" in args_:
plugin.__instanceEnabled__ = True

def __init__(cls, *args, **kwargs):
cls.__pre11__ = False
cls.__contextEnabled__ = False
cls.__instanceEnabled__ = False

if hasattr(cls, "process_context"):
cls.__pre11__ = True
cls.process = cls.process_context
del(cls.process_context)
def append_logger(plugin):
"""Append logger to plugin
if hasattr(cls, "process_instance"):
cls.__pre11__ = True
cls.process = cls.process_instance
del(cls.process_instance)
The logger will include a plug-in's final name, as defined
by the subclasser. For example, if a plug-in is defined, subclassing
:class:`Plugin`, it's given name will be present in log records.
# Repair is deprecated
if hasattr(cls, "repair_context"):
cls.__pre11__ = True
cls.repair = cls.repair_context
del(cls.repair_context)
"""

module = plugin.__module__
name = plugin.__name__

# Package name appended, for filtering of LogRecord instances
logname = "pyblish.%s.%s" % (module, name)
plugin.log = logging.getLogger(logname)
plugin.log.setLevel(logging.DEBUG)

if hasattr(cls, "repair_instance"):
cls.__pre11__ = True
cls.repair = cls.repair_instance
del(cls.repair_instance)
# All messages are handled by root-logger
plugin.log.propagate = True

args_ = inspect.getargspec(cls.process).args

if "instance" in args_:
cls.__instanceEnabled__ = True
class MetaPlugin(type):
"""Rewrite plug-ins written prior to 1.1
if "context" in args_:
cls.__contextEnabled__ = True
..warning:: In case of plug-ins written prior to 1.1,
that also process both instance and context,
only the instance process will remain available.
# Forwards-compatibility with asset
if "asset" in args_:
cls.__instanceEnabled__ = True
"""

def __init__(cls, *args, **kwargs):
append_logger(cls)
evaluate_pre11(cls)
evaluate_enabledness(cls)
return super(MetaPlugin, cls).__init__(*args, **kwargs)


Expand Down Expand Up @@ -436,8 +475,7 @@ def add(self, other):
"""

if other not in self:
return self.append(other)
return self.append(other)

def remove(self, other):
"""Remove member from self
Expand Down Expand Up @@ -506,6 +544,28 @@ class Context(AbstractEntity):
def id(self):
return "Context"

def __contains__(self, key):
"""Support both Instance objects and `id` strings
Example:
>>> context = Context()
>>> instance = context.create_instance("MyInstance")
>>> "MyInstance" in context
True
>>> instance in context
True
>>> "NotExists" in context
False
"""

try:
key = key.id
except:
pass

return key in self._children

def __init__(self, *args, **kwargs):
super(Context, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -574,16 +634,17 @@ class Instance(AbstractEntity):
:class:`Context.create_instance()`.
Attributes:
name (str): Name of instance, used in plugins
id (str): Unique identifier of instance
name (str): Name of instance
parent (AbstractEntity): Optional parent of instance
"""

def __eq__(self, other):
return self.name == getattr(other, "name", None)
return self.id == getattr(other, "id", None)

def __ne__(self, other):
return self.name != getattr(other, "name", None)
return self.id != getattr(other, "id", None)

def __repr__(self):
return u"%s.%s(\"%s\")" % (__name__, type(self).__name__, self)
Expand Down
4 changes: 0 additions & 4 deletions pyblish/plugins/collect_current_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import pyblish.lib


@pyblish.api.log
class CollectCurrentDate(pyblish.api.Collector):
"""Inject the current time into the Context"""

hosts = ['*']
version = (0, 1, 0)

def process(self, context):
"""Formatting is coming from configuration"""
date = pyblish.lib.time()
Expand Down
4 changes: 0 additions & 4 deletions pyblish/plugins/collect_current_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
import pyblish.api


@pyblish.api.log
class CollectCurrentUser(pyblish.api.Collector):
"""Inject the currently logged on user into the Context"""

hosts = ['*']
version = (0, 1, 0)

def process(self, context):
context.set_data('user', value=getpass.getuser())
4 changes: 0 additions & 4 deletions pyblish/plugins/collect_current_working_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
import pyblish.api


@pyblish.api.log
class CollectCurrentWorkingDirectory(pyblish.api.Collector):
"""Inject the current working directory into Context"""

hosts = ['*']
version = (0, 1, 0)

def process(self, context):
context.set_data('cwd', value=os.getcwd())
3 changes: 2 additions & 1 deletion pyblish/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import pyblish.logic
import pyblish.plugin

log = logging.getLogger("pyblish")
log = pyblish.lib.setup_log(level=logging.ERROR)


def publish(context=None, plugins=None, **kwargs):
Expand Down Expand Up @@ -101,6 +101,7 @@ def conform(*args, **kwargs):

collect = select
integrate = conform
run = publish # Alias


def _convenience(order, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pyblish/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

VERSION_MAJOR = 1
VERSION_MINOR = 1
VERSION_PATCH = 3
VERSION_PATCH = 4

version_info = (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH)
version = '%i.%i.%i' % version_info
Expand Down
9 changes: 9 additions & 0 deletions tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,14 @@ def test_context_itemgetter():
assert context[1].name == "MyInstanceB"


def test_in():
"""Querying whether an Instance is in a Context works"""

context = pyblish.api.Context()
context.create_instance("MyInstance")
assert "MyInstance" in context
assert "NotExist" not in context


if __name__ == '__main__':
test_add_remove_instances()
3 changes: 1 addition & 2 deletions tests/test_di.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ def process(self, context):
context.create_asset(name, family="myFamily")
count["#"] += 1


class ValidateColor(pyblish.api.Validator):
"""Called twice"""
families = ["myFamily"]
Expand Down Expand Up @@ -363,4 +362,4 @@ def listRelatives(self, node):
assert_equals(result["error"], None)

assert_equals(len(instances), 2)
assert_equals(instances, ["bobby_char", "rocket_char"])
assert_equals(instances, ["bobby_char", "rocket_char"])
Loading

0 comments on commit f7a9d39

Please sign in to comment.