assorted.py :  » Wiki » FelloWiki » FelloWiki-0.01a1.dev-r36 » fellowiki » util » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Wiki » FelloWiki 
FelloWiki » FelloWiki 0.01a1.dev r36 » fellowiki » util » assorted.py
# Copyright (c) 2006 Jan Niklas Fingerle
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

"""fellowiki assorted utilities

TODO: overview

assorted should read "unsorted" ;-) TODO: Sort. Sort of. Ish. 
    
"""

import re
from turbogears import identity

# TODO: howto support CElementTree (portably!, utf8-support?)

from elementtree.ElementTree import Element


def attributes_from_dict(dict_):
    """initialize class instance from __init__ arguments

        Taken from Python Cookbook, 2nd edition, recipe 6.18 
        TODO: elaborate, license
        
    """

    self_ = dict_.pop('self')
    for name, value in dict_.iteritems():
        setattr(self_, name, value)
   
def save_add(val_1, val_2):
    """add to values, None being a neutral element to the addition
    
    parameters: val_1,
                val_2: two values to be added
    returns: val_1 + val_2 iff both aren't None, else the one, that isn't
             None, or None if both are None
    throws: whatever may be thrown by adding val_1 and val_2 if both aren't
            None
    
    """
    if val_2 is None:
        return val_1
    elif val_1 is None:
        return val_2
    else:
        return val_1 + val_2

def add_element_contents(etree_1, etree_2, ret_type):
    """adds the content of to elementtree elements
    
    parameters: etree_1,
                etree_2:  two elementtree.ElementTree.Element instances
                ret_type: elementtype of the Element instance to be returned
    returns: new Element instance of type ret_type with the contents of 
             etree_1 and etree_2 concatenated. If any of the etree_n has a 
             .tail (i.e. text following the end tag), it (the tail) will be 
             ignored.

    """

    # The tricky thing is, that the last text (non-element) content of etree_1
    # resides in the ".tail" of the last contained element if there is one. 
    # Otherwise, the ".text" is the only content, therefore it is the last
    # text content.

    ret = Element(ret_type)
    if len(etree_1) == 0:
        ret.text = save_add(etree_1.text, etree_2.text)
        ret[:] = etree_2[:]
    else:
        last_sub = etree_1[len(etree_1)-1]
        last_sub.tail = save_add(last_sub.tail, etree_2.text)
        ret.text = etree_1.text
        ret[:] = etree_1[:-1] + [last_sub] + etree_2[:]
    return ret

def check_for_permission(perm):
    """check for a permission explicitly"""
    if not (perm is None or
            perm in identity.current.permissions):
        raise identity.IdentityFailure, "don't have permission %s" % perm
        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.