2020-04-24 20:33:55 +02:00
|
|
|
from sys import platform as OperatingSystem
|
|
|
|
|
|
|
|
Quotes = '"\''
|
|
|
|
|
|
|
|
if OperatingSystem == "win32":
|
|
|
|
PathSlash = '\\'
|
|
|
|
else:
|
|
|
|
PathSlash = '/'
|
|
|
|
FileProtocol = "file:" + 2*PathSlash
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
|
2020-04-24 20:33:55 +02:00
|
|
|
def ConvertToStandardPathFormat(Path):
|
|
|
|
""" Example,
|
|
|
|
Input: '"file:///some/path/somefile.extension"
|
|
|
|
Output: /some/path/somefile.extension
|
|
|
|
"""
|
|
|
|
Path = Path.strip(Quotes)
|
|
|
|
if Path.startswith(FileProtocol):
|
|
|
|
Path = Path[len(FileProtocol):]
|
|
|
|
return Path
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
|
2020-04-24 20:33:55 +02:00
|
|
|
def GetTextAfter(Text, ReadlinesTextFile):
|
|
|
|
for Lines in range(len(ReadlinesTextFile)):
|
|
|
|
Line = ReadlinesTextFile[Lines].strip('\n')
|
|
|
|
if Line.startswith(Text):
|
|
|
|
return Line[len(Text):]
|
|
|
|
return ''
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
|
2020-04-24 20:33:55 +02:00
|
|
|
def SingleQuoteString(String):
|
|
|
|
if len(String) > 0:
|
|
|
|
if String[0] != '\'' or String[-1] != '\'':
|
|
|
|
String = '\'' + String + '\''
|
|
|
|
return String
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
|
2020-04-24 20:33:55 +02:00
|
|
|
def DoubleQuoteString(String):
|
|
|
|
if len(String) > 0:
|
|
|
|
if String[0] != '"' or String[-1] != '"':
|
|
|
|
String = '"' + String + '"'
|
|
|
|
return String
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
|
2020-04-24 20:33:55 +02:00
|
|
|
def ListIntoString(List, QuoteItems=0, Seprator=' '):
|
|
|
|
if QuoteItems == 2:
|
|
|
|
for i in range(len(List)):
|
|
|
|
Quoteditem = DoubleQuoteString(List[i])
|
|
|
|
List[i] = Quoteditem
|
|
|
|
elif QuoteItems == 1:
|
|
|
|
for i in range(len(List)):
|
|
|
|
Quoteditem = SingleQuoteString(List[i])
|
|
|
|
List[i] = Quoteditem
|
|
|
|
Stringoflist = (Seprator).join(List)
|
|
|
|
return Stringoflist
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
|
2020-04-24 20:33:55 +02:00
|
|
|
# strip=0 => remove both ' & ", 1 => remove ', 2 => remove "
|
|
|
|
def UnquoteString(String, strip=0):
|
|
|
|
while True:
|
|
|
|
if (
|
2020-04-25 16:11:36 +02:00
|
|
|
strip != 2 and
|
|
|
|
String.startswith('"') and
|
|
|
|
String.endswith('"')
|
|
|
|
):
|
2020-04-24 20:33:55 +02:00
|
|
|
String = String.strip('"')
|
|
|
|
elif (
|
|
|
|
strip != 1
|
|
|
|
and String.startswith("'")
|
2020-04-25 16:11:36 +02:00
|
|
|
and String.endswith("'")
|
|
|
|
):
|
2020-04-24 20:33:55 +02:00
|
|
|
String = String.strip("'")
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
return String
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
|
2020-04-24 20:33:55 +02:00
|
|
|
def StandardVariableName(Variable):
|
|
|
|
Variable = Variable.casefold()
|
|
|
|
Variable = Variable.replace('_', '').replace(' ', '')
|
|
|
|
return Variable
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
"""
|
|
|
|
def DictionaryToJsonStr(Dict, BaseIndentation=0):
|
|
|
|
BI = '\t'*BaseIndentation
|
|
|
|
JsonStr = BI+'{\n'
|
|
|
|
for k, v in Dict.items():
|
|
|
|
JsonStr += BI+'\t"'+k+'" : "'+v+'",\n'
|
|
|
|
JsonStr = JsonStr[:-2]
|
|
|
|
JsonStr += '\n'+BI+'}'
|
|
|
|
return JsonStr
|
|
|
|
"""
|
2020-04-24 20:33:55 +02:00
|
|
|
|
|
|
|
def StringToKeyValuePair(String, Seprator):
|
|
|
|
SepratorAt = String.find(Seprator)
|
|
|
|
if SepratorAt >= 0:
|
|
|
|
Key = String[:SepratorAt]
|
|
|
|
Value = String[SepratorAt+1:]
|
|
|
|
return Key, Value
|
|
|
|
else:
|
|
|
|
return "", String
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
|
2020-04-24 20:33:55 +02:00
|
|
|
def FormatStrForDictinary(String):
|
|
|
|
String = String.strip(" \n\r")
|
|
|
|
return UnquoteString(String)
|
|
|
|
|
2020-04-25 16:11:36 +02:00
|
|
|
|
2020-04-24 20:33:55 +02:00
|
|
|
def StrListToDictionary(
|
|
|
|
List,
|
2020-04-25 16:11:36 +02:00
|
|
|
Seprator='=',
|
|
|
|
FormatFunction=FormatStrForDictinary
|
|
|
|
):
|
2020-04-24 20:33:55 +02:00
|
|
|
Dictionary = {}
|
|
|
|
for i in List:
|
|
|
|
k, v = StringToKeyValuePair(i, Seprator)
|
|
|
|
k, v = FormatFunction(k), FormatFunction(v)
|
|
|
|
if len(k) > 0:
|
|
|
|
Dictionary[k] = v
|
|
|
|
return Dictionary
|