<s> import json <EOL> class Serializer ( json . JSONEncoder ) : <EOL> def default ( self , o ) : <EOL> if isinstance ( o , Serializable ) : <EOL> return o . serialize ( ) <EOL> if isinstance ( o , EnumEntry ) : <EOL> return o . name <EOL> return json . JSONEncoder . default ( self , o ) <EOL> class Serializable ( object ) : <EOL> @ classmethod <EOL> def deserialize ( cls , text ) : <EOL> return cls ( ** json . loads ( text ) ) <EOL> def serialize ( self , d = None ) : <EOL> if d is None : <EOL> d = self . __dict__ <EOL> return json . dumps ( d , cls = Serializer ) <EOL> class DaqServerRequest ( Serializable ) : <EOL> def __init__ ( self , command , params = None ) : <EOL> self . command = command <EOL> self . params = params or { } <EOL> class DaqServerResponse ( Serializable ) : <EOL> def __init__ ( self , status , message = None , data = None ) : <EOL> self . status = status <EOL> self . message = message . strip ( ) . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:U+0020>' ) if message else '<STR_LIT>' <EOL> self . data = data or { } <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . status , self . message or '<STR_LIT>' ) <EOL> class EnumEntry ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def __cmp__ ( self , other ) : <EOL> return cmp ( self . name , str ( other ) ) <EOL> class Enum ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> for a in args : <EOL> setattr ( self , a , EnumEntry ( a ) ) <EOL> def __call__ ( self , value ) : <EOL> if value not in self . __dict__ : <EOL> raise ValueError ( '<STR_LIT>' . format ( value ) ) <EOL> return self . __dict__ [ value ] <EOL> def __iter__ ( self ) : <EOL> for e in self . __dict__ : <EOL> yield self . __dict__ [ e ] <EOL> Status = Enum ( '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' ) </s>
