<s> from __future__ import division <EOL> from collections import OrderedDict <EOL> from wlauto import Parameter , Instrument <EOL> from wlauto . exceptions import InstrumentError , ConfigError <EOL> from wlauto . utils . hwmon import discover_sensors <EOL> from wlauto . utils . types import list_of_strs <EOL> HWMON_SENSORS = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , lambda x : x / <NUM_LIT:10> ** <NUM_LIT:6> ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , lambda x : x / <NUM_LIT:10> ** <NUM_LIT:3> ) , <EOL> } <EOL> HWMON_SENSOR_PRIORITIES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class HwmonInstrument ( Instrument ) : <EOL> name = '<STR_LIT>' <EOL> description = """<STR_LIT>""" <EOL> parameters = [ <EOL> Parameter ( '<STR_LIT>' , kind = list_of_strs , default = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> global_alias = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) <EOL> ] <EOL> def __init__ ( self , device , ** kwargs ) : <EOL> super ( HwmonInstrument , self ) . __init__ ( device , ** kwargs ) <EOL> if self . sensors : <EOL> self . sensor_kinds = { } <EOL> for kind in self . sensors : <EOL> if kind in HWMON_SENSORS : <EOL> self . sensor_kinds [ kind ] = HWMON_SENSORS [ kind ] <EOL> else : <EOL> message = '<STR_LIT>' . format ( kind , HWMON_SENSORS . keys ( ) ) <EOL> raise ConfigError ( message ) <EOL> else : <EOL> self . sensor_kinds = HWMON_SENSORS <EOL> self . sensors = [ ] <EOL> def initialize ( self , context ) : <EOL> self . sensors = [ ] <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> discovered_sensors = discover_sensors ( self . device , self . sensor_kinds . keys ( ) ) <EOL> for sensor in sorted ( discovered_sensors , key = lambda s : HWMON_SENSOR_PRIORITIES . index ( s . kind ) ) : <EOL> self . logger . debug ( '<STR_LIT>' . format ( sensor . filepath ) ) <EOL> self . sensors . append ( sensor ) <EOL> def setup ( self , context ) : <EOL> for sensor in self . sensors : <EOL> sensor . clear_readings ( ) <EOL> def fast_start ( self , context ) : <EOL> for sensor in reversed ( self . sensors ) : <EOL> sensor . take_reading ( ) <EOL> def fast_stop ( self , context ) : <EOL> for sensor in self . sensors : <EOL> sensor . take_reading ( ) <EOL> def update_result ( self , context ) : <EOL> for sensor in self . sensors : <EOL> try : <EOL> report_type , units , conversion = HWMON_SENSORS [ sensor . kind ] <EOL> if report_type == '<STR_LIT>' : <EOL> before , after = sensor . readings <EOL> diff = conversion ( after - before ) <EOL> context . result . add_metric ( sensor . label , diff , units ) <EOL> elif report_type == '<STR_LIT>' : <EOL> before , after = sensor . readings <EOL> mean = conversion ( ( after + before ) / <NUM_LIT:2> ) <EOL> context . result . add_metric ( sensor . label , mean , units ) <EOL> context . result . add_metric ( sensor . label + '<STR_LIT>' , conversion ( before ) , units ) <EOL> context . result . add_metric ( sensor . label + '<STR_LIT>' , conversion ( after ) , units ) <EOL> else : <EOL> raise InstrumentError ( '<STR_LIT>' . format ( report_type ) ) <EOL> except ValueError , e : <EOL> self . logger . error ( '<STR_LIT>' . format ( sensor . kind , sensor . label ) ) <EOL> self . logger . error ( '<STR_LIT>' . format ( e ) ) </s>
