summaryrefslogtreecommitdiff
blob: e81b470feba8bb33fd6d17e15c97bc412e1b596d (plain)
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
#!/usr/bin/env python
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Universal Select Tool
# Base Module
# umodule.py mephx.x@gmail.com

import os
import re
import sys
import stat
import string
import traceback


from uio import filesystem
from uio import printsystem

from umodule import *

verbose = False

version = '0.2'
modules_dir = '/usr/share/uselect/modules/'


class UniversalSelectTool:
	
	def __init__(self):

		return

	def get_modules(self):
		self.modules = []
		for module in filesystem.list_dir(modules_dir):
			if re.match('__init__.py$', module):
				continue
			match = re.match('(.*).py$', module)
			if match:
				import modules
				modname = match.group(1)
				modpath = 'modules.'+ modname
				__import__(modpath)
				module = eval(modpath + '.module')
				self.modules.append(module)		
		
				
	def get_module(self, name):
		import modules
		modname = name
		modpath = 'modules.'+ modname
		__import__(modpath)
		module = eval(modpath + '.module')
		return module

	def parse_argv(self, args):
		global verbose, version
		module = None
		modules = None
		action = None
		printsystem.use_colors(True)
		for arg in args:
			if arg == '-v':
				verbose = True
				printsystem.verbose()
				args = args[1:]
			elif arg == '-nc':
				printsystem.use_colors(False)
				args = args[1:]
			elif arg == '-version':
				printsystem.print_version(version)
				args = args[1:]			
		if len(args) < 1:
			self.get_modules()
			modules = self.modules
		elif len(args) == 1:
			module = self.get_module(args[0])
		elif len(args) >= 2:
			module = self.get_module(args[0])
			action = module.get_action(args[1])
			action.build()
			action.do_action(args[2:])
		if len(args) == 2:
			args = None
		else:
			args = args[2:]
		
		return [module, modules, args, action]


def main():
	uselect = UniversalSelectTool()
	try:
		list = uselect.parse_argv(sys.argv[1:])
		
		printsystem.print_ui(module = list[0], \
			modules = list[1], args = list[2], action = list[3])
	except UserWarning, warning:
		printsystem.print_exception(warning, True)
	except Exception, exception:
		printsystem.print_exception(exception)
		if verbose:
			traceback.print_exc()
			printsystem.print_line('')
		exit(1)
	exit(0)

if __name__ == '__main__': main()