-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytools.py
executable file
·149 lines (134 loc) · 4.12 KB
/
mytools.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
#!/usr/local/bin/env python
#title :mytools.py
#description :Updates local MongoDB data from Opmantek and SNow CMDB
#author :Ricky Laney
#date :20171215
#version :1.0
#usage :python mytools.py or ./mytools.py
#notes :
#python_version :2.7.13
#==============================================================================
from __future__ import absolute_import, division, print_function
from getpass import getpass
#import logging
from functools import wraps
import os
import signal
import sys
from subprocess import getoutput
import time
sig1 = signal.signal(signal.SIGPIPE, signal.SIG_DFL) # IOError: Broken pipe
sig2 = signal.signal(signal.SIGINT, signal.SIG_DFL) # KeyboardInterrupt: Ctrl-C
#FORMAT1 = '%(asctime)s:%(name)s:%(message)s'
#FORMAT2 = '%(asctime)s: %(levelname)s: %(lineno)d: %(message)s'
#file_name = os.path.basename(__file__).replace('.py', '.log')
#
#logging.basicConfig(filename=MY_REPOS, filemode='w',
# format=FORMAT, level=logging.INFO)
#
#logger = logging.getLogger(file_name)
#logger.setLevel(logging.DEBUG)
#
#formatter = logging.Formatter(FORMAT)
#
#file_handler = logging.FileHandler('sample.log')
#file_handler.setLevel(logging.ERROR)
#file_handler.setFormatter(formatter)
#
#stream_handler = logging.StreamHandler()
#stream_handler.setFormatter(formatter)
#
#logger.addHandler(file_handler)
#logger.addHandler(stream_handler)
def get_input(prompt=''):
try:
line = raw_input(prompt)
except NameError:
line = input(prompt)
return line
def get_creds():
''' Prompts for, and returns, a username and password. '''
username = get_input('Enter Username: ')
password = None
while not password:
password = getpass()
password_verify = getpass('Retype Password: ')
if password != password_verify:
print('Your passwords do not match. Try again.')
password = None
return username, password
def byteify(data):
if isinstance(data, dict):
return {byteify(key): byteify(value)
for key, value in data.iteritems()}
elif isinstance(data, list):
return [byteify(element) for element in data]
elif isinstance(data, unicode):
return data.encode('utf-8')
else:
return data
def timerfunc(func):
"""A timer decorator
"""
@wraps(func)
def function_timer(*args, **kwargs):
"""A nested function for timing other functions
"""
start = time.time()
value = func(*args, **kwargs)
end = time.time()
runtime = end - start
msg = "The runtime for {func} took {time} seconds to complete"
print(msg.format(func=func.__name__, time=runtime))
return value
return function_timer
def select_editor(file_to_edit, lineno=0):
# Choices to open the file with.
CHOICES = """
0) None
1) Vim
2) Sublime
3) VSCode
4) PyCharm
5) MacVim
"""
choice_map = {
0: 'None',
1: 'Vim',
2: 'Sublime',
3: 'VSCode',
4: 'PyCharm',
5: 'MacVim'
}
_cwd = os.getcwd()
print(CHOICES)
# grab the line number after header while the user is thinking
with open(file_to_edit, 'r') as f:
lines = f.readlines()
lineno = len(lines)
editor = get_input("Select a number: ")
if not editor or editor == "0":
exit()
elif editor == "1":
os.system(f"vim +{lineno} {file_to_edit}")
exit()
elif editor == "2":
os.system(f"subl -n -a {_cwd} {file_to_edit}")
exit()
elif editor == "3":
os.system(f"code -n -a {_cwd} -g {file_to_edit}:{lineno}")
exit()
elif editor == "4":
os.system(f"charm {_cwd} {file_to_edit}:{lineno}")
exit()
elif editor == "5":
os.system(f"mvim +{lineno} {file_to_edit}")
exit()
elif editor == "6":
os.system(f"atom -n {file_to_edit}")
exit()
else:
os.system("clear")
print("\nI do not understand your answer.\n")
print("Press <Ctrl + C> to quit.\n")
return select_editor()