Show More
@@ -1,105 +1,106 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ Proxy module for accessing platform specific utility functions. |
|
2 | """ Proxy module for accessing platform specific utility functions. | |
3 |
|
3 | |||
4 | Importing this module should give you the implementations that are correct |
|
4 | Importing this module should give you the implementations that are correct | |
5 | for your operation system, from platutils_PLATFORMNAME module. |
|
5 | for your operation system, from platutils_PLATFORMNAME module. | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #***************************************************************************** |
|
8 | #***************************************************************************** | |
9 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
|
9 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #***************************************************************************** |
|
13 | #***************************************************************************** | |
14 |
|
14 | |||
15 | import os |
|
15 | import os | |
16 | import sys |
|
16 | import sys | |
|
17 | import warnings | |||
17 |
|
18 | |||
18 | # Import the platform-specific implementations |
|
19 | # Import the platform-specific implementations | |
19 | if os.name == 'posix': |
|
20 | if os.name == 'posix': | |
20 | import platutils_posix as _platutils |
|
21 | import platutils_posix as _platutils | |
21 | elif sys.platform == 'win32': |
|
22 | elif sys.platform == 'win32': | |
22 | import platutils_win32 as _platutils |
|
23 | import platutils_win32 as _platutils | |
23 | else: |
|
24 | else: | |
24 | import platutils_dummy as _platutils |
|
25 | import platutils_dummy as _platutils | |
25 | import warnings |
|
26 | import warnings | |
26 | warnings.warn("Platutils not available for platform '%s', some features may be missing" % |
|
27 | warnings.warn("Platutils not available for platform '%s', some features may be missing" % | |
27 | os.name) |
|
28 | os.name) | |
28 | del warnings |
|
29 | del warnings | |
29 |
|
30 | |||
30 |
|
31 | |||
31 | # Functionality that's logically common to all platforms goes here, each |
|
32 | # Functionality that's logically common to all platforms goes here, each | |
32 | # platform-specific module only provides the bits that are OS-dependent. |
|
33 | # platform-specific module only provides the bits that are OS-dependent. | |
33 |
|
34 | |||
34 | # XXX - I'm still not happy with a module global for this, but at least now |
|
35 | # XXX - I'm still not happy with a module global for this, but at least now | |
35 | # there is a public, cross-platform way of toggling the term title control on |
|
36 | # there is a public, cross-platform way of toggling the term title control on | |
36 | # and off. We should make this a stateful object later on so that each user |
|
37 | # and off. We should make this a stateful object later on so that each user | |
37 | # can have its own instance if needed. |
|
38 | # can have its own instance if needed. | |
38 | def toggle_set_term_title(val): |
|
39 | def toggle_set_term_title(val): | |
39 | """Control whether set_term_title is active or not. |
|
40 | """Control whether set_term_title is active or not. | |
40 |
|
41 | |||
41 | set_term_title() allows writing to the console titlebar. In embedded |
|
42 | set_term_title() allows writing to the console titlebar. In embedded | |
42 | widgets this can cause problems, so this call can be used to toggle it on |
|
43 | widgets this can cause problems, so this call can be used to toggle it on | |
43 | or off as needed. |
|
44 | or off as needed. | |
44 |
|
45 | |||
45 | The default state of the module is for the function to be disabled. |
|
46 | The default state of the module is for the function to be disabled. | |
46 |
|
47 | |||
47 | Parameters |
|
48 | Parameters | |
48 | ---------- |
|
49 | ---------- | |
49 | val : bool |
|
50 | val : bool | |
50 | If True, set_term_title() actually writes to the terminal (using the |
|
51 | If True, set_term_title() actually writes to the terminal (using the | |
51 | appropriate platform-specific module). If False, it is a no-op. |
|
52 | appropriate platform-specific module). If False, it is a no-op. | |
52 | """ |
|
53 | """ | |
53 | _platutils.ignore_termtitle = not(val) |
|
54 | _platutils.ignore_termtitle = not(val) | |
54 |
|
55 | |||
55 |
|
56 | |||
56 | def set_term_title(title): |
|
57 | def set_term_title(title): | |
57 | """Set terminal title using the necessary platform-dependent calls.""" |
|
58 | """Set terminal title using the necessary platform-dependent calls.""" | |
58 |
|
59 | |||
59 | if _platutils.ignore_termtitle: |
|
60 | if _platutils.ignore_termtitle: | |
60 | return |
|
61 | return | |
61 | _platutils.set_term_title(title) |
|
62 | _platutils.set_term_title(title) | |
62 |
|
63 | |||
63 |
|
64 | |||
64 | class FindCmdError(Exception): |
|
65 | class FindCmdError(Exception): | |
65 | pass |
|
66 | pass | |
66 |
|
67 | |||
67 | def find_cmd(cmd): |
|
68 | def find_cmd(cmd): | |
68 | """Find full path to executable cmd in a cross platform manner. |
|
69 | """Find full path to executable cmd in a cross platform manner. | |
69 |
|
70 | |||
70 | This function tries to determine the full path to a command line program |
|
71 | This function tries to determine the full path to a command line program | |
71 | using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the |
|
72 | using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the | |
72 | time it will use the version that is first on the users `PATH`. If |
|
73 | time it will use the version that is first on the users `PATH`. If | |
73 | cmd is `python` return `sys.executable`. |
|
74 | cmd is `python` return `sys.executable`. | |
74 |
|
75 | |||
75 | Parameters |
|
76 | Parameters | |
76 | ---------- |
|
77 | ---------- | |
77 | cmd : str |
|
78 | cmd : str | |
78 | The command line program to look for. |
|
79 | The command line program to look for. | |
79 | """ |
|
80 | """ | |
80 | if cmd == 'python': |
|
81 | if cmd == 'python': | |
81 | return sys.executable |
|
82 | return sys.executable | |
82 | try: |
|
83 | try: | |
83 | path = _platutils.find_cmd(cmd) |
|
84 | path = _platutils.find_cmd(cmd) | |
84 | except: |
|
85 | except: | |
85 | raise FindCmdError('command could not be found: %s' % cmd) |
|
86 | raise FindCmdError('command could not be found: %s' % cmd) | |
86 | # which returns empty if not found |
|
87 | # which returns empty if not found | |
87 | if path == '': |
|
88 | if path == '': | |
88 | raise FindCmdError('command could not be found: %s' % cmd) |
|
89 | raise FindCmdError('command could not be found: %s' % cmd) | |
89 | return path |
|
90 | return path | |
90 |
|
91 | |||
91 | def get_long_path_name(path): |
|
92 | def get_long_path_name(path): | |
92 | """Expand a path into its long form. |
|
93 | """Expand a path into its long form. | |
93 |
|
94 | |||
94 | On Windows this expands any ~ in the paths. On other platforms, it is |
|
95 | On Windows this expands any ~ in the paths. On other platforms, it is | |
95 | a null operation. |
|
96 | a null operation. | |
96 | """ |
|
97 | """ | |
97 | return _platutils.get_long_path_name(path) |
|
98 | return _platutils.get_long_path_name(path) | |
98 |
|
99 | |||
99 | #----------------------------------------------------------------------------- |
|
100 | #----------------------------------------------------------------------------- | |
100 | # Deprecated functions |
|
101 | # Deprecated functions | |
101 | #----------------------------------------------------------------------------- |
|
102 | #----------------------------------------------------------------------------- | |
102 | def freeze_term_title(): |
|
103 | def freeze_term_title(): | |
103 | warnings.warn("This function is deprecated, use toggle_set_term_title()") |
|
104 | warnings.warn("This function is deprecated, use toggle_set_term_title()") | |
104 | _platutils.ignore_termtitle = True |
|
105 | _platutils.ignore_termtitle = True | |
105 |
|
106 |
General Comments 0
You need to be logged in to leave comments.
Login now