Commits

Jan Brohl committed de575d9

virtualpath mit tal-angelehnter semantik
wrapper für verwendung in templates

Comments (0)

Files changed (1)

src/simplerest/util.py

 
 def default_hook(obj):
     """
-    for use as object_hook in json.loads
-    
+    for use as object_hook in json.loads    
     """
     stuff = dict()
     for k, v in obj.items():
 
 class VirtualPath(DictMixin):
     def __init__(self, path=[], **kwargs):
-        self.__path = path
-        self.__kwargs = kwargs
+        self._path = path
+        self._kwargs = kwargs
 
     def __getitem__(self, key):
-        return self.__class__(path=self.__path + [key], **self.__kwargs)
+        return self.__class__(path=self._path + [key], **self._kwargs)
 
     def __str__(self):
         return str(self())
 
     @property
     def path_as_list(self):
-        return list(self.__path)
+        return list(self._path)
 
     def __eq__(self, other):
         return (isinstance(other, self.__class__)
-                and all(other.kwarg(k) == v for k, v in self.__kwargs.items())
+                and all(other.kwarg(k) == v for k, v in self._kwargs.items())
                 and self.path_as_list == other.path_as_list
                 and other == self)
 
     def kwarg(self, name):
-        return self.__kwargs.get(name, None)
+        return self._kwargs.get(name, None)
 
     def __repr__(self):
         return "%s(%r, %s)" % (self.__class__.__name__,
                                self.path_as_list,
-                               ", ".join("%r=%r" % kv for kv in self.__kwargs.items()))
+                               ", ".join("%r=%r" % kv for kv in self._kwargs.items()))
+
+
+class TALVirtualPath(VirtualPath):
+    def __init__(self, path=[], basepath=[], **kwargs):
+        VirtualPath.__init__(self, path=path, basepath=basepath, **kwargs)
+
+    @property
+    def path_as_list(self):
+        return list(self.kwarg("basepath") or []) + self._path
+
+    def __getitem__(self, key):
+        if key == "container":
+            p = self.path_as_list
+            if p:
+                p = p[:-1]
+            return self.__class__(path=p, **self._kwargs)
+        elif key == "root":
+            return self.__class__(path=[], **self._kwargs)
+        else:
+            return VirtualPath.__getitem__(self, key)
+
 
 class CallableVirtualPath(VirtualPath):
     def __init__(self, path=[], oncall=(lambda path, kwargs:
 
     def __call__(self, **kwargs):
         self.__kwargs["oncall"](self.__path, kwargs)
+
+
+class NoCallWrapper(object):
+    def __init__(self, virtualpath):
+        self ._vp = virtualpath
+
+    def __getitem__(self, key):
+        return self.__class__(self._vp[key])
+
+    def __str__(self):
+        return str(self._vp)