<s> """<STR_LIT>""" <EOL> import re <EOL> from django . shortcuts import render_to_response <EOL> from django . http import HttpResponse <EOL> from django . conf import settings <EOL> from graphite . account . models import Profile <EOL> from graphite . util import getProfile , getProfileByUsername , defaultUser , json <EOL> from graphite . logger import log <EOL> import hashlib <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> def header ( request ) : <EOL> "<STR_LIT>" <EOL> context = { } <EOL> context [ '<STR_LIT:user>' ] = request . user <EOL> context [ '<STR_LIT>' ] = getProfile ( request ) <EOL> context [ '<STR_LIT>' ] = settings . DOCUMENTATION_URL <EOL> context [ '<STR_LIT>' ] = settings . LOGIN_URL <EOL> return render_to_response ( "<STR_LIT>" , context ) <EOL> def browser ( request ) : <EOL> "<STR_LIT>" <EOL> context = { <EOL> '<STR_LIT>' : request . GET . urlencode ( ) , <EOL> '<STR_LIT:target>' : request . GET . get ( '<STR_LIT:target>' ) <EOL> } <EOL> if context [ '<STR_LIT>' ] : <EOL> context [ '<STR_LIT>' ] = context [ '<STR_LIT>' ] . replace ( '<STR_LIT:#>' , '<STR_LIT>' ) <EOL> if context [ '<STR_LIT:target>' ] : <EOL> context [ '<STR_LIT:target>' ] = context [ '<STR_LIT:target>' ] . replace ( '<STR_LIT:#>' , '<STR_LIT>' ) <EOL> return render_to_response ( "<STR_LIT>" , context ) <EOL> def search ( request ) : <EOL> query = request . POST [ '<STR_LIT>' ] <EOL> if not query : <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> patterns = query . split ( ) <EOL> regexes = [ re . compile ( p , re . I ) for p in patterns ] <EOL> def matches ( s ) : <EOL> for regex in regexes : <EOL> if regex . search ( s ) : <EOL> return True <EOL> return False <EOL> results = [ ] <EOL> index_file = open ( settings . INDEX_FILE ) <EOL> for line in index_file : <EOL> if matches ( line ) : <EOL> results . append ( line . strip ( ) ) <EOL> if len ( results ) >= <NUM_LIT:100> : <EOL> break <EOL> index_file . close ( ) <EOL> result_string = '<STR_LIT:U+002C>' . join ( results ) <EOL> return HttpResponse ( result_string , mimetype = '<STR_LIT>' ) <EOL> def myGraphLookup ( request ) : <EOL> "<STR_LIT>" <EOL> profile = getProfile ( request , allowDefault = False ) <EOL> assert profile <EOL> nodes = [ ] <EOL> leafNode = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } <EOL> branchNode = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> try : <EOL> path = str ( request . GET [ '<STR_LIT:path>' ] ) <EOL> if path : <EOL> if path . endswith ( '<STR_LIT:.>' ) : <EOL> userpath_prefix = path <EOL> else : <EOL> userpath_prefix = path + '<STR_LIT:.>' <EOL> else : <EOL> userpath_prefix = "<STR_LIT>" <EOL> matches = [ graph for graph in profile . mygraph_set . all ( ) . order_by ( '<STR_LIT:name>' ) if graph . name . startswith ( userpath_prefix ) ] <EOL> log . info ( "<STR_LIT>" % ( profile . user . username , path , userpath_prefix , len ( matches ) ) ) <EOL> branch_inserted = set ( ) <EOL> leaf_inserted = set ( ) <EOL> for graph in matches : <EOL> isBranch = False <EOL> dotPos = graph . name . find ( '<STR_LIT:.>' , len ( userpath_prefix ) ) <EOL> if dotPos >= <NUM_LIT:0> : <EOL> isBranch = True <EOL> name = graph . name [ len ( userpath_prefix ) : dotPos ] <EOL> if name in branch_inserted : continue <EOL> branch_inserted . add ( name ) <EOL> else : <EOL> name = graph . name [ len ( userpath_prefix ) : ] <EOL> if name in leaf_inserted : continue <EOL> leaf_inserted . add ( name ) <EOL> node = { '<STR_LIT:text>' : str ( name ) } <EOL> if isBranch : <EOL> node . update ( { '<STR_LIT:id>' : str ( userpath_prefix + name + '<STR_LIT:.>' ) } ) <EOL> node . update ( branchNode ) <EOL> else : <EOL> m = hashlib . md5 ( ) <EOL> m . update ( name ) <EOL> md5 = m . hexdigest ( ) <EOL> node . update ( { '<STR_LIT:id>' : str ( userpath_prefix + md5 ) , '<STR_LIT>' : str ( graph . url ) } ) <EOL> node . update ( leafNode ) <EOL> nodes . append ( node ) <EOL> except : <EOL> log . exception ( "<STR_LIT>" ) <EOL> if not nodes : <EOL> no_graphs = { '<STR_LIT:text>' : "<STR_LIT>" , '<STR_LIT:id>' : '<STR_LIT>' } <EOL> no_graphs . update ( leafNode ) <EOL> nodes . append ( no_graphs ) <EOL> return json_response ( nodes , request ) <EOL> def userGraphLookup ( request ) : <EOL> "<STR_LIT>" <EOL> user = request . GET . get ( '<STR_LIT:user>' ) <EOL> path = request . GET [ '<STR_LIT:path>' ] <EOL> if user : <EOL> username = user <EOL> graphPath = path [ len ( username ) + <NUM_LIT:1> : ] <EOL> elif '<STR_LIT:.>' in path : <EOL> username , graphPath = path . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> else : <EOL> username , graphPath = path , None <EOL> nodes = [ ] <EOL> branchNode = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> leafNode = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } <EOL> try : <EOL> if not username : <EOL> profiles = Profile . objects . exclude ( user = defaultUser ) <EOL> for profile in profiles : <EOL> if profile . mygraph_set . count ( ) : <EOL> node = { <EOL> '<STR_LIT:text>' : str ( profile . user . username ) , <EOL> '<STR_LIT:id>' : str ( profile . user . username ) <EOL> } <EOL> node . update ( branchNode ) <EOL> nodes . append ( node ) <EOL> else : <EOL> profile = getProfileByUsername ( username ) <EOL> assert profile , "<STR_LIT>" % username <EOL> if graphPath : <EOL> prefix = graphPath . rstrip ( '<STR_LIT:.>' ) + '<STR_LIT:.>' <EOL> else : <EOL> prefix = '<STR_LIT>' <EOL> matches = [ graph for graph in profile . mygraph_set . all ( ) . order_by ( '<STR_LIT:name>' ) if graph . name . startswith ( prefix ) ] <EOL> inserted = set ( ) <EOL> for graph in matches : <EOL> relativePath = graph . name [ len ( prefix ) : ] <EOL> nodeName = relativePath . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> if nodeName in inserted : <EOL> continue <EOL> inserted . add ( nodeName ) <EOL> if '<STR_LIT:.>' in relativePath : <EOL> node = { <EOL> '<STR_LIT:text>' : str ( nodeName ) , <EOL> '<STR_LIT:id>' : str ( username + '<STR_LIT:.>' + prefix + nodeName + '<STR_LIT:.>' ) , <EOL> } <EOL> node . update ( branchNode ) <EOL> else : <EOL> m = hashlib . md5 ( ) <EOL> m . update ( nodeName ) <EOL> md5 = m . hexdigest ( ) <EOL> node = { <EOL> '<STR_LIT:text>' : str ( nodeName ) , <EOL> '<STR_LIT:id>' : str ( username + '<STR_LIT:.>' + prefix + md5 ) , <EOL> '<STR_LIT>' : str ( graph . url ) , <EOL> } <EOL> node . update ( leafNode ) <EOL> nodes . append ( node ) <EOL> except : <EOL> log . exception ( "<STR_LIT>" % username ) <EOL> if not nodes : <EOL> no_graphs = { '<STR_LIT:text>' : "<STR_LIT>" , '<STR_LIT:id>' : '<STR_LIT>' } <EOL> no_graphs . update ( leafNode ) <EOL> nodes . append ( no_graphs ) <EOL> return json_response ( nodes , request ) <EOL> def json_response ( nodes , request = None ) : <EOL> if request : <EOL> jsonp = request . REQUEST . get ( '<STR_LIT>' , False ) <EOL> else : <EOL> jsonp = False <EOL> json_data = json . dumps ( nodes ) <EOL> if jsonp : <EOL> response = HttpResponse ( "<STR_LIT>" % ( jsonp , json_data ) , mimetype = "<STR_LIT>" ) <EOL> else : <EOL> response = HttpResponse ( json_data , mimetype = "<STR_LIT:application/json>" ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return response <EOL> def any ( iterable ) : <EOL> for i in iterable : <EOL> if i : <EOL> return True <EOL> return False </s>
