<s> from climb . config import config <EOL> from climb . paths import split_path <EOL> from grafcli . exceptions import InvalidPath , MissingHostName , MissingTemplateCategory <EOL> from grafcli . resources . remote import RemoteResources <EOL> from grafcli . resources . templates import DashboardsTemplates , RowsTemplates , PanelTemplates , CATEGORIES <EOL> from grafcli . resources . local import LocalResources <EOL> LOCAL_DIR = '<STR_LIT>' <EOL> class Resources ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _resources = { <EOL> '<STR_LIT>' : LocalResources ( LOCAL_DIR ) , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : DashboardsTemplates ( ) , <EOL> '<STR_LIT>' : RowsTemplates ( ) , <EOL> '<STR_LIT>' : PanelTemplates ( ) , <EOL> } , <EOL> } <EOL> def list ( self , path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> manager , parts = self . _parse_path ( path ) <EOL> except MissingHostName : <EOL> return [ host for host in config [ '<STR_LIT>' ] <EOL> if config . getboolean ( '<STR_LIT>' , host ) ] <EOL> except MissingTemplateCategory : <EOL> return CATEGORIES <EOL> if not manager and not parts : <EOL> return sorted ( self . _resources . keys ( ) ) <EOL> return manager . list ( * parts ) <EOL> def get ( self , path ) : <EOL> """<STR_LIT>""" <EOL> manager , parts = self . _parse_path ( path ) <EOL> if not parts : <EOL> raise InvalidPath ( "<STR_LIT>" ) <EOL> return manager . get ( * parts ) <EOL> def save ( self , path , document ) : <EOL> """<STR_LIT>""" <EOL> manager , parts = self . _parse_path ( path ) <EOL> return manager . save ( document , * parts ) <EOL> def remove ( self , path ) : <EOL> """<STR_LIT>""" <EOL> manager , parts = self . _parse_path ( path ) <EOL> if not parts : <EOL> raise InvalidPath ( "<STR_LIT>" ) <EOL> return manager . remove ( * parts ) <EOL> def _parse_path ( self , path ) : <EOL> parts = split_path ( path ) <EOL> if not parts : <EOL> return None , [ ] <EOL> resource = parts . pop ( <NUM_LIT:0> ) <EOL> if resource == '<STR_LIT>' : <EOL> if not parts : <EOL> raise MissingHostName ( "<STR_LIT>" ) <EOL> host = parts . pop ( <NUM_LIT:0> ) <EOL> if host not in self . _resources [ '<STR_LIT>' ] : <EOL> self . _resources [ '<STR_LIT>' ] [ host ] = RemoteResources ( host ) <EOL> manager = self . _resources [ '<STR_LIT>' ] [ host ] <EOL> elif resource == '<STR_LIT>' : <EOL> if not parts : <EOL> raise MissingTemplateCategory ( "<STR_LIT>" ) <EOL> category = parts . pop ( <NUM_LIT:0> ) <EOL> if category not in self . _resources [ '<STR_LIT>' ] : <EOL> raise InvalidPath ( "<STR_LIT>" . format ( category ) ) <EOL> manager = self . _resources [ '<STR_LIT>' ] [ category ] <EOL> else : <EOL> try : <EOL> manager = self . _resources [ resource ] <EOL> except KeyError : <EOL> raise InvalidPath ( "<STR_LIT>" . format ( resource ) ) <EOL> return manager , parts </s>
