<s> import log <EOL> import sys <EOL> import subprocess <EOL> import os . path <EOL> import glob <EOL> from importlib import import_module <EOL> class WallpaperSetter : <EOL> def __init__ ( self ) : <EOL> self . _logger = log . getChild ( self . __class__ . __name__ ) <EOL> def set ( self , path , args ) : <EOL> raise NotImplementedError ( ) <EOL> class ShellWallpaperSetter ( WallpaperSetter ) : <EOL> TIMEOUT_SEC = <NUM_LIT:5> <EOL> def _cmd ( self , path , args ) : <EOL> raise NotImplementedError ( ) <EOL> def _cb ( self , status , out , err , ex ) : <EOL> self . _logger . debug ( '<STR_LIT>' , <EOL> str ( status ) , repr ( out ) , repr ( err ) ) <EOL> if ex : <EOL> self . _logger . exception ( ex ) <EOL> return status == <NUM_LIT:0> and not ex <EOL> def set ( self , path , args ) : <EOL> p = None <EOL> cmd = self . _cmd ( path , args ) <EOL> try : <EOL> self . _logger . debug ( '<STR_LIT>' , cmd ) <EOL> p = subprocess . Popen ( cmd , <EOL> stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> out , err = p . communicate ( timeout = self . TIMEOUT_SEC ) <EOL> except Exception as ex : <EOL> if p : <EOL> p . kill ( ) <EOL> return self . _cb ( p . poll ( ) , p . stdout . read ( ) , p . stderr . read ( ) , ex ) <EOL> else : <EOL> return self . _cb ( None , None , None , ex ) <EOL> else : <EOL> return self . _cb ( p . poll ( ) , out , err , None ) <EOL> class Gnome2Setter ( ShellWallpaperSetter ) : <EOL> def _cmd ( self , path , args ) : <EOL> return [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> '<STR_LIT>' . format ( path ) ] <EOL> class Gnome3Setter ( ShellWallpaperSetter ) : <EOL> def _cmd ( self , path , args ) : <EOL> return [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> '<STR_LIT>' . format ( path ) ] <EOL> class WallpaperSetterFactory : <EOL> def __init__ ( self , name ) : <EOL> self . registered = dict ( ) <EOL> self . name = name <EOL> def register ( self , name , c ) : <EOL> if name in self . registered and c is not self . registered [ name ] : <EOL> raise NameError ( <EOL> '<STR_LIT>' . format ( name , self . registered [ name ] ) ) <EOL> self . registered [ name ] = c <EOL> def get ( self , name ) : <EOL> if name not in self . registered : <EOL> raise NameError ( <EOL> '<STR_LIT>' . format ( name ) ) <EOL> return self . registered [ name ] <EOL> def load_ext_setters ( path ) : <EOL> logger = log . getChild ( '<STR_LIT>' ) <EOL> for i in glob . iglob ( os . path . join ( path , '<STR_LIT>' ) ) : <EOL> if os . path . basename ( i ) == '<STR_LIT>' : continue <EOL> logger . debug ( '<STR_LIT>' , i ) <EOL> name , ext = os . path . splitext ( os . path . basename ( i ) ) <EOL> try : <EOL> logger . debug ( '<STR_LIT>' , name ) <EOL> import_module ( name ) <EOL> except : <EOL> logger . warning ( '<STR_LIT>' , name , exc_info = <NUM_LIT:1> ) <EOL> _default_wallpaper_factory = WallpaperSetterFactory ( '<STR_LIT:default>' ) <EOL> register = _default_wallpaper_factory . register <EOL> get = _default_wallpaper_factory . get <EOL> if sys . platform == '<STR_LIT>' : <EOL> register ( '<STR_LIT>' , Gnome2Setter ) <EOL> register ( '<STR_LIT>' , Gnome3Setter ) </s>
