Skip to content
Snippets Groups Projects
Commit c6a02a76 authored by sveseli's avatar sveseli
Browse files

add web service prototype code, initial db schema implmentation, setup script

parent 89d1c031
No related branches found
No related tags found
No related merge requests found
Showing
with 419 additions and 0 deletions
../src/python
\ No newline at end of file
setup.sh 0 → 100644
#!/bin/sh
# DM setup script for Bourne-type shells
# This file is typically sourced in user's .bashrc file
myDir=`dirname $BASH_SOURCE`
currentDir=`pwd` && cd $myDir
if [ ! -z "$DM_ROOT_DIR" -a "$DM_ROOT_DIR" != `pwd` ]; then
echo "WARNING: Resetting DM_ROOT_DIR environment variable (old value: $DM_ROOT_DIR)"
fi
export DM_ROOT_DIR=`pwd`
if [ -z $DM_DATA_DIR ]; then
export DM_DATA_DIR=$DM_ROOT_DIR/../data
if [ -d $DM_DATA_DIR ]; then
cd $DM_DATA_DIR
export DM_DATA_DIR=`pwd`
fi
fi
if [ ! -d $DM_DATA_DIR ]; then
#echo "WARNING: $DM_DATA_DIR directory does not exist. Developers should point DM_DATA_DIR to the desired area."
unset DM_DATA_DIR
fi
if [ -z $DM_VAR_DIR ]; then
export DM_VAR_DIR=$DM_ROOT_DIR/../var
if [ -d $DM_VAR_DIR ]; then
cd $DM_VAR_DIR
export DM_VAR_DIR=`pwd`
fi
fi
# Check support setup
if [ -z $DM_SUPPORT_DIR ]; then
export DM_SUPPORT_DIR=$DM_ROOT_DIR/../support
if [ -d $DM_SUPPORT_DIR ]; then
cd $DM_SUPPORT_DIR
export DM_SUPPORT_DIR=`pwd`
fi
fi
if [ ! -d $DM_SUPPORT_DIR ]; then
echo "ERROR: $DM_SUPPORT_DIR directory does not exist. Developers should point DM_SUPPORT_DIR to the desired area."
return 1
fi
export DM_HOST_ARCH=`uname | tr [A-Z] [a-z]`-`uname -m`
# Add to path only if directory exists.
prependPathIfDirExists() {
_dir=$1
if [ -d ${_dir} ]; then
PATH=${_dir}:$PATH
fi
}
# Setup epics variables
PATH=$DM_ROOT_DIR/bin:$PATH
PATH=.:$PATH
export PATH
if [ -z $LD_LIBRARY_PATH ]; then
LD_LIBRARY_PATH=.
else
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
fi
export LD_LIBRARY_PATH
# Setup python path. First check for wx, then try to find it locally, and
# then re-check for it.
# Check if we have local python
if [ -z $DM_PYTHON_DIR ]; then
pythonDir=$DM_SUPPORT_DIR/python/$DM_HOST_ARCH
else
pythonDir=$DM_PYTHON_DIR
fi
if [ -d $pythonDir ]; then
cd $pythonDir
pythonDir=`pwd`
export PATH=`pwd`/bin:$PATH
export LD_LIBRARY_PATH=`pwd`/lib:$LD_LIBRARY_PATH
export DM_PYTHON_DIR=$pythonDir
fi
if [ -z $PYTHONPATH ]; then
PYTHONPATH=$DM_ROOT_DIR/lib/python
else
PYTHONPATH=$DM_ROOT_DIR/lib/python:$PYTHONPATH
fi
export PYTHONPATH
# Get back to where we were before invoking the setup script
cd $currentDir
# Print out user environment
echo
echo "Your DM environment is defined as follows:"
echo
env | grep DM_ | grep -v GDM_
echo
echo
#!/usr/bin/env python
#######################################################################
DM_SESSION_ROLE_HTTP_HEADER = 'Dm-Session-Role'
DM_STATUS_CODE_HTTP_HEADER = 'Dm-Status-Code'
DM_STATUS_MESSAGE_HTTP_HEADER = 'Dm-Status-Message'
DM_EXCEPTION_TYPE_HTTP_HEADER = 'Dm-Exception-Type'
#!/usr/bin/env python
#######################################################################
DM_ADMIN_ROLE = 'admin'
DM_USER_ROLE = 'user'
#!/usr/bin/env python
#######################################################################
DM_SERVICE_PROTOCOL_HTTP = 'http'
DM_SERVICE_PROTOCOL_HTTPS = 'https'
#!/usr/bin/env python
#######################################################################
DM_OK = 0
DM_ERROR = 1
DM_CONFIGURATION_ERROR = 2
DM_INTERNAL_ERROR = 3
DM_INVALID_ARGUMENT_ERROR = 4
DM_INVALID_REQUEST_ERROR = 5
DM_COMMAND_FAILED_ERROR = 6
#!/usr/bin/env python
#
# Command failed exception class.
#
#######################################################################
from dm.common.constants import dmStatus
from dm.common.exceptions.dmException import DmException
#######################################################################
class CommandFailed(DmException):
def __init__ (self, error='', **kwargs):
DmException.__init__(self, error, dmStatus.DM_COMMAND_FAILED_ERROR, **kwargs)
#!/usr/bin/env python
#
# Configuration error class.
#
#######################################################################
from dm.common.constants import dmStatus
from dm.common.exceptions.dmException import DmException
#######################################################################
class ConfigurationError(DmException):
def __init__ (self, error='', **kwargs):
DmException.__init__(self, error, dmStatus.DM_CONFIGURATION_ERROR, **kwargs)
#!/usr/bin/env python
#
# Base DM exception class.
#
#######################################################################
import exceptions
import json
from dm.common.constants import dmStatus
#######################################################################
class DmException(exceptions.Exception):
"""
Base DM exception class.
Usage:
DmException(errorMessage, errorCode)
DmException(args=errorMessage)
DmException(exception=exceptionObject)
"""
def __init__(self, error='', code=dmStatus.DM_ERROR, **kwargs):
args = error
if args == '':
args = kwargs.get('args', '')
ex = kwargs.get('exception', None)
if ex != None:
if isinstance(ex, exceptions.Exception):
exArgs = '%s' % (ex)
if args == '':
args = exArgs
else:
args = "%s (%s)" % (args, exArgs)
exceptions.Exception.__init__(self, args)
self._code = code
def getArgs(self):
return self.args
def getErrorCode(self):
return self._code
def getErrorMessage(self):
return '%s' % (self.args)
def getClassName(self):
return '%s' % (self.__class__.__name__)
def getExceptionType(self):
return '%s' % (self.__class__.__name__).split('.')[-1]
def getJsonRep(self):
return json.dumps({
'errorMessage' : self.getErrorMessage(),
'errorCode' : self.getErrorCode(),
'exceptionType' : self.getExceptionType(),
})
#!/usr/bin/env python
#
# DM exception map
#
#######################################################################
from dm.common.constants import dmStatus
exceptionMap = {
dmStatus.DM_ERROR : 'dmException.DmException',
dmStatus.DM_CONFIGURATION_ERROR : 'configurationError.ConfigurationError',
dmStatus.DM_INTERNAL_ERROR : 'internalError.InternalError',
dmStatus.DM_INVALID_ARGUMENT_ERROR : 'invalidArgument.InvalidArgument',
dmStatus.DM_INVALID_REQUEST_ERROR : 'invalidRequest.InvalidRequest',
dmStatus.DM_COMMAND_FAILED_ERROR : 'commandFailed.CommandFailed',
}
#######################################################################
# Testing
if __name__ == '__main__':
for item in exceptionMap.items():
print item
#!/usr/bin/env python
#
# Internal error class.
#
#######################################################################
from dm.common.constants import dmStatus
from dm.common.exceptions.dmException import DmException
#######################################################################
class InternalError(DmException):
def __init__ (self, error='', **kwargs):
DmException.__init__(self, error, dmStatus.DM_INTERNAL_ERROR, **kwargs)
#!/usr/bin/env python
#
# Invalid argument error class.
#
#######################################################################
from dm.common.constants import dmStatus
from dm.common.exceptions.dmException import DmException
#######################################################################
class InvalidArgument(DmException):
def __init__ (self, error='', **kwargs):
DmException.__init__(self, error, dmStatus.DM_INVALID_ARGUMENT_ERROR, **kwargs)
#!/usr/bin/env python
#
# Invalid request error class.
#
#######################################################################
from dm.common.constants import dmStatus
from dm.common.exceptions.dmException import DmException
#######################################################################
class InvalidRequest(DmException):
def __init__ (self, error='', **kwargs):
DmException.__init__(self, error, dmStatus.DM_INVALID_REQUEST_ERROR, **kwargs)
#!/usr/bin/env python
#
# DM Object class.
#
#######################################################################
import UserDict
import UserList
import types
import json
from dm.common.exceptions.invalidArgument import InvalidArgument
from dm.common.utility import loggingManager
class DmObject(UserDict.UserDict):
""" Base dm object class. """
def __init__(self, dict={}):
if isinstance(dict, types.DictType):
UserDict.UserDict.__init__(self, dict)
elif isinstance(dict, UserDict.UserDict):
UserDict.UserDict.__init__(self, dict.data)
else:
raise InvalidArgument('DmObject must be initialized using dictionary.')
self._jsonPreprocessKeyList = []
self._logger = None
def getLogger(self):
if not self._logger:
self._logger = loggingManager.getLogger(self.__class__.__name__)
return self._logger
@classmethod
def getFromDict(cls, dict):
inst = cls()
for key in dict.keys():
inst[key] = dict[key]
return inst
def getDictRep(self):
# Dict representation is dict
dictRep = {}
for (key,obj) in self.data.items():
if isinstance(obj, DmObject):
dictRep[key] = obj.getDictRep()
else:
if obj is not None:
dictRep[key] = obj
return dictRep
def getDictJsonPreprocessedRep(self):
dictRep = self.getDictRep()
# Convert designated keys into string values.
for key in self._jsonPreprocessKeyList:
value = dictRep.get(key)
if value is not None:
dictRep[key] = '%s' % value
return dictRep
def getJsonRep(self):
dictRep = self.getDictJsonPreprocessedRep()
return json.dumps(dictRep)
@classmethod
def fromJsonString(cls, jsonString):
return cls.getFromDict(json.loads(jsonString))
#######################################################################
# Testing.
if __name__ == '__main__':
x = {'name' : 'XYZ', 'one':1, 'two':2 }
o = DmObject(x)
print 'DM Object: ', o
print 'Type of DM object: ', type(o)
print 'JSON Rep: ', o.getJsonRep()
print 'Type of JSON rep: ', type(o.getJsonRep())
j = '{"name" : "XYZ", "one":1, "two":2 }'
print 'String: ', j
x2 = DmObject.fromJsonString(j)
print 'DM Object 2: ', x2
print 'Type of DM object 2: ', type(x2)
#!/usr/bin/env python
#
# Base object manager class.
#
#######################################################################
import threading
from dm.common.utility import loggingManager
#######################################################################
class DmObjectManager:
""" Base object manager class. """
def __init__(self):
self._logger = loggingManager.getLogger(self.__class__.__name__)
self._lock = threading.RLock()
def getLogger(self):
return self._logger
def acquireLock(self):
self._lock.acquire()
def releaseLock(self):
self._lock.release()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment