PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/RNpicker/src/ctbto/sandbox/scrapbook.py

https://github.com/gaubert/java-balivernes
Python | 488 lines | 453 code | 24 blank | 11 comment | 11 complexity | fadedd89ae60345fa63215be58f3c005 MD5 | raw file
  1. #from StringIO import StringIO
  2. #import operator
  3. #import string
  4. import subprocess
  5. import base64
  6. import os
  7. import zlib
  8. import ctbto.common.utils
  9. #import ctbto.common.time_utils as time_utils
  10. import re
  11. import getopt
  12. import tokenize
  13. import StringIO
  14. import logging.config
  15. from org.ctbto.conf.conf_helper import Conf
  16. from ctbto.db.rndata import RemoteFSDataSource
  17. from ctbto.query import RequestParser
  18. from ctbto.transformer import XML2HTMLRenderer
  19. #
  20. str1 = """Dallas Cowboys football practice at Valley Ranch was delayed on Wednesday
  21. #
  22. for nearly two hours. One of the players, while on his way to the locker
  23. #
  24. room happened to look down and notice a suspicious looking, unknown white
  25. #
  26. powdery substance on the practice field.
  27. #
  28. #
  29. The coaching staff immediately suspended practice while the FBI was
  30. #
  31. called in to investigate. After a complete field analysis, the FBI
  32. #
  33. determined that the white substance unknown to the players was the goal
  34. #
  35. line.
  36. #
  37. #
  38. Practice was resumed when FBI Special Agents decided that the team would not
  39. #
  40. be likely to encounter the substance again.
  41. #
  42. """
  43. def f(a,b):
  44. return a+b
  45. class curry:
  46. def __init__(self, fun, *args, **kwargs):
  47. self.fun = fun
  48. self.pending = args[:]
  49. self.kwargs = kwargs.copy()
  50. def __call__(self, *args, **kwargs):
  51. if kwargs and self.kwargs:
  52. kw = self.kwargs.copy()
  53. kw.update(kwargs)
  54. else:
  55. kw = kwargs or self.kwargs
  56. return self.fun(*(self.pending + args), **kw)
  57. def testCurrying():
  58. double = curry(string.ljust,width=20)
  59. res = double("totototo")
  60. print "Res [%s]\n"%(res)
  61. def testsubProcess():
  62. res = subprocess.call(["/home/aubert/sftpTest.sh","08_sep_01"])
  63. #res = subprocess.call(["ls","-la"])
  64. print "res=%d\n"%(res)
  65. def testCompress():
  66. zstr1 = zlib.compress(str1)
  67. print "Length of zipped str1 =", len(zstr1)
  68. uncompressed = zlib.decompress(zstr1)
  69. print "Lenght of unzipped str1 = ",len(uncompressed)
  70. s = base64.b64encode("ADDDDDD")
  71. print "S=%s\n"%(s)
  72. def testRemoteDataSource():
  73. l = "/tmp/toto/titi/t.tmp"
  74. print "basename = %s\n"%(os.path.basename(l))
  75. input = RemoteFSDataSource("/ops/data/rn/spectrum/08_aug_14/fjp26_001_882731g.s")
  76. for line in input:
  77. print "the line = %s\n"%(line)
  78. def testMakedirs():
  79. ctbto.common.utils.makedirs("/tmp/tata/r")
  80. def testCheckThatNoTagsAreLeft():
  81. srcMustMatch=""" <Hello firstTag="${_j_j_j_}>
  82. <T1> ${TAG_2} </T1>
  83. </Hello>
  84. """
  85. srcNoMatch=""" <Hello>
  86. <T1> ${BALDLDLDL </T1>
  87. </Hello>
  88. """
  89. pattern="\${\w*}"
  90. res = re.findall(pattern, srcNoMatch)
  91. print "NoMatch =[%s]\n"%(len(res))
  92. res = re.findall(pattern, srcMustMatch)
  93. print "Match =[%s]\n"%(len(res))
  94. def testParseSpectrumInfo():
  95. strToParse = "spectrum=PREL/QC/BK"
  96. pattern ="(?P<command>\s*spectrum\s*=\s*)(?P<values>[\w+\s*/\s*]*\w)\s*"
  97. reSpec = re.compile(pattern, re.IGNORECASE)
  98. m = reSpec.match(strToParse)
  99. if m is not None:
  100. #print "Matooch =[%s],matched=%s\n"%(len(res),res)
  101. print "command = %s\n"%(m.group('command'))
  102. values = m.group('values')
  103. print "vakues = %s\n"%(values)
  104. vals = values.split('/')
  105. if len(vals) == 0:
  106. print "There is a problem\n"
  107. for val in vals:
  108. print "val = %s\n"%(val.strip().upper())
  109. def checksumTest(str):
  110. chksum = 0L
  111. toggle = 0
  112. i = 0
  113. while i < len(str):
  114. ch = str[i]
  115. if ord(ch) > 0:
  116. if toggle: chksum = chksum << 1
  117. chksum = chksum + ord(ch)
  118. chksum = chksum & 0X7fffffff
  119. toggle = not toggle
  120. else:
  121. chksum = chksum + 1
  122. i = i + 1
  123. return chksum
  124. def testXml2Html():
  125. r = XML2HTMLRenderer('/home/aubert/dev/src-reps/java-balivernes/RNpicker/etc/conf/templates','ArrHtml.html')
  126. result = r.render('/home/aubert/dev/src-reps/java-balivernes/RNpicker/etc/ext/sampml-full-239646.xml')
  127. utils.printInFile(result,"/tmp/Transformed.html")
  128. def parserTest():
  129. # need to setup the ENV containing the the path to the conf file:
  130. #IGNORE:W0212
  131. os.environ[Conf._ENVNAME] = "/home/aubert/dev/src-reps/java-balivernes/RNpicker/etc/conf/rnpicker.config"
  132. r = RequestParser()
  133. lStr = "spectrum=ALL, analysis=CURR/QC"
  134. print "split str = %s\n"%(lStr.split(','))
  135. d = r.parse(lStr,'PAR')
  136. print "dict %s\n"%(d)
  137. def _get_closing_bracket_index(index,s):
  138. tolook = s[index+2:]
  139. openingBrack = 1
  140. closing_brack_index = index+2
  141. i = 0
  142. for c in tolook:
  143. if c == ')':
  144. if openingBrack == 1:
  145. return closing_brack_index
  146. else:
  147. openingBrack -= 1
  148. elif c == '(':
  149. if tolook[i-1] == '%':
  150. openingBrack +=1
  151. # inc index
  152. closing_brack_index +=1
  153. i += 1
  154. return closing_brack_index
  155. def replace_vars(a_str):
  156. data = {'Hello':{'one':'1','two':'2'},'Bye':{'trois':'one','quatre':'4'}}
  157. toparse = a_str
  158. index = toparse.find("%(")
  159. reg = re.compile(r"%\((?P<group>\w*)\[(?P<option>(.*))\]\)")
  160. # if found opening %( look for end bracket)
  161. if index >= 0:
  162. # look for closing brackets while counting openings one
  163. closing_brack_index = _get_closing_bracket_index(index,a_str)
  164. print "closing bracket %d"%(closing_brack_index)
  165. var = toparse[index:closing_brack_index+1]
  166. m = reg.match(var)
  167. if m == None:
  168. print "Error. Cannot match a %(group[option]) in %s but found an opening bracket %(. Probably malformated expression"%(var)
  169. else:
  170. print "found %s[%s]\n"%(m.group('group'),m.group('option'))
  171. g = replace_vars(m.group('group'))
  172. o = replace_vars(m.group('option'))
  173. try:
  174. dummy = data[g][o]
  175. except KeyError, ke: #IGNORE:W0612
  176. print "Error, property %s[%s] doesn't exist in this configuration file \n"%(g,o)
  177. return None
  178. toparse = toparse.replace(var,dummy)
  179. return replace_vars(toparse)
  180. else:
  181. return toparse
  182. token_pat = re.compile("\s*(?:(\d+)|(.))")
  183. class literal_token(object):
  184. def __init__(self, value):
  185. self.value = int(value)
  186. def nud(self):
  187. return self.value
  188. def expression(rbp=0):
  189. global token
  190. t = token
  191. token = next()
  192. left = t.nud()
  193. while rbp < token.lbp:
  194. t = token
  195. token = next()
  196. left = t.led(left)
  197. return left
  198. class operator_add_token(object):
  199. lbp = 10
  200. def led(self, left):
  201. right = expression(10)
  202. return left + right
  203. class end_token(object):
  204. lbp = 0
  205. def my_tokenize(program):
  206. for number, operator in token_pat.findall(program):
  207. if number:
  208. yield literal_token(number)
  209. elif operator == "+":
  210. yield operator_add_token()
  211. else:
  212. raise SyntaxError("unknown operator")
  213. yield end_token()
  214. def parse(program):
  215. global token, next
  216. next = my_tokenize(program).next
  217. token = next()
  218. return expression()
  219. def python_tokenizer(program):
  220. result = []
  221. g = tokenize.generate_tokens(StringIO.StringIO(program).readline) # tokenize the string
  222. for toknum, tokval, tok3, tok4,_ in g:
  223. result.append((toknum, tokval, tok3, tok4))
  224. return result
  225. class TheClass(object):
  226. def __init__(self,val):
  227. """ constructor """
  228. super(TheClass,self).__init__()
  229. self.constant = "TheConstant"
  230. self.value = val
  231. def print_hello(self):
  232. print("Hello. constant: [%s], value: [%s]\n"%(self.constant,self.value))
  233. def forname(modname, classname):
  234. module = __import__(modname,globals(),locals(),['NoName'],-1)
  235. classobj = getattr(module, classname)
  236. return classobj
  237. def new_instance(modname,classname,*args):
  238. classobj = forname(modname,classname)
  239. return classobj(*args)
  240. def test_new_class():
  241. cobj = forname("ctbto.sandbox.scrapbook","TheClass")
  242. instance = new_instance("ctbto.sandbox.scrapbook","TheClass","MyMessage")
  243. instance.print_hello()
  244. def get_package_path():
  245. import org.ctbto.conf
  246. fmod_path = org.ctbto.conf.__path__
  247. test_dir = "%s/tests"%fmod_path[0]
  248. print("path = %s\n"%fmod_path)
  249. print("test dir = %s\n"%test_dir)
  250. fi = open('%s/test.config'%(test_dir))
  251. for line in fi:
  252. print(line)
  253. def reassoc(head,tail,res,memo=''):
  254. # stop condition, no more fuel
  255. if len(tail) == 0:
  256. # if command separate
  257. if head.startswith('-'):
  258. res.extend([memo,head])
  259. return
  260. else:
  261. res.append(memo + head)
  262. return
  263. if head.endswith(','):
  264. reassoc(tail[0],tail[1:] if len(tail) > 1 else [],res,memo+head)
  265. elif head.startswith(','):
  266. reassoc(tail[0],tail[1:] if len(tail) > 1 else [],res,memo+head)
  267. elif head.startswith('-'):
  268. # we do have a command so separate it from the rest
  269. res.append(head)
  270. reassoc(tail[0],tail[1:] if len(tail) > 1 else [],res,'')
  271. else:
  272. # it is not a command
  273. reassoc(tail[0],tail[1:] if len(tail) > 1 else [],res,memo+head)
  274. def get_head_and_tail(a_args):
  275. res = []
  276. reassoc(a_args[0],a_args[1:],res)
  277. print "res = %s\n"%(res)
  278. def group(*choices) : return '(' + '|'.join(choices) + ')'
  279. msgformat1 = "[A-Za-z]{3}"
  280. msgformat2 = "[A-Za-z]{3}\d+"
  281. msgformat3 = "[A-Za-z]{3}\d+\.\d+"
  282. msgformat_regexpr = re.compile(group(msgformat1,msgformat2,msgformat3))
  283. date ="((19|20|21)\d\d)[-/.]?((0)?[1-9]|1[012])[-/.]?(0[1-9]|[12][0-9]|3[01])([tT ]([0-1][0-9]|2[0-3])([:]([0-5][0-9]))?([:]([0-5][0-9]))?([.]([0-9])+)?)?"
  284. date_regexpr = re.compile(date)
  285. def check_regexpr():
  286. res = msgformat_regexpr.match('2004/04/04')
  287. print "res = %s\n"%(res)
  288. def test_reg_expr():
  289. #res = parse("1 + 2")
  290. #res = python_tokenizer("retrieve spectrum.a where mdc > 2")
  291. #res = python_tokenizer("retrieve spectrum[CURR,BK,SPHD] where techno = radionuclide and id = 1234567 and mdc= 124.56 in file=\"/tmp/produced.data\", filetype=\"SAMPML\"")
  292. # as defined in RFC 2822 (do not support square brackets and double quotes)
  293. email_address_regex = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
  294. email ="guillaume@ctbo.org"
  295. m = re.match(email_address_regex,email)
  296. print "res = %s\n"%(m.group())
  297. def email_parser():
  298. import email
  299. #fd = open('/tmp/req_messages/34366629.msg')
  300. fd = open('/tmp/req_messages/34383995.msg')
  301. msg = email.message_from_file(fd)
  302. #print("msg = %s\n"%(msg))
  303. if not msg.is_multipart():
  304. to_parse = msg.get_payload()
  305. print("to_parse %s\n"%(to_parse))
  306. else:
  307. print("multipart")
  308. for part in msg.walk():
  309. #print(part.get_content_type())
  310. # if we have a text/plain this a IMS2.0 message so we try to parse it
  311. if part.get_content_type() == 'text/plain':
  312. print("To Parser %s\n"%(part.get_payload()))
  313. def dirwalk(dir):
  314. "walk a directory tree, using a generator"
  315. for f in os.listdir(dir):
  316. fullpath = os.path.join(dir,f)
  317. if os.path.isdir(fullpath) and not os.path.islink(fullpath):
  318. for x in dirwalk(fullpath): # recurse into subdir
  319. yield x
  320. else:
  321. yield fullpath
  322. def loggers():
  323. logging.config.fileConfig("/home/aubert/workspace/RNpicker/etc/conf/logging_rnpicker.config")
  324. toto_log = logging.getLogger("toto")
  325. print("hello")
  326. toto_log.info("My Message")
  327. if __name__ == '__main__':
  328. loggers()