root/xcap/tls.py

Revision 261, 1.6 kB (checked in by Denis Bilenko <denis@ag-projects.com>, 2 months ago)

more 'except:' clean up

Line 
1# Copyright (C) 2007 AG Projects.
2#
3
4"""TLS helper classes"""
5
6__all__ = ['Certificate', 'PrivateKey']
7
8from gnutls.crypto import X509Certificate,  X509PrivateKey
9
10from application import log
11from application.process import process
12
13class _FileError(Exception): pass
14
15def file_content(file):
16    path = process.config_file(file)
17    if path is None:
18        raise _FileError("File '%s' does not exist" % file)
19    try:
20        f = open(path, 'rt')
21    except Exception:
22        raise _FileError("File '%s' could not be open" % file)
23    try:
24        return f.read()
25    finally:
26        f.close()
27
28class Certificate(object):
29    """Configuration data type. Used to create a gnutls.crypto.X509Certificate object
30       from a file given in the configuration file."""
31    def __new__(typ, value):
32        if isinstance(value, str):
33            try:
34                return X509Certificate(file_content(value))
35            except Exception, e:
36                log.warn("Certificate file '%s' could not be loaded: %s" % (value, str(e)))
37                return None
38        else:
39            raise TypeError, 'value should be a string'
40
41class PrivateKey(object):
42    """Configuration data type. Used to create a gnutls.crypto.X509PrivateKey object
43       from a file given in the configuration file."""
44    def __new__(typ, value):
45        if isinstance(value, str):
46            try:
47                return X509PrivateKey(file_content(value))
48            except Exception, e:
49                log.warn("Private key file '%s' could not be loaded: %s" % (value, str(e)))
50                return None
51        else:
52            raise TypeError, 'value should be a string'
Note: See TracBrowser for help on using the browser.