summaryrefslogtreecommitdiff
path: root/uio.py
blob: c2aa423acf25696b2ecf8c42e496c72151c8d487 (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
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
#!/usr/bin/env python
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Universal Select Tool
# Uselect Input/Output Module
# uio.py mephx.x@gmail.com

import os
import pwd
import stat

# Aligning
space = ' '
right = '\t'

# Globals
bold = lime = red = reset = yellow = notice = ''
error = warning =  bullet = ok = highlight = ''

verbose = False

class FileSystem:
	""" FileSystem Class """
	def __init__(self):
		""" FileSystem Contructor """
		self.home = os.getenv('HOME')
		self.uid = pwd.getpwuid(os.getuid())[0]
		self.environment = self.home + '/.uselect/'
		if not os.path.exists(self.environment):
			os.mkdir(self.environment)
		self.environment += 'bin/'
		if not os.path.exists(self.environment):
			os.mkdir(self.environment)
				
	def read_file(self, path):
		""" Reads a File and returns lines[] """
		file = open(path,'r')
		lines = file.readlines()
		file.close
		return lines

	def write_file(self, path ,lines):
		""" Writes "lines[]" in File "path" """
		if os.path.exists(path):
			raise Exception("File in " + path + " already Exists!")
			return
		file = open(path,'w')
		for line in lines:
			file.writelines(line + '\n')
		file.close
		return lines
		
	def execute_cmd(self, cmd, args):
		""" Executes "cmd" and returns output """
		if not os.path.exists(cmd):
			raise Exception('File "' + path + '" not found!')
			return
		for arg in args:
			cmd += ' ' + arg
		return os.popen(cmd).readlines()
		
	def delete_file(self, path):
		""" Deletes file in "path" """
		if os.path.exists(path):
			os.unlink(path)

	def make_exec_file(self, path):
		""" Makes file in path u+rwx """
		if not os.path.exists(path):
			raise Exception('File "' + path + '" not found!')
			return
		os.chmod(path, stat.S_IXUSR + stat.S_IRUSR + stat.S_IWUSR)
	
	def create_symlink(self, source, destination):
		self.delete_file(destination)
		os.symlink(source, destination)
	
	def list_dir(self, path):
		""" Lists path directory """
		if not os.path.exists(path):
			raise Exception("File " + path + " not found!")
			return
		else:
			return os.listdir(path)
	def path_exists(self, path):
		return os.path.exists(path)
	def real_path(self, path):
		return os.path.realpath(path)
		
class PrintSystem:
	""" PrintSystem Class """
		
	def __init__(self):
		""" PrintSystem Constructor """
		return
		
	def verbose(self):
		global verbose
		verbose = True
		return
		
	def use_colors(self, usecolors):
		global bold, lime, red, reset, yellow, error, warning, bullet,\
			ok, highlight, notice
		if usecolors:
			# Text Colors
			bold = '\033[1m'
			lime = '\033[32m'
			red = '\033[31m'
			reset = '\033[0m'
			yellow = '\033[1;33m'
			blue = '\033[1;34m'
			# Bullets
		else:
			bold = lime = red = reset =	yellow = ''
			
		error = bold + '(' + red + '!' + reset + bold + ')' + reset
		warning = bold + '(' + yellow + '!' + reset + bold + ')' + reset
		bullet = bold + '(' + lime + '*' + reset + bold + ')' + reset
		notice = bold + '(' + blue + '*' + reset + bold + ')' + reset
		ok = bold + '(' + lime + '>' + reset + bold + ')' + reset
		highlight = lime + bold
	# Glocal Print Functions
	def print_line(self, line, _verbose = False):
		""" Prints line with type"""
		global verbose
		if _verbose and not verbose:
			return
		print line
		return
		
	def print_table(self,list, _verbose = False):
		""" Prints Lists of the form list[[a,b]] """
		global verbose
		if _verbose and not verbose:
			return
		for item in list:
			print('  ' + item[0] + reset + '\r' + 3 * right + item[1])
		return
		
	def print_exception(self, exception, iswarning = False):
		""" Prints Exceptions in a standart way """
		if iswarning:
			type = warning + ' Warning! '
		else:
			type = error + ' Error! '
		self.print_line('\n' + type + str(exception) + '\n')
	
	def format_action(self, action):
		table = []
		if action.parameters != '':
			for line in action.usage:
				table.append([line,''])
			table.append(['',''])
		count = 1
		for option in action.options:
			table.append([option[1] + space + \
				eval(option[0]),''])
			count += 1
			
		return table
	# Screen Functions
	def print_ui(self, module = None, modules = None, \
		action = None, args = None):
		if module == None:
			# uselect Usage
			# uselect Options
			self.print_usage()
			self.print_line('')
			self.print_options()
			self.print_line('')
			self.print_modules(modules)
			self.print_line('')
		elif action == None:
			# Modules Usage
			self.print_usage(module)
			self.print_line('')
			self.print_module(module)
			self.print_line('')
			# Modules Actions
			self.print_actions(module)
			self.print_line('')
		elif args == None:
			# Actions Usage
			self.print_usage(module, action)
			self.print_line('')
			# Action
			self.print_action(module, action)
			self.print_line('')
		else:
			# This means Action Done
			for line in action.output:
				print(line)
		return

	def print_module(self, module):
		self.print_line(bold + lime + 'Module' + space + reset \
		+ bold +module.name + lime + ':' + reset)
		module.get_actions()
		self.print_line('Author:' + space + module.author + space \
			+ 'Version:' + space + module.version)
			
	def print_modules(self, modules):
		self.print_line(lime + bold + 'Modules:' + reset)
		list = []
		for module in modules:
			list.append([bold + module.name, bullet + space + module.description])
		self.print_table(list)
		
	def print_actions(self, module):
		self.print_line(highlight + 'Actions:' + reset)
		if len(module.actions) == 0:
			print '  Module ' + module.name + \
				' has no actions!'
			return
		list = []
		for action in module.actions:
			self.print_table([[bold + action.name + reset, \
				action.description]])
		
	def print_action(self, module, action):	
		self.print_table([[bold + action.description + reset, '']])
		self.print_line('')
		self.print_table(self.format_action(action))
			
	def print_version(self, version):
		self.print_line(bold + 'Universal Select Tool - ' \
			+ lime + 'uselect' + reset)
		self.print_line(bold + 'Version ' + reset + version + '\n')
		
	def print_usage(self, module = None, action = None):
		""" General Usage Printer """
		if module != None:
			module_name = module.name
		else:
			module_name = '<module>'
		if action != None:
			action_name = action.name
			options = action.parameters
		else:
			action_name = '<action>'
			options = '<options>'
		self.print_line(bold + lime + 'Usage:' + reset + ' uselect <options> ' + module_name \
		+ space + action_name + space + options)
		
	def print_options(self):
		self.print_line(highlight + space + 'Options:' + reset)
		self.print_table([ \
			[bold + '-v', bullet + space + 'Verbose Mode'], \
			[bold + '-nc', bullet + space + 'No Colors'], \
			# [bold + '-profile', bullet + space + 'Profile Mode'], \
			[bold + '-version', bullet + space + 'Version Information']])