File: __init__.py

package info (click to toggle)
python-pyshortcuts 1.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,632 kB
  • sloc: python: 1,097; makefile: 42
file content (137 lines) | stat: -rw-r--r-- 4,873 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
"""
pyshortcuts
"""
import os
import sys
from pathlib import Path
from argparse import ArgumentParser, RawDescriptionHelpFormatter

from .version import version as __version__
from .utils import (fix_filename, new_filename, fix_varname, isotime,
                    get_pyexe, bytes2str, str2bytes, get_homedir, get_cwd,
                    uname, scut_ext, ico_ext)

from .gformat import gformat
from .debugtimer import debugtimer

make_shortcut =  get_folders = None
if uname.startswith('lin'):
    from .linux import make_shortcut, get_folders
elif uname.startswith('darwin'):
    from .darwin import make_shortcut, get_folders
elif uname.startswith('win'):
    from .windows import make_shortcut, get_folders

# back compat
platform = uname

from .shortcut import shortcut, Shortcut

try:
    import wx
    from .wxgui import ShortcutFrame
except ImportError:
    ShortCutFrame = None


def get_desktop():
    "for back compatibility"
    return get_folders().desktop

def shortcut_cli():
    '''
    command-line interface to creating desktop shortcuts
    '''
    desc = 'create desktop and start menu shortcuts'

    parser = ArgumentParser(description=desc,
                            formatter_class=RawDescriptionHelpFormatter)


    parser.add_argument('-v', '--version', dest='version', action='store_true',
                        default=False, help='show version')

    parser.add_argument('-n', '--name', dest='name', default=None,
                        help='name for shortcut link')

    parser.add_argument('-i', '--icon', dest='icon', default=None,
                        help='name of icon file')

    parser.add_argument('-f', '--folder', dest='folder', default=None,
                        help='subfolder on desktop to put shortcut')

    parser.add_argument('-e', '--executable', dest='exe', default=None,
                        help='name of executable to use (python)')

    parser.add_argument('-x', '--no-executable', dest='noexe', action='store_true',
                        default=False, help='use no implied executable [False]')

    parser.add_argument('-t', '--terminal', dest='terminal', action='store_true',
                        default=True, help='run script in a Terminal [True]')

    parser.add_argument('-g', '--gui', dest='gui', action='store_true',
                        default=False, help='run script as GUI, with no Terminal [False]')

    parser.add_argument('-d', '--desktop', dest='desktop', action='store_true',
                        default=True, help='create desktop shortcut [True]')

    parser.add_argument('-s', '--startmenu', dest='startmenu', action='store_true',
                        default=True, help='create Start Menu shortcut [True]')

    parser.add_argument('-w', '--wxgui', dest='wxgui', action='store_true',
                        default=False, help='run GUI version of pyshortcut [False]')

    parser.add_argument('-b', '--bootstrap', dest='bootstrap', action='store_true',
                        default=False, help='make desktop shortcut to wxGUI')

    parser.add_argument('scriptname', nargs='?',
                        help='script name, including arguments')

    args = parser.parse_args()

    if args.version:
        print(f"pyshortcut {__version__}")

    if (args.wxgui or args.bootstrap) and ShortcutFrame is None:
        print("wxpython is required to run GUI")
        sys.exit()

    if args.bootstrap:
        bindir = 'bin'
        if uname.startswith('win'):
            bindir = 'Scripts'
        fpath = Path(__file__)
        icon = Path(fpath.parent, 'icons', f'ladder.{ico_ext[0]}'
                        ).resolve().as_posix()
        script = Path(sys.prefix, bindir, 'pyshortcut').resolve().as_posix()
        make_shortcut(f"{script} --wxgui", name='PyShortcut',
                      terminal=False, icon=icon)

    elif args.wxgui:
        app = wx.App()
        ShortcutFrame().Show(True)
        app.MainLoop()

    else:
        if args.gui:
            args.terminal = False

        if args.scriptname is None:
            print("pyshortcut: must provide one script.  try 'pyshortcut -h'")
        else:
            p_icon = Path(args.icon).resolve()
            if p_icon.exists():
                icon = p_icon.resolve().as_posix()
            else:
                parent = p_icon.parent
                stem  = p_icon.stem
                for ext in ico_ext:
                    x = Path(parent, f"{stem}.{ext}").absolute()
                    if x.exists():
                        icon = x.resolve().as_posix()
            make_shortcut(args.scriptname, name=args.name,
                          terminal=args.terminal, folder=args.folder,
                          icon=icon, desktop=args.desktop,
                          startmenu=args.startmenu, executable=args.exe,
                          noexe=args.noexe)