utils.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. '''This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
  2. Copyright 2011 by the AToMPM team and licensed under the LGPL
  3. See COPYING.lesser and README.md in the root of this project for full details'''
  4. import pprint, json, re, math, threading, httplib
  5. class Utilities :
  6. '''
  7. do 'callback()' when 'condition' is satisfied, polling every 'delay'
  8. seconds until it is '''
  9. @staticmethod
  10. def doWhen(condition, delay, callback) :
  11. if condition() :
  12. callback()
  13. else :
  14. t = threading.Timer(delay,Utilities.doWhen,[condition,delay,callback])
  15. t.start()
  16. '''
  17. flatten an array of arrays into a single array '''
  18. @staticmethod
  19. def flatten(arrays) :
  20. return [item for array in arrays for item in array]
  21. '''
  22. read data from disk and return contents... if isJSON is true, return
  23. parsed contents (or parsed asm, if path describes a *.model file) '''
  24. @staticmethod
  25. def fread(path,isJson=True,relative=True) :
  26. try :
  27. if relative :
  28. path = './'+path
  29. f = open(path,'r')
  30. contents = f.read()
  31. f.close()
  32. if isJson :
  33. contents = json.loads(contents)
  34. if path.endswith('.model') :
  35. contents = contents['asm']
  36. return contents
  37. except Exception, e :
  38. raise IOError('crashed while reading data :: '+str(e))
  39. '''
  40. split a full type of the form '/path/to/metamodel/type' and return
  41. '/path/to/metamodel' '''
  42. @staticmethod
  43. def getMetamodel(fulltype) :
  44. return re.match("(.*)/.*",fulltype).group(1)
  45. '''
  46. split a full type of the form '/path/to/metamodel/type' and return
  47. 'type' '''
  48. @staticmethod
  49. def getType(fulltype) :
  50. return re.match(".*/(.*)",fulltype).group(1)
  51. '''
  52. send a synchronous http request '''
  53. @staticmethod
  54. def httpReq(method,host,uri,data) :
  55. headers = {'Content-Type': 'text/plain'}
  56. conn = httplib.HTTPConnection(host)
  57. conn.request(method, uri, json.dumps(data), headers)
  58. resp = conn.getresponse()
  59. conn.close()
  60. return {'statusCode':resp.status, 'reason':resp.reason}
  61. '''
  62. increment the numeric part of sequence# of the form 'src#number' '''
  63. @staticmethod
  64. def incrementSequenceNumber(sn) :
  65. matches = re.match("(.*)#(\d*)",sn)
  66. return matches.group(1)+'#'+str(int(matches.group(2))+1)
  67. '''
  68. return true if the provided code is 2xx '''
  69. @staticmethod
  70. def isHttpSuccessCode(statusCode) :
  71. return math.floor(statusCode/100.0) == 2
  72. '''
  73. pretty-print anything '''
  74. @staticmethod
  75. def pprint(o) :
  76. pp = pprint.PrettyPrinter(indent=4,width=40)
  77. pp.pprint(o)
  78. '''
  79. print a himesis graph '''
  80. @staticmethod
  81. def printHG(hg) :
  82. pp = pprint.PrettyPrinter(indent=4,width=40)
  83. for v in hg.vs :
  84. pp.pprint(v.attributes())
  85. if len(hg.es) > 0 :
  86. print(hg.get_adjacency())
  87. '''
  88. same as JavaScript setTimeout... do 'callback()' after 'delay' seconds
  89. have elapsed '''
  90. @staticmethod
  91. def setTimeout(delay, callback, args=[]) :
  92. t = threading.Timer(delay,callback,args)
  93. t.start()
  94. return t
  95. '''
  96. returns the numeric part of sequence# of the form 'src#number' '''
  97. @staticmethod
  98. def sn2int(sn) :
  99. return int(re.match(".*#(\d*)",sn).group(1))