Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

# Account Recognizers 

# 

# Classes used in account discovery to recognize when a particular account 

# should be used based on environmental conditions (window title, hostname, 

# username, current directory, etc.) 

 

# License {{{1 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see http://www.gnu.org/licenses/. 

 

# Imports {{{1 

from .utilities import gethostname, getusername, error_source 

from shlib import cwd, to_path 

from inform import Error, is_collection, is_str, log, notify, warn 

from fnmatch import fnmatch 

try: 

from urllib.parse import urlparse 

except ImportError: 

from urlparse import urlparse 

import os 

 

# Utilities {{{1 

# flatten {{{2 

def flatten(collection, split=False): 

# if split is specified, create list from string by splitting at whitespace 

if split and is_str(collection): 

collection = collection.split() 

 

if is_collection(collection): 

for each in collection: 

for e in flatten(each): 

yield e 

else: 

yield collection 

 

# Recognizer Base Class {{{1 

class Recognizer(object): 

def all_urls(self): 

urls = {} 

if hasattr(self, 'recognizers'): 

for each in self.recognizers: 

urls.update(each.all_urls()) 

if hasattr(self, 'get_urls'): 

urls.update(self.get_urls()) 

return urls 

 

def get_name(self): 

return self.__class__.__name__ 

 

# RecognizeAll {{{1 

class RecognizeAll(Recognizer): 

def __init__(self, *recognizers, **kwargs): 

self.recognizers = recognizers 

self.script = kwargs.get('script', True) 

 

def match(self, data, account, verbose=False): 

try: 

match = all([ 

each.match(data, account, verbose) for each in self.recognizers 

]) 

if match: 

if verbose: 

log(' %s: matches.' % self.get_name()) 

return self.script 

except Exception as err: 

raise Error(str(err), culprit=err.__class__.__name__) 

if verbose: 

log(' %s: no match.' % self.get_name()) 

 

def __repr__(self): 

args = [repr(each) for each in self.recognizers] 

if self.script: 

args.append('script=%r' % self.script) 

return "%s(%s)" % (self.__class__.__name__, ', '.join(args)) 

 

 

# RecognizeAny {{{1 

class RecognizeAny(Recognizer): 

def __init__(self, *recognizers, **kwargs): 

self.recognizers = recognizers 

self.script = kwargs.get('script', True) 

 

def match(self, data, account, verbose=False): 

try: 

match = Any([ 

each.match(data, account, verbose) for each in self.recognizers 

]) 

if match: 

if verbose: 

log(' %s: matches.' % self.get_name()) 

return self.script 

except Exception as err: 

raise Error(str(err), culprit=err.__class__.__name__) 

if verbose: 

log(' %s: no match.' % self.get_name()) 

 

def __repr__(self): 

args = [repr(each) for each in self.recognizers] 

if self.script: 

args.append('script=%r' % self.script) 

return "%s(%s)" % (self.__class__.__name__, ', '.join(args)) 

 

 

# RecognizeTitle {{{1 

class RecognizeTitle(Recognizer): 

def __init__(self, *titles, **kwargs): 

self.titles = flatten(titles, split=False) 

self.script = kwargs.get('script', True) 

 

def match(self, data, account, verbose=False): 

try: 

actual = data.get('rawtitle') 

if actual: 

for candidate in self.titles: 

if fnmatch(actual, candidate): 

if verbose: 

log(' %s: matches.' % self.get_name()) 

return self.script 

except Exception as err: 

raise Error(str(err), culprit=err.__class__.__name__) 

if verbose: 

log(' %s: no match.' % self.get_name()) 

 

def __repr__(self): 

args = [repr(each) for each in self.titles] 

if self.script: 

args.append('script=%r' % self.script) 

return "%s(%s)" % (self.__class__.__name__, ', '.join(args)) 

 

 

# RecognizeURL {{{1 

class RecognizeURL(Recognizer): 

def __init__(self, *urls, **kwargs): 

self.urls = flatten(urls, split=True) 

self.script = kwargs.get('script', True) 

self.name = kwargs.get('name', None) 

self.exact_path = kwargs.get('exact_path', False) 

 

def match(self, data, account, verbose=False): 

try: 

for url in self.urls: 

url = urlparse(url) 

protocol = url.scheme 

host = url.netloc 

path = url.path 

 

# data may contain the following fields after successful title 

# recognition: 

# rawdata: the original title 

# title: the processed title 

# url: the full url 

# browser: the name of the browser 

