utils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. '''*****************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Raphael Mannadiar (raphael.mannadiar@mail.mcgill.ca)
  4. This file is part of AToMPM.
  5. AToMPM is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU Lesser General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or (at your option) any later
  8. version.
  9. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  14. *****************************************************************************'''
  15. import pprint, json, re, math, threading, httplib
  16. class Utilities :
  17. '''
  18. do 'callback()' when 'condition' is satisfied, polling every 'delay'
  19. seconds until it is '''
  20. @staticmethod
  21. def doWhen(condition, delay, callback) :
  22. if condition() :
  23. callback()
  24. else :
  25. t = threading.Timer(delay,Utilities.doWhen,[condition,delay,callback])
  26. t.start()
  27. '''
  28. flatten an array of arrays into a single array '''
  29. @staticmethod
  30. def flatten(arrays) :
  31. return [item for array in arrays for item in array]
  32. '''
  33. read data from disk and return contents... if isJSON is true, return
  34. parsed contents (or parsed asm, if path describes a *.model file) '''
  35. @staticmethod
  36. def fread(path,isJson=True,relative=True) :
  37. try :
  38. if relative :
  39. path = './'+path
  40. f = open(path,'r')
  41. contents = f.read()
  42. f.close()
  43. if isJson :
  44. contents = json.loads(contents)
  45. if path.endswith('.model') :
  46. contents = contents['asm']
  47. return contents
  48. except Exception, e :
  49. raise IOError('crashed while reading data :: '+str(e))
  50. '''
  51. split a full type of the form '/path/to/metamodel/type' and return
  52. '/path/to/metamodel' '''
  53. @staticmethod
  54. def getMetamodel(fulltype) :
  55. return re.match("(.*)/.*",fulltype).group(1)
  56. '''
  57. split a full type of the form '/path/to/metamodel/type' and return
  58. 'type' '''
  59. @staticmethod
  60. def getType(fulltype) :
  61. return re.match(".*/(.*)",fulltype).group(1)
  62. '''
  63. send a synchronous http request '''
  64. @staticmethod
  65. def httpReq(method,host,uri,data) :
  66. headers = {'Content-Type': 'text/plain'}
  67. conn = httplib.HTTPConnection(host)
  68. conn.request(method, uri, json.dumps(data), headers)
  69. resp = conn.getresponse()
  70. conn.close()
  71. return {'statusCode':resp.status, 'reason':resp.reason}
  72. '''
  73. increment the numeric part of sequence# of the form 'src#number' '''
  74. @staticmethod
  75. def incrementSequenceNumber(sn) :
  76. matches = re.match("(.*)#(\d*)",sn)
  77. return matches.group(1)+'#'+str(int(matches.group(2))+1)
  78. '''
  79. return true if the provided code is 2xx '''
  80. @staticmethod
  81. def isHttpSuccessCode(statusCode) :
  82. return math.floor(statusCode/100.0) == 2
  83. '''
  84. pretty-print anything '''
  85. @staticmethod
  86. def pprint(o) :
  87. pp = pprint.PrettyPrinter(indent=4,width=40)
  88. pp.pprint(o)
  89. '''
  90. print a himesis graph '''
  91. @staticmethod
  92. def printHG(hg) :
  93. pp = pprint.PrettyPrinter(indent=4,width=40)
  94. for v in hg.vs :
  95. pp.pprint(v.attributes())
  96. if len(hg.es) > 0 :
  97. print(hg.get_adjacency())
  98. '''
  99. same as JavaScript setTimeout... do 'callback()' after 'delay' seconds
  100. have elapsed '''
  101. @staticmethod
  102. def setTimeout(delay, callback, args=[]) :
  103. t = threading.Timer(delay,callback,args)
  104. t.start()
  105. return t
  106. '''
  107. returns the numeric part of sequence# of the form 'src#number' '''
  108. @staticmethod
  109. def sn2int(sn) :
  110. return int(re.match(".*#(\d*)",sn).group(1))