-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathorgplist.py
230 lines (196 loc) · 6.31 KB
/
orgplist.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import sublime
import sublime_plugin
import os
import base64
import urllib.request
import OrgExtended.asettings as sets
import OrgExtended.orgutil.util as util
import uuid
import re
import logging
from OrgExtended.orgutil.addmethod import *
log = logging.getLogger(__name__)
class ExclusivityLists:
def __init__(self):
self.lookup = {}
def Add(self, l):
for i in l:
self.lookup[i] = l
def Find(self,k):
if(k in self.lookup):
return self.lookup[k]
return None
class PListExclusiveLists:
def __init__(self):
self.keys = {}
def Add(self,k,ex):
self.keys[k] = ex
def Has(self,k):
return k in self.keys
def AddList(self,k,l):
rlist = None
if(k in self.keys):
rlist = self.keys[k]
else:
rlist = ExclusivityLists()
self.keys[k] = rlist
rlist.Add(l)
def AddBool(self,k):
self.AddList(k,['t','true','false','nil','on','yes','no','off'])
def Get(self,k):
if(k in self.keys):
return self.keys[k]
return None
def GetParam(self,k,p):
l = self.Get(k)
if(l):
return l.Find(p)
RE_FN_MATCH = re.compile(r"\s+[:]([a-zA-Z][a-zA-Z0-9-_]*)\s+(([a-zA-Z][a-zA-Z0-9]*\s*[=]\s*[\"][^\"]+[\"])|((([ ](?!:))|[^ ()\"])+)|([(][^)]+[)])|([\"][^\"]+[\"]))")
class PList:
def __init__(self,plist):
self.params = plist
self.exList = PListExclusiveLists()
def AddExclusiveList(self,el):
self.exList = el
def Get(self, name, defaultValue):
if(name in self.params and self.params[name]):
v = self.params[name]
if(isinstance(v,str)):
v = v.strip()
if(v.startswith("\"")):
v = v[1:]
if(v.endswith("\"")):
v = v[:-1]
return v
return defaultValue
def FormatData(self,x):
if(x.startswith("\"")):
x = x[1:]
if(x.endswith("\"")):
x = x[:-1]
return x
def GetStr(self,name,defaultValue):
v = self.Get(name,defaultValue)
if(isinstance(v,list)):
v = ' '.join(v)
return v
def GetInt(self,name,defaultValue):
v = self.Get(name,defaultValue)
try:
return int(v)
except:
return defaultValue
def GetBool(self,name):
v = self.Get(name,"no")
if(isinstance(v,list) and len(v) == 1):
v = v[0]
if(v and isinstance(v,str)):
v = v.lower()
if(v == 'yes' or v == 't' or v == 'true' or v == '1' or v == 'on'):
return True
return False
def GetFloat(self,name,defaultValue):
v = self.Get(name,defaultValue)
try:
return float(v)
except:
return defaultValue
def GetList(self,name,defaultValue):
v = self.Get(name,defaultValue)
if(not isinstance(v,list)):
return util.ToList(v)
return v
def GetIntList(self,name,defaultValue):
v = self.Get(name,defaultValue)
if(not isinstance(v,list)):
return util.ToIntList(v)
return v
def GetDict(self,name,defaultValue):
out = {}
v = self.Get(name,defaultValue)
if(isinstance(v,str)):
vs = v.split('=')
if(len(vs) == 2):
out[vs[0].strip()] = self.FormatData(vs[1].strip())
return out
if(isinstance(v,list)):
for i in v:
vs = i.split('=')
if(len(vs) == 2):
out[vs[0].strip()] = self.FormatData(vs[1].strip())
if(len(out) > 0):
return out
return defaultValue
def Add(self,key,val):
PList.addToParam(self.params,key,val)
def Has(self,key):
return key in self.params
def Replace(self,key,val):
self.params[key] = val
def AddFromPList(self,strData):
if(None == strData):
return
d = PList.plistParse(strData)
for k in d:
val = d[k]
if(isinstance(val,list)):
for vv in val:
self.addToParam(self.params,k,vv,self.exList)
else:
self.addToParam(self.params,k,val,self.exList)
@staticmethod
def addToParam(params,key,val,exList=None):
if(isinstance(val,str)):
val = val.strip()
if(val.startswith("\"")):
val = val[1:]
if(val.endswith("\"")):
val = val[:-1]
if(key in params):
v = params[key]
# Promote to a list if its just a string.
if(isinstance(v,str)):
params[key] = []
params[key].append(v)
if(exList and exList.Has(key)):
if(not isinstance(val,list)):
val = util.ToList(val)
for vv in val:
eList = exList.GetParam(key,vv)
# Remove exclusive items from the list.
if(eList):
params[key] = list(set(params[key]) - set(eList))
params[key].append(vv)
else:
params[key].append(val)
else:
if(exList and exList.Has(key)):
if(not isinstance(val,list)):
val = util.ToList(val)
for vv in val:
eList = exList.GetParam(key,vv)
# Remove exclusive items from the list.
if(eList and key in params):
params[key] = list(set(params[key]) - set(eList))
elif(not key in params):
params[key] = []
params[key].append(vv)
else:
params[key] = val
@staticmethod
def plistParse(data):
if(isinstance(data,list)):
data = " ".join(data)
paramstr = " " + data
params = {}
for m in RE_FN_MATCH.finditer(paramstr):
key = m.group(1)
val = m.group(2)
PList.addToParam(params,key,val)
return params
@staticmethod
def createPList(data=None):
if(data == None):
data = ""
params = PList.plistParse(data)
return PList(params)