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

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

Update MILK stack to use the new ndgCommon clients suite + improve
tests by adding scaffold to do proper tidyups after tests run.

Line 
1import urllib, logging
2from milk_server.lib.base import *
3from ndg.common.src.clients.xmldb.eXist.atomclient import AtomClient
4
5def getExistClient(providerID):
6    '''
7    Use the config data to set up and return an eXist client
8    '''
9    logging.info("Setting up eXist client to provider, '%s'" %providerID)
10    # firstly check if there is a current connection available
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('|')]
Note: See TracBrowser for help on using the repository browser.