~vcs-imports/kupfer/master-new

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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

"""
kupfer
ɹǝɟdnʞ

Copyright 2007--2009 Ulrik Sverdrup <ulrik.sverdrup@gmail.com>

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/>.
"""

def get_options(default_opts=""):
	"""
	Usage:
		-s, -S list     add list of sources
		-d dir, -D dir  add dir as dir source
		-r dir, -R dir  add dir as recursive dir source

		--depth d use recursive depth d

		--help          show usage help
		--debug         enable debug info

		small letter:   catalog item in catalog
		capital letter: direct inclusion

		list of sources:
		  a             applications
		  b             firefox bookmarks
		  c             recent documents
		  e             epiphany bookmarks
		  p             nautilus places
	
	The default is "-S ap -s aecp -D ~"
	"""
	from getopt import getopt, GetoptError
	from sys import argv

	opts = argv[1:]
	if "--debug" in opts:
		import debug
		opts.remove("--debug") 

	if len(opts) < 1:
		opts ="-S ap -s aecp -D ~".split()

	try:
		opts, args = getopt(opts, "S:s:D:d:R:r:", ["depth=", "help"])
	except GetoptError, info:
		print info
		print get_options.__doc__
		raise SystemExit

	options = {}
	
	for k, v in opts:
		if k in ("-s", "-S"):
			if k == "-s":
				key = "sources"
			else:
				key = "include_sources"
			options.setdefault(key, []).extend(v)
		elif k in ("-d", "-D", "-r", "-R"):
			lst = options.get(k, [])
			lst.append(v)
			options[k] = lst
		elif k == "--depth":
			options["depth"] = v
		elif k == "--help":
			print get_options.__doc__
			raise SystemExit

	return options

def main():
	import sys
	from os import path

	from kupfer import browser
	from kupfer import objects, extensions

	print __doc__

	options = get_options()

	s_sources = []
	S_sources = []
	default_depth = 1

	def dir_source(opt):
		for d in options[opt]:
			abs = path.abspath(path.expanduser(d))
			yield objects.DirectorySource(abs)

	def file_source(opt):
		depth = int(options.get("depth", default_depth))
		for d in options[opt]:
			abs = path.abspath(path.expanduser(d))
			yield objects.FileSource((abs,), depth)

	sources = {
			"a": objects.AppSource(),
			"c": objects.RecentsSource(),
			"p": objects.PlacesSource(),
			"b": extensions.bookmarks.BookmarksSource(),
			"e": extensions.bookmarks.EpiphanySource(),
			"s": extensions.screen.ScreenSessionsSource(),
			"w": extensions.windows.WindowsSource(),
	}

	files = {
			"-d": dir_source,
			"-r": file_source,
	}

	for item in options.get("sources", ()):
		s_sources.append(sources[item])
	for item in options.get("include_sources", ()):
		S_sources.append(sources[item])
	
	for k, v in files.items():
		K = k.upper()
		if k in options:
			s_sources.extend(v(k))
		if K in options:
			S_sources.extend(v(K))

	if len(s_sources):
		S_sources.append(objects.SourcesSource(s_sources))
	
	if len(S_sources) == 1:
		root_catalog, = S_sources
	elif len(S_sources) > 1:
		root_catalog = objects.MultiSource(S_sources)
	else:
		print "No sources"
		raise SystemExit(1)

	w = browser.WindowController(root_catalog)
	w.main()

if __name__ == '__main__':
	main()