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

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

import re 

 

from pyrocko import guts 

from pyrocko.guts import make_typed_list_class, String, StringChoice, List, \ 

Int, Object, Union, Bool, Defer 

 

from pyrocko.io.io_common import FileLoadError 

 

ResourceTypeList = make_typed_list_class(String) 

 

 

guts_prefix = 'wadl' 

guts_xmlns = 'http://wadl.dev.java.net/2009/02' 

 

re_rmsite = re.compile(r'https?://[^/]+') 

re_multisl = re.compile(r'/+') 

 

 

def clean_path(p): 

p = re_rmsite.sub('', p) 

p = re_multisl.sub('/', p) 

if not p.startswith('/'): 

p = '/' + p 

 

return p 

 

 

class HTTPMethods(StringChoice): 

choices = [ 

'GET', 

'POST', 

'PUT', 

'HEAD', 

'DELETE'] 

 

 

UriList = make_typed_list_class(String) 

 

StatusCodeList = make_typed_list_class(Int) 

 

 

class ParamStyle(StringChoice): 

choices = [ 

'plain', 

'query', 

'matrix', 

'header', 

'template'] 

 

 

class Doc(Object): 

xmltagname = 'doc' 

title = String.T(optional=True, xmlstyle='attribute') 

 

 

class Method2(Union): 

members = [HTTPMethods, String] 

 

 

class Include(Object): 

xmltagname = 'include' 

href = String.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

 

 

class Option(Object): 

xmltagname = 'option' 

value = String.T(xmlstyle='attribute') 

media_type = String.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

 

 

class Link(Object): 

xmltagname = 'link' 

resource_type = String.T( 

optional=True, xmlstyle='attribute', xmltagname='resource_type') 

rel = String.T(optional=True, xmlstyle='attribute') 

rev = String.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

 

 

class Grammars(Object): 

xmltagname = 'grammars' 

doc_list = List.T(Doc.T()) 

include_list = List.T(Include.T()) 

 

 

class DerefError(Exception): 

pass 

 

 

class Element(Object): 

 

def __init__(self, *args, **kwargs): 

Object.__init__(self, *args, **kwargs) 

self._hyper = None 

 

def _update(self, hyper): 

self._hyper = hyper 

 

id_ = getattr(self, 'id', None) 

 

if id_: 

hyper[self.xmltagname][id_] = self 

 

for child in self.get_children(): 

child._update(hyper) 

 

def get_children(self): 

raise NotImplementedError() 

 

def deref(self): 

if not self._hyper: 

raise Exception('Must call _update() before calling deref()') 

 

obj = self 

seen = set() 

while True: 

seen.add(obj) 

href = getattr(obj, 'href', None) 

if href: 

try: 

obj = obj._hyper[obj.xmltagname][href.lstrip('#')] 

if obj in seen: 

raise DerefError('cyclic reference') 

 

except KeyError: 

raise DerefError(href) 

 

else: 

return obj 

 

 

class Param(Element): 

xmltagname = 'param' 

href = String.T(optional=True, xmlstyle='attribute') 

name = String.T(optional=True, xmlstyle='attribute') 

style = ParamStyle.T(optional=True, xmlstyle='attribute') 

id = String.T(optional=True, xmlstyle='attribute') 

type = String.T(default='xs:string', optional=True, xmlstyle='attribute') 

default = String.T(optional=True, xmlstyle='attribute') 

required = Bool.T(default='false', optional=True, xmlstyle='attribute') 

repeating = Bool.T(default='false', optional=True, xmlstyle='attribute') 

fixed = String.T(optional=True, xmlstyle='attribute') 

path = String.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

option_list = List.T(Option.T()) 

link = Link.T(optional=True) 

 

def describe(self, indent): 

return indent + self.name 

 

def get_children(self): 

return [] 

 

 

class Representation(Element): 

xmltagname = 'representation' 

id = String.T(optional=True, xmlstyle='attribute') 

element = String.T(optional=True, xmlstyle='attribute') 

media_type = String.T(optional=True, xmlstyle='attribute') 

href = String.T(optional=True, xmlstyle='attribute') 

profile = UriList.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

param_list = List.T(Param.T()) 

 

def get_children(self): 

return self.param_list 

 

 

class Request(Element): 

xmltagname = 'request' 

doc_list = List.T(Doc.T()) 

param_list = List.T(Param.T()) 

representation_list = List.T(Representation.T()) 

 

def iter_params(self): 

for param in self.param_list: 

param = param.deref() 

yield param 

 

def describe(self, indent): 

lines = [] 

for param in self.iter_params(): 

lines.append(param.describe(indent)) 

 

return lines 

 

def get_children(self): 

return self.param_list + self.representation_list 

 

 

class Response(Element): 

xmltagname = 'response' 

status = StatusCodeList.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

param_list = List.T(Param.T()) 

representation_list = List.T(Representation.T()) 

 

def get_children(self): 

return self.param_list + self.representation_list 

 

 

class Method(Element): 

xmltagname = 'method' 

id = String.T(optional=True, xmlstyle='attribute') 

name = String.T(optional=True, xmlstyle='attribute') 

href = String.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

request = Request.T(optional=True) 

response_list = List.T(Response.T()) 

 

def describe(self, indent): 

lines = [indent + self.name] 

if self.request: 

lines.extend(self.request.describe(' ' + indent)) 

 

return lines 

 

def get_children(self): 

return ([self.request] if self.request else []) + self.response_list 

 

 

class Resource(Element): 

xmltagname = 'resource' 

id = String.T(optional=True, xmlstyle='attribute') 

type = String.T(optional=True, xmlstyle='attribute') 

query_type = String.T( 

default='application/x-www-form-urlencoded', 

optional=True, 

xmlstyle='attribute') 

path = String.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

param_list = List.T(Param.T()) 

method_list = List.T(Method.T()) 

resource_list = List.T(Defer('Resource.T')) 

 

def iter_resources(self): 

yield self.path, self 

for res in self.resource_list: 

yield self.path + '/' + res.path, res 

 

def iter_methods(self): 

for method in self.method_list: 

method = method.deref() 

yield method 

 

def describe(self, indent): 

lines = [] 

for met in self.iter_methods(): 

lines.extend(met.describe(' ' + indent)) 

 

return lines 

 

def get_children(self): 

return self.param_list + self.method_list + self.resource_list 

 

 

class Resources(Element): 

xmltagname = 'resources' 

base = String.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

resource_list = List.T(Resource.T()) 

 

def iter_resources(self): 

for res in self.resource_list: 

for p, sr in res.iter_resources(): 

yield self.base + '/' + p, sr 

 

def get_children(self): 

return self.resource_list 

 

 

class ResourceType(Element): 

xmltagname = 'resource_type' 

id = String.T(optional=True, xmlstyle='attribute') 

doc_list = List.T(Doc.T()) 

param_list = List.T(Param.T()) 

method_list = List.T(Method.T()) 

resource_list = List.T(Resource.T()) 

 

def get_children(self): 

return self.param_list + self.method_list + self.resource_list 

 

 

class Application(Element): 

xmltagname = 'application' 

guessable_xmlns = [guts_xmlns] 

 

doc_list = List.T(Doc.T()) 

grammars = Grammars.T(optional=True) 

resources_list = List.T(Resources.T()) 

resource_type_list = List.T(ResourceType.T(xmltagname='resource_type')) 

method_list = List.T(Method.T()) 

representation_list = List.T(Representation.T()) 

param_list = List.T(Param.T()) 

 

def get_children(self): 

return self.resources_list + self.resource_type_list \ 

+ self.method_list + self.representation_list \ 

+ self.param_list 

 

def update(self, force=False): 

if self._hyper is None or force: 

hyper = dict( 

resource_type={}, 

resource={}, 

method={}, 

representation={}, 

param={}) 

 

self._update(hyper) 

 

def iter_resources(self): 

self.update() 

for rs in self.resources_list: 

for p, res in rs.iter_resources(): 

yield clean_path(p), res 

 

def iter_requests(self): 

for res_path, res in self.iter_resources(): 

for method in res.iter_methods(): 

if method.request: 

yield res_path, method.name, method.request 

 

def supported_param_names(self, path, method='GET'): 

path = clean_path(path) 

for res_path, method_name, request in self.iter_requests(): 

if res_path == path and method_name == method: 

return [param.name for param in request.param_list] 

 

def describe(self, indent=''): 

lines = [] 

for res_path, res in self.iter_resources(): 

lines.append(indent + res_path) 

lines.extend(res.describe(indent)) 

 

return lines 

 

def __str__(self): 

return '\n'.join(self.describe()) 

 

 

def load_xml(*args, **kwargs): 

wadl = guts.load_xml(*args, **kwargs) 

if not isinstance(wadl, Application): 

FileLoadError('Not a WADL file.') 

 

return wadl 

 

 

if __name__ == '__main__': 

import os 

import sys 

import urllib.request 

 

if os.path.exists(sys.argv[1]): 

wadl = load_xml(filename=sys.argv[1]) 

else: 

f = urllib.request.urlopen(sys.argv[1]) 

wadl = load_xml(stream=f) 

 

print(wadl)