Back to index

plone3  3.1.7
Classes | Functions | Variables
wicked.utils Namespace Reference

Classes

class  Memoizer

Functions

def linkcache
def getWicked
def match
def cleanUID
def packBrain
def getMatch
def counter
def test_suite

Variables

tuple _marker = object()
tuple _m = Memoizer()
 memoize = _m.memoize
 memoizedproperty = _m.memoizedproperty
 clearbefore = _m.clearbefore

Function Documentation

def wicked.utils.cleanUID (   brain)
takes a brain, returns a hashable value

Definition at line 45 of file utils.py.

00045 
00046 def cleanUID(brain):
00047     """takes a brain, returns a hashable value"""
00048     from Missing import Value
00049     uid = brain.UID
00050     if uid is Value:
00051         uid = brain.data_record_id_
00052     return uid
00053 

Here is the caller graph for this function:

Definition at line 180 of file utils.py.

00180 
00181 def counter():
00182     count=0
00183     while True:
00184         count+=1
00185         yield count
00186 

def wicked.utils.getMatch (   chunk,
  brains,
  normalled = None 
)
Given a set of query results and the wicked link text, return
the single brain that represents the correct object to link to, or
None.

Assumes that brains are already ordered oldest to newest, so
the first absolute match is the one returned.  Matches on id
take priority over matches on title

all strings are normalized and interned for comparison matches.

>>> from testing.general import pdo
>>> mkbrain = lambda i: pdo(getId='-'.join([str(x) for x in i]), Title='%s %s' %i, created=i[1])
>>> seed = zip('abc', range(3))
>>> brains = [mkbrain(i) for i in seed]
>>> chunk = ''
>>> normalled = ''

Test null conditions

>>> getMatch(chunk, brains)
>>> getMatch(chunk, brains, normalled)
>>> getMatch(chunk, brains[:1], normalled)

Test single brain matches

>>> getMatch('', brains[:1], 'a-0').getId
'a-0'

>>> getMatch(brains[0].getId, brains[:1], 'blah').getId
'a-0'

>>> getMatch(brains[0].Title, brains[:1]).getId
'a-0'

Test multi brain matches. brain 0 should win over brain 3
for all matches

>>> from copy import copy
>>> newbrain = copy(brains[0])
>>> newbrain.update(dict(created=3))
>>> brains =   brains + [newbrain]
>>> getMatch('', brains, 'a-0').created
0

>>> getMatch(brains[0].getId, brains).created
0

>>> getMatch(brains[0].Title, brains).created
0

Test title to chunk match

>>> brains[3].Title='A unique title'
>>> getMatch(brains[3].Title, brains).Title
'A unique title'

Definition at line 87 of file utils.py.

00087 
00088 def getMatch(chunk, brains, normalled=None):
00089     """
00090     Given a set of query results and the wicked link text, return
00091     the single brain that represents the correct object to link to, or
00092     None.
00093     
00094     Assumes that brains are already ordered oldest to newest, so
00095     the first absolute match is the one returned.  Matches on id
00096     take priority over matches on title
00097 
00098     all strings are normalized and interned for comparison matches.
00099 
00100     >>> from testing.general import pdo
00101     >>> mkbrain = lambda i: pdo(getId='-'.join([str(x) for x in i]), Title='%s %s' %i, created=i[1])
00102     >>> seed = zip('abc', range(3))
00103     >>> brains = [mkbrain(i) for i in seed]
00104     >>> chunk = ''
00105     >>> normalled = ''
00106 
00107     Test null conditions
00108 
00109     >>> getMatch(chunk, brains)
00110     >>> getMatch(chunk, brains, normalled)
00111     >>> getMatch(chunk, brains[:1], normalled)
00112 
00113     Test single brain matches
00114 
00115     >>> getMatch('', brains[:1], 'a-0').getId
00116     'a-0'
00117     
00118     >>> getMatch(brains[0].getId, brains[:1], 'blah').getId
00119     'a-0'
00120 
00121     >>> getMatch(brains[0].Title, brains[:1]).getId
00122     'a-0'
00123 
00124     Test multi brain matches. brain 0 should win over brain 3
00125     for all matches
00126 
00127     >>> from copy import copy
00128     >>> newbrain = copy(brains[0])
00129     >>> newbrain.update(dict(created=3))
00130     >>> brains =   brains + [newbrain]
00131     >>> getMatch('', brains, 'a-0').created
00132     0
00133     
00134     >>> getMatch(brains[0].getId, brains).created
00135     0
00136 
00137     >>> getMatch(brains[0].Title, brains).created
00138     0
00139 
00140     Test title to chunk match
00141 
00142     >>> brains[3].Title='A unique title'
00143     >>> getMatch(brains[3].Title, brains).Title
00144     'A unique title'
00145     """
00146     normalled_chunk = normalled
00147     if not normalled_chunk:
00148         normalled_chunk = normalize(chunk)
00149     if not isinstance(brains, list):
00150         # make a copy to AdvancedQuery sequencing issues
00151         brains = [x for x in brains]
00152 
00153     # inspect single return case
00154 
00155     if len(brains) == 1 and \
00156            (brains[0].getId == normalled_chunk \
00157             or brains[0].getId.strip() == chunk.strip() \
00158             or normalize(brains[0].Title) == normalled_chunk):
00159         return brains[0]
00160 
00161     # first, match id
00162 
00163     # reversing the brains into a dict clobbers younger matches with
00164     # the same id. we'll match against the normalled chunk, then the
00165     # chunk (for single work chunks)
00166 
00167     btup = [(brain.getId, brain) for brain in brains]
00168     id_dict = dict(reversed(btup))
00169     for unk in normalled_chunk, chunk,:
00170         if id_dict.has_key(unk):
00171             return id_dict[unk]
00172 
00173     # second, match Title
00174     brains=[brain for brain in brains \
00175             if normalize(brain.Title) == normalled_chunk]
00176     
00177     return brains and brains[0] or None
00178 
00179 

