Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / python / shortcut.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4
5 """Creation of  Windows shortcuts.
6
7 Requires win32all.
8 """
9
10 from win32com.shell import shell
11 import pythoncom
12 import os
13
14
15 def open(filename):
16     """Open an existing shortcut for reading.
17
18     @return: The shortcut object
19     @rtype: Shortcut
20     """
21     sc=Shortcut()
22     sc.load(filename)
23     return sc
24
25
26 class Shortcut:
27     """A shortcut on Win32.
28     >>> sc=Shortcut(path, arguments, description, workingdir, iconpath, iconidx)
29     @param path: Location of the target
30     @param arguments: If path points to an executable, optional arguments to
31                       pass
32     @param description: Human-readable decription of target
33     @param workingdir: Directory from which target is launched
34     @param iconpath: Filename that contains an icon for the shortcut
35     @param iconidx: If iconpath is set, optional index of the icon desired
36     """
37
38     def __init__(self, 
39                  path=None,
40                  arguments=None, 
41                  description=None,
42                  workingdir=None,
43                  iconpath=None,
44                  iconidx=0):
45         self._base = pythoncom.CoCreateInstance(
46             shell.CLSID_ShellLink, None,
47             pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink
48         )
49         data = map(None, 
50                    ['"%s"' % os.path.abspath(path), arguments, description,
51                     os.path.abspath(workingdir), os.path.abspath(iconpath)], 
52                    ("SetPath", "SetArguments", "SetDescription",
53                    "SetWorkingDirectory") )
54         for value, function in data:
55             if value and function:
56                 # call function on each non-null value
57                 getattr(self, function)(value)
58         if iconpath:
59             self.SetIconLocation(iconpath, iconidx)
60
61     def load( self, filename ):
62         """Read a shortcut file from disk."""
63         self._base.QueryInterface(pythoncom.IID_IPersistFile).Load(filename)
64     
65     def save( self, filename ):
66         """Write the shortcut to disk.
67
68         The file should be named something.lnk.
69         """
70         self._base.QueryInterface(pythoncom.IID_IPersistFile).Save(filename, 0)
71     
72     def __getattr__( self, name ):
73         if name != "_base":
74             return getattr(self._base, name)
75         raise AttributeError, "%s instance has no attribute %s" % \
76                 (self.__class__.__name__, name)