-
Notifications
You must be signed in to change notification settings - Fork 1
/
__core.py
61 lines (50 loc) · 1.58 KB
/
__core.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
import os
import ctypes
import subprocess
import xml.etree.cElementTree as ET
def executeNonQuery(cmd):
_ =subprocess.getoutput(cmd)
def execute(cmd):
binOut = subprocess.check_output(cmd, shell=True)
strOut = ""
try:
strOut = binOut.decode("utf-8")
except UnicodeDecodeError:
strOut = binOut.decode("latin-1")
return __cleanLinebreaks(strOut)
def executeAndGetLines(cmd):
out = execute(cmd)
strList = stripList(out.split("\n"))
return removeEmpty(strList)
def removeEmpty(strList):
return [x for x in strList if x != ""]
def stripList(strList):
for idx, elem in enumerate(strList):
strList[idx] = elem.strip()
return strList
def __cleanLinebreaks(text):
return text.replace("\r\n", "\n")
def xmlToDict(xmlFile):
tree = ET.parse(xmlFile)
treeRoot = tree.getroot()
root = {}
__rekXmlIterator(treeRoot, root)
return root
def __rekXmlIterator(treeElement, inputStructure):
for node in treeElement:
tag = node.tag.split("}")[1]
if len(node) > 0:
inputStructure[tag] = {}
__rekXmlIterator(node, inputStructure[tag])
else: inputStructure[tag] = node.text
def extractFirstElementfromDict(inputDict):
return list(inputDict.keys())[0]
def isAdmin():
try:
is_admin = os.getuid() == 0
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
return is_admin
class NotAdminException(Exception):
def __init__(self):
super().__init__("The requested operation requires elevation (Run as administrator)")