# protocol: the url scheme (ex. http, https, ...) 

# host: the url host name or IP address 

# path: the path component of the url 

# does not include options or anchor 

if host == data.get('host'): 

 

def path_matches(expected, actual): 

if not expected: 

# path was not specified, treat it as don't care 

return True 

if self.exact_path: 

# exact path match expected 

return expected == actual 

else: 

# otherwise just match what was given 

return actual.startswith(expected) 

 

if path_matches(path, data.get('path')): 

if ( 

protocol == data.get('protocol') or 

protocol not in REQUIRED_PROTOCOLS 

): 

if verbose: 

log(' %s: matches.' % self.get_name()) 

return self.script 

else: 

msg = 'url matches, but uses wrong protocol.' 

notify(msg) 

raise Error(msg, culprit=account.get_name()) 

except Exception as err: 

raise Error(str(err), culprit=err.__class__.__name__) 

if verbose: 

log(' %s: no match.' % self.get_name()) 

 

def get_urls(self): 

return {self.name: self.urls} 

 

def __repr__(self): 

args = [repr(each) for each in self.urls] 

if self.script: 

args.append('script=%r' % self.script) 

if self.name: 

args.append('name=%r' % self.name) 

return "%s(%s)" % (self.__class__.__name__, ', '.join(args)) 

 

# RecognizeCWD {{{1 

class RecognizeCWD(Recognizer): 

def __init__(self, *dirs, **kwargs): 

self.dirs = flatten(dirs, split=True) 

self.script = kwargs.get('script', True) 

 

def match(self, data, account, verbose=False): 

try: 

cwd = cwd() 

for directory in self.dirs: 

if cwd.samefile(to_path(directory)): 

if verbose: 

log(' %s: matches.' % self.get_name()) 

return self.script 

except Exception as err: 

raise Error(str(err), culprit=err.__class__.__name__) 

if verbose: 

log(' %s: no match.' % self.get_name()) 

 

def __repr__(self): 

args = [repr(each) for each in self.dirs] 

if self.script: 

args.append('script=%r' % self.script) 

return "%s(%s)" % (self.__class__.__name__, ', '.join(args)) 

 

 

# RecognizeHost {{{1 

class RecognizeHost(Recognizer): 

def __init__(self, *hosts, **kwargs): 

self.hosts = flatten(hosts, split=True) 

self.script = kwargs.get('script', True) 

 

def match(self, data, account, verbose=False): 

try: 

hostname = gethostname() 

for host in self.hosts: 

if host == hostname: 

if verbose: 

log(' %s: matches.' % self.get_name()) 

return self.script 

except Exception as err: 

raise Error(str(err), culprit=err.__class__.__name__) 

if verbose: 

log(' %s: no match.' % self.get_name()) 

 

def __repr__(self): 

args = [repr(each) for each in self.hosts] 

if self.script: 

args.append('script=%r' % self.script) 

return "%s(%s)" % (self.__class__.__name__, ', '.join(args)) 

 

 

# RecognizeUser {{{1 

class RecognizeUser(Recognizer): 

def __init__(self, *users, **kwargs): 

self.users = flatten(users, split=True) 

self.script = kwargs.get('script', True) 

 

def match(self, data, account, verbose=False): 

try: 

username = getusername() 

if username in self.users: 

if verbose: 

log(' %s: matches.' % self.get_name()) 

return self.script 

except Exception as err: 

raise Error(str(err), culprit=err.__class__.__name__) 

if verbose: 

log(' %s: no match.' % self.get_name()) 

 

def __repr__(self): 

args = [repr(each) for each in self.users] 

if self.script: 

args.append('script=%r' % self.script) 

return "%s(%s)" % (self.__class__.__name__, ', '.join(args)) 

 

# RecognizeEnvVar {{{1 

class RecognizeEnvVar(Recognizer): 

def __init__(self, name, value, script=True): 

self.name = name 

self.value = value 

self.script = script 

 

def match(self, data, account, verbose=False): 

try: 

if name in os.environ and value == os.environ[name]: 

if verbose: 

log(' %s: matches.' % self.get_name()) 

return self.script 

except Exception as err: 

raise Error(str(err), culprit=err.__class__.__name__) 

if verbose: 

log(' %s: no match.' % self.get_name()) 

 

def __repr__(self): 

return "%s(%s)" % (self.__class__.__name__, ', '.join([ 

repr(each) for each in [self.name, self.value, self.script] 

]))