<s> r"""<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import os , sys <EOL> import binascii <EOL> from win32com import storagecon <EOL> from win32com . shell import shell , shellcon <EOL> from win32com import storagecon <EOL> import win32api <EOL> import pythoncom <EOL> import pywintypes <EOL> from winsys import core , constants , exc , fs , utils <EOL> CSIDL = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = shellcon ) <EOL> STGM = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = storagecon ) <EOL> STGFMT = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = storagecon ) <EOL> FMTID = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = pythoncom ) <EOL> FMTID . update ( constants . Constants . from_pattern ( "<STR_LIT>" , namespace = shell ) ) <EOL> PIDSI = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = storagecon ) <EOL> PIDDSI = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = storagecon ) <EOL> PIDMSI = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = shellcon ) <EOL> PIDASI = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = shellcon ) <EOL> PID_VOLUME = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = shellcon ) <EOL> SHCONTF = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = shellcon ) <EOL> SHGDN = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = shellcon ) <EOL> SLGP = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = shell ) <EOL> SFGAO = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = shellcon ) <EOL> PROPERTIES = { <EOL> FMTID . SummaryInformation : PIDSI , <EOL> FMTID . DocSummaryInformation : PIDDSI , <EOL> FMTID . MediaFileSummaryInformation : PIDMSI , <EOL> FMTID . AudioSummaryInformation : PIDASI , <EOL> FMTID . Volume : PID_VOLUME , <EOL> } <EOL> class x_shell ( exc . x_winsys ) : <EOL> pass <EOL> class x_not_a_shortcut ( x_shell ) : <EOL> pass <EOL> WINERROR_MAP = { <EOL> } <EOL> wrapped = exc . wrapper ( WINERROR_MAP , x_shell ) <EOL> def _rpidl ( parent , child ) : <EOL> l = len ( parent ) <EOL> if child [ : l ] != parent : <EOL> raise x_shell ( "<STR_LIT>" % ( parent , child ) ) <EOL> else : <EOL> return child [ l : ] <EOL> _desktop = shell . SHGetDesktopFolder ( ) <EOL> PyIShellFolder = type ( _desktop ) <EOL> def get_path ( folder_id ) : <EOL> return fs . entry ( shell . SHGetPathFromIDList ( shell . SHGetSpecialFolderLocation ( <NUM_LIT:0> , folder_id ) ) ) <EOL> def desktop_folder ( common = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> return get_path ( ( shellcon . CSIDL_DESKTOP , shellcon . CSIDL_COMMON_DESKTOPDIRECTORY ) [ common ] ) <EOL> def special_folder ( folder_id ) : <EOL> return shell . SHGetSpecialFolderPath ( None , CSIDL . constant ( folder_id ) , <NUM_LIT:0> ) <EOL> def application_data ( common = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> return get_path ( ( CSIDL . APPDATA , CSIDL . COMMON_APPDATA ) [ common ] ) <EOL> def favourites ( common = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> return get_path ( ( CSIDL . FAVORITES , CSIDL . COMMON_FAVORITES ) [ common ] ) <EOL> bookmarks = favourites <EOL> def start_menu ( common = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> return get_path ( ( CSIDL . STARTMENU , CSIDL . COMMON_STARTMENU ) [ common ] ) <EOL> def programs ( common = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> return get_path ( ( CSIDL . PROGRAMS , CSIDL . COMMON_PROGRAMS ) [ common ] ) <EOL> def startup ( common = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> return get_path ( ( shellcon . CSIDL_STARTUP , shellcon . CSIDL_COMMON_STARTUP ) [ common ] ) <EOL> def personal_folder ( ) : <EOL> "<STR_LIT>" <EOL> return get_path ( shellcon . CSIDL_PERSONAL ) <EOL> my_documents = personal_folder <EOL> def recent ( ) : <EOL> "<STR_LIT>" <EOL> return get_path ( shellcon . CSIDL_RECENT ) <EOL> def sendto ( ) : <EOL> "<STR_LIT>" <EOL> return get_path ( shellcon . CSIDL_SENDTO ) <EOL> def _file_operation ( <EOL> operation , <EOL> source_path , <EOL> target_path = None , <EOL> allow_undo = True , <EOL> no_confirm = False , <EOL> rename_on_collision = True , <EOL> silent = False , <EOL> hWnd = None <EOL> ) : <EOL> source_path = source_path or "<STR_LIT>" <EOL> if isinstance ( source_path , basestring ) : <EOL> source_path = os . path . abspath ( source_path ) <EOL> else : <EOL> source_path = [ os . path . abspath ( i ) for i in source_path ] <EOL> target_path = target_path or "<STR_LIT>" <EOL> if isinstance ( target_path , basestring ) : <EOL> target_path = os . path . abspath ( target_path ) <EOL> else : <EOL> target_path = [ os . path . abspath ( i ) for i in target_path ] <EOL> flags = <NUM_LIT:0> <EOL> if allow_undo : flags |= shellcon . FOF_ALLOWUNDO <EOL> if no_confirm : flags |= shellcon . FOF_NOCONFIRMATION <EOL> if rename_on_collision : flags |= shellcon . FOF_RENAMEONCOLLISION <EOL> if silent : flags |= shellcon . FOF_SILENT <EOL> result , n_aborted = shell . SHFileOperation ( <EOL> ( hWnd or <NUM_LIT:0> , operation , source_path , target_path , flags , None , None ) <EOL> ) <EOL> if result != <NUM_LIT:0> : <EOL> raise x_winshell ( result ) <EOL> elif n_aborted : <EOL> raise x_winshell ( "<STR_LIT>" % n_aborted ) <EOL> def copy_file ( <EOL> source_path , <EOL> target_path , <EOL> allow_undo = True , <EOL> no_confirm = False , <EOL> rename_on_collision = True , <EOL> silent = False , <EOL> hWnd = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> _file_operation ( <EOL> shellcon . FO_COPY , <EOL> source_path , <EOL> target_path , <EOL> allow_undo , <EOL> no_confirm , <EOL> rename_on_collision , <EOL> silent , <EOL> hWnd <EOL> ) <EOL> def move_file ( <EOL> source_path , <EOL> target_path , <EOL> allow_undo = True , <EOL> no_confirm = False , <EOL> rename_on_collision = True , <EOL> silent = False , <EOL> hWnd = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> _file_operation ( <EOL> shellcon . FO_MOVE , <EOL> source_path , <EOL> target_path , <EOL> allow_undo , <EOL> no_confirm , <EOL> rename_on_collision , <EOL> silent , <EOL> hWnd <EOL> ) <EOL> def rename_file ( <EOL> source_path , <EOL> target_path , <EOL> allow_undo = True , <EOL> no_confirm = False , <EOL> rename_on_collision = True , <EOL> silent = False , <EOL> hWnd = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> _file_operation ( <EOL> shellcon . FO_RENAME , <EOL> source_path , <EOL> target_path , <EOL> allow_undo , <EOL> no_confirm , <EOL> rename_on_collision , <EOL> silent , <EOL> hWnd <EOL> ) <EOL> def delete_file ( <EOL> source_path , <EOL> allow_undo = True , <EOL> no_confirm = False , <EOL> rename_on_collision = True , <EOL> silent = False , <EOL> hWnd = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> _file_operation ( <EOL> shellcon . FO_DELETE , <EOL> source_path , <EOL> None , <EOL> allow_undo , <EOL> no_confirm , <EOL> rename_on_collision , <EOL> silent , <EOL> hWnd <EOL> ) <EOL> class Shortcut ( core . _WinSysObject ) : <EOL> def __init__ ( self , filepath = core . UNSET , ** kwargs ) : <EOL> self . _shell_link = wrapped ( <EOL> pythoncom . CoCreateInstance , <EOL> shell . CLSID_ShellLink , <EOL> None , <EOL> pythoncom . CLSCTX_INPROC_SERVER , <EOL> shell . IID_IShellLink <EOL> ) <EOL> self . filepath = filepath <EOL> if self . filepath and os . path . exists ( self . filepath ) : <EOL> wrapped ( <EOL> self . _shell_link . QueryInterface , <EOL> pythoncom . IID_IPersistFile <EOL> ) . Load ( <EOL> self . filepath <EOL> ) <EOL> for k , v in kwargs . iteritems ( ) : <EOL> setattr ( self , k , v ) <EOL> def as_string ( self ) : <EOL> return ( "<STR_LIT>" % self . path ) or "<STR_LIT>" <EOL> def dumped ( self , level = <NUM_LIT:0> ) : <EOL> output = [ ] <EOL> output . append ( self . as_string ( ) ) <EOL> output . append ( "<STR_LIT>" ) <EOL> for attribute in [ "<STR_LIT>" , "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:path>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> output . append ( "<STR_LIT>" % ( attribute , getattr ( self , attribute ) ) ) <EOL> return utils . dumped ( "<STR_LIT:\n>" . join ( output ) , level ) <EOL> @ classmethod <EOL> def from_lnk ( cls , lnk_filepath ) : <EOL> return cls ( lnk_filepath ) <EOL> @ classmethod <EOL> def from_target ( cls , target_filepath , lnk_filepath = core . UNSET , ** kwargs ) : <EOL> target_filepath = os . path . abspath ( target_filepath ) <EOL> if lnk_filepath is core . UNSET : <EOL> lnk_filepath = os . path . join ( os . getcwd ( ) , os . path . basename ( target_filepath ) + "<STR_LIT>" ) <EOL> return cls ( <EOL> lnk_filepath , <EOL> path = target_filepath , <EOL> ** kwargs <EOL> ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> if exc_type is None : <EOL> self . write ( ) <EOL> def _get_arguments ( self ) : <EOL> return self . _shell_link . GetArguments ( ) <EOL> def _set_arguments ( self , arguments ) : <EOL> self . _shell_link . SetArguments ( arguments ) <EOL> arguments = property ( _get_arguments , _set_arguments ) <EOL> def _get_description ( self ) : <EOL> return self . _shell_link . GetDescription ( ) <EOL> def _set_description ( self , description ) : <EOL> self . _shell_link . SetDescription ( description ) <EOL> description = property ( _get_description , _set_description ) <EOL> def _get_hotkey ( self ) : <EOL> return self . _shell_link . GetHotkey ( ) <EOL> def _set_hotkey ( self , hotkey ) : <EOL> self . _shell_link . SetHotkey ( hotkey ) <EOL> hotkey = property ( _get_hotkey , _set_hotkey ) <EOL> def _get_icon_location ( self ) : <EOL> path , index = self . _shell_link . GetIconLocation ( ) <EOL> return fs . entry ( path ) , index <EOL> def _set_icon_location ( self , icon_location ) : <EOL> self . _shell_link . SetIconLocation ( * icon_location ) <EOL> icon_location = property ( _get_icon_location , _set_icon_location ) <EOL> def _get_path ( self ) : <EOL> filepath , data = self . _shell_link . GetPath ( SLGP . UNCPRIORITY ) <EOL> return fs . entry ( filepath ) <EOL> def _set_path ( self , path ) : <EOL> self . _shell_link . SetPath ( path ) <EOL> path = property ( _get_path , _set_path ) <EOL> def _get_show_cmd ( self ) : <EOL> return self . _shell_link . GetShowCmd ( ) <EOL> def _set_show_cmd ( self , show_cmd ) : <EOL> self . _shell_link . SetShowCmd ( show_cmd ) <EOL> show_cmd = property ( _get_show_cmd , _set_show_cmd ) <EOL> def _get_working_directory ( self ) : <EOL> return fs . dir ( self . _shell_link . GetWorkingDirectory ( ) ) <EOL> def _set_working_directory ( self , working_directory ) : <EOL> self . _shell_link . SetWorkingDirectory ( working_directory ) <EOL> working_directory = property ( _get_working_directory , _set_working_directory ) <EOL> def write ( self , filepath = core . UNSET ) : <EOL> if not filepath : <EOL> filepath = self . filepath <EOL> if filepath is None : <EOL> raise x_shell ( errmsg = "<STR_LIT>" ) <EOL> wrapped ( <EOL> self . _shell_link . QueryInterface , <EOL> pythoncom . IID_IPersistFile <EOL> ) . Save ( <EOL> self . filepath , <EOL> filepath == self . filepath <EOL> ) <EOL> self . filepath = filepath <EOL> return self <EOL> def shortcut ( source = core . UNSET ) : <EOL> if source is None : <EOL> return None <EOL> elif source is core . UNSET : <EOL> return Shortcut ( ) <EOL> elif isinstance ( source , Shortcut ) : <EOL> return source <EOL> elif source . endswith ( "<STR_LIT>" ) : <EOL> return Shortcut . from_lnk ( source ) <EOL> else : <EOL> return Shortcut . from_target ( source ) <EOL> class PropertySet ( core . _WinSysObject ) : <EOL> def __init__ ( self , property_set_storage , fmtid ) : <EOL> self . property_set_storage = property_set_storage <EOL> self . fmtid = fmtid <EOL> def as_string ( self ) : <EOL> return FMTID . name_from_value ( self . fmtid ) <EOL> def as_dict ( self ) : <EOL> try : <EOL> property_storage = self . property_set_storage . Open ( self . fmtid , STGM . READ | STGM . SHARE_EXCLUSIVE ) <EOL> except pythoncom . com_error : <EOL> error = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if error . strerror == '<STR_LIT>' : <EOL> return { } <EOL> else : <EOL> raise <EOL> properties = { } <EOL> for name , property_id , vartype in property_storage : <EOL> if name is None : <EOL> property_names = PROPERTIES . get ( self . fmtid , constants . Constants ( ) ) <EOL> name = property_names . name_from_value ( property_id , unicode ( hex ( property_id ) ) ) <EOL> try : <EOL> for value in property_storage . ReadMultiple ( [ property_id ] ) : <EOL> properties [ name ] = value <EOL> except TypeError : <EOL> properties [ name ] = None <EOL> return properties <EOL> def __getattr__ ( self , attr ) : <EOL> return self . as_dict ( ) [ attr ] <EOL> def keys ( self ) : <EOL> return list ( self . as_dict ( ) . iterkeys ( ) ) <EOL> def values ( self ) : <EOL> return list ( self . as_dict ( ) . itervalues ( ) ) <EOL> def items ( self ) : <EOL> return list ( self . as_dict ( ) . iteritems ( ) ) <EOL> class Properties ( core . _WinSysObject ) : <EOL> def __init__ ( self , filepath ) : <EOL> self . _pidl , _ = shell . SHILCreateFromPath ( os . path . abspath ( filepath ) , <NUM_LIT:0> ) <EOL> self . _pss = _desktop . BindToStorage ( self . _pidl , None , pythoncom . IID_IPropertySetStorage ) <EOL> def property_set ( self , fmtid ) : <EOL> return PropertySet ( self . _pss , FMTID . constant ( fmtid ) ) <EOL> __getattr__ = property_set <EOL> __getitem__ = property_set <EOL> def __iter__ ( self ) : <EOL> for fmtid , clsid , flags , ctime , mtime , atime in self . _pss : <EOL> yield self . property_set ( fmtid ) <EOL> if fmtid == FMTID . DocSummaryInformation : <EOL> fmtid = pythoncom . FMTID_UserDefinedProperties <EOL> yield self . property_set ( fmtid ) <EOL> def dumped ( self , level = <NUM_LIT:0> ) : <EOL> output = [ ] <EOL> for ps in self : <EOL> output . append ( "<STR_LIT>" % ( FMTID . name_from_value ( ps . fmtid ) , utils . dumped_dict ( ps . as_dict ( ) , level ) ) ) <EOL> return utils . dumped ( "<STR_LIT:\n>" . join ( output ) , level ) <EOL> def properties ( source ) : <EOL> if source is None : <EOL> return None <EOL> elif isinstance ( source , Properties ) : <EOL> return source <EOL> else : <EOL> return Properties ( source ) <EOL> PyIID = type ( pywintypes . IID ( "<STR_LIT>" ) ) <EOL> class ShellEntry ( core . _WinSysObject ) : <EOL> def __init__ ( self , parent_obj , rpidl ) : <EOL> self . _parent_obj = parent_obj <EOL> self . _rpidl = rpidl <EOL> def as_string ( self ) : <EOL> return self . _parent_obj . GetDisplayNameOf ( self . _rpidl , SHGDN . NORMAL ) <EOL> @ classmethod <EOL> def from_pidl ( cls , pidl , parent_obj = None ) : <EOL> if parent_obj is None : <EOL> parent_obj = _desktop . BindToObject ( pidl [ : - <NUM_LIT:1> ] , None , shell . IID_IShellFolder ) <EOL> rpidl = pidl [ - <NUM_LIT:1> : ] <EOL> else : <EOL> rpidl = pidl <EOL> return cls ( parent_obj , rpidl ) <EOL> @ classmethod <EOL> def from_path ( cls , path ) : <EOL> _ , pidl , flags = _desktop . ParseDisplayName ( <NUM_LIT:0> , None , path , SFGAO . FOLDER ) <EOL> if flags & SFGAO . FOLDER : <EOL> return ShellFolder . from_pidl ( pidl ) <EOL> else : <EOL> return ShellItem . from_pidl ( pidl ) <EOL> @ classmethod <EOL> def factory ( cls , shell_entry = core . UNSET , parent_obj = None ) : <EOL> if shell_entry is None : <EOL> return None <EOL> elif shell_entry is core . UNSET : <EOL> return ShellFolder ( _desktop , [ ] ) <EOL> elif isinstance ( shell_entry , ShellEntry ) : <EOL> return shell_entry <EOL> elif isinstance ( shell_entry , int ) : <EOL> return ShellFolder . from_pidl ( shell . SHGetSpecialFolderLocation ( <NUM_LIT:0> , shell_entry ) ) <EOL> else : <EOL> if isinstance ( shell_entry , list ) : <EOL> return cls . from_pidl ( shell_entry , parent_obj ) <EOL> elif isinstance ( shell_entry , basestring ) : <EOL> return cls . from_path ( shell_entry ) <EOL> @ property <EOL> def name ( self ) : <EOL> return self . as_string ( ) <EOL> @ property <EOL> def attributes ( self ) : <EOL> return constants . Attributes ( self . _parent_obj . GetAttributesOf ( [ self . _rpidl ] , - <NUM_LIT:1> ) , SFGAO ) <EOL> def attribute ( self , attr ) : <EOL> """<STR_LIT>""" <EOL> value = SFGAO . constant ( attr ) <EOL> return bool ( self . _parent_obj . GetAttributesOf ( [ self . _rpidl ] , value ) & value ) <EOL> class ShellItem ( ShellEntry ) : <EOL> pass <EOL> class ShellFolder ( ShellEntry ) : <EOL> def __init__ ( self , parent_obj , rpidl ) : <EOL> ShellEntry . __init__ ( self , parent_obj , rpidl ) <EOL> if self . _rpidl : <EOL> self . _folder = self . _parent_obj . BindToObject ( self . _rpidl , None , shell . IID_IShellFolder ) <EOL> else : <EOL> self . _folder = self . _parent_obj <EOL> def __iter__ ( self ) : <EOL> for folder in self . _folder . EnumObjects ( None , SHCONTF . FOLDERS ) : <EOL> yield ShellFolder ( self . _folder , folder ) <EOL> for item in self . _folder . EnumObjects ( None , SHCONTF . NONFOLDERS ) : <EOL> yield ShellItem ( self . _folder , item ) <EOL> def walk ( self , depthfirst = False ) : <EOL> top = self . _folder <EOL> folders = [ self . factory ( f , self . _folder ) for f in self . _folder . EnumObjects ( None , SHCONTF . FOLDERS ) ] <EOL> non_folders = [ ShellItem ( f , self . _folder ) for f in self . _folder . EnumObjects ( None , SHCONTF . NONFOLDERS ) ] <EOL> if not depthfirst : <EOL> yield top , folders , non_folders <EOL> shell_entry = ShellEntry . factory <EOL> def shell_folder ( shell_folder = core . UNSET , parent = core . UNSET ) : <EOL> if shell_folder is None : <EOL> return None <EOL> elif shell_folder is core . UNSET : <EOL> return ShellFolder ( [ ] , _desktop ) <EOL> elif isinstance ( shell_folder , PyIShellFolder ) : <EOL> return ShellFolder ( shell_folder ) <EOL> elif isinstance ( shell_folder , basestring ) : <EOL> pidl , flags = shell . SHILCreateFromPath ( os . path . abspath ( shell_folder ) , <NUM_LIT:0> ) <EOL> if pidl is None : <EOL> pidl = shell . SHGetFolderLocation ( None , CSIDL . constant ( shell_folder ) , None , <NUM_LIT:0> ) <EOL> return ShellFolder ( _desktop . BindToObject ( pidl , None , shell . IID_IShellFolder ) ) <EOL> elif isinstance ( shell_folder , list ) : <EOL> if parent is core . UNSET : <EOL> raise x_shell ( errctx = "<STR_LIT>" , errmsg = "<STR_LIT>" ) <EOL> return ShellFolder ( parent . BindToObject ( shell_folder , None , shell . IID_IShellFolder ) ) <EOL> else : <EOL> raise x_shell ( errctx = "<STR_LIT>" ) <EOL> desktop = ShellFolder ( _desktop , [ ] ) </s>
