~vcs-imports/kupfer/master-new

60 by Ulrik Sverdrup
move launch of prog to main.py
1
#!/usr/bin/env python
2
# -*- coding: UTF-8 -*-
3
4
"""
5
kupfer
6
ɹǝɟdnʞ
212 by Ulrik Sverdrup
Add COPYING file. Add Gpl licensiation text to main.py
7
238 by Ulrik Sverdrup
Fix License: License is GPLv3 or any later version
8
Copyright 2007--2009 Ulrik Sverdrup <ulrik.sverdrup@gmail.com>
212 by Ulrik Sverdrup
Add COPYING file. Add Gpl licensiation text to main.py
9
238 by Ulrik Sverdrup
Fix License: License is GPLv3 or any later version
10
This program is free software: you can redistribute it and/or modify
212 by Ulrik Sverdrup
Add COPYING file. Add Gpl licensiation text to main.py
11
it under the terms of the GNU General Public License as published by
238 by Ulrik Sverdrup
Fix License: License is GPLv3 or any later version
12
the Free Software Foundation, either version 3 of the License, or
212 by Ulrik Sverdrup
Add COPYING file. Add Gpl licensiation text to main.py
13
(at your option) any later version.
14
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
238 by Ulrik Sverdrup
Fix License: License is GPLv3 or any later version
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
212 by Ulrik Sverdrup
Add COPYING file. Add Gpl licensiation text to main.py
18
GNU General Public License for more details.
19
20
You should have received a copy of the GNU General Public License
238 by Ulrik Sverdrup
Fix License: License is GPLv3 or any later version
21
along with this program.  If not, see <http://www.gnu.org/licenses/>.
60 by Ulrik Sverdrup
move launch of prog to main.py
22
"""
23
122 by Ulrik Sverdrup
Add new options to main.py and arrange them elegantly
24
def get_options(default_opts=""):
87 by Ulrik Sverdrup
Add command-line options
25
	"""
26
	Usage:
210 by Ulrik Sverdrup
Put sources into -s -S command-line switches
27
		-s, -S list     add list of sources
121 by Ulrik Sverdrup
Implement RecentsSource: Recently used files.
28
		-d dir, -D dir  add dir as dir source
29
		-r dir, -R dir  add dir as recursive dir source
136 by Ulrik Sverdrup
add --help flag
30
87 by Ulrik Sverdrup
Add command-line options
31
		--depth d use recursive depth d
121 by Ulrik Sverdrup
Implement RecentsSource: Recently used files.
32
136 by Ulrik Sverdrup
add --help flag
33
		--help          show usage help
168 by Ulrik Sverdrup
Add --debug option to enable debug info. Preparse it.
34
		--debug         enable debug info
136 by Ulrik Sverdrup
add --help flag
35
121 by Ulrik Sverdrup
Implement RecentsSource: Recently used files.
36
		small letter:   catalog item in catalog
37
		capital letter: direct inclusion
210 by Ulrik Sverdrup
Put sources into -s -S command-line switches
38
39
		list of sources:
40
		  a             applications
41
		  b             firefox bookmarks
42
		  c             recent documents
43
		  e             epiphany bookmarks
44
		  p             nautilus places
87 by Ulrik Sverdrup
Add command-line options
45
	
210 by Ulrik Sverdrup
Put sources into -s -S command-line switches
46
	The default is "-S ap -s aecp -D ~"
87 by Ulrik Sverdrup
Add command-line options
47
	"""
48
	from getopt import getopt, GetoptError
49
	from sys import argv
50
168 by Ulrik Sverdrup
Add --debug option to enable debug info. Preparse it.
51
	opts = argv[1:]
52
	if "--debug" in opts:
53
		import debug
54
		opts.remove("--debug") 
55
56
	if len(opts) < 1:
210 by Ulrik Sverdrup
Put sources into -s -S command-line switches
57
		opts ="-S ap -s aecp -D ~".split()
122 by Ulrik Sverdrup
Add new options to main.py and arrange them elegantly
58
87 by Ulrik Sverdrup
Add command-line options
59
	try:
210 by Ulrik Sverdrup
Put sources into -s -S command-line switches
60
		opts, args = getopt(opts, "S:s:D:d:R:r:", ["depth=", "help"])
87 by Ulrik Sverdrup
Add command-line options
61
	except GetoptError, info:
62
		print info
63
		print get_options.__doc__
64
		raise SystemExit
65
66
	options = {}
67
	
68
	for k, v in opts:
210 by Ulrik Sverdrup
Put sources into -s -S command-line switches
69
		if k in ("-s", "-S"):
70
			if k == "-s":
71
				key = "sources"
72
			else:
73
				key = "include_sources"
74
			options.setdefault(key, []).extend(v)
122 by Ulrik Sverdrup
Add new options to main.py and arrange them elegantly
75
		elif k in ("-d", "-D", "-r", "-R"):
