import sys, os, time, re, json, datetime, ctypes, subprocessif os.name == "nt":# Windowsimport msvcrtelse:# Not Windows \o/import selectclass Utils:def __init__(self, name = "Python Script"):self.name = name# Init our colors before we need to print anythingcwd = os.getcwd()os.chdir(os.path.dirname(os.path.realpath(__file__)))if os.path.exists("colors.json"):self.colors_dict = json.load(open("colors.json"))else:self.colors_dict = {}os.chdir(cwd)def check_admin(self):# Returns whether or not we're admintry:is_admin = os.getuid() == 0except AttributeError:is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0return is_admindef elevate(self, file):# Runs the passed file as adminif self.check_admin():returnif os.name == "nt":ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, file, None, 1)else:try:p = subprocess.Popen(["which", "sudo"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)c = p.communicate()[0].decode("utf-8", "ignore").replace("\n", "")os.execv(c, [ sys.executable, 'python'] + sys.argv)except:exit(1)def compare_versions(self, vers1, vers2, **kwargs):# Helper method to compare ##.## strings## vers1 < vers2 = True# vers1 = vers2 = None# vers1 > vers2 = False# Sanitize the padspad = str(kwargs.get("pad", ""))sep = str(kwargs.get("separator", "."))ignore_case = kwargs.get("ignore_case", True)# Cast as stringsvers1 = str(vers1)vers2 = str(vers2)if ignore_case:vers1 = vers1.lower()vers2 = vers2.lower()# Split and pad listsv1_parts, v2_parts = self.pad_length(vers1.split(sep), vers2.split(sep))# Iterate and comparefor i in range(len(v1_parts)):# Remove non-numericv1 = ''.join(c.lower() for c in v1_parts[i] if c.isalnum())v2 = ''.join(c.lower() for c in v2_parts[i] if c.isalnum())# Equalize the lengthsv1, v2 = self.pad_length(v1, v2)# Compareif str(v1) < str(v2):return Trueelif str(v1) > str(v2):return False# Never differed - return None, must be equalreturn Nonedef pad_length(self, var1, var2, pad = "0"):# Pads the vars on the left side to make them equal lengthpad = "0" if len(str(pad)) < 1 else str(pad)[0]if not type(var1) == type(var2):# Type mismatch! Just return what we gotreturn (var1, var2)if len(var1) < len(var2):if type(var1) is list:var1.extend([str(pad) for x in range(len(var2) - len(var1))])else:var1 = "{}{}".format((pad*(len(var2)-len(var1))), var1)elif len(var2) < len(var1):if type(var2) is list:var2.extend([str(pad) for x in range(len(var1) - len(var2))])else:var2 = "{}{}".format((pad*(len(var1)-len(var2))), var2)return (var1, var2)def check_path(self, path):# Let's loop until we either get a working path, or no changestest_path = pathlast_path = Nonewhile True:# Bail if we've looped at least once and the path didn't changeif last_path != None and last_path == test_path: return Nonelast_path = test_path# Check if we stripped everything outif not len(test_path): return None# Check if we have a valid pathif os.path.exists(test_path):return os.path.abspath(test_path)# Check for quotesif test_path[0] == test_path[-1] and test_path[0] in ('"',"'"):test_path = test_path[1:-1]continue# Check for a tilde and expand if neededif test_path[0] == "~":tilde_expanded = os.path.expanduser(test_path)if tilde_expanded != test_path:# Got a changetest_path = tilde_expandedcontinue# Let's check for spaces - strip from the left first, then the rightif test_path[0] in (" ","\t"):test_path = test_path[1:]continueif test_path[-1] in (" ","\t"):test_path = test_path[:-1]continue# Maybe we have escapes to handle?test_path = "\\".join([x.replace("\\", "") for x in test_path.split("\\\\")])def grab(self, prompt, **kwargs):# Takes a prompt, a default, and a timeout and shows it with that timeout# returning the resulttimeout = kwargs.get("timeout", 0)default = kwargs.get("default", None)# If we don't have a timeout - then skip the timed sectionsif timeout <= 0:if sys.version_info >= (3, 0):return input(prompt)else:return str(raw_input(prompt))# Write our promptsys.stdout.write(prompt)sys.stdout.flush()if os.name == "nt":start_time = time.time()i = ''while True:if msvcrt.kbhit():c = msvcrt.getche()if ord(c) == 13: # enter_keybreakelif ord(c) >= 32: #space_chari += cif len(i) == 0 and (time.time() - start_time) > timeout:breakelse:i, o, e = select.select( [sys.stdin], [], [], timeout )if i:i = sys.stdin.readline().strip()print('') # needed to move to next lineif len(i) > 0:return ielse:return defaultdef cls(self):os.system('cls' if os.name=='nt' else 'clear')def cprint(self, message, **kwargs):strip_colors = kwargs.get("strip_colors", False)if os.name == "nt":strip_colors = Truereset = u"\u001b[0m"# Requires sys importfor c in self.colors:if strip_colors:message = message.replace(c["find"], "")else:message = message.replace(c["find"], c["replace"])if strip_colors:return messagesys.stdout.write(message)print(reset)# Needs work to resize the string if color chars exist'''# Header drawing methoddef head(self, text = None, width = 55):if text == None:text = self.nameself.cls()print(" {}".format("#"*width))len_text = self.cprint(text, strip_colors=True)mid_len = int(round(width/2-len(len_text)/2)-2)middle = " #{}{}{}#".format(" "*mid_len, len_text, " "*((width - mid_len - len(len_text))-2))if len(middle) > width+1:# Get the differencedi = len(middle) - width# Add the padding for the ...#di += 3# Trim the stringmiddle = middle[:-di]newlen = len(middle)middle += "...#"find_list = [ c["find"] for c in self.colors ]# Translate colored string to lenmiddle = middle.replace(len_text, text + self.rt_color) # always reset just in caseself.cprint(middle)print("#"*width)'''# Header drawing methoddef head(self, text = None, width = 55):if text == None:text = self.nameself.cls()print(" {}".format("#"*width))mid_len = int(round(width/2-len(text)/2)-2)middle = " #{}{}{}#".format(" "*mid_len, text, " "*((width - mid_len - len(text))-2))if len(middle) > width+1:# Get the differencedi = len(middle) - width# Add the padding for the ...#di += 3# Trim the stringmiddle = middle[:-di] + "...#"print(middle)print("#"*width)def resize(self, width, height):print('033円[8;{};{}t'.format(height, width))def custom_quit(self):self.head()print("by CorpNewt\n")print("Thanks for testing it out, for bugs/comments/complaints")print("send me a message on Reddit, or check out my GitHub:\n")print("www.reddit.com/u/corpnewt")print("www.github.com/corpnewt\n")# Get the time and wish them a good morning, afternoon, evening, and nighthr = datetime.datetime.now().time().hourif hr > 3 and hr < 12:print("Have a nice morning!\n\n")elif hr >= 12 and hr < 17:print("Have a nice afternoon!\n\n")elif hr >= 17 and hr < 21:print("Have a nice evening!\n\n")else:print("Have a nice night!\n\n")exit(0)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。