forked from mgoral/gogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gogo.py
executable file
·266 lines (226 loc) · 8.01 KB
/
gogo.py
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
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
"""
Copyright (C) 2013-2014 Michal Goral
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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import sys
import os
import errno
import getpass
import gettext
import locale
import operator
__version__ = "1.3.0"
t = gettext.translation(
domain='gogo',
fallback=True)
gettext.install('gogo')
_ = t.gettext
HELP_MSG = _(
"""gogo - bookmark your favorite directories
usage:
gogo [OPTIONS]|[DIR_ALIAS]
options:
-a alias : add current directory as alias to the configuration
-l, --ls : list aliases
-e, --edit : open configuration file in $EDITOR
-h, --help : show this message
-v, --version : print version number and exit
examples:
gogo alias
gogo alias/child/directory
See ~/.config/gogo/gogo.conf for configuration details."""
)
DEFAULT_CONFIG = [
_("# This is an example 'gogo' config file") + '\n',
_("# Each line starting with '#' character is considered a comment.") + '\n',
_("# Each entry should be in the following format:") + '\n',
_("# dir_alias = /dir/path/") + '\n',
_("# Example:") + '\n',
"\n",
"default = %s\n" % os.path.expanduser("~"),
"\n",
_("# 'default' is a special alias which is used when no alias is given to gogo.") + '\n',
_("# If you don't specify it in a configuration file, it'll point to your home dir.") + '\n',
"\n",
_("# You can also connect to directory on ssh server but syntax is slightly different:") + '\n',
_("# dir_alias = ssh://server_name:chosen_shell /dir/path/") + '\n\n',
_("# You can omit shell if you wish but in this case gogo will use ${SHELL} variable.") + '\n',
_("# dir_alias = ssh://second_server /dir/path/") + '\n',
"\n",
"sshloc = ssh://%[email protected]:%s %s\n" %
(getpass.getuser(), os.environ["SHELL"], os.path.expanduser("~")),
"- = -\n"
"gogo = ~/.config/gogo\n",
]
configName = "gogo.conf"
configDir = "%s/%s" % (os.path.expanduser("~"), "/.config/gogo")
configPath = os.path.join(configDir, configName)
def echo(text, output=sys.stdout, endline=True):
if output == sys.stdout:
if endline is True:
output.write("echo '%s';" % text)
else:
output.write("echo -n '%s';" % text)
else:
output.write("%s" % text)
if endline is True:
output.write("\n")
def call(cmd):
sys.stdout.write("%s;\n" % cmd)
sys.exit(0)
def printVersion():
echo("gogo %s" % __version__)
sys.exit(0)
def fatalError(msg, status=1):
echo(msg, sys.stderr)
sys.exit(status)
def _changeDirectory(directory):
if directory.startswith("~/"):
directory = directory.replace("~", os.path.expanduser("~"))
call("cd '%s'" % directory)
def _sshToAddress(address):
addressPart, directory = address.split(" ", 1)
splitted = addressPart.split(":")
server = splitted[0]
shell = splitted[1] if len(splitted) > 1 else "${SHELL}"
call("ssh %s -t 'cd %s; %s'" % (server, directory, shell))
def processRequest(request):
if request.startswith("ssh://"):
address = request.replace("ssh://", "", 1)
_sshToAddress(address)
else:
_changeDirectory(request)
def printConfig(config):
echo(_("Current gogo configuration (sorted alphabetically):"))
if len(config) > 0:
justification = len(max(list(config.keys()), key=len)) + 2
# sort
configList = [(key, config[key]) for key in list(config.keys())]
configList.sort(key = operator.itemgetter(0))
for key, val in configList:
#keyStr = "%s" % key.decode(locale.getpreferredencoding())
keyStr = "%s" % key.encode().decode(locale.getpreferredencoding())
valStr = " : %s" % str(val)
echo(keyStr.rjust(justification), endline=False)
echo(valStr)
else:
echo(_(" [ NO CONFIGURATION ] "), sys.stderr)
def createNonExistingConfigDir():
try:
os.makedirs(configDir)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(configDir):
pass
else: raise
def openConfigInEditor():
try:
editor = os.environ["EDITOR"]
except KeyError:
echo(_("No $EDITOR set. Trying vi."), sys.stderr)
editor = "vi"
call("%s %s" % (editor, configPath))
sys.exit(1)
def readConfig():
createNonExistingConfigDir()
lines = None
try:
with open(configPath, 'r') as file_:
lines = file_.readlines()
except IOError:
lines = DEFAULT_CONFIG
with open(configPath, 'w+') as file_:
file_.writelines(lines)
return lines
def preparePath(path):
path = path.strip('"\' ')
return path
def prepareAlias(alias):
alias = alias.strip()
return alias
def parseConfig(lines):
configDict = {}
for lineNo, line in enumerate(lines):
line = line.strip()
if not line.startswith('#') and line != "":
split = line.strip().split('=', 1)
try:
key = prepareAlias(split[0])
val = preparePath(split[1])
except IndexError:
fatalError(_("Error at parsing a config file..\n at line %s:\n %s" % (lineNo, line) ))
configDict[key] = val
return configDict
def addAlias(alias, currentConfig):
if currentConfig.get(alias) is None:
currentDir = os.getcwd()
with open(configPath, "a") as file_:
file_.write("%s = %s\n" % (alias, currentDir))
else:
fatalError(_("Alias '%s' already exists!") % alias)
def parseAlias(alias, config):
"""Parse alias inputted by a user. Returns a tuple: ('dir', 'remainder_path').
It's because of a feature that user can input something like "alias/child/directory" and gogo
should change directory into "dir/child/directory"."""
# If user chooses an alias with a slash in it, so be it.
newdir = config.get(alias)
if newdir is not None:
return (newdir, "")
splitted = alias.split("/", 1)
newdir = config.get(splitted[0])
if newdir is None:
fatalError(_("'%s' not found in a configuration file!" % alias))
if len(splitted) == 1:
return (newdir, "")
else:
return (newdir, splitted[1])
def main():
lines = readConfig()
argNo = len(sys.argv[1:])
if 0 == argNo:
config = parseConfig(lines)
processRequest(config.get("default", os.path.expanduser("~")))
elif 1 == argNo:
arg = sys.argv[1]
if arg == "-h" or arg == "--help":
echo(HELP_MSG)
elif arg == "-v" or arg == "--version":
printVersion()
elif arg == "-l" or arg == "--ls":
config = parseConfig(lines)
printConfig(config)
elif arg == "-e" or arg == "--edit":
openConfigInEditor()
elif arg == "-a":
fatalError(_("Alias to add not specified!"))
else:
config = parseConfig(lines)
newdir, remainder = parseAlias(arg, config)
if len(remainder) > 0: # fix for e.g. 'gogo -' which would result in '-/'
processRequest(os.path.join(newdir, remainder))
else:
processRequest(newdir)
elif 2 == argNo:
arg = sys.argv[1]
if arg == "-a":
alias = sys.argv[2]
config = parseConfig(lines)
addAlias(alias, config)
else:
fatalError(HELP_MSG, 2)
else:
fatalError(HELP_MSG, 3)
try:
main()
except KeyboardInterrupt:
sys.exit(2)