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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
import os, sys
from ranger.ext.waitpid_no_intr import waitpid_no_intr
from subprocess import Popen, PIPE
devnull = open(os.devnull, 'a')
ALLOWED_FLAGS = 'sdpSDP'
class Applications(object):
"""
This class contains definitions on how to run programs and should
be extended in ranger.defaults.apps
The user can decide what program to run, and if he uses eg. 'vim', the
function app_vim() will be called. However, usually the user
simply wants to "start" the file without specific instructions.
In such a case, app_default() is called, where you should examine
the context and decide which program to use.
All app functions have a name starting with app_ and return a string
containing the whole command or a tuple containing a list of the
arguments.
It has one argument, which is the AppContext instance.
You should define app_default, app_pager and app_editor since
internal functions depend on those. Here are sample implementations:
def app_default(self, context):
if context.file.media:
if context.file.video:
# detach videos from the filemanager
context.flags += 'd'
return self.app_mplayer(context)
else:
return self.app_editor(context)
def app_pager(self, context):
return ('less', ) + tuple(context)
def app_editor(self, context):
return ('vim', ) + tuple(context)
"""
def app_self(self, context):
"""Run the file itself"""
return "./" + context.file.basename
def get(self, app):
"""Looks for an application, returns app_default if it doesn't exist"""
try:
return getattr(self, 'app_' + app)
except AttributeError:
return self.app_default
def has(self, app):
"""Returns whether an application is defined"""
return hasattr(self, 'app_' + app)
def all(self):
"""Returns a list with all application functions"""
methods = self.__class__.__dict__
return [meth[4:] for meth in methods if meth.startswith('app_')]
class AppContext(object):
"""
An AppContext object abstracts the spawning of processes.
At initialization of the object you can define many high-level options.
When you call the run() function, those options are evaluated and
translated into Popen() calls.
An instances of this class is passed as the only argument to
app_xyz calls of the Applications object.
Attributes:
action -- a string with a command or a list of arguments for
the Popen call.
app -- the name of the app function. ("vim" for app_vim.)
app is used to get an action if the user didn't specify one.
mode -- a number, mainly used in determining the action in app_xyz()
flags -- a string with flags which change the way programs are run
files -- a list containing files, mainly used in app_xyz
file -- an arbitrary file from that list (or None)
fm -- the filemanager instance
wait -- boolean, wait for the end or execute programs in parallel?
stdout -- directly passed to Popen
stderr -- directly passed to Popen
stdin -- directly passed to Popen
shell -- directly passed to Popen. Should the string be shell-interpreted?
List of allowed flags:
s: silent mode. output will be discarded.
d: detach the process.
p: redirect output to the pager
An uppercase key ensures that a certain flag will not be used.
"""
def __init__(self, app='default', files=None, mode=0, flags='', fm=None,
stdout=None, stderr=None, stdin=None, shell=None,
wait=True, action=None):
"""
The necessary parameters are fm and action or app.
"""
if files is None:
self.files = []
else:
self.files = list(files)
try:
self.file = self.files[0]
except IndexError:
self.file = None
self.app = app
self.action = action
self.mode = mode
self.flags = flags
self.fm = fm
self.stdout = stdout
self.stderr = stderr
self.stdin = stdin
self.wait = wait
if shell is None:
self.shell = isinstance(action, str)
else:
self.shell = shell
def __iter__(self):
"""Iterates over all file paths"""
if self.files:
for f in self.files:
yield f.path
def squash_flags(self):
"""Remove duplicates and lowercase counterparts of uppercase flags"""
for flag in self.flags:
if ord(flag) <= 90:
bad = flag + flag.lower()
self.flags = ''.join(c for c in self.flags if c not in bad)
def get_action(self, apps=None):
"""Get the action from app_xyz"""
if apps is None and self.fm:
apps = self.fm.apps
if apps is None:
raise RuntimeError("AppContext has no source for applications!")
app = apps.get(self.app)
self.action = app(self)
self.shell = isinstance(self.action, str)
def run(self):
"""
Run the application in the way specified by the options.
This function ensures that there is an action.
"""
self.squash_flags()
if self.action is None:
self.get_action()
# ---------------------------- determine keywords for Popen()
kw = {}
kw['stdout'] = sys.stderr
kw['stderr'] = sys.stderr
kw['args'] = self.action
if kw['args'] is None:
return None
for word in ('shell', 'stdout', 'stdin', 'stderr'):
if getattr(self, word) is not None:
kw[word] = getattr(self, word)
if 's' in self.flags or 'd' in self.flags:
kw['stdout'] = kw['stderr'] = kw['stdin'] = devnull
# --------------------------- run them
if 'p' in self.flags:
kw['stdout'] = PIPE
kw['stderr'] = PIPE
process1 = Popen(**kw)
process2 = run(app='pager', stdin=process1.stdout, fm=self.fm)
return process2
elif 'd' in self.flags:
process = Popen(**kw)
return process
else:
self._activate_ui(False)
try:
process = Popen(**kw)
if self.wait:
waitpid_no_intr(process.pid)
finally:
self._activate_ui(True)
return process
def _activate_ui(self, boolean):
if self.fm and self.fm.ui:
if boolean:
self.fm.ui.initialize()
else:
self.fm.ui.suspend()
def run(action=None, **kw):
"""Shortcut for creating and immediately running an AppContext."""
app = AppContext(action=action, **kw)
return app.run()
def tup(*args):
"""
This helper function creates a tuple out of the arguments.
('a', ) + tuple(some_iterator)
is equivalent to:
tup('a', *some_iterator)
"""
return tuple(args)
|