Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Condition rename: myapp/gargoyle.py shadows gargoyle package #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ Usage
Gargoyle is typically used in two fashions. The first and simplest, is as a decorator. The decorator will automatically integrate with filters registered to the ``User`` model, as well as IP address::

from gargoyle.decorators import switch_is_active

@switch_is_active('my switch name')
def my_view(request):
return 'foo'

The second use is with the ``is_active`` method. This allows you to perform validation on your own custom objects::

from gargoyle import gargoyle

def my_function(request):
if gargoyle.is_active('my switch name', request):
return 'foo'
Expand All @@ -52,7 +52,7 @@ The second use is with the ``is_active`` method. This allows you to perform vali

# with custom objects
from gargoyle import gargoyle

def my_method(user):
if gargoyle.is_active('my switch name', user):
return 'foo'
Expand All @@ -62,15 +62,16 @@ The second use is with the ``is_active`` method. This allows you to perform vali
Condition Sets
==============

Gargoyle provides an easy way to hook in your own condition sets to allow additional filters. Simply place a ConditionSet class in ``myapp/gargoyle.py`` and it will automatically discover it::
Gargoyle provides an easy way to hook in your own condition sets to allow additional filters. Simply place a ConditionSet class in ``myapp/gargoyle_conditions.py`` and it will automatically discover it::

from gargoyle import gargoyle
from gargoyle import conditions
from django.contrib.sites.models import Site

class SiteConditionSet(conditions.ModelConditionSet):
percent = conditions.Percent()
domain = conditions.String()

gargoyle.register(SiteConditionSet(Site))

And now you can pass it into is_active::
Expand Down
26 changes: 13 additions & 13 deletions gargoyle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,35 @@ def autodiscover():
from django.conf import settings

for app in settings.INSTALLED_APPS:
# For each app, we need to look for an api.py inside that app's
# package. We can't use os.path here -- recall that modules may be
# For each app, we need to look for a gargoyle_conditions.py inside that
# app's package. We can't use os.path here -- recall that modules may be
# imported different ways (think zip files) -- so we need to get
# the app's __path__ and look for admin.py on that path.
# the app's __path__ and look for gargoyle_conditions.py on that path.

# Step 1: find out the app's __path__ Import errors here will (and
# should) bubble up, but a missing __path__ (which is legal, but weird)
# fails silently -- apps that do weird things with __path__ might
# need to roll their own admin registration.
# need to roll their own gargoyle condition registration.
try:
app_path = import_module(app).__path__
except AttributeError:
continue

# Step 2: use imp.find_module to find the app's admin.py. For some
# reason imp.find_module raises ImportError if the app can't be found
# but doesn't actually try to import the module. So skip this app if
# its admin.py doesn't exist
# Step 2: use imp.find_module to find the app's gargoyle_conditions.py.
# For some # reason imp.find_module raises ImportError if the app can't
# be found # but doesn't actually try to import the module. So skip this
# app if its gargoyle_conditions.py doesn't exist
try:
imp.find_module('gargoyle', app_path)
imp.find_module('gargoyle_conditions', app_path)
except ImportError:
continue

# Step 3: import the app's admin file. If this has errors we want them
# to bubble up.
import_module("%s.gargoyle" % app)
# Step 3: import the app's gargoyle_conditions file. If this has errors
# we want them to bubble up.
import_module("%s.gargoyle_conditions" % app)

# load builtins
from gargoyle.builtins import *

# autodiscover was successful, reset loading flag.
LOADING = False