<s> """<STR_LIT>""" <EOL> import cgi <EOL> import re <EOL> try : <EOL> from urllib import unquote <EOL> from urlparse import urlparse <EOL> from urlparse import urlunparse <EOL> except ImportError : <EOL> from urllib . parse import unquote <EOL> from urllib . parse import urlparse <EOL> from urllib . parse import urlunparse <EOL> from docopt import docopt <EOL> from pyramid . i18n import get_localizer <EOL> from pyramid . i18n import get_locale_name <EOL> from pyramid . i18n import make_localizer <EOL> from pyramid . i18n import TranslationStringFactory <EOL> from pyramid . interfaces import ITranslationDirectories <EOL> from pyramid . location import inside <EOL> from pyramid . paster import bootstrap <EOL> from pyramid . paster import setup_logging <EOL> from pyramid . renderers import render <EOL> from pyramid . threadlocal import get_current_registry <EOL> from pyramid . threadlocal import get_current_request <EOL> from pyramid . url import resource_url <EOL> from pyramid . view import render_view_to_response <EOL> from repoze . lru import LRUCache <EOL> from zope . deprecation import deprecated <EOL> from kotti import DBSession <EOL> _ = TranslationStringFactory ( '<STR_LIT>' ) <EOL> def get_localizer_for_locale_name ( locale_name ) : <EOL> registry = get_current_registry ( ) <EOL> tdirs = registry . queryUtility ( ITranslationDirectories , default = [ ] ) <EOL> return make_localizer ( locale_name , tdirs ) <EOL> def translate ( * args , ** kwargs ) : <EOL> request = get_current_request ( ) <EOL> if request is None : <EOL> localizer = get_localizer_for_locale_name ( '<STR_LIT>' ) <EOL> else : <EOL> localizer = get_localizer ( request ) <EOL> return localizer . translate ( * args , ** kwargs ) <EOL> def get_paste_items ( context , request ) : <EOL> from kotti . resources import Node <EOL> items = [ ] <EOL> info = request . session . get ( '<STR_LIT>' ) <EOL> if info : <EOL> ids , action = info <EOL> for id in ids : <EOL> item = DBSession . query ( Node ) . get ( id ) <EOL> if item is None or not item . type_info . addable ( context , request ) : <EOL> continue <EOL> if action == '<STR_LIT>' and inside ( context , item ) : <EOL> continue <EOL> if context == item : <EOL> continue <EOL> items . append ( item ) <EOL> return items <EOL> def render_view ( context , request , name = '<STR_LIT>' , secure = True ) : <EOL> from kotti . security import authz_context <EOL> with authz_context ( context , request ) : <EOL> response = render_view_to_response ( context , request , name , secure ) <EOL> if response is not None : <EOL> return response . ubody <EOL> class TemplateStructure ( object ) : <EOL> def __init__ ( self , html ) : <EOL> self . html = html <EOL> def __html__ ( self ) : <EOL> return self . html <EOL> __unicode__ = __html__ <EOL> def __getattr__ ( self , key ) : <EOL> return getattr ( self . html , key ) <EOL> class LinkBase ( object ) : <EOL> def __call__ ( self , context , request ) : <EOL> return TemplateStructure ( <EOL> render ( <EOL> self . template , <EOL> dict ( link = self , context = context , request = request ) , <EOL> request , <EOL> ) <EOL> ) <EOL> def selected ( self , context , request ) : <EOL> """<STR_LIT>""" <EOL> parsed = urlparse ( unquote ( request . url ) ) <EOL> path = parsed . path . split ( '<STR_LIT:/>' ) <EOL> if '<STR_LIT>' not in path [ - <NUM_LIT:1> ] : <EOL> path [ - <NUM_LIT:1> ] = '<STR_LIT>' + path [ - <NUM_LIT:1> ] <EOL> path = '<STR_LIT:/>' . join ( path ) <EOL> url = urlunparse ( ( parsed [ <NUM_LIT:0> ] , parsed [ <NUM_LIT:1> ] , path , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return url == self . url ( context , request ) <EOL> def permitted ( self , context , request ) : <EOL> from kotti . security import view_permitted <EOL> return view_permitted ( context , request , self . name ) <EOL> def visible ( self , context , request ) : <EOL> permitted = self . permitted ( context , request ) <EOL> if permitted : <EOL> if self . predicate is not None : <EOL> return self . predicate ( context , request ) <EOL> else : <EOL> return True <EOL> return False <EOL> @ property <EOL> def path ( self ) : <EOL> return self . name <EOL> path = deprecated ( <EOL> path , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ) <EOL> class LinkRenderer ( LinkBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , predicate = None ) : <EOL> self . name = name <EOL> self . predicate = predicate <EOL> def __call__ ( self , context , request ) : <EOL> return TemplateStructure ( render_view ( context , request , name = self . name ) ) <EOL> @ staticmethod <EOL> def selected ( context , request ) : <EOL> return False <EOL> class LinkParent ( LinkBase ) : <EOL> """<STR_LIT>""" <EOL> template = '<STR_LIT>' <EOL> def __init__ ( self , title , children ) : <EOL> self . title = title <EOL> self . children = children <EOL> def visible ( self , context , request ) : <EOL> return any ( ch . visible ( context , request ) for ch in self . children ) <EOL> def selected ( self , context , request ) : <EOL> return any ( ch . selected ( context , request ) for ch in self . children ) <EOL> def get_visible_children ( self , context , request ) : <EOL> return [ ch for ch in self . children if ch . visible ( context , request ) ] <EOL> class Link ( LinkBase ) : <EOL> template = '<STR_LIT>' <EOL> def __init__ ( self , name , title = None , predicate = None , target = None ) : <EOL> self . name = name <EOL> if title is None : <EOL> title = name . replace ( '<STR_LIT:->' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) . title ( ) <EOL> self . title = title <EOL> self . predicate = predicate <EOL> self . target = target <EOL> def url ( self , context , request ) : <EOL> return resource_url ( context , request ) + '<STR_LIT>' + self . name <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , Link ) and repr ( self ) == repr ( other ) <EOL> def __repr__ ( self ) : <EOL> return u'<STR_LIT>' . format ( self . name , self . title ) <EOL> class ActionButton ( Link ) : <EOL> def __init__ ( self , path , title = None , no_children = False , <EOL> css_class = u"<STR_LIT>" ) : <EOL> super ( ActionButton , self ) . __init__ ( path , title ) <EOL> self . no_children = no_children <EOL> self . css_class = css_class <EOL> class DontCache ( Exception ) : <EOL> pass <EOL> _CACHE_ATTR = '<STR_LIT>' <EOL> def request_container ( ) : <EOL> request = get_current_request ( ) <EOL> if request is None : <EOL> return None <EOL> cache = getattr ( request , _CACHE_ATTR , None ) <EOL> if cache is None : <EOL> cache = { } <EOL> setattr ( request , _CACHE_ATTR , cache ) <EOL> return cache <EOL> def cache ( compute_key , container_factory ) : <EOL> marker = object ( ) <EOL> def decorator ( func ) : <EOL> def replacement ( * args , ** kwargs ) : <EOL> cache = container_factory ( ) <EOL> if cache is None : <EOL> return func ( * args , ** kwargs ) <EOL> try : <EOL> key = compute_key ( * args , ** kwargs ) <EOL> except DontCache : <EOL> return func ( * args , ** kwargs ) <EOL> key = u'<STR_LIT>' . format ( func . __module__ , func . __name__ , key ) <EOL> cached_value = cache . get ( key , marker ) <EOL> if cached_value is marker : <EOL> cached_value = cache [ key ] = func ( * args , ** kwargs ) <EOL> else : <EOL> pass <EOL> return cached_value <EOL> replacement . __doc__ = func . __doc__ <EOL> return replacement <EOL> return decorator <EOL> def request_cache ( compute_key ) : <EOL> return cache ( compute_key , request_container ) <EOL> class LRUCacheSetItem ( LRUCache ) : <EOL> __setitem__ = LRUCache . put <EOL> _lru_cache = LRUCacheSetItem ( <NUM_LIT:1000> ) <EOL> def lru_cache ( compute_key ) : <EOL> return cache ( compute_key , lambda : _lru_cache ) <EOL> def clear_cache ( ) : <EOL> request = get_current_request ( ) <EOL> if request is not None : <EOL> setattr ( request , _CACHE_ATTR , None ) <EOL> _lru_cache . clear ( ) <EOL> def extract_from_settings ( prefix , settings = None ) : <EOL> """<STR_LIT>""" <EOL> from kotti import get_settings <EOL> settings = settings if settings is not None else get_settings ( ) <EOL> extracted = { } <EOL> for key , value in settings . items ( ) : <EOL> if key . startswith ( prefix ) : <EOL> extracted [ key [ len ( prefix ) : ] ] = value <EOL> return extracted <EOL> def disambiguate_name ( name ) : <EOL> parts = name . split ( u'<STR_LIT:->' ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> try : <EOL> index = int ( parts [ - <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> parts . append ( u'<STR_LIT:1>' ) <EOL> else : <EOL> parts [ - <NUM_LIT:1> ] = unicode ( index + <NUM_LIT:1> ) <EOL> else : <EOL> parts . append ( u'<STR_LIT:1>' ) <EOL> return u'<STR_LIT:->' . join ( parts ) <EOL> def title_to_name ( title , blacklist = ( ) , max_length = None ) : <EOL> """<STR_LIT>""" <EOL> if max_length is None : <EOL> from kotti . resources import Node <EOL> max_length = Node . name . property . columns [ <NUM_LIT:0> ] . type . length - <NUM_LIT:10> <EOL> request = get_current_request ( ) <EOL> if request is not None : <EOL> locale_name = get_locale_name ( request ) <EOL> else : <EOL> locale_name = '<STR_LIT>' <EOL> from kotti import get_settings <EOL> urlnormalizer = get_settings ( ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> name = unicode ( urlnormalizer ( title , locale_name , max_length = max_length ) ) <EOL> if name not in blacklist : <EOL> return name <EOL> name += u'<STR_LIT>' <EOL> while name in blacklist : <EOL> name = disambiguate_name ( name ) <EOL> return name <EOL> def camel_case_to_name ( text ) : <EOL> """<STR_LIT>""" <EOL> return re . sub ( <EOL> r'<STR_LIT>' , r'<STR_LIT>' , text ) . lower ( ) <EOL> def command ( func , doc ) : <EOL> args = docopt ( doc ) <EOL> config_uri = args [ '<STR_LIT>' ] <EOL> pyramid_env = bootstrap ( config_uri ) <EOL> setup_logging ( config_uri ) <EOL> try : <EOL> func ( args ) <EOL> finally : <EOL> pyramid_env [ '<STR_LIT>' ] ( ) <EOL> return <NUM_LIT:0> <EOL> ViewLink = Link <EOL> deprecated ( <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" <EOL> ) <EOL> def _to_fieldstorage ( fp , filename , mimetype , size , ** _kwds ) : <EOL> """<STR_LIT>""" <EOL> f = cgi . FieldStorage ( ) <EOL> f . file = fp <EOL> f . filename = filename <EOL> f . type = mimetype <EOL> f . length = size <EOL> return f </s>
