-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelios.py
More file actions
151 lines (130 loc) · 4.76 KB
/
helios.py
File metadata and controls
151 lines (130 loc) · 4.76 KB
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
''' Helios - init, start, save, reset workspaces
copyright 2020, Franz Ludwig Kostelezky <info@kostelezky.com>
add command-alias:
- Windows: add directory to %PATH%
'''
import os
import sys
import pickle
import webbrowser
class Helios():
'''
docstring
'''
def __init__(self):
''' Call this at first time
Path to browser may vary on different operating systems.
Class-variables:
- (str) _browser_path
- (dict of 2d array) _workspace
'''
self._browser_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
self._workspace = {
'main': [['github.com/flemk/helios'],
['echo hello world']]
}
def workspace_print(self, workspace=None):
''' Print out all workspaces
'''
if workspace is None:
# print all workspaces
for workspace, content in self._workspace.items():
web, cmd = content
print('%s:' % (workspace))
print(' -web:')
[print(' %s' % (el)) for el in web]
print(' -cmd:')
[print(' %s' % (el)) for el in cmd]
print('\n')
else:
# print only one workspace
web, cmd = self._workspace[workspace]
print('%s:' % (workspace))
print(' -web:')
[print(' %s' % (el)) for el in web]
print(' -cmd:')
[print(' %s' % (el)) for el in cmd]
input('PRESS ENTER TO EXIT')
def workspace_edit(self, workspace=None, action=None, appendix=None):
'''
'''
if action is None or workspace is None:
print('Information missing (action|workspace)')
return 1
if action == 'new':
print('Creating new workspace %s' % (workspace))
self._workspace[workspace] = [[], []]
elif action == 'remove':
if input('Do you want to remove %s ? (y/n)' % (workspace)) == 'y':
del self._workspace[workspace]
else:
print('removing aborted')
elif action == 'add-web' and appendix is not None:
print('Appending web: %s to %s' % (appendix, workspace))
self._workspace[workspace][0].append(appendix)
elif action == 'add-cmd' and appendix is not None:
print('Appending cmd: %s to %s' % (appendix, workspace))
self._workspace[workspace][1].append(appendix)
else:
print('no action has been specified')
return 1
def workspace_start(self, workspace='main'):
'''
'''
for el in self._workspace[workspace][0]:
webbrowser.get(self._browser_path).open(el)
for el in self._workspace[workspace][1]:
os.system(el)
def print_usage():
''' prints usage via print and awaits input (pauses) then exits.
'''
print('helios.py param1 [param2, ...]\n')
print('for more information visit: github.com/flemk/helios')
print('')
print('param1')
print(' help')
print(' rise [name of workspace]: opens workspace')
print(' list [name of workspace]')
print(' edit <name of workspace>')
print(' new')
print(' remove')
print(' add-web <weblink>: add weblink to workspace. No spaces allowed.')
print(' add-cmd <command>: add cmd to workspace. No spaces allowed')
input('PRESS ENTER TO EXIT')
def main():
''' selects which action should be performed
'''
# if helios has already been configured, load configuration of config_path
script_path = os.path.dirname(sys.argv[0])
config_path = os.path.join(script_path, 'config.pkl')
try:
#h = load(config_path, allow_pickle=True)
h = pickle.load(open(config_path, 'rb'))
except FileNotFoundError:
h = Helios()
#save(config_path, h)
with open(config_path, 'wb') as file:
pickle.dump(h, file, pickle.HIGHEST_PROTOCOL)
# parse command line arguments
args = sys.argv
l = len(args)
if l > 1:
if args[1] == 'help':
print_usage()
elif args[1] == 'list':
t = None if l < 3 else args[2]
h.workspace_print(t)
elif args[1] == 'rise' and l >= 2:
t = 'main' if l <= 2 else args[2]
print('Opening %s' % (t))
h.workspace_start(t)
elif args[1] == 'edit' and l >= 4:
print('Editing %s' % (args[2]))
t = None if l <= 4 else args[4]
h.workspace_edit(workspace=args[2], action=args[3], appendix=t)
with open(config_path, 'wb') as file:
pickle.dump(h, file, pickle.HIGHEST_PROTOCOL)
else:
print_usage()
if __name__ == "__main__":
main()