import bottle, jsonrpc, sys

def with_rpc(orig_func):
    '''Function decorator to provide RPC service proxy'''
    def wrapped_func(*arg, **kwarg):
        app = bottle.default_app()
        svc = app.config['coinrpc.svc']
        return orig_func(svc, *arg, **kwarg)
    return wrapped_func
    
@bottle.get('/help')
@with_rpc
def help(rpc):
    hdoc = rpc.help()
    return hdoc.replace('\n', '<br>')

if __name__ == '__main__':
    app = bottle.default_app()
    try:
        conf_file = sys.argv[1]
    except IndexError:
        conf_file = 'coin.conf'

    config = app.config.load_config(conf_file)

    url = 'http://%s:%s@%s:%s' % (
        config['coinrpc.user'],
        config['coinrpc.pass'],
        config['coinrpc.host'],
        config['coinrpc.port'],
    )

    svc = jsonrpc.ServiceProxy(url)
    config['coinrpc.svc'] = svc

    app.run(**config)

import bottle, jsonrpc, sys

def with_coinrpc(*items):
    '''Function decorator to provide coinrpc config items'''
    def wrap_func(orig_func):
        app = bottle.default_app()
        keys = tuple(['coinrpc.' + i for i in items])
        def wrapped_func(*arg, **kwarg):
            config_items = tuple([app.config[k] for k in keys])
            arg = config_items + arg
            return orig_func(*arg, **kwarg)
        return wrapped_func
    return wrap_func
    
@bottle.get('/help')
@with_coinrpc('svc')
def help(svc):
    hdoc = svc.help()
    return hdoc.replace('\n', '<br>')

if __name__ == '__main__':
    app = bottle.default_app()
    try:
        conf_file = sys.argv[1]
    except IndexError:
        conf_file = 'coin.conf'

    config = app.config.load_config(conf_file)

    url = 'http://%s:%s@%s:%s' % (
        config['coinrpc.user'],
        config['coinrpc.pass'],
        config['coinrpc.host'],
        config['coinrpc.port'],
    )

    svc = jsonrpc.ServiceProxy(url)
    config['coinrpc.svc'] = svc

    app.run(**config)

Make config-helper decorator more generic
Make config-helper decorator more generic

Instead of only pulling 'coinrpc.svc' from app.config, pull out any
number of items.

  import bottle, jsonrpc, sys
  
- def with_rpc(orig_func):
-     '''Function decorator to provide RPC service proxy'''
-     def wrapped_func(*arg, **kwarg):
+ def with_coinrpc(*items):
+     '''Function decorator to provide coinrpc config items'''
+     def wrap_func(orig_func):
          app = bottle.default_app()
-         svc = app.config['coinrpc.svc']
+         keys = tuple(['coinrpc.' + i for i in items])
+         def wrapped_func(*arg, **kwarg):
+             config_items = tuple([app.config[k] for k in keys])
+             arg = config_items + arg
-         return orig_func(svc, *arg, **kwarg)
+             return orig_func(*arg, **kwarg)
+         return wrapped_func
-     return wrapped_func
+     return wrap_func
      
  @bottle.get('/help')
- @with_rpc
+ @with_coinrpc('svc')
- def help(rpc):
+ def help(svc):
-     hdoc = rpc.help()
+     hdoc = svc.help()
      return hdoc.replace('\n', '<br>')
  
  if __name__ == '__main__':
      app = bottle.default_app()
      try:
          conf_file = sys.argv[1]
      except IndexError:
          conf_file = 'coin.conf'
  
      config = app.config.load_config(conf_file)
  
      url = 'http://%s:%s@%s:%s' % (
          config['coinrpc.user'],
          config['coinrpc.pass'],
          config['coinrpc.host'],
          config['coinrpc.port'],
      )
  
      svc = jsonrpc.ServiceProxy(url)
      config['coinrpc.svc'] = svc
  
      app.run(**config)
  
Make config-helper decorator more generic
## Code Before:
import bottle, jsonrpc, sys

def with_rpc(orig_func):
    '''Function decorator to provide RPC service proxy'''
    def wrapped_func(*arg, **kwarg):
        app = bottle.default_app()
        svc = app.config['coinrpc.svc']
        return orig_func(svc, *arg, **kwarg)
    return wrapped_func
    
@bottle.get('/help')
@with_rpc
def help(rpc):
    hdoc = rpc.help()
    return hdoc.replace('\n', '<br>')

if __name__ == '__main__':
    app = bottle.default_app()
    try:
        conf_file = sys.argv[1]
    except IndexError:
        conf_file = 'coin.conf'

    config = app.config.load_config(conf_file)

    url = 'http://%s:%s@%s:%s' % (
        config['coinrpc.user'],
        config['coinrpc.pass'],
        config['coinrpc.host'],
        config['coinrpc.port'],
    )

    svc = jsonrpc.ServiceProxy(url)
    config['coinrpc.svc'] = svc

    app.run(**config)

## Instruction:
Make config-helper decorator more generic
## Code After:
import bottle, jsonrpc, sys

def with_coinrpc(*items):
    '''Function decorator to provide coinrpc config items'''
    def wrap_func(orig_func):
        app = bottle.default_app()
        keys = tuple(['coinrpc.' + i for i in items])
        def wrapped_func(*arg, **kwarg):
            config_items = tuple([app.config[k] for k in keys])
            arg = config_items + arg
            return orig_func(*arg, **kwarg)
        return wrapped_func
    return wrap_func
    
@bottle.get('/help')
@with_coinrpc('svc')
def help(svc):
    hdoc = svc.help()
    return hdoc.replace('\n', '<br>')

if __name__ == '__main__':
    app = bottle.default_app()
    try:
        conf_file = sys.argv[1]
    except IndexError:
        conf_file = 'coin.conf'

    config = app.config.load_config(conf_file)

    url = 'http://%s:%s@%s:%s' % (
        config['coinrpc.user'],
        config['coinrpc.pass'],
        config['coinrpc.host'],
        config['coinrpc.port'],
    )

    svc = jsonrpc.ServiceProxy(url)
    config['coinrpc.svc'] = svc

    app.run(**config)
