source: MILK/trunk/milk_server/milk_server/lib/Utilities.py @ 5305

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/lib/Utilities.py@5305
Revision 5305, 2.5 KB checked in by cbyrom, 10 years ago (diff)

Improve structure of DiscoveryState? module by adding a new object,
PagerState? to hold the data connected to the results table paging.
Also tidy up code layout and simplify logic. Create new template
method, pagerControl, to represent the results table paging control

  • to allow reuse across the results and short_results templates.
Line 
1import urllib, logging
2from ndg.common.src.clients.xmldb.eXist.atomclient import AtomClient
3
4def getExistClient(providerID):
5    '''
6    Use the config data to set up and return an eXist client
7    '''
8    logging.info("Setting up eXist client to provider, '%s'" %providerID)
9    # firstly check if there is a current connection available
10    from milk_server.lib.base import g, request
11    eXistClient = g.eXistDBCons.get(providerID)
12    if eXistClient:
13        logging.info("- found existing client to provider - returning this")
14        return eXistClient
15
16    existHost = request.environ['ndgConfig'].get('NDG_EXIST', providerID)
17    configFile = request.environ['ndgConfig'].get('NDG_EXIST','passwordFile')
18    eXistClient = AtomClient(dbHostName = existHost, configFileName = configFile)
19   
20    # add the client to the global variables for re-use
21    g.eXistDBCons[providerID] = eXistClient
22    logging.info("Returning eXist client")
23    return eXistClient
24
25
26def recreateListFromUnicode(string):
27    ''' Parse a list that has been passed as unicode over http and
28    recreate it in its list form
29    '''
30    if not string:
31        return
32   
33    newList = []
34    element = []
35    for c in string:
36        if c == '[' or c == ' ' or c == ']' or c == '\'':
37            continue
38        elif c == ',':
39            if (len(element) > 0):
40                newList.append(''.join(element))
41                element = []
42            continue
43        element.append(c)
44
45    # don't forget the last element
46    if (len(element) > 0):
47        newList.append(''.join(element))
48       
49    return newList
50
51
52def isTabRequired(pageTabs, string):
53    for tab in pageTabs:
54        if tab[0] == string:
55            return False
56   
57    return True
58       
59
60def urlListEncode(urlList):
61    """
62    Encode a list of URLs so that they can be embedded in another URL
63    """
64    return '|'.join(urllib.quote(x, '%') for x in urlList)
65
66
67def urlListDecode(string):
68    """
69    Decode an encoded list of URLs.
70    """
71    return [urllib.unquote(x) for x in string.split('|')]
72
73
74def getURLConstraints(urlConstraints):
75    '''
76    Given a set of contraints passed as a uri parameter, convert these into
77    a dict and return this, avoiding duplicate entries
78    @return dict with key, val = param, value
79    '''
80    constraints = {}
81    inputs = urlConstraints.split('&')
82    for input in inputs:
83        data = input.split('=')
84        if len(data) > 2:
85            import pdb
86            pdb.set_trace()
87        key, val = input.split('=')
88        if val:
89            constraints[key] = val
90    return constraints
91
Note: See TracBrowser for help on using the repository browser.