<s> """<STR_LIT>""" <EOL> from desktop import _is_x11 , _get_x11_vars , _readfrom , use_desktop <EOL> import re <EOL> def _xwininfo ( identifier , action ) : <EOL> if identifier is None : <EOL> args = "<STR_LIT>" <EOL> else : <EOL> args = "<STR_LIT>" + identifier <EOL> s = _readfrom ( _get_x11_vars ( ) + "<STR_LIT>" % ( args , action ) , shell = <NUM_LIT:1> ) <EOL> if action == "<STR_LIT>" : <EOL> d = { } <EOL> for line in s . split ( "<STR_LIT:\n>" ) : <EOL> fields = line . split ( "<STR_LIT::>" ) <EOL> if len ( fields ) < <NUM_LIT:2> : <EOL> continue <EOL> key , value = fields [ <NUM_LIT:0> ] . strip ( ) , "<STR_LIT::>" . join ( fields [ <NUM_LIT:1> : ] ) . strip ( ) <EOL> d [ key ] = value <EOL> return d <EOL> else : <EOL> return s <EOL> def _get_int_properties ( d , properties ) : <EOL> results = [ ] <EOL> for property in properties : <EOL> results . append ( int ( d [ property ] ) ) <EOL> return results <EOL> def find_all ( name ) : <EOL> return <NUM_LIT:1> <EOL> def find_named ( name ) : <EOL> return name is not None <EOL> def find_by_name ( name ) : <EOL> return lambda n , t = name : n == t <EOL> class Window : <EOL> "<STR_LIT>" <EOL> _name_pattern = re . compile ( r'<STR_LIT>' ) <EOL> _absent_names = "<STR_LIT>" , "<STR_LIT>" <EOL> def __init__ ( self , identifier ) : <EOL> "<STR_LIT>" <EOL> self . identifier = identifier <EOL> self . find_all = find_all <EOL> self . find_named = find_named <EOL> self . find_by_name = find_by_name <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . identifier <EOL> def _get_handle_and_name ( self , text ) : <EOL> fields = text . strip ( ) . split ( "<STR_LIT:U+0020>" ) <EOL> handle = fields [ <NUM_LIT:0> ] <EOL> name = "<STR_LIT:U+0020>" . join ( fields [ <NUM_LIT:1> : ] ) <EOL> if len ( name ) > <NUM_LIT:1> and name [ <NUM_LIT:0> ] == '<STR_LIT:">' and name [ - <NUM_LIT:1> ] == '<STR_LIT:">' : <EOL> name = name [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> if name in self . _absent_names : <EOL> return handle , None <EOL> else : <EOL> return handle , name <EOL> def _get_this_handle_and_name ( self , line ) : <EOL> fields = line . split ( "<STR_LIT::>" ) <EOL> return self . _get_handle_and_name ( "<STR_LIT::>" . join ( fields [ <NUM_LIT:1> : ] ) ) <EOL> def _get_descendant_handle_and_name ( self , line ) : <EOL> match = self . _name_pattern . search ( line ) <EOL> if match : <EOL> return self . _get_handle_and_name ( line [ : match . start ( ) ] . strip ( ) ) <EOL> else : <EOL> raise OSError , "<STR_LIT>" % line <EOL> def _descendants ( self , s , fn ) : <EOL> handles = [ ] <EOL> adding = <NUM_LIT:0> <EOL> for line in s . split ( "<STR_LIT:\n>" ) : <EOL> if line . endswith ( "<STR_LIT>" ) or line . endswith ( "<STR_LIT>" ) : <EOL> if not adding : <EOL> adding = <NUM_LIT:1> <EOL> elif adding and line : <EOL> handle , name = self . _get_descendant_handle_and_name ( line ) <EOL> if fn ( name ) : <EOL> handles . append ( handle ) <EOL> return [ Window ( handle ) for handle in handles ] <EOL> def children ( self , all = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> s = _xwininfo ( self . identifier , "<STR_LIT>" ) <EOL> return self . _descendants ( s , all and self . find_all or self . find_named ) <EOL> def descendants ( self , all = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> s = _xwininfo ( self . identifier , "<STR_LIT>" ) <EOL> return self . _descendants ( s , all and self . find_all or self . find_named ) <EOL> def find ( self , callable ) : <EOL> """<STR_LIT>""" <EOL> s = _xwininfo ( self . identifier , "<STR_LIT>" ) <EOL> return self . _descendants ( s , callable ) <EOL> def name ( self ) : <EOL> "<STR_LIT>" <EOL> d = _xwininfo ( self . identifier , "<STR_LIT>" ) <EOL> return self . _get_this_handle_and_name ( d [ "<STR_LIT>" ] ) [ <NUM_LIT:1> ] <EOL> def size ( self ) : <EOL> "<STR_LIT>" <EOL> d = _xwininfo ( self . identifier , "<STR_LIT>" ) <EOL> return _get_int_properties ( d , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def position ( self ) : <EOL> "<STR_LIT>" <EOL> d = _xwininfo ( self . identifier , "<STR_LIT>" ) <EOL> return _get_int_properties ( d , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def displayed ( self ) : <EOL> """<STR_LIT>""" <EOL> d = _xwininfo ( self . identifier , "<STR_LIT>" ) <EOL> return d [ "<STR_LIT>" ] != "<STR_LIT>" <EOL> def visible ( self ) : <EOL> "<STR_LIT>" <EOL> d = _xwininfo ( self . identifier , "<STR_LIT>" ) <EOL> return d [ "<STR_LIT>" ] == "<STR_LIT>" <EOL> def list ( desktop = None ) : <EOL> """<STR_LIT>""" <EOL> root_window = root ( desktop ) <EOL> window_list = [ window for window in root_window . descendants ( ) if window . displayed ( ) ] <EOL> window_list . insert ( <NUM_LIT:0> , root_window ) <EOL> return window_list <EOL> def root ( desktop = None ) : <EOL> """<STR_LIT>""" <EOL> if _is_x11 ( ) : <EOL> return Window ( None ) <EOL> else : <EOL> raise OSError , "<STR_LIT>" % use_desktop ( desktop ) <EOL> def find ( callable , desktop = None ) : <EOL> """<STR_LIT>""" <EOL> return root ( desktop ) . find ( callable ) </s>
