Total Complexity | 6 |
Total Lines | 39 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | """Interface to the operating system.""" |
||
2 | |||
3 | 1 | import logging |
|
4 | 1 | import os |
|
5 | 1 | import platform |
|
6 | 1 | import subprocess |
|
7 | |||
8 | 1 | ||
9 | log = logging.getLogger(__name__) |
||
10 | |||
11 | 1 | ||
12 | def launch(path): |
||
13 | 1 | """Open a file with its default program.""" |
|
14 | 1 | name = platform.system() |
|
15 | 1 | log.info("Opening %s", path) |
|
16 | 1 | try: |
|
17 | function = { |
||
18 | 'Windows': _launch_windows, |
||
19 | 'Darwin': _launch_mac, |
||
20 | 'Linux': _launch_linux, |
||
21 | 1 | }[name] |
|
22 | 1 | except KeyError: |
|
23 | raise RuntimeError("Unrecognized platform: {}".format(name)) from None |
||
24 | 1 | else: |
|
25 | return function(path) |
||
26 | |||
27 | |||
28 | def _launch_windows(path): # pragma: no cover (manual test) |
||
29 | os.startfile(path) # pylint: disable=no-member |
||
30 | return True |
||
31 | |||
32 | |||
33 | def _launch_mac(path): # pragma: no cover (manual test) |
||
34 | return subprocess.call(['open', path]) == 0 |
||
35 | |||
36 | |||
37 | def _launch_linux(path): # pragma: no cover (manual test) |
||
38 | return subprocess.call(['xdg-open', path]) == 0 |
||
39 |