76
			lst = options.get(k, [])
77
			lst.append(v)
78
			options[k] = lst
87 by Ulrik Sverdrup
Add command-line options
79
		elif k == "--depth":
80
			options["depth"] = v
136 by Ulrik Sverdrup
add --help flag
81
		elif k == "--help":
82
			print get_options.__doc__
83
			raise SystemExit
210 by Ulrik Sverdrup
Put sources into -s -S command-line switches
84
87 by Ulrik Sverdrup
Add command-line options
85
	return options
86
60 by Ulrik Sverdrup
move launch of prog to main.py
87
def main():
88
	import sys
89
	from os import path
90
215 by Ulrik Sverdrup
Move main application into kupfer/ package
91
	from kupfer import browser
220 by Ulrik Sverdrup
extensions: Put windows and screen plugins in kupfer.extensions
92
	from kupfer import objects, extensions
240 by Ulrik Sverdrup
Make DataController a singleton service
93
	from kupfer import data
67 by Ulrik Sverdrup
Fix some printing isssues
94
95
	print __doc__
96
87 by Ulrik Sverdrup
Add command-line options
97
	options = get_options()
168 by Ulrik Sverdrup
Add --debug option to enable debug info. Preparse it.
98
122 by Ulrik Sverdrup
Add new options to main.py and arrange them elegantly
99
	s_sources = []
100
	S_sources = []
101
	default_depth = 1
112 by Ulrik Sverdrup
Fix main program, add options for direct/indirect inclusion of sources
102
122 by Ulrik Sverdrup
Add new options to main.py and arrange them elegantly
103
	def dir_source(opt):
104
		for d in options[opt]:
112 by Ulrik Sverdrup
Fix main program, add options for direct/indirect inclusion of sources
105
			abs = path.abspath(path.expanduser(d))
122 by Ulrik Sverdrup
Add new options to main.py and arrange them elegantly
106
			yield objects.DirectorySource(abs)
107
108
	def file_source(opt):
162 by Ulrik Sverdrup
Bug fix: depth option has to be int, not string!
109
		depth = int(options.get("depth", default_depth))
184 by Ulrik Sverdrup
Make sure each -r/-R opt is added as an independent source
110
		for d in options[opt]:
111
			abs = path.abspath(path.expanduser(d))
112
			yield objects.FileSource((abs,), depth)
122 by Ulrik Sverdrup
Add new options to main.py and arrange them elegantly
113
114
	sources = {
210 by Ulrik Sverdrup
Put sources into -s -S command-line switches
115
			"a": objects.AppSource(),
116
			"c": objects.RecentsSource(),
117
			"p": objects.PlacesSource(),
225 by Ulrik Sverdrup
extensions: Move bookmarks plugins to kupfer.extensions
118
			"b": extensions.bookmarks.BookmarksSource(),
119
			"e": extensions.bookmarks.EpiphanySource(),
220 by Ulrik Sverdrup
extensions: Put windows and screen plugins in kupfer.extensions
120
			"s": extensions.screen.ScreenSessionsSource(),
121
			"w": extensions.windows.WindowsSource(),
122 by Ulrik Sverdrup
Add new options to main.py and arrange them elegantly
122
	}
123
124
	files = {
125
			"-d": dir_source,
126
			"-r": file_source,
127
	}
128
210 by Ulrik Sverdrup
Put sources into -s -S command-line switches
129
	for item in options.get("sources", ()):
130
		s_sources.append(sources[item])
131
	for item in options.get("include_sources", ()):
132
		S_sources.append(sources[item])
122 by Ulrik Sverdrup
Add new options to main.py and arrange them elegantly
133
	
134
	for k, v in files.items():
135
		K = k.upper()
136
		if k in options:
137
			s_sources.extend(v(k))
138
		if K in options:
139
			S_sources.extend(v(K))
140
141
	if len(s_sources):
142
		S_sources.append(objects.SourcesSource(s_sources))
143
	
241 by Ulrik Sverdrup
Make DataController handle the Sources list (S_sources)
144
	if not S_sources:
89 by Ulrik Sverdrup
If using only one source, use that as root source
145
		print "No sources"
211 by Ulrik Sverdrup
Minor changes and code cleanup in main.py
146
		raise SystemExit(1)
89 by Ulrik Sverdrup
If using only one source, use that as root source
147
240 by Ulrik Sverdrup
Make DataController a singleton service
148
	dc = data.DataController()
241 by Ulrik Sverdrup
Make DataController handle the Sources list (S_sources)
149
	dc.set_sources(S_sources)
240 by Ulrik Sverdrup
Make DataController a singleton service
150
	w = browser.WindowController()
60 by Ulrik Sverdrup
move launch of prog to main.py
151
	w.main()
152
153
if __name__ == '__main__':
154
	main()