Source

tracko / app / settings.py

Full commit
import models

meta = {
  # name -> (label, default, parser-func)
  'recommended_announce_interval': (
    'Client\'s recommended announce interval, sec.', 60, int),
  'min_peer_expiration_period': (
    'Lower bound of peer\'s expiration period, sec.', 300, int),
  'n_max_peers_to_return': (
    'Maximum number of peers to return', 200, int),
  'n_default_peers_to_return': (
    'Default number of peers to return', 50, int),
  'ga_token': (
    'Google Analytics tracking token', '', str),}

def get_value(name):
  setting_meta = meta.get(name)
  if setting_meta is None:
    raise KeyError('No such setting: %s' % (name,))
  settings = models.AppSettings.get_or_insert(key_name='singleton')
  return getattr(settings, name, setting_meta[1])

def get_values(*names):
  result = []
  settings = models.AppSettings.get_or_insert(key_name='singleton')
  for name in names:
    setting_meta = meta.get(name)
    if setting_meta is None:
      raise KeyError('No such setting: %s' % (name,))
    result.append(getattr(settings, name, setting_meta[1]))
  return result

def set_values(**names_and_values):
  settings = models.AppSettings.get_or_insert(key_name='singleton')
  for name, value in names_and_values.iteritems():
    setting_meta = meta.get(name)
    if setting_meta is None:
      raise KeyError('No such setting: %s' % (name,))
    setattr(settings, name, value)
  settings.put()