/usr/lib/python2.7/dist-packages/pyptlib/config.py is in python-pyptlib 0.0.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Parts of pyptlib that are useful both to clients and servers.
"""
import os
SUPPORTED_TRANSPORT_VERSIONS = ['1']
def env_has_k(k, v):
"""
Default validator for :func:`get_env`.
:returns: str -- The value of the envvar `k` if it is set.
:raises: :class:`ValueError` if `k` was not found.
"""
if v is None: raise ValueError('Missing environment variable %s' % k)
return v
class Config(object):
"""
pyptlib's configuration.
:var string stateLocation: Location where application should store state.
:var list managedTransportVer: List of managed-proxy protocol versions that Tor supports.
:var list transports: Strings of pluggable transport names that Tor wants us to handle.
:var bool allTransportsEnabled: True if Tor wants us to spawn all the transports.
"""
def __init__(self, stateLocation,
managedTransportVer=None,
transports=None):
self.stateLocation = stateLocation
self.managedTransportVer = managedTransportVer or SUPPORTED_TRANSPORT_VERSIONS
transports = transports or []
self.allTransportsEnabled = False
if '*' in transports:
self.allTransportsEnabled = True
transports.remove('*')
self.transports = transports
def getStateLocation(self):
"""
:returns: string -- The state location.
"""
return self.stateLocation
def getManagedTransportVersions(self):
"""
:returns: list -- The managed-proxy protocol versions that Tor supports.
"""
return self.managedTransportVer
def getAllTransportsEnabled(self):
"""
Check if Tor wants the application to spawn all its transpotrs.
:returns: bool -- True if Tor wants the application to spawn all its transports.
"""
return self.allTransportsEnabled
def get_env(key, validate=env_has_k):
"""
Get the value of an environment variable.
:param str key: Environment variable key.
:param f validate: Function that takes a `var` and a `value`, and returns
a (maybe transformed) value if it is valid, or throws an exception.
If the environment does not set `var`, `value` is passed in as `None`.
The default validator is :func:`env_has_k` which passes any value
which is set (i.e. not `None`).
:returns: str -- The value of the envrionment variable.
:raises: :class:`pyptlib.config.EnvError` if environment variable could not be
found, or if it did not pass validation.
"""
try:
return validate(key, os.getenv(key))
except ProxyError:
raise
except Exception, e:
raise EnvError("error parsing env-var: %s: %s" % (key, e), e)
class ProxyError(Exception):
"""
Thrown when the proxy specifier is incomplete or corrupted.
"""
def __init__(self, message=None, cause=None):
self.message = message
self.cause = cause
def __str__(self):
return self.message or self.cause.message
class EnvError(Exception):
"""
Thrown when the environment is incomplete or corrupted.
"""
def __init__(self, message=None, cause=None):
self.message = message
self.cause = cause
def __str__(self):
return self.message or self.cause.message
def checkClientMode():
"""
Read the environment and return true if we are supposed to be a
client. Return false if we are supposed to be a server.
:raises: :class:`pyptlib.config.EnvError` if the environment was not
properly set up
"""
if 'TOR_PT_CLIENT_TRANSPORTS' in os.environ: return True
if 'TOR_PT_SERVER_TRANSPORTS' in os.environ: return False
raise EnvError('neither TOR_PT_{SERVER,CLIENT}_TRANSPORTS set')
|