Here is the call graph for this function:

Here is the caller graph for this function:

def wicked.utils.getWicked (   field,
  instance,
  event = None 
)

Definition at line 30 of file utils.py.

00030 
00031 def getWicked(field, instance, event=None):
00032     if not event:
00033         event = WickedEvent()
00034     return getMultiAdapter((field, instance, event), IWickedFilter)
00035 

Here is the caller graph for this function:

def wicked.utils.linkcache (   func)

Definition at line 16 of file utils.py.

00016 
00017 def linkcache(func):
00018     def cache(wfilter, chunk, normalized):
00019         # cache depends on query and match
00020         # this could use some untangling
00021         # generic function? 
00022         value = wfilter.cache.get(normalized)
00023         if not value:
00024             value = func(wfilter, chunk, normalized)
00025             if value:
00026                 uid = value[0]['uid']
00027                 wfilter.cache.set((normalized, uid), value)
00028         return value
00029     return cache

Here is the call graph for this function:

def wicked.utils.match (   query)

Definition at line 36 of file utils.py.

00036 
00037 def match(query):
00038     def match(self, best_match=True):
00039         data = query(self)
00040         if data and best_match:
00041             return [getMatch(self.chunk, data, normalled=self.normalled)]
00042         return data
00043     return match
00044 

Here is the call graph for this function:

Here is the caller graph for this function:

def wicked.utils.packBrain (   brain)
converts dataobjects in to template ready dictionaries

>>> from Missing import Value
>>> class brain(object):
...     def getPath(self):
...         return '/path'
...     data_record_id_=123
...     UID='unique'
...     getIcon='/donkey.gif'
>>> packBrain(brain())['uid']
'unique'

packBrain needs to filter out Missing.Value that make creep in.

>>> fbrain = brain()
>>> brain.UID=Value
>>> packBrain(fbrain)['uid']
123

Definition at line 54 of file utils.py.

00054 
00055 def packBrain(brain):
00056     """
00057     converts dataobjects in to template ready dictionaries
00058 
00059     >>> from Missing import Value
00060     >>> class brain(object):
00061     ...     def getPath(self):
00062     ...         return '/path'
00063     ...     data_record_id_=123
00064     ...     UID='unique'
00065     ...     getIcon='/donkey.gif'
00066     >>> packBrain(brain())['uid']
00067     'unique'
00068 
00069     packBrain needs to filter out Missing.Value that make creep in.
00070 
00071     >>> fbrain = brain()
00072     >>> brain.UID=Value
00073     >>> packBrain(fbrain)['uid']
00074     123
00075     """
00076     # missing values are not hashable nor can they be dict keys.  they
00077     # should never see the light of day. In the rare case that the UID
00078     # index is updated for an object, substitute the record id
00079 
00080     uid = cleanUID(brain)
00081     
00082     return dict(path=brain.getPath(),
00083                 icon=brain.getIcon,
00084                 uid=uid,
00085                 rid=brain.data_record_id_)
00086 

Here is the call graph for this function:

Definition at line 221 of file utils.py.

00221 
00222 def test_suite():
00223     import unittest
00224     from zope.testing import doctest
00225     optionflags = doctest.REPORT_ONLY_FIRST_FAILURE | doctest.ELLIPSIS
00226     return doctest.DocTestSuite('wicked.utils',
00227                                 optionflags=optionflags)

Variable Documentation

Definition at line 215 of file utils.py.

tuple wicked.utils._marker = object()

Definition at line 187 of file utils.py.

wicked.utils.clearbefore = _m.clearbefore

Definition at line 218 of file utils.py.

wicked.utils.memoize = _m.memoize

Definition at line 216 of file utils.py.

wicked.utils.memoizedproperty = _m.memoizedproperty

Definition at line 217 of file utils.py.