PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/web/framework/helpers/__init__.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 114 lines | 102 code | 5 blank | 7 comment | 1 complexity | 13463cfcdcb5915caa5f9cf60bd8334a MD5 | raw file
  1"""
  2Galaxy web framework helpers
  3"""
  4
  5import time
  6from cgi import escape
  7from datetime import datetime, timedelta
  8from galaxy import eggs
  9from galaxy.util import hash_util
 10from galaxy.util.json import to_json_string
 11eggs.require( "MarkupSafe" ) #required by WebHelpers
 12eggs.require( "WebHelpers" )
 13from webhelpers import date
 14from webhelpers.html.tags import stylesheet_link, javascript_link
 15
 16eggs.require( "Routes" )
 17from routes import url_for
 18
 19server_starttime = int(time.time())
 20
 21def time_ago( x ):
 22    """
 23    Convert a datetime to a string.
 24    """
 25    delta = timedelta(weeks=1)
 26
 27    # If the date is more than one week ago, then display the actual date instead of in words
 28    if (datetime.utcnow() - x) > delta: # Greater than a week difference
 29        return x.strftime("%b %d, %Y")
 30    else:
 31        date_array = date.distance_of_time_in_words( x, datetime.utcnow() ).replace(",", "").split(" ")
 32        return "~%s %s ago" % (date_array[0], date_array[1])
 33
 34def iff( a, b, c ):
 35    """
 36    Ternary shortcut
 37    """
 38    if a:
 39        return b
 40    else:
 41        return c
 42
 43def truncate(content, length=100, suffix='...'):
 44    """
 45    Smart string truncation
 46    """
 47    if len(content) <= length:
 48        return content
 49    else:
 50        return content[:length].rsplit(' ', 1)[0] + suffix
 51
 52# Quick helpers for static content
 53
 54def css( *args ):
 55    """
 56    Take a list of stylesheet names (no extension) and return appropriate string
 57    of link tags.
 58
 59    Cache-bust with time that server started running on
 60    """
 61    return "\n".join( [ stylesheet_link( url_for( "/static/style/%s.css?v=%s" % (name, server_starttime) ) ) for name in args ] )
 62
 63def js_helper( prefix, *args ):
 64    """
 65    Take a prefix and list of javascript names and return appropriate
 66    string of script tags.
 67
 68    Cache-bust with time that server started running on
 69    """
 70    return "\n".join( [ javascript_link( url_for( "/%s%s.js?v=%s" % (prefix, name, server_starttime ) ) ) for name in args ] )
 71
 72def js( *args ):
 73    """
 74    Take a prefix and list of javascript names and return appropriate
 75    string of script tags.
 76    """
 77    return js_helper( 'static/scripts/', *args )
 78
 79def templates( *args ):
 80    """
 81    Take a list of template names (no extension) and return appropriate
 82    string of script tags.
 83    """
 84    return js_helper( 'static/scripts/templates/compiled/', *args )
 85
 86# Hashes
 87
 88def md5( s ):
 89    """
 90    Return hex encoded md5 hash of string s
 91    """
 92    m = hash_util.md5()
 93    m.update( s )
 94    return m.hexdigest()
 95
 96# Unicode help
 97
 98def to_unicode( a_string ):
 99    """
100    Convert a string to unicode in utf-8 format; if string is already unicode,
101    does nothing because string's encoding cannot be determined by introspection.
102    """
103    a_string_type = type ( a_string )
104    if a_string_type is str:
105        return unicode( a_string, 'utf-8' )
106    elif a_string_type is unicode:
107        return a_string
108
109def is_true ( val ):
110    """
111    Returns true if input is a boolean and true or is a string and looks like a true value.
112    """
113    return val == True or val in [ 'True', 'true', 'T', 't' ]
114