utils.py 3.1 KB

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