Revision b4600d4f lib/query.py

b/lib/query.py
1234 1234
  if not ctx.curlive_data:
1235 1235
    return _FS_NODATA
1236 1236

  
1237
  return _GetStatsField(field, kind, ctx.curlive_data)
1238

  
1239

  
1240
def _GetStatsField(field, kind, data):
1241
  """Gets a value from live statistics.
1242

  
1243
  If the value is not found, L{_FS_UNAVAIL} is returned. If the field kind is
1244
  numeric a conversion to integer is attempted. If that fails, L{_FS_UNAVAIL}
1245
  is returned.
1246

  
1247
  @param field: Live field name
1248
  @param kind: Data kind, one of L{constants.QFT_ALL}
1249
  @type data: dict
1250
  @param data: Statistics
1251

  
1252
  """
1237 1253
  try:
1238
    value = ctx.curlive_data[field]
1254
    value = data[field]
1239 1255
  except KeyError:
1240 1256
    return _FS_UNAVAIL
1241 1257

  
......
1249 1265
    return int(value)
1250 1266
  except (ValueError, TypeError):
1251 1267
    logging.exception("Failed to convert node field '%s' (value %r) to int",
1252
                      value, field)
1268
                      field, value)
1253 1269
    return _FS_UNAVAIL
1254 1270

  
1255 1271

  
......
2509 2525
  @type ctx: L{NetworkQueryData}
2510 2526

  
2511 2527
  """
2512

  
2513
  try:
2514
    value = ctx.curstats[field]
2515
  except KeyError:
2516
    return _FS_UNAVAIL
2517

  
2518
  if kind == QFT_TEXT:
2519
    return value
2520

  
2521
  assert kind in (QFT_NUMBER, QFT_UNIT)
2522

  
2523
  # Try to convert into number
2524
  try:
2525
    return int(value)
2526
  except (ValueError, TypeError):
2527
    logging.exception("Failed to convert network field '%s' (value %r) to int",
2528
                      field, value)
2529
    return _FS_UNAVAIL
2528
  return _GetStatsField(field, kind, ctx.curstats)
2530 2529

  
2531 2530

  
2532 2531
def _BuildNetworkFields():

Also available in: Unified diff