[GME-commit] GMESRC/Tests/GPyUnit/Regr/Mga/utils Builder.py,NONE,1.1 __init__.py,NONE,1.1 mga2xme.py,NONE,1.1 xme2mga.py,NONE,1.1

gme-commit at list.isis.vanderbilt.edu gme-commit at list.isis.vanderbilt.edu
Fri Jun 10 22:25:50 CDT 2005


Update of /project/gme-repository/GMESRC/Tests/GPyUnit/Regr/Mga/utils
In directory escher:/tmp/cvs-serv874/Regr/Mga/utils

Added Files:
	Builder.py __init__.py mga2xme.py xme2mga.py 
Log Message:
Testcases, utils.


CVS User: Zoltan Molnar, ISIS (zolmol)

--- NEW FILE: xme2mga.py ---
# creates .mga file from .xme file
# If directory specified as first parameter all .xme files in the directory will be converted
# the .mga files are placed into the directory specified by the 2nd parameter
#
# the paradigm used is guessed based on the file name:
# _tc_sf_*.xme => the substring between the 2nd and 3rd '_' characters represents the paradigm name, in this case SF 
#
#
import sys
import win32com.client
import os

def xmefiles( dir, begin_pattern = "", end_pattern = "" ):
	ld = os.listdir( dir)
	xmes = []
	for k in ld:
		if k.endswith( end_pattern + ".xme") and k.startswith( begin_pattern):
			xmes.append( k )
	
	return xmes


def doit( source, target):
	errors = 0
	errors_with_files = ""

	fs = []
	source_dir = ''
	target_dir = target

	if os.path.isdir( source):
		source_dir = source
		dir2load = os.path.abspath(os.path.join( os.path.curdir, source_dir))
		fs = xmefiles( dir2load)
	elif os.path.isfile( source):
		fs.append( source)
	else:
		raise 'File/Directory not given consistently'

	
	for i in fs:
		#fname   = os.path.join( target_dir,  s.path.split(i)[1] + ".xme" )
		xmename = os.path.abspath( os.path.join( os.path.curdir, i))

		try:
			gme = win32com.client.Dispatch("GME.Application")
			gme.Visible = 0
			
			paradigm = "MetaGME"
			# naming pattern: _tc5_A_sf.mga.xme
			# the part between the last '_' and the first '.' char indicates the paradigm name
			j = i[ : i.find('.')]
			if j.count('_') >= 1:
			    u = j.rfind('_')
			    p = j[ u + 1 : ]
			    if p == 'sf':
			        paradigm = "SF"
			    elif p == 'gr':
			        paradigm = "GRATISII"
			    elif p == 'fl':
			        paradigm = "FloatAttr"
			    elif p == 'me':
			        paradigm = "MetaGME"
			    else:
			        print 'Other paradigm !!!'
			        pass
			else:
			    print 'Other naming pattern !!!'
			    pass
			
			name = 'newproject'
			i = os.path.split(i)[1]
			if i.count('.') > 0:
			    name = i[:i.rfind('.')]

			mgan = os.path.abspath( os.path.join( target_dir, name ))
			gme.CreateProject( paradigm, "MGA=" + mgan)
			
			gme.ImportProject(xmename)
			gme.SaveProject()
			gme.CloseProject(0)
		except:
			errors = errors + 1
			errors_with_files = errors_with_files + mganame + "\n"
			gme.CloseProject(0)
	pass
	return ( errors, errors_with_files)


#main
if __name__ == "__main__":
	if len( sys.argv) < 3:
		source = "xme"
		target = "./"
		print "="*79
		print "Usage: xme2mga.py Source Target"
		print "       where Source can be a directory or file"
		print "             Target must be a directory"
		print "i.e.: xme2mga.py", source, target
		print "i.e.: xme2mga.py xme\\_tc5_sf_A.mga.xme models\\"
		print "="*79
		sys.exit(1)
	else:
		source = sys.argv[1]
		target = sys.argv[2]

	(nerrs, errfiles) = doit( source, target)
	if nerrs > 0:
		print "Problematic files: \n" + errfiles

--- NEW FILE: __init__.py ---

--- NEW FILE: mga2xme.py ---
# makes .xme file from .mga format
# If directory specified as first parameter all .mga files in the directory will be converted
# the .xme files are placed into the directory specified by the 2nd parameter

import sys
import win32com.client
import os

def mgafiles( dir, begin_pattern = "", end_pattern = "" ):
	ld = os.listdir( dir)
	mgas = []
	for k in ld:
		if k.endswith( end_pattern + ".mga") and k.startswith( begin_pattern):
			mgas.append( k )
	
	return mgas


def doit( source, target):
	errors = 0
	errors_with_files = ""
	fs = []
	source_dir = ''
	target_dir = target

	if os.path.isdir( source):
		source_dir = source
		fs = mgafiles( os.path.abspath(os.path.join( os.path.curdir, source_dir)))
	elif os.path.isfile( source):
		fs.append( source)
	else:
		raise 'File/Directory not given consistently'
	
	for i in fs:
		mganame = os.path.join( source_dir, i)
		mganame = os.path.abspath( os.path.join( os.path.curdir, mganame))

		xmename = os.path.join( target_dir, os.path.split(i)[1] + ".xme")
		xmename = os.path.abspath( os.path.join( os.path.curdir, xmename))

		try:
			gme = win32com.client.Dispatch("GME.Application")
			gme.Visible = 0
			gme.OpenProject( "MGA=" + mganame )
			
			gme.ExportProject( xmename )
			gme.CloseProject(0)
		except:
			errors = errors + 1
			errors_with_files = errors_with_files + mganame + "\n"
			gme.CloseProject(0)
	pass
	return ( errors, errors_with_files)


#main
if __name__ == "__main__":
	if len( sys.argv) < 3:
		source = "..\\models"
		target = "xme"
		print "="*79
		print "Usage: mga2xme.py Source Target"
		print "       where Source can be a directory or file"
		print "             Target must be a directory"
		print "i.e.: mga2xme.py", source, target
		print "i.e.: mga2xme.py ..\\models\\_tc5_sf_A.mga xme\\"
		print "="*79
		sys.exit(1)
	else:
		source = sys.argv[1]
		target = sys.argv[2]
	
	(nerrs, errfiles) = doit( source, target)
	if nerrs > 0:
		print "Problematic files: \n" + errfiles

--- NEW FILE: Builder.py ---
import unittest
import win32com.client
import win32ui
import pythoncom
import os

def findInProj( project, obj_name = "", obj_kind = ""):
	""" Returns an object in project, satisfying the obj_name and obj_kind criteria, if speficied 
	"""
	
	# create a filter
	filter = project.CreateFilter()
	
	# use Name filter
	filter.Name = obj_name
	filter.Kind = obj_kind

	try:
		some_fcos = project.AllFCOs( filter )
		if some_fcos.Count > 0:
			return some_fcos.Item( 1 )
		else:
			print "findInProj >> Object not found : name = '" + obj_name + "' kind = '" + obj_kind + "'"
			assert 0
	except:
		print "findInProj >> Exception : name = '" + obj_name + "' kind = '" + obj_kind + "'"
		assert 0

	pass

def folder( project, kind):
	""" Returns an IMgaMetaFolder pointer, based on the folder kind given """
	metaproj = project.RootMeta		# an IMgaMetaProject
	metaroot = metaproj.RootFolder		# an IMgaMetaFolder
	
	assert project.RootFolder.MetaFolder == metaroot
	
	mfol = metaroot.LegalChildFolderByName(kind)
	return mfol

def kind(project, kind):
	""" Returns an IMgaMetaFCO pointer, based on the kind given """
	metaproj = project.RootMeta		# an IMgaMetaProject
	metaroot = metaproj.RootFolder		# an IMgaMetaFolder
	
	assert project.RootFolder.MetaFolder == metaroot
	
	mfco = metaroot.DefinedFCOByName(kind, 1)
	return mfco

def role(project, model, role_str):
	""" Returns an IMgaMetaRole pointer, based on role_str and the container model (IMgaFCO) given """
	try:
	 metaproj = project.RootMeta		# an IMgaMetaProject
	 metaroot = metaproj.RootFolder		# an IMgaMetaFolder

         metacont = kind(project, model.MetaBase.Name)
         metarole = metacont.RoleByName(role_str)

        except:
         print 'No such kind:', role_str, 'in', model
         raise
        return metarole


def new(p, cont, role_str):
	""" Creates an fco, in role_str role in container model specified """
	return cont.CreateChildObject( role(p, cont, role_str))

def newObjInFold(p, fold, kind_str):
	""" Creates an object in a folder/rootfolder, based on kind_str"""
	return fold.CreateRootObject( kind( p, kind_str))

def newFolder(p, parent, folder_kind_str):
	""" Creates a folder_kind_str folder in parent"""
	return parent.CreateFolder( folder( p, folder_kind_str))

def connect(p, cont, s, d, role_str):
	""" Helper method connecting plain fcos/ports, when no references are involved
	"""
	z0 = win32com.client.Dispatch("Mga.MgaFCOs")
	return cont.CreateSimpleConn( role(p, cont, role_str), s, d, z0, z0)

def connectRefP(p, cont, s, d, r1, r2, role_str):
	""" Helper method connecting ports.
	s: source fco/port
	d: destination fco/port
	r1: modelreference, 'containing' s
	r2: modelreference, 'containing' d
	r1 or r2 might be 0, when that end of the connection is an fco or modelport (no reference involved)
	"""
	z1 = win32com.client.Dispatch("Mga.MgaFCOs")
	z2 = win32com.client.Dispatch("Mga.MgaFCOs")
	if r1:
		z1.Append( r1)
	if r2:
		z2.Append( r2)
	return cont.CreateSimpleConn( role(p, cont, role_str), s, d, z1, z2)


def creaP(mganame, parad):
	project = win32com.client.Dispatch("Mga.MgaProject")

	# may delete old file if exists
	# if os.path.isfile( mganame):
	#	os.remove( mganame)

	try:
		project.Create( "MGA=" + mganame, parad)
		project.BeginTransaction( project.CreateTerritory( None, None, None), 0)
		return project
	except:
		return 0

def saveP(project):
	try:
		project.CommitTransaction()
	except:
		project.AbortTransaction()
	project.Save()
	project.Close(0)

def populate(p):
	"""Sample: The way the tester could build up a model needed for a test
	"""
	folder1 = newFolder( p, p.RootFolder, 'SheetFolder')
	folder1.Name = 'SheetFolder1'
	
	parsh1 = newObjInFold( p, folder1, 'ParadigmSheet')
	parsh1.Name = 'ParadigmSheet1'
	
	at1 = new( p, parsh1, 'Atom')
	at1.Name = 'Atom1'

	at2 = new( p, parsh1, 'Atom')
	at2.Name = 'Atom2'
	
	ap1 = new( p, parsh1, 'AtomProxy')
	ap1.Name = 'AtProx1'

	ap2 = new( p, parsh1, 'AtomProxy')
	ap2.Name = 'AtProx2'
	
	ap1.Referred = at1
	ap2.Referred = at2



More information about the GME-commit mailing list