Skip to content

Commit 2698548

Browse files
committed
Convert gblvars class to dict
1 parent 64ac1e8 commit 2698548

File tree

1 file changed

+45
-50
lines changed

1 file changed

+45
-50
lines changed

gitcheck/gitcheck.py

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,7 @@
2222

2323

2424
# Global vars
25-
class gblvars:
26-
verbose = False
27-
debugmod = False
28-
checkremote = False
29-
checkUntracked = False
30-
checkall = False
31-
email = False
32-
watchInterval = 0
33-
bellOnActionNeeded = False
34-
searchDir = None
35-
depth = None
36-
quiet = False
37-
ignoreBranch = r'^$' # empty string
38-
ignoreLocal = r'^$' # empty string
25+
argopts = {}
3926

4027

4128
# Class for terminal Color
@@ -62,14 +49,14 @@ class html:
6249

6350

6451
def showDebug(mess, level='info'):
65-
if gblvars.debugmod:
52+
if argopts.get('debugmod', False):
6653
print(mess)
6754

6855

6956
# Search all local repositories from current directory
7057
def searchRepositories():
7158
showDebug('Beginning scan... building list of git folders')
72-
dir = gblvars.searchDir
59+
dir = argopts.get('searchDir', None)
7360
if dir is not None and dir[-1:] == '/':
7461
dir = dir[:-1]
7562
curdir = os.path.abspath(os.getcwd()) if dir is None else dir
@@ -81,7 +68,7 @@ def searchRepositories():
8168

8269
for directory, dirnames, filenames in os.walk(curdir):
8370
level = directory.count(os.sep) - startinglevel
84-
if gblvars.depth is None or level <= gblvars.depth:
71+
if argopts.get('depth', None) is None or level <= argopts.get('depth', None):
8572
if '.git' in dirnames:
8673
showDebug(" Add %s repository" % directory)
8774
repo.append(directory)
@@ -98,7 +85,7 @@ def checkRepository(rep, branch):
9885
ditem = []
9986
gsearch = re.compile(r'^.?([A-Z]) (.*)')
10087

101-
if re.match(gblvars.ignoreBranch, branch):
88+
if re.match(argopts.get('ignoreBranch', r'^$'), branch):
10289
return False
10390

10491
changes = getLocalFilesChange(rep)
@@ -147,7 +134,7 @@ def checkRepository(rep, branch):
147134
r,
148135
count
149136
)
150-
if ischange or not gblvars.quiet:
137+
if ischange or not argopts.get('quiet', False):
151138
# Remove trailing slash from repository/directory name
152139
if rep[-1:] == '/':
153140
rep = rep[:-1]
@@ -196,16 +183,17 @@ def checkRepository(rep, branch):
196183
strlocal = ""
197184
html.strlocal = ""
198185

199-
if gblvars.email:
186+
if argopts.get('email', False):
200187
html.msg += "<li>%s/%s %s %s %s</li>\n" % (html.prjname, branch, html.strlocal, html.topush, html.topull)
201188

202189
else:
203190
print("%(prjname)s/%(branch)s %(strlocal)s%(topush)s%(topull)s" % locals())
204191

205-
if gblvars.verbose:
192+
if argopts.get('verbose', False):
206193
if ischange > 0:
207194
filename = " |--Local"
208-
if not gblvars.email: print(filename)
195+
if not argopts.get('email', False):
196+
print(filename)
209197
html.msg += '<ul><li><b>Local</b></li></ul>\n<ul>\n'
210198
for c in changes:
211199
filename = " |--%s%s%s %s%s" % (
@@ -215,7 +203,7 @@ def checkRepository(rep, branch):
215203
c[1],
216204
tcolor.DEFAULT)
217205
html.msg += '<li> <b style="color:orange">[To Commit] </b>%s</li>\n' % c[1]
218-
if not gblvars.email: print(filename)
206+
if not argopts.get('email', False): print(filename)
219207
html.msg += '</ul>\n'
220208
if branch != "":
221209
remotes = getRemoteRepositories(rep)
@@ -224,7 +212,7 @@ def checkRepository(rep, branch):
224212
if len(commits) > 0:
225213
rname = " |--%(r)s" % locals()
226214
html.msg += '<ul><li><b>%(r)s</b></li>\n</ul>\n<ul>\n' % locals()
227-
if not gblvars.email: print(rname)
215+
if not argopts.get('email', False): print(rname)
228216
for commit in commits:
229217
pcommit = " |--%s[To Push]%s %s%s%s" % (
230218
tcolor.MAGENTA,
@@ -233,7 +221,7 @@ def checkRepository(rep, branch):
233221
commit,
234222
tcolor.DEFAULT)
235223
html.msg += '<li><b style="color:blue">[To Push] </b>%s</li>\n' % commit
236-
if not gblvars.email: print(pcommit)
224+
if not argopts.get('email', False): print(pcommit)
237225
html.msg += '</ul>\n'
238226

239227
if branch != "":
@@ -243,7 +231,7 @@ def checkRepository(rep, branch):
243231
if len(commits) > 0:
244232
rname = " |--%(r)s" % locals()
245233
html.msg += '<ul><li><b>%(r)s</b></li>\n</ul>\n<ul>\n' % locals()
246-
if not gblvars.email: print(rname)
234+
if not argopts.get('email', False): print(rname)
247235
for commit in commits:
248236
pcommit = " |--%s[To Pull]%s %s%s%s" % (
249237
tcolor.MAGENTA,
@@ -252,7 +240,7 @@ def checkRepository(rep, branch):
252240
commit,
253241
tcolor.DEFAULT)
254242
html.msg += '<li><b style="color:blue">[To Pull] </b>%s</li>\n' % commit
255-
if not gblvars.email: print(pcommit)
243+
if not argopts.get('email', False): print(pcommit)
256244
html.msg += '</ul>\n'
257245

258246
return actionNeeded
@@ -262,12 +250,12 @@ def getLocalFilesChange(rep):
262250
files = []
263251
#curdir = os.path.abspath(os.getcwd())
264252
snbchange = re.compile(r'^(.{2}) (.*)')
265-
onlyTrackedArg = "" if gblvars.checkUntracked else "uno"
253+
onlyTrackedArg = "" if argopts.get('checkUntracked', False) else "uno"
266254
result = gitExec(rep, "status -s" + onlyTrackedArg)
267255

268256
lines = result.split('\n')
269257
for l in lines:
270-
if not re.match(gblvars.ignoreLocal, l):
258+
if not re.match(argopts.get('ignoreLocal', r'^$'), l):
271259
m = snbchange.match(l)
272260
if m:
273261
files.append([m.group(1), m.group(2)])
@@ -348,22 +336,22 @@ def gitExec(path, cmd):
348336

349337
# Check all git repositories
350338
def gitcheck():
351-
showDebug("Global Vars: %s" % vars(gblvars))
339+
showDebug("Global Vars: %s" % argopts)
352340

353341
repo = searchRepositories()
354342
actionNeeded = False
355343

356-
if gblvars.checkremote:
344+
if argopts.get('checkremote', False):
357345
for r in repo:
358346
print ("Updating %s remotes..." % r)
359347
updateRemote(r)
360348

361-
if gblvars.watchInterval > 0:
349+
if argopts.get('watchInterval', 0) > 0:
362350
print(tcolor.RESET)
363351

364352
showDebug("Processing repositories... please wait.")
365353
for r in repo:
366-
if (gblvars.checkall):
354+
if (argopts.get('checkall', False)):
367355
branch = getAllBranches(r)
368356
else:
369357
branch = getDefaultBranch(r)
@@ -373,7 +361,7 @@ def gitcheck():
373361
html.timestamp = strftime("%Y-%m-%d %H:%M:%S")
374362
html.msg += "</ul>\n<p>Report created on %s</p>\n" % html.timestamp
375363

376-
if actionNeeded and gblvars.bellOnActionNeeded:
364+
if actionNeeded and argopts.get('bellOnActionNeeded', False):
377365
print(tcolor.BELL)
378366

379367

@@ -435,6 +423,12 @@ def initEmailConfig():
435423
json.dump(config, fp=open(filename, 'w'), indent=4)
436424
print('Please, modify config file located here : %s' % filename)
437425

426+
def readDefaultConfig():
427+
filename = expanduser('~/.gitcheck')
428+
if os.path.exists(filename):
429+
pass
430+
431+
438432

439433
def usage():
440434
print("Usage: %s [OPTIONS]" % (sys.argv[0]))
@@ -473,41 +467,42 @@ def main():
473467
print(e.msg)
474468
sys.exit(2)
475469

470+
readDefaultConfig()
476471
for opt, arg in opts:
477472
if opt in ["-v", "--verbose"]:
478-
gblvars.verbose = True
473+
argopts['verbose'] = True
479474
elif opt in ["--debug"]:
480-
gblvars.debugmod = True
475+
argopts['debugmod'] = True
481476
elif opt in ["-r", "--remote"]:
482-
gblvars.checkremote = True
477+
argopts['checkremote'] = True
483478
elif opt in ["-u", "--untracked"]:
484-
gblvars.checkUntracked = True
479+
argopts['checkUntracked'] = True
485480
elif opt in ["-b", "--bell"]:
486-
gblvars.bellOnActionNeeded = True
481+
argopts['bellOnActionNeeded'] = True
487482
elif opt in ["-w", "--watch"]:
488483
try:
489-
gblvars.watchInterval = float(arg)
484+
argopts['watchInterval'] = float(arg)
490485
except ValueError:
491486
print("option %s requires numeric value" % opt)
492487
sys.exit(2)
493488
elif opt in ["-i", "--ignore-branch"]:
494-
gblvars.ignoreBranch = arg
489+
argopts['ignoreBranch'] = arg
495490
elif opt in ["-l", "--localignore"]:
496-
gblvars.ignoreLocal = arg
491+
argopts['ignoreLocal'] = arg
497492
elif opt in ["-d", "--dir"]:
498-
gblvars.searchDir = arg
493+
argopts['searchDir'] = arg
499494
elif opt in ["-m", '--maxdepth']:
500495
try:
501-
gblvars.depth = int(arg)
496+
argopts['depth'] = int(arg)
502497
except ValueError:
503498
print("option %s requires int value" % opt)
504499
sys.exit(2)
505500
elif opt in ["-q", "--quiet"]:
506-
gblvars.quiet = True
501+
argopts['quiet'] = True
507502
elif opt in ["-e", "--email"]:
508-
gblvars.email = True
503+
argopts['email'] = True
509504
elif opt in ["-a", "--all-branch"]:
510-
gblvars.checkall = True
505+
argopts['checkall'] = True
511506
elif opt in ["--init-email"]:
512507
initEmailConfig()
513508
sys.exit(0)
@@ -521,11 +516,11 @@ def main():
521516
while True:
522517
gitcheck()
523518

524-
if gblvars.email:
519+
if argopts.get('email', False):
525520
sendReport(html.msg)
526521

527-
if gblvars.watchInterval > 0:
528-
time.sleep(gblvars.watchInterval)
522+
if argopts.get('watchInterval', 0) > 0:
523+
time.sleep(argopts.get('watchInterval', 0))
529524
else:
530525
break
531526

0 commit comments

Comments
 (0)