Render the template with the specified local variables and return a single byte or unicode string . If it is a byte string the encoding must match self . encoding . This method must be thread - safe! Local variables may be provided in dictionaries ( * args ) or directly as keywords ( ** kwargs ) .

def render(self, *args, **kwargs):
        """ Render the template with the specified local variables and return
        a single byte or unicode string. If it is a byte string, the encoding
        must match self.encoding. This method must be thread-safe!
        Local variables may be provided in dictionaries (*args)
        or directly, as keywords (**kwargs).
        """
        raise NotImplementedError

def render ( self , * args , * * kwargs ) : raise NotImplementedError


This matches comments and all kinds of quoted strings but does NOT match comments ( # ... ) within quoted strings . ( trust me )

def re_pytokens(cls):
        ''' This matches comments and all kinds of quoted strings but does
            NOT match comments (#...) within quoted strings. (trust me) '''
        return re.compile(r'''
            (''(?!')|""(?!")|'{6}|"{6}    # Empty strings (all 4 types)
             |'(?:[^\\']|\\.)+?'          # Single quotes (')
             |"(?:[^\\"]|\\.)+?"          # Double quotes (")
             |'{3}(?:[^\\]|\\.|\n)+?'{3}  # Triple-quoted strings (')
             |"{3}(?:[^\\]|\\.|\n)+?"{3}  # Triple-quoted strings (")
             |\#.*                        # Comments
            )''', re.VERBOSE)


Removes comments ( # ... ) from python code .

def split_comment(cls, code):
        """ Removes comments (#...) from python code. """
        if '#' not in code: return code
        #: Remove comments only (leave quoted strings as they are)
        subf = lambda m: '' if m.group(0)[0]=='#' else m.group(0)
        return re.sub(cls.re_pytokens, subf, code)


Render the template using keyword arguments as local variables .

def render(self, *args, **kwargs):
        """ Render the template using keyword arguments as local variables. """
        for dictarg in args: kwargs.update(dictarg)
        stdout = []
        self.execute(stdout, kwargs)
        return ''.join(stdout)

def render ( self , * args , * * kwargs ) : for dictarg in args : kwargs . update ( dictarg ) stdout = [ ] self . execute ( stdout , kwargs ) return '' . join ( stdout )


since plugin s functions are not in the global scope you can use this wrapper to call a function whether it s in global scope or in one of the plugins

def call_func(fn_name, *args, **kwargs):
    '''
    since plugin's functions are not in the global scope, you can
    use this wrapper to call a function, whether it's in global
    scope or in one of the plugins
    '''
    if fn_name in globals():
        return globals()[fn_name](*args, **kwargs)
    else:
        for loaded_plugin in state['loaded_plugins']:
            for attrib_key in dir(loaded_plugin):
                attrib = loaded_plugin.__dict__.get(attrib_key)
                if isinstance(attrib, types.FunctionType):
                    if attrib.__name__ == fn_name:
                        return attrib(*args, **kwargs)

def call_func ( fn_name , * args , * * kwargs ) : if fn_name in globals ( ) : return globals ( ) [ fn_name ] ( * args , * * kwargs ) else : for loaded_plugin in state [ 'loaded_plugins' ] : for attrib_key in dir ( loaded_plugin ) : attrib = loaded_plugin . __dict__ . get ( attrib_key ) if isinstance ( attrib , types . FunctionType ) : if attrib . __name__ == fn_name : return attrib ( * args , * * kwargs )


def track_history():
    '''
    maintain a list of the 10 most recent pages loaded per earch particular user
    '''
    global session
    # ignore everything that's not a page being loaded by the user:
    if request.fullpath.startswith('/assets'):
        return
    # loaded in background by report page
    if request.fullpath.startswith('/report/data'):
        return
    session = request.environ.get('beaker.session')
    session['history'] = session.get('history', deque())
    # loaded in background by timeline
    if len(session['history']) and request.fullpath == '/events/xml' and session['history'][len(session['history']) - 1] == '/events/timeline':
        return
    # note the url always misses the '#foo' part
    url = request.fullpath
    if request.query_string:
        url += "?%s" % request.query_string
    if len(session['history']) and url == session['history'][len(session['history']) - 1]:
        return
    session['history'].append(url)
    if len(session['history']) > 10:
        session['history'].popleft()
    session.save()


much like http : // localhost : 9200 / anthracite / event / _search?q = * : * &pretty = true but : displays only the actual events not index etc they are sorted and uses unix timestamps

def events_json():
    '''
    much like http://localhost:9200/anthracite/event/_search?q=*:*&pretty=true
    but: displays only the actual events, not index etc, they are sorted, and uses unix timestamps
    '''
    response.set_header("Access-Control-Allow-Origin", "*")
    response.set_header("Access-Control-Allow-Credentials", "true")
    response.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
    response.set_header("Access-Control-Allow-Headers", "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control")
    return {"events": backend.get_events_raw()}


def events_csv():
    '''
    returns the first line of every event
    '''
    response.content_type = 'text/plain'
    events = []
    for event in backend.get_events_raw():
        desc = event['desc'].replace("\n", '  ').replace("\r", ' ').strip()
        formatted = [event['id'], str(event['date']), desc, ' '.join(event['tags'])]
        events.append(','.join(formatted))
    return "\n".join(events)

def
events_csv
(
)
:
response
.
content_type
=
'text/plain'
events
=
[
]
for
event
in
backend
.
get_events_raw
(
)
:
desc
=
event
[
'desc'
]
.
replace
(
"\n"
,
'  '
)
.
replace
(
"\r"
,
' '
)
.
strip
(
)
formatted
=
[
event
[
'id'
]
,
str
(
event
[
'date'
]
)
,
desc
,
' '
.
join
(
event
[
'tags'
]
)
]
events
.
append
(
','
.
join
(
formatted
)
)
return
"\n"
.
join
(
events
)


in : something like 12 / 31 / 2012 10 : 25 : 35 PM which is local time . out : unix timestamp

def local_datepick_to_unix_timestamp(datepick):
    '''
    in: something like 12/31/2012 10:25:35 PM, which is local time.
    out: unix timestamp
    '''
    import time
    import datetime
    return int(time.mktime(datetime.datetime.strptime(datepick, "%m/%d/%Y %I:%M:%S %p").timetuple()))

def
local_datepick_to_unix_timestamp
(
datepick
)
:
import
time
import
datetime
return
int
(
time
.
mktime
(
datetime
.
datetime
.
strptime
(
datepick
,
"%m/%d/%Y %I:%M:%S %p"
)
.
timetuple
(
)
)
)


loads all the plugins sub - modules returns encountered errors doesn t raise them because whoever calls this function defines how any errors are handled . meanwhile loading must continue

def load_plugins(plugins_to_load, config):
    '''
    loads all the plugins sub-modules
    returns encountered errors, doesn't raise them because
    whoever calls this function defines how any errors are
    handled. meanwhile, loading must continue
    '''
    import plugins
    errors = []
    add_urls = {}
    remove_urls = []
    loaded_plugins = []
    plugins_dir = os.path.dirname(plugins.__file__)
    wd = os.getcwd()
    os.chdir(plugins_dir)
    for module in plugins_to_load:
        try:
            print "importing plugin '%s'" % module
            imp = __import__('plugins.' + module, {}, {}, ['*'])
            loaded_plugins.append(imp)
            try:
                add_urls[module] = imp.add_urls
            except Exception:
                pass
            try:
                remove_urls.extend(imp.remove_urls)
            except Exception:
                pass
        except Exception, e:
            errors.append(PluginError(module, "Failed to add plugin '%s'" % module, e))
            continue

    os.chdir(wd)
    state = {
        'add_urls': add_urls,
        'remove_urls': remove_urls,
        'loaded_plugins': loaded_plugins
    }
    #  make some vars accessible for all imported plugins
    __builtins__['state'] = state
    __builtins__['config'] = config
    return (state, errors)


def iso8601_to_unix_timestamp(self, iso8601):
        '''
            elasticsearch returns something like 2013-03-20T20:41:16

        '''
        unix = calendar.timegm(datetime.datetime.strptime(iso8601, "%Y-%m-%dT%H:%M:%S").timetuple())
        return unix

def iso8601_to_unix_timestamp ( self , iso8601 ) : unix = calendar . timegm ( datetime . datetime . strptime ( iso8601 , "%Y-%m-%dT%H:%M:%S" ) . timetuple ( ) ) return unix


return format that s optimized for elasticsearch

def get_events_raw(self, query=None):
        '''
        return format that's optimized for elasticsearch
        '''
        hits = self.es_get_events(query)
        events = hits['hits']['hits']
        for (i, event) in enumerate(events):
            event_id = event['_id']
            events[i] = event['_source']
            events[i]['id'] = event_id
            events[i]['date'] = self.iso8601_to_unix_timestamp(events[i]['date'])
        return events

def get_events_raw ( self , query = None ) : hits = self . es_get_events ( query ) events = hits [ 'hits' ] [ 'hits' ] for ( i , event ) in enumerate ( events ) : event_id = event [ '_id' ] events [ i ] = event [ '_source' ] events [ i ] [ 'id' ] = event_id events [ i ] [ 'date' ] = self . iso8601_to_unix_timestamp ( events [ i ] [ 'date' ] ) return events


desc is the entire desc with \ n replaced with . this output doesn t attempt to shorten the desc string .

def events_csv_vimeo_analytics():
    '''
    desc is the entire desc, with '\n' replaced with '  '. this output doesn't attempt to shorten the desc string.
    '''
    response.content_type = 'text/plain'

    def line_yielder(events):
        for event in events:
            yield ','.join([str(field).replace(',', '') for field in event])

    return "\n".join(line_yielder(events_vimeo_analytics()))

def events_csv_vimeo_analytics ( ) : response . content_type = 'text/plain' def line_yielder ( events ) : for event in events : yield ',' . join ( [ str ( field ) . replace ( ',' , '' ) for field in event ] ) return "\n" . join ( line_yielder ( events_vimeo_analytics ( ) ) )