<s> import json <EOL> import importlib <EOL> import site <EOL> from openaps . configurable import Configurable <EOL> class ExtraConfig ( Configurable ) : <EOL> prefix = '<STR_LIT>' <EOL> pass <EOL> class Device ( Configurable ) : <EOL> vendor = None <EOL> required = [ '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> optional = [ ] <EOL> prefix = '<STR_LIT>' <EOL> _uses = [ ] <EOL> def __init__ ( self , name , vendor ) : <EOL> self . name = name <EOL> self . vendor = vendor <EOL> self . fields = dict ( vendor = vendor . __name__ ) <EOL> self . extra = ExtraConfig ( name ) <EOL> def read ( self , args = None , config = None ) : <EOL> if args : <EOL> self . name = args . name <EOL> if getattr ( args , '<STR_LIT>' , None ) : <EOL> self . fields [ '<STR_LIT>' ] = args . extra . format ( name = self . name , ** self . fields ) <EOL> self . vendor . set_config ( args , self . extra ) <EOL> else : <EOL> self . vendor . set_config ( args , self ) <EOL> if config : <EOL> self . fields . update ( dict ( config . items ( self . section_name ( ) ) ) ) <EOL> extra_ini = self . fields . get ( '<STR_LIT>' , None ) <EOL> if extra_ini : <EOL> extra = config . Read ( name = extra_ini ) <EOL> self . extra . fields . update ( dict ( extra . items ( self . section_name ( ) ) ) ) <EOL> def get ( self , k , * args ) : <EOL> return self . fields . get ( k , self . extra . get ( k , * args ) ) <EOL> def format_url ( self ) : <EOL> parts = [ '<STR_LIT>' . format ( self . vendor . __name__ . split ( '<STR_LIT:.>' ) . pop ( ) , self . name ) , ] <EOL> parts . append ( self . vendor . display_device ( self ) ) <EOL> return '<STR_LIT>' . join ( parts ) <EOL> def register_uses ( self , uses ) : <EOL> for u in uses . usages : <EOL> if u not in self . _uses : <EOL> self . _uses . append ( u ) <EOL> def store ( self , config ) : <EOL> extra_ini = self . fields . get ( '<STR_LIT>' , None ) <EOL> if extra_ini : <EOL> extra = config . Read ( name = extra_ini ) <EOL> self . extra . store ( extra ) <EOL> extra . save ( ) <EOL> super ( Device , self ) . store ( config ) <EOL> @ classmethod <EOL> def FromImport ( klass , candidate , config = None ) : <EOL> name = candidate . get ( '<STR_LIT:name>' ) <EOL> fields = candidate . get ( name ) <EOL> from openaps import vendors <EOL> vendor = vendors . lookup_dotted ( fields [ '<STR_LIT>' ] , config ) <EOL> inst = klass ( name , vendor ) <EOL> inst . fields = fields <EOL> inst . extra . fields = candidate [ '<STR_LIT>' ] <EOL> return inst <EOL> @ classmethod <EOL> def FromConfig ( klass , vendors , config ) : <EOL> devices = [ ] <EOL> for candidate in config . sections ( ) : <EOL> if candidate . startswith ( klass . prefix ) : <EOL> name = json . loads ( candidate . split ( '<STR_LIT:U+0020>' ) . pop ( ) ) <EOL> vendor = vendors . lookup ( config . get ( candidate , '<STR_LIT>' ) . split ( '<STR_LIT:.>' ) . pop ( ) , config ) <EOL> device = klass ( name , vendor ) <EOL> device . read ( config = config ) <EOL> devices . append ( device ) <EOL> return devices </s>
