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

Commit

Permalink
Backported
Browse files Browse the repository at this point in the history
r26501 | srichter | 2004-07-13 14:12:57 -0400 (Tue, 13 Jul 2004) | 7 lines
r26502 | srichter | 2004-07-13 14:13:52 -0400 (Tue, 13 Jul 2004) | 2 lines
r26505 | srichter | 2004-07-13 15:04:10 -0400 (Tue, 13 Jul 2004) | 3 lines
r26506 | srichter | 2004-07-13 15:16:57 -0400 (Tue, 13 Jul 2004) | 2 lines
r26509 | srichter | 2004-07-13 17:47:43 -0400 (Tue, 13 Jul 2004) | 2 lines
  • Loading branch information
strichter committed Aug 12, 2004
1 parent 647515c commit 1c25cf2
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions __init__.py
@@ -0,0 +1,101 @@
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Dependable Framework.
$Id$
"""
from interfaces import IDependable
from zope.app.annotation.interfaces import IAnnotations
from zope.app.traversing.api import getParent, canonicalPath, getPath
from zope.interface import implements


class PathSetAnnotation(object):
"""Abstract base class for annotations that are sets of paths.
To make this into a concrete class, a subclass must set the class
attribute 'key' to a unique annotation key. A subclass may also
choose to rename the methods.
"""

def __init__(self, context):
self.context = context
try:
parent = getParent(self.context)
except TypeError:
parent = None
if parent is not None:
pp = getPath(parent)
if not pp.endswith("/"):
pp += "/"
self.pp = pp # parentpath
else:
self.pp = ""
self.pplen = len(self.pp)

def addPath(self, path):
path = self._make_relative(path)
annotations = IAnnotations(self.context)
old = annotations.get(self.key, ())
fixed = map(self._make_relative, old)
if path not in fixed:
fixed.append(path)
new = tuple(fixed)
if new != old:
annotations[self.key] = new

def removePath(self, path):
path = self._make_relative(path)
annotations = IAnnotations(self.context)
old = annotations.get(self.key, ())
if old:
fixed = map(self._make_relative, old)
fixed = [loc for loc in fixed if loc != path]
new = tuple(fixed)
if new != old:
if new:
annotations[self.key] = new
else:
del annotations[self.key]

def getPaths(self):
annotations = IAnnotations(self.context)
locs = annotations.get(self.key, ())
return tuple(map(self._make_absolute, locs))

def _make_relative(self, path):
if path.startswith("/") and self.pp:
path = canonicalPath(path)
if path.startswith(self.pp):
path = path[self.pplen:]
while path.startswith("/"):
path = path[1:]
return path

def _make_absolute(self, path):
if not path.startswith("/") and self.pp:
path = self.pp + path
return path


class Dependable(PathSetAnnotation):
"""See IDependable."""

implements(IDependable)

key = "zope.app.dependable.Dependents"

addDependent = PathSetAnnotation.addPath
removeDependent = PathSetAnnotation.removePath
dependents = PathSetAnnotation.getPaths

0 comments on commit 1c25cf2

Please sign in to comment.