/wscompose/validate.py

https://github.com/netconstructor/modestmaps-py · Python · 378 lines · 199 code · 138 blank · 41 comment · 65 complexity · dada321bee31f95afdff8aff12b7455e MD5 · raw file

  1. # -*-python-*-
  2. __version__ = "1.1"
  3. __author__ = "Aaron Straup Cope"
  4. __url__ = "http://www.aaronland.info/python/wscompose"
  5. __date__ = "$Date: 2008/01/04 06:23:46 $"
  6. __copyright__ = "Copyright (c) 2007-2008 Aaron Straup Cope. BSD license : http://www.modestmaps.com/license."
  7. import re
  8. import string
  9. import urlparse
  10. class validate :
  11. def __init__ (self) :
  12. self.re = {
  13. 'coord' : re.compile(r"^-?\d+(?:\.\d+)?$"),
  14. 'adjust' : re.compile(r"^(\d+(?:\.\d*)?|\d*?\.\d+)$"),
  15. 'num' : re.compile(r"^\d+$"),
  16. 'provider' : re.compile(r"^(\w+)$"),
  17. 'label' : re.compile(r"^(?:[a-z0-9-_\.]+)$"),
  18. 'hull' : re.compile(r"^(marker|dot|plot)$")
  19. }
  20. # ##########################################################
  21. def regexp (self, label, string) :
  22. if not self.re.has_key(label) :
  23. return False
  24. return self.re[label].match(string)
  25. # ##########################################################
  26. def ensure_args (self, args, required) :
  27. for i in required :
  28. if not args.has_key(i) :
  29. raise Exception, "Required argument %s missing" % i
  30. return True
  31. # ##########################################################
  32. def bbox (self, input) :
  33. bbox = input.split(",")
  34. if len(bbox) != 4 :
  35. raise Exception, "Missing or incomplete %s parameter" % 'bbox'
  36. bbox = map(string.strip, bbox)
  37. for pt in bbox :
  38. if not self.regexp('coord', pt) :
  39. raise Exception, "Not a valid lat/long : %s" % pt
  40. return map(float, bbox)
  41. # ##########################################################
  42. def bbox_adjustment (self, input) :
  43. if not self.regexp('adjust', str(input)) :
  44. raise Exception, "Not a valid adjustment %s " % input
  45. return float(input)
  46. # ##########################################################
  47. def latlon (self, input) :
  48. if not self.regexp('coord', input) :
  49. raise Exception, "Not a valid lat/long : %s" % input
  50. return float(input)
  51. # ##########################################################
  52. def zoom (self, input) :
  53. return self.__num(input)
  54. # ##########################################################
  55. def dimension (self, input) :
  56. return self.__num(input)
  57. # ##########################################################
  58. def radius (self, input) :
  59. return self.__num(input)
  60. # ##########################################################
  61. def provider (self, input) :
  62. if input.startswith('http://'):
  63. # probably a URI template thing, let it slide
  64. return input
  65. input = input.upper()
  66. if not self.regexp('provider', input) :
  67. raise Exception, "Not a valid provider : %s" % input
  68. return input
  69. # ###########################################################
  70. def marker_label (self, input) :
  71. if not self.regexp('label', input) :
  72. raise Exception, "Not a valid marker label"
  73. return unicode(input)
  74. # ##########################################################
  75. def markers (self, markers) :
  76. valid = []
  77. for pos in markers :
  78. marker_data = {'width':75, 'height':75, 'adjust_cone_height' : 0}
  79. details = pos.split(",")
  80. details = map(string.strip, details)
  81. if len(details) < 3 :
  82. raise Exception, "Missing or incomplete %s parameter : %s" % ('marker', pos)
  83. #
  84. # Magic center/zoom markers
  85. #
  86. if details[0] == 'center' :
  87. marker_data['fill'] = 'center'
  88. marker_data['label'] = 'center'
  89. try :
  90. marker_data['provider'] = self.provider(details[1])
  91. except Exception, e :
  92. raise Exception, e
  93. try :
  94. marker_data['zoom'] = self.zoom(details[2])
  95. except Exception, e :
  96. raise Exception, e
  97. #
  98. # Pinwin name/label
  99. #
  100. else :
  101. try :
  102. marker_data['label'] = self.marker_label(details[0])
  103. except Exception, e :
  104. raise Exception, e
  105. # Pinwin location
  106. try :
  107. marker_data['latitude'] = self.latlon(details[1])
  108. except Exception, e :
  109. raise Exception, e
  110. try :
  111. marker_data['longitude'] = self.latlon(details[2])
  112. except Exception, e :
  113. raise Exception, e
  114. #
  115. # Shared
  116. #
  117. #
  118. # Pinwin size
  119. #
  120. if len(details) > 3 :
  121. if len(details) < 4 :
  122. raise Exception, "Missing height parameter"
  123. try :
  124. marker_data['width'] = self.dimension(details[3])
  125. except Exception, e :
  126. raise Exception, e
  127. try :
  128. marker_data['height'] = self.dimension(details[4])
  129. except Exception, e :
  130. raise Exception, e
  131. # URI for content to fill the pinwin with
  132. if len(details) > 5 :
  133. try :
  134. parts = urlparse.urlparse(details[5])
  135. except Exception, e :
  136. raise Exception, e
  137. if parts[1] == '' and parts[0] != 'file' :
  138. raise Exception, "Unknown URL"
  139. marker_data['fill'] = details[5]
  140. # Done
  141. valid.append(marker_data)
  142. return valid
  143. # ##########################################################
  144. def plots (self, plots) :
  145. valid = []
  146. for pos in plots :
  147. details = pos.split(",")
  148. details = map(string.strip, details)
  149. if len(details) < 3 :
  150. raise Exception, "Missing or incomplete %s parameter : %s" % ('plot', pos)
  151. data = {}
  152. try :
  153. data['label'] = self.marker_label(details[0])
  154. except Exception, e :
  155. raise Exception, e
  156. # Pinwin location
  157. try :
  158. data['latitude'] = self.latlon(details[1])
  159. except Exception, e :
  160. raise Exception, e
  161. try :
  162. data['longitude'] = self.latlon(details[2])
  163. except Exception, e :
  164. raise Exception, e
  165. valid.append(data)
  166. return valid
  167. # ##########################################################
  168. def dots (self, dots) :
  169. valid = []
  170. for pos in dots :
  171. details = pos.split(",")
  172. details = map(string.strip, details)
  173. cnt = len(details)
  174. if cnt < 3 :
  175. raise Exception, "Missing or incomplete %s parameter : %s" % ('dot', pos)
  176. data = {}
  177. try :
  178. data['label'] = self.marker_label(details[0])
  179. except Exception, e :
  180. raise Exception, e
  181. # Pinwin location
  182. try :
  183. data['latitude'] = self.latlon(details[1])
  184. except Exception, e :
  185. raise Exception, e
  186. try :
  187. data['longitude'] = self.latlon(details[2])
  188. except Exception, e :
  189. raise Exception, e
  190. #
  191. if cnt > 3 :
  192. try :
  193. data['radius'] = self.radius(details[3])
  194. except Exception, e :
  195. raise Exception, e
  196. else :
  197. data['radius'] = 18
  198. #
  199. if cnt > 4 :
  200. # fix me
  201. pass
  202. else :
  203. data['colour'] = 'red'
  204. #
  205. valid.append(data)
  206. return valid
  207. # ##########################################################
  208. def polylines (self, lines) :
  209. valid = []
  210. for poly in lines :
  211. points = []
  212. for pt in poly.split(" ") :
  213. coord = pt.split(",")
  214. if len(coord) != 2 :
  215. raise Exception, "Polyline coordinate missing data"
  216. (lat, lon) = map(string.strip, coord)
  217. lat = self.latlon(lat)
  218. lon = self.latlon(lon)
  219. points.append({'latitude':lat, 'longitude':lon})
  220. valid.append(points)
  221. return valid
  222. # ##########################################################
  223. def convex (self, hulls) :
  224. valid = []
  225. for label in hulls :
  226. if not self.regexp('hull', label) :
  227. raise Exception, "Unknown marker type for convex hulls"
  228. valid.append(label)
  229. return valid
  230. # ##########################################################
  231. def json_callback(self, func) :
  232. if not self.re['label'].match(func) :
  233. raise Exception, "Invalid JSON callback name"
  234. return func
  235. # ##########################################################
  236. def __num (self, input) :
  237. if not self.regexp('num', input) :
  238. raise Exception, "Not a valid number : %s" % p
  239. return int(input)
  240. # ##########################################################