Esempio n. 1
0
def _create_proxy():
  '''
  Read all submodules and if config_file is defined
  add them to the ConfigProxy object that stores
  all information in .ini files. The usage in other
  modules is the same for both cases when no parameter 
  is imported dirctly from the submodule.
  '''
  global proxy, __all__
  proxy=_ConfigProxy(_config_path)
  for ignore, name, ispackage in _pkgutil.iter_modules([_package_dir]):
    if ispackage or name in ['baseconfig', 'configobj']:
      continue
    try:
      modi=__import__('quicknxs.config.'+name, fromlist=[name])
    except Exception, error:
      _warn("Could not import module %s,\n %s: %s"%(name, error.__class__.__name__, error))
      continue
    if 'config_path' in modi.__dict__:
      moddict={}
      for key, value in modi.__dict__.items():
        if key.startswith('_') or key=='config_path' or\
           hasattr(value, '__file__') or hasattr(value, '__module__') or\
           type(value).__name__=='module':
          continue
        moddict[key]=value
      config_holder=proxy.add_path_config(name, moddict, modi.config_path) #@UnusedVariable
    elif 'config_file' in modi.__dict__:
      moddict={}
      for key, value in modi.__dict__.items():
        if key.startswith('_') or key=='config_file' or\
           hasattr(value, '__file__') or hasattr(value, '__module__') or\
           type(value).__name__=='module':
          continue
        moddict[key]=value
      config_holder=proxy.add_config(name, moddict, storage=modi.config_file) #@UnusedVariable
    else:
      # if config_file is not defined in the module, just use the module itself
      config_holder=modi #@UnusedVariable
    # add item to the package * import
    __all__.append(name)
    exec "global %s;%s=config_holder"%(name, name)
Esempio n. 2
0
def voicetext(environ, start_response):
    """
    After validating the incoming request, retrieve the audio file from
    the upstream VoiceText service, check it, and return it.
    """

    remote_addr = environ.get('REMOTE_ADDR', '')
    if not remote_addr:
        _warn("Relay denied -- no remote IP address")
        start_response(_CODE_403, _HEADERS_JSON)
        return _MSG_DENIED

    if not environ.get('HTTP_USER_AGENT', '').startswith(_AWESOMETTS):
        _warn("Relay denied -- unauthorized user agent")
        start_response(_CODE_403, _HEADERS_JSON)
        return _MSG_DENIED

    if environ.get('REQUEST_METHOD') != 'GET':
        _warn("Relay denied -- unacceptable request method")
        start_response(_CODE_405, _HEADERS_JSON)
        return _MSG_UNACCEPTABLE

    data = environ.get('QUERY_STRING')

    # do a very rough sanity check without generating a bunch of junk objects;
    # remember that most Japanese characters encode to 9-byte strings and we
    # allow up to 100 Japanese characters (or 900 bytes) in the client
    if not (data and len(data) < 1000 and data.count('&') > 4
            and data.count('=') < 8 and 'format=' in data and 'format=wav'
            not in data and 'pitch=' in data and 'speaker=' in data
            and 'speed=' in data and 'text=' in data and 'volume=' in data):
        _warn("Relay denied -- unacceptable query string")
        start_response(_CODE_400, _HEADERS_JSON)
        return _MSG_UNACCEPTABLE

    # apply rate-limiting
    with _limit_lock:
        now = int(_time())

        # remove expired entries
        for level in _limit_levels:
            expired = now - level.within
            lookup = level.lookup
            for addr, info in lookup.items():
                if info['created'] < expired:
                    del lookup[addr]

        # check maximum levels
        for level in _limit_levels:
            lookup = level.lookup
            try:
                info = lookup[remote_addr]
            except KeyError:
                total = len(lookup)
                if total >= level.max_total:
                    _warn(
                        "Relay denied -- already have %d total callers "
                        "within the last %d seconds", total, level.within)
                    start_response(_CODE_503, _HEADERS_JSON)
                    return _MSG_CAPACITY
            else:
                calls = info['calls']
                if calls >= level.max_single:
                    _warn(
                        "Relay denied -- already had %d individual calls "
                        "from %s within the last %d seconds", calls,
                        remote_addr, level.within)
                    start_response(_CODE_429, _HEADERS_JSON)
                    return _MSG_TOO_MANY

        # caller is good to go; update their call counts
        summaries = []
        for level in _limit_levels:
            lookup = level.lookup
            try:
                info = lookup[remote_addr]
            except KeyError:
                lookup[remote_addr] = info = {'created': now, 'calls': 1}
            else:
                info['calls'] += 1
            summaries.append("%d-second window has %d/%d individual calls for "
                             "%s and %d/%d total unique callers" %
                             (level.within, info['calls'], level.max_single,
                              remote_addr, len(lookup), level.max_total))
        _info("Relay accepted -- %s", "; ".join(summaries))

    try:
        response = _url_open(_Request(_API_VOICETEXT_ENDPOINT, data,
                                      _API_VOICETEXT_AUTH),
                             timeout=_API_VOICETEXT_TIMEOUT)

        if response.getcode() != 200:
            raise IOError("non-200 status code from upstream service")

        mime = response.info().gettype()
        if not mime.startswith('audio/'):
            raise IOError("non-audio format from upstream service")

        payload = [response.read()]

    except Exception as exception:  # catch all, pylint:disable=broad-except
        _error("Relay failed -- %s", exception)
        start_response(_CODE_502, _HEADERS_JSON)
        return _MSG_UPSTREAM

    else:
        start_response(_CODE_200, [('Content-Type', mime)])
        return payload

    finally:
        try:
            response.close()
        except Exception:  # catch all, pylint:disable=broad-except
            pass
def _warn_(msg):
	print '-'*50
	print 'Do something before invoke info()'
	h = _warn(msg)
	print '-'*50
	return h
def _warn_(msg):
    print '-' * 50
    print 'Do something before invoke info()'
    h = _warn(msg)
    print '-' * 50
    return h
Esempio n. 5
0
 async def remove(self):
     args = [self.getValue(self.__primary_key__)]
     rows = await execute(self.__delete__, args)
     if rows != 1:
         logging._warn('failed to remove by primary key: affected rows:%s' % rows)
Esempio n. 6
0
 async def update(self):
     args = list(map(self.getValue, self.__field__))
     args.append(self.getValue(self.__primary_key__))
     rows = await execute(self.__update__, args)
     if rows != 1:
         logging._warn('failed to update by primary key: affected rows:%s' % rows)
Esempio n. 7
0
 async def save(self):
     args = list(map(self.getValueOrDefault, self.__fields__))
     args.append(self.getValueOrDefault(self.__primary_key__))
     rows = await execute(self.__insert__, args)
     if rows != 1:
         logging._warn('failed to insert record: affected rows:%s' % rows)