-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaliasmgr_util.py
827 lines (702 loc) · 28.7 KB
/
aliasmgr_util.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
'''
aliasmgr_util.py
Various utilities for Alias Manager (cmdline & gui)
Created on Sep 24, 2013
@author: Christopher Welborn
'''
import re
import gtk
import os
import sys
import aliasmgr_integrator
import aliasmgr_settings
# For changing file mode when scripts are generated.
from stat import S_IREAD, S_IWRITE, S_IEXEC
# Shorthand for read, write, and exec.
S_RWX = S_IREAD | S_IWRITE | S_IEXEC
settings = aliasmgr_settings.am_settings()
integrator = aliasmgr_integrator.am_integrator()
# Command/Alias Object ------------------------------------
class Command():
def __init__(self, name=None, cmd=None, comment=None, exported=None):
# set defaults.
self.name = name if name else ''
self.cmd = cmd if cmd else []
self.comment = comment if comment else ''
self.exported = exported if exported else 'New'
self.shebang = '#!/bin/bash'
def __repr__(self):
""" return string representation of this command. """
commentstr = self.comment if self.comment else '(No Comment)'
return '{} : {} \n'.format(self.name, commentstr)
def __str__(self):
return self.__repr__()
def to_scriptfile(self, filepath=None, overwrite=False):
""" Convert a function/alias to its own script file. """
if not self.name:
raise ValueError('Cannot convert a command with no name.')
elif not self.cmd:
raise ValueError('Cannot convert a command with no content.')
# Ensure a full path is built.
if filepath:
basedir, filename = os.path.split(filepath)
if not basedir:
basedir = os.getcwd()
if not filename:
filename = '{}.sh'.format(self.name)
else:
basedir = os.getcwd()
filename = '{}.sh'.format(self.name)
# Don't clobber existing scripts.
if not overwrite:
uniqueid = 2
while os.path.exists(filename):
filename = '{}{}.sh'.format(self.name, uniqueid)
uniqueid += 1
# Build the full path.
filepath = os.path.join(basedir, filename)
desctype = 'a function' if self.isfunction() else 'an alias'
descfile = settings.get('aliasfile', 'an alias file.')
desclines = [
'# Script generated with {}:'.format(settings.versionstr),
'# Original code was {} in {}.'.format(desctype, descfile),
]
desc = '{}\n'.format('\n'.join(desclines))
content = '\n{}\n'.format(self.to_function())
if not content.endswith('\n'):
content = '{}\n'.format(content)
with open(filepath, 'w') as f:
f.write('{}\n\n'.format(self.shebang))
f.write(desc)
f.write(content)
f.write('\n# Call the function when this script runs.\n')
f.write('{} $@\n'.format(self.name))
f.flush()
# Chmod to allow execution by the user.
os.chmod(filepath, S_RWX)
return filepath
def isfunction(self):
""" Returns true/false depending on linecount """
return (len(self.cmd) > 1)
def isexported(self):
lowerexported = self.exported.lower()
return ((lowerexported == 'yes') or (lowerexported == 'new'))
def setexport(self, bexported, bnew=False):
""" Sets the appropriate string value for cmd.exported using booleans,
Note: String values were used at first for automatic pretty
printing in command line output, but later used to determine
if an alias was new or not, therefore 'automagically'
exporting new functions.
**Aliases are never exported no matter what the export value is**
"""
if bnew:
self.exported = "New"
else:
if bexported:
self.exported = "Yes"
else:
self.exported = "No"
def to_function(self):
""" Return a string containing a function definition for this cmd """
if not (self.name and self.cmd):
return ''
if self.isfunction():
cmdlines = [' {}'.format(l) for l in self.cmd]
else:
cmdlines = [' {} $@'.format(self.cmd[0])]
content = ['function {} {{'.format(self.name)]
if self.comment:
content.append(' # {}\n'.format(self.comment))
content.extend(cmdlines)
content.append('}')
return '\n'.join(content)
# Dialog/Msgbox --------------------------------------------------------
class Dialogs():
""" Dialog boxes and Messageboxes made easier/portable
** for dialogs you might want to set the filter after initializing
like:
mydialog = dlg()
mydialog.filter = [["All Files", "*"], ["Test Files", "*.txt"]]
# dlg() will build the filter using dlg.filter before running
"""
def __init__(self):
# main dialog window
self.dlgwindow = gtk.FileChooserDialog()
self.msgwindow = None
# shortcode for dialog actions
self.open = gtk.FILE_CHOOSER_ACTION_OPEN
self.save = gtk.FILE_CHOOSER_ACTION_SAVE
self.saveas = gtk.FILE_CHOOSER_ACTION_SAVE
self.folder = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER
self.foldercreate = gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER
# shortcode for msg types
self.info = gtk.MESSAGE_INFO
self.error = gtk.MESSAGE_ERROR
self.question = gtk.MESSAGE_QUESTION
self.warning = gtk.MESSAGE_WARNING
self.other = gtk.MESSAGE_OTHER
# filter types
self.filter_all = ["All files", "*"]
self.filter_text = ["Text files", "*.txt"]
self.filter_shell = ["Shell files", "*.sh"]
# last dialog path
self.lastpath = None
# options
self.show_hidden = True
# filter to use by default
self.filter = [self.filter_all, self.filter_shell]
def clear_filter(self):
""" removes all current filters in dlgwindow """
for filter_item in self.dlgwindow.list_filters():
self.dlgwindow.remove_filter(filter_item)
def build_filter(self, lst_filters=None, bclear_filters=True):
""" build dlgwindow filters from list of filters
lst_filters = [["Description", "*.type"], ["Desc2", "*.typ2"]]
"""
if bclear_filters:
self.clear_filter()
# use self.filter to build from instead of custom filters
if lst_filters is None:
lst_filters = self.filter
for filter_itm in lst_filters:
dfilter = gtk.FileFilter()
dfilter.set_name(filter_itm[0])
dfilter.add_pattern(filter_itm[1])
self.dlgwindow.add_filter(dfilter)
def dialog(self, stitle=None, gtkaction=None): # noqa
stitle = stitle or 'Select file...'
if gtkaction is None:
gtkaction = gtk.FILE_CHOOSER_ACTION_OPEN
# Create File Dialog (GTK)
if (gtkaction is None) or (gtkaction == self.open):
gtkbtn = gtk.STOCK_OPEN
elif gtkaction == self.save:
gtkbtn = gtk.STOCK_SAVE
elif gtkaction == self.saveas:
gtkbtn = gtk.STOCK_SAVE
elif gtkaction == self.folder:
gtkbtn = gtk.STOCK_OK
elif gtkaction == self.foldercreate:
gtkbtn = gtk.STOCK_OK
else:
gtkbtn = gtk.STOCK_OPEN
# Add app name to title if its not already given.
if not stitle.lower().startswith(settings.name.lower()):
stitle = '{}: {}'.format(settings.name, stitle)
# Create Dialog.
self.dlgwindow = gtk.FileChooserDialog(
stitle,
None,
gtkaction,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtkbtn, gtk.RESPONSE_OK))
self.dlgwindow.set_default_response(gtk.RESPONSE_OK)
# build filters
self.build_filter()
# Show hidden files because user files are in /.local and /.config
self.dlgwindow.set_show_hidden(self.show_hidden)
# Got lastpath?
try:
if self.lastpath is not None:
self.dlgwindow.set_current_folder(self.lastpath)
except Exception:
pass
# Show Dialog, get response
response = self.dlgwindow.run()
# Get Filename Selected
if response == gtk.RESPONSE_OK:
respFile = self.dlgwindow.get_filename()
# Save last path
self.lastpath = self.dlgwindow.get_current_folder()
# close dialog
self.dlgwindow.destroy()
return respFile
else:
# Return an empty string (CANCELED)
self.dlgwindow.destroy()
return ""
# MessageBox ---------------------------------------
def msgbox(self, smessage, gtktype=None, gtkbuttons=None):
if gtkbuttons is None:
gtkbuttons = gtk.BUTTONS_OK
if gtktype is None:
gtktype = gtk.MESSAGE_INFO
# get type
if gtktype in {self.info, self.warning, self.error}:
if gtkbuttons != gtk.BUTTONS_OK:
btns = gtkbuttons
else:
btns = gtk.BUTTONS_OK
elif gtktype == self.question:
btns = gtk.BUTTONS_YES_NO
# Shows a messagebox with the INFO icon and OK button
self.msgwindow = gtk.MessageDialog(None,
gtk.DIALOG_MODAL,
gtktype,
buttons=btns)
self.msgwindow.set_markup('<b>{}</b>'.format(settings.name))
self.msgwindow.format_secondary_markup(smessage)
self.msgwindow.run()
self.msgwindow.destroy()
def msgbox_warn(self, message):
""" Show a warning messagebox. """
self.msgbox(message, gtktype=self.warning)
def msgbox_yesno(self, smessage):
self.msgwindow = gtk.MessageDialog(None,
gtk.DIALOG_MODAL,
gtk.MESSAGE_QUESTION,
buttons=gtk.BUTTONS_YES_NO)
self.msgwindow.set_markup('<b>{}</b>'.format(settings.name))
self.msgwindow.format_secondary_markup(smessage)
response = self.msgwindow.run()
self.msgwindow.destroy()
return response
# Functions ----------------------------------------
def get_def_count(contents, defword):
""" Counts lines beginning with 'defword',
to retrieve actual 'alias' and 'function' defs in
the file.
returns: count (Integer)
"""
if '\n' in contents:
lines = contents.split('\n')
else:
lines = [contents]
cnt = 0
for line in lines:
if line.startswith(defword):
cnt += 1
return cnt
def getfilecontents(aliasfile=None):
""" Return alias files raw content (string)
Shows a message if alias file cannot be found.
Returns None on failure.
"""
if aliasfile is None:
aliasfile = settings.get("aliasfile")
if not os.path.isfile(aliasfile):
# print("Alias file not found!: " + aliasfile)
dlg = Dialogs()
dlg.msgbox('Alias file not found!:\n{}'.format(aliasfile), dlg.error)
return None
# Load file
try:
with open(aliasfile, 'r') as fread:
filecontent = fread.read()
return filecontent
except (IOError, OSError) as exio:
msg = 'Unable to read file:\n{}'.format(aliasfile)
fullmsg = '{}\n{}'.format(msg, str(exio))
dlg = Dialogs()
dlg.msgbox(fullmsg, dlg.error)
return None
def parsealiasline_old(sline):
""" old deprecated method of parsing alias info from a line. """
# Detect Alias --------------------------------
if (sline.startswith("alias")):
# Replace TABS/NEWLINE
sline = sline.replace('\t', '').replace('\n', '')
# Trim 'alias'
sbuf = sline.replace("alias ", "")
# Trim Comments
if "#" in sbuf:
sbuf = sbuf[:sbuf.index("#")]
# Get comment
scomment = sline[sline.index("#") + 1:]
# Trim leading space
scomment = scomment.strip(' ')
else:
# No Comment
scomment = ""
# Retrieve name and command, without quotes.
aliasparts = sbuf.split('=')
sname = aliasparts[0].strip(" ")
scommand = aliasparts[1].strip(" ").strip('"').strip("'")
# Add command to list (Name, Command, Comment, Exported)
# [Exported not needed for alias])
return Command(name=sname,
cmd=[scommand],
comment=scomment)
return None
def parse_aliases(filecontents):
""" parse all aliases from file, return a list of Command() objects """
# this one handles quotes inside of commands better...
name_pat = re.compile(r'alias[ ]?(?P<name>[\d\w_\-]+)=(?P<cmdinfo>.+)')
cmd_comment_pat = re.compile(r'(?P<command>.+)[#](?P<comment>.+)?')
cmd_nocomment_pat = re.compile(r'(?P<command>.+)')
lst_commands = []
# Get all alias lines, with (name, raw command and comment)
aliases = name_pat.findall(filecontents)
for name, rawcmd in aliases:
# use regex for separating comments from command.
if '#' in rawcmd:
cmdmatch = cmd_comment_pat.search(rawcmd)
else:
# use normal regex.
cmdmatch = cmd_nocomment_pat.search(rawcmd)
if cmdmatch:
groups = cmdmatch.groupdict()
command = groups['command']
comment = groups.get('comment', '')
# Add alias to list as a Command() object...
lst_commands.append(
Command(
name=stripchars(name, ' \t\n'),
cmd=[stripquotes(stripchars(command, ' \t\n'))],
comment=stripchars(comment, '# \t\n'),
exported='[n/a]'))
return lst_commands
def parse_exports(filecontents):
""" parse all exports from file contents, return a list of exported names.
"""
exportpat = re.compile(r'export[ ]+?(?P<export>.+)', flags=re.MULTILINE)
exports = exportpat.findall(filecontents)
if exports:
exports = [stripchars(e, ' \t\n') for e in exports]
return exports
def parse_functions(filecontents): # noqa
""" parse all functions from the alias file. """
# Get all exports from the file
exports = parse_exports(filecontents)
# Parse all functions from the file.
# Flag for setting if we are inside a function
bfunction = False
# Flag for setting if we found function comments
bcomment = False
scomment = ''
# List of parsed Command() objects...
commands = []
# Temporary list for raw function contents
lst_contents = []
# Initialize Tab Depths
tabdepth = 0
spacedepth = 0
for sline in filecontents.split('\n'):
# Detect Function -------------------------------
if sline.replace('\t', '').replace(' ', '').startswith("function"):
# Grab function name
ssplit = sline.split(" ")
sname = ssplit[1].replace('\n', '').replace("()", "")
# Find initial tab/space depth
if tabdepth == 0:
if sline.startswith('\t'):
sbuf = sline
tabdepth, sbuf = trimcount(sbuf, '\t')
if spacedepth == 0:
if sline.startswith(" "):
sbuf = sline
spacedepth, sbuf = trimcount(sbuf, ' ')
# We are now inside a function
bfunction = True
# Inside Function ----------------------------------
if bfunction:
# Detect comment
if sline.replace('\t', '').replace(' ', '').startswith("#"):
# First comment only
if not bcomment:
# Found comment
scomment = sline.replace('\t', '').replace('\n', '')[1:]
scomment = scomment.strip(' ')
# Set flag
bcomment = True
# Add raw contents
# Skip over function definition if multi-line function
if not sline.strip(" ").replace('\n', '').endswith("()"):
lst_contents.append(sline.replace('\n', ''))
# Found end of function (could be a single line though)
# Parse contents, decide which to keep
if ((sline.strip().replace('\n', '').endswith(" }")) or
(sline.replace('\n', '')
.replace('\t', '').replace(' ', '') == "}")):
# End of function
bfunction = False
# Reset comment finder
bcomment = False
# Keep function contents
lst_keep = []
for itm in lst_contents:
# Save trimmed version of line
strim = itm.replace('\t', '').replace(
' ', '').replace('\n', '')
snotabs = itm.replace('\t', '').replace('\n', '')
# Figure out which contents to keep. No Braces.
# and (not strim.startswith("#")):
if (strim != "{") and (strim != "}"):
# Trim single line definition
if "()" in itm:
itm = itm[itm.index("()") + 2:]
snotabs = itm.replace('\t', '').replace('\n', '')
strim = snotabs.replace(' ', '')
if strim.startswith("{"):
snotabs = snotabs[snotabs.index("{") + 1:]
if snotabs.endswith("}"):
snotabs = snotabs[:snotabs.index("}")]
itm = snotabs
# Trim leading { and following }...
if snotabs.startswith("{"):
snotabs = snotabs[1:]
if snotabs.endswith("}"):
snotabs = snotabs[:len(snotabs) - 1]
itm = snotabs
# Trim initial tabdepth from itm
if itm.startswith('\t'):
itm = itm[tabdepth:]
# Trim one more tab depth
if itm.startswith('\t'):
itm = itm[1:]
# Append Function Contents, don't add initial coment
is_initcomment = (
itm.startswith("#") and (scomment in itm)
)
if (scomment and not is_initcomment):
lst_keep.append(itm)
elif scomment == "":
lst_keep.append(itm)
# Append function name/contents/comment /exported [set with
# fixexports()]
sexported = 'Yes' if (sname in exports) else 'No'
commands.append(Command(name=sname,
cmd=lst_keep,
comment=scomment,
exported=sexported))
# Reset comment string
scomment = ''
# Reset contents finder list
lst_contents = []
# Return finished list
return commands
def readfile(aliasfile=None):
""" Read alias/script file, return a list of command() objects.
Returns empty list on failure.
"""
# Get alias file contents
filecontents = getfilecontents(aliasfile=aliasfile)
aliaslinecnt = get_def_count(filecontents, 'alias')
functionlinecnt = get_def_count(filecontents, 'function')
commands = []
if filecontents is None:
return []
# Get all aliases from the file.(also, initialize lst_commands)
aliases = parse_aliases(filecontents)
commands = commands + aliases
# Get all functions form the file (also fills in exported items)
functions = parse_functions(filecontents)
commands = commands + functions
# Validate parsing of aliases/functions
warnmsg = []
if aliaslinecnt != len(aliases):
msg = 'Could not parse all aliases, may be missing some.\n{}\n{}'
msg = msg.format(' alias lines: {}'.format(str(aliaslinecnt)),
' parsed aliases: {}'.format(str(len(aliases))))
warnmsg.append(msg)
if functionlinecnt != len(functions):
msg = 'Could not parse all functions, may be missing some.\n{}\n{}'
msg = msg.format(' function lines: {}'.format(str(functionlinecnt)),
'parsed functions: {}'.format(str(len(functions))))
warnmsg.append(msg)
if warnmsg:
print(
'\nreadfile: missing aliases/functions: \n{}'.format(
'\n'.join(warnmsg)))
Dialogs().msgbox_warn('\n'.join(warnmsg))
return commands
def readexports():
""" Reads exports only, returns a list of exports """
aliasfile = settings.get("aliasfile")
with open(aliasfile, 'r') as fread:
# Temp list for exports
lst_temp = []
# Get file contents
slines = fread.readlines()
for sline in slines:
snotabs = sline.replace('\t', '').replace('\n', '')
if snotabs.startswith("export"):
# Found export, retrieve name
lst_temp.append(snotabs.split(" ")[1])
# Finished with file, return list of exports
return lst_temp
return False
def input_text(message, default=''):
"""
Display a dialog with a text entry.
Returns the text, or None if canceled.
"""
dlg = gtk.MessageDialog(None,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_OTHER,
gtk.BUTTONS_OK_CANCEL,
message)
entry = gtk.Entry()
entry.set_text(default)
entry.show()
dlg.vbox.pack_end(entry)
entry.connect('activate', lambda _: dlg.response(gtk.RESPONSE_OK))
dlg.set_default_response(gtk.RESPONSE_OK)
r = dlg.run()
text = entry.get_text().decode('utf8')
dlg.destroy()
if r == gtk.RESPONSE_OK:
return text
else:
return None
def filename_safe(sfilename, maxchars=30):
""" trim filename to maxchars if needed for printing output"""
if maxchars < 1:
maxchars = 30
# Make sure the filename isn't too long (30 chars)
if len(sfilename) > maxchars:
sfilename = "..." + sfilename[len(sfilename) - maxchars:]
# Return filename
return sfilename
def trim_markup(sstring):
""" Trims all basic markup tags from string """
sfinal = sstring
# Don't need all these cycles if text doesn't have markup at all
if ("<" in sfinal) and (">" in sfinal):
lst_tags = ["i", "b", "u"]
for stag in lst_tags:
sfinal = sfinal.replace(
"<" + stag + ">", "").replace("</" + stag + ">", "")
return sfinal
else:
# No markup
return sfinal
def pick_aliasfile(saliasfile="", bexit_on_refusal=True):
""" force user to select a good alias file, or create a blank one """
sfile = saliasfile
dlg = Dialogs()
while (not os.path.isfile(sfile)):
dlg.msgbox(
"No alias file found, click ok to select one to use.", dlg.info)
sfile = dlg.dialog("Select an alias file to use:")
# good aliasfile?
if os.path.isfile(sfile):
settings.setsave("aliasfile", sfile)
else:
# print("pick_aliasfile: Alias file not found!: " + sfile)
response = dlg.msgbox_yesno('\n'.join((
'Alias file not found,'
' Would you like to create a blank alias file?')))
if response == gtk.RESPONSE_NO:
# print("No alias file, quitting...")
if bexit_on_refusal:
dlg.msgbox(
'{} cannot run with an alias file, goodbye,'.format(
settings.name),
dlg.info)
sys.exit(1)
else:
return ""
else:
integrator = aliasmgr_integrator.am_integrator()
sblankfile = os.path.join(integrator.home, "bash.alias.sh")
create_blank_file(sblankfile, True)
# return good alias filename
return sfile
def create_blank_file(sfilename, bshow_exists_warning=False):
""" Creates a blank alias file """
if (bshow_exists_warning and
os.path.isfile(sfilename)):
dlg = Dialogs()
resp = dlg.msgbox_yesno("File exists:\n" + sfilename + '\n\n' +
"Would you like to overwrite it?")
if resp == gtk.RESPONSE_NO:
return False
try:
with open(sfilename, 'w') as fwrite:
fwrite.write("# Blank alias file for " + settings.name)
settings.setsave("aliasfile", sfilename)
return True
except Exception as ex:
dlg.msgbox("Could not create blank file:\n" +
sfilename + '\n\n' +
"<b><u>Error:</u></b>\n" + str(ex))
print("aliasmgr.py: Error creating blank file:\n" +
str(ex))
return False
def integration_choice():
""" Ask the user if they want to automatically integrate
Alias Manager with bashrc
"""
dlg = Dialogs()
resp = dlg.msgbox_yesno(' '.join((
'{} will add a line to bashrc that will ',
'allow you to easily integrate alias scripts into bashrc.\n',
'If your bashrc requires root permissions to edit, you will ',
'have to enter root\'s password.\n\n',
'Would you like to add this line to bashrc?')).format(settings.name))
if resp == gtk.RESPONSE_YES:
integrator.integrate_self()
settings.setsave('integration', 'true')
print('Integrated {} into bashrc...'.format(settings.name))
return True
else:
settings.setsave('integration', 'false')
return False
def chmod_file(sfilename):
""" makes file executable,
if gksudo/kdesudo is needed, the user is asked before entering a pw
"""
# chmod to destination file
stat_exec = os.access(sfilename, os.X_OK)
# print("chmod_file executable: " + str(stat_res))
if stat_exec:
return "chmod_file: already executable."
else:
st_owner = os.stat(sfilename).st_uid
# needs root, user not root
if st_owner == 0 and os.getuid() != 0:
dlg = Dialogs()
do_chmod = dlg.msgbox_yesno(
"For this script to work it needs "
"to be executable, and you will need "
"to enter root's password.\n"
"Would you like to make this script executable?")
if do_chmod == gtk.RESPONSE_YES:
# use elevation command to chmod
selevcmd = ''
if os.path.isfile('/usr/bin/kdesudo'):
selevcmd = 'kdesudo'
elif os.path.isfile('/usr/bin/gksudo'):
selevcmd = 'gksudo'
else:
return ("No elevcmd found to use!, can't chmod!")
try:
os.system(selevcmd + ' chmod a+x ' + sfilename)
return (selevcmd + ' chmod +x ' + sfilename)
except Exception:
return "Unable to use elevcmd to chmod!"
else:
return "chmod declined."
else:
# doesn't need root, user is or isnt root.
os.system('chmod a+x ' + sfilename)
return ('chmod +x ' + sfilename)
def stripchars(original, chars):
""" remove chars from beginning and end of string """
if hasattr(chars, 'lower'):
chars = [c for c in chars]
if original:
while original and (original[0] in chars):
original = original[1:]
if original:
while original and (original[-1] in chars):
original = original[:-1]
return original
def stripquotes(original):
""" Trims a single quote from the string """
if ((original.startswith("'") and original.endswith("'")) or
(original.startswith('"') and original.endswith('"'))):
return original[1:-1]
return original
def trimcount(originalstring, chartotrim):
""" trims a char from the beginning of string,
and returns a count and the trimmed string.
example:
spacecnt, trimmed = trimcount(' no spaces', ' ')
# returns: (3, "no spaces")
"""
cnt = 0
while originalstring.startswith(chartotrim):
cnt += 1
originalstring = originalstring[1:]
return cnt, originalstring