Пример #1
0
 def store(self):
     if not self._store:
         self.saveFieldsToCookies()
         modelFilename = self.modelFilename()
         # MK will need access to the Python classes for the model.
         # We expect to find them with the actual model file,
         # so we update sys.path appropriately:
         extraDir = os.path.dirname(modelFilename)
         # We need the parent directory of the model package:
         extraDir = os.path.dirname(extraDir)
         if extraDir not in sys.path:
             sys.path.insert(1, extraDir)
         req = self.request()
         self._store = MySQLObjectStore(host=req.value('host'),
                                        user=req.value('user'),
                                        passwd=req.value('password'))
         self._store.readModelFileNamed(modelFilename)
         self._store.connect()
         recentModels = self.request().cookie('recentModels', [])
         if recentModels:
             recentModels = recentModels.split(';')
         if modelFilename not in recentModels:
             recentModels.append(modelFilename)
             recentModels = ';'.join(recentModels)
             self.response().setCookie('recentModels',
                                       ';'.join(recentModels))
     return self._store
Пример #2
0
def main():

	# Set up the store
#	store = MySQLObjectStore(user='******', passwd='password')
	store = MySQLObjectStore()
	store.readModelFileNamed('../Middle/Videos')

	movie = Movie()
	movie.setTitle('The Terminator')
	movie.setYear(1984)
	movie.setRating('r')
	store.addObject(movie)

	james = Person()
	james.setName('James Cameron')
	james.setBirthDate(DateFrom('8/16/1954'))
	movie.addToDirectors(james)

	ahnuld = Person()
	ahnuld.setName('Arnold Schwarzenegger')
	ahnuld.setBirthDate(DateFrom('7/30/1947'))
	store.addObject(ahnuld)

	terminator = Role()
	terminator.setKaracter('Terminator')
	terminator.setPerson(ahnuld)
	movie.addToCast(terminator)

	store.saveChanges()
Пример #3
0
def main():

    # Set up the store
    # store = MySQLObjectStore(user='******', passwd='password')
    store = MySQLObjectStore()
    store.readModelFileNamed('../Middle/Videos')

    movie = Movie()
    movie.setTitle('The Terminator')
    movie.setYear(1984)
    movie.setRating('r')
    store.addObject(movie)

    james = Person()
    james.setName('James Cameron')
    james.setBirthDate(date(1954, 8, 16))
    movie.addToDirectors(james)

    ahnuld = Person()
    ahnuld.setName('Arnold Schwarzenegger')
    ahnuld.setBirthDate(date(1947, 7, 30))
    store.addObject(ahnuld)

    terminator = Role()
    terminator.setKaracter('Terminator')
    terminator.setPerson(ahnuld)
    movie.addToCast(terminator)

    store.saveChanges()
Пример #4
0
	def store(self):
		if not self._store:
			self.saveFieldsToCookies()
			modelFilename = self.modelFilename()
			# MK will need access to the Python classes for the model.
			# We expect to find them with the actual model file,
			# so we update sys.path appropriately:
			extraDir = os.path.dirname(modelFilename)
			# We need the parent directory of the model package:
			extraDir = os.path.dirname(extraDir)
			if extraDir not in sys.path:
				sys.path.insert(1, extraDir)
			req = self.request()
			self._store = MySQLObjectStore(host=req.value('host'),
				user=req.value('user'), passwd=req.value('password'))
			self._store.readModelFileNamed(modelFilename)
			self._store.connect()
			recentModels = self.request().cookie('recentModels', [])
			if recentModels:
				recentModels = recentModels.split(';')
			if modelFilename not in recentModels:
				recentModels.append(modelFilename)
				recentModels = ';'.join(recentModels)
				self.response().setCookie('recentModels', ';'.join(recentModels))
		return self._store
Пример #5
0
def test(filename, pyFilename, deleteData):
	curDir = os.getcwd()
	os.chdir(workDir)
	try:
		filename = '../'+filename

		if os.path.splitext(filename)[1]=='':
			filename += '.mkmodel'
		pyFilename = os.path.join(filename, pyFilename)
		if not os.path.exists(pyFilename):
			print 'No such file', pyFilename
			return

		print 'Testing %s...' % filename

		# Set up the store
		store = MySQLObjectStore()
		store.readModelFileNamed(filename)
		assert store.model()._havePythonClasses # @@@@@@

		# Clear the database
		if deleteData:
			print 'Deleting all database records for test...'
			for klass in store.model().klasses().values():
				if not klass.isAbstract():
					ObjectStore.Store.executeSQL('delete from %s;' % klass.name())

		# Run the test
		results = {}
		execfile(pyFilename, results)
		assert results.has_key('test'), 'No test defined in %s.' % filename
		results['test'](store)
	finally:
		os.chdir(curDir)
Пример #6
0
    def setUp(self):
        UserManagerToSomewhereTest.setUp(self)
        model = self.makeModel()
        from MiddleKit.Design.Generate import Generate
        generate = Generate().generate
        # @@ 2001-02-18 ce: woops: hard coding MySQL
        generate(pyClass='MySQLPythonGenerator', model=model, outdir='.')
        generate(pyClass='MySQLSQLGenerator', model=model, outdir='.')
        print
        os.system('mysql < Create.sql')

        from MiddleKit.Run.MySQLObjectStore import MySQLObjectStore
        store = MySQLObjectStore()
        store.setSQLEcho(
            None
        )  # @@ 2001-02-19 ce: this will probably be the MK default shortly and we won't need this
        store.setModel(model)

        from MiddleKit.Run.MiddleObject import MiddleObject
        from UserKit.UserManagerToMiddleKit import UserManagerToMiddleKit
        from UserForMKTest import UserForMKTest
        assert issubclass(UserForMKTest, MiddleObject)
        from UserKit.User import User
        UserForMKTest.__bases__ = UserForMKTest.__bases__ + (User, )
        assert issubclass(UserForMKTest, MiddleObject)

        def __init__(self, manager, name, password):
            base1 = self.__class__.__bases__[0]
            base2 = self.__class__.__bases__[1]
            base1.__init__(self)
            base2.__init__(self, manager=manager, name=name, password=password)

        UserForMKTest.__init__ = __init__
        self.mgr = self.userManagerClass()(userClass=UserForMKTest,
                                           store=store)
Пример #7
0
class TestThreads(Test):

    def __init__(self):
        Test.__init__(self)
        self._numObjects = 64
        self._numThreads = [1, 2, 4, 8, 16, 32]
        self._numReads = 32

    def timedMain(self):
        import time
        start = time.time()
        self.main()
        end = time.time()
        duration = end - start
        print
        print 'secs: %0.2f' % duration
        print 'mins: %0.2f' % (duration/60.0)

    def readArgs(self, args):
        self._modelNames = ['MKBasic']

    def testEmpty(self):
        self.createDatabase()
        self.createStore()
        self.createObjects()
        self.testConcurrentReads()

    def createStore(self):
        from MiddleKit.Run.MySQLObjectStore import MySQLObjectStore
        self._store = MySQLObjectStore(**storeArgs)
        self._store.readModelFileNamed(self._modelName)

    def createObjects(self):
        from Thing import Thing
        for i in range(self._numObjects):
            t = Thing()
            t.setB(1)
            t.setI(2)
            t.setL(3)
            t.setF(4.0)
            t.setS('five')
            self._store.addObject(t)
        self._store.saveChanges()
        things = self._store.fetchObjectsOfClass('Thing')
        assert len(things) == self._numObjects, \
            '%i, %i' % (len(things), self._numObjects)

    def testConcurrentReads(self):
        for numThreads in self._numThreads:
            print '>> numThreads:', numThreads
            self.testReaderThreads(numThreads)

    def testReaderThreads(self, numThreads):

        class Reader(threading.Thread):

            def __init__(self, store, numReads):
                threading.Thread.__init__(self)
                self._store = store
                self._numReads = numReads

            def run(self):
                store = self._store
                for i in range(self._numReads):
                    # print '%x:%03i' % (id(self), i),
                    objects = store.fetchObjectsOfClass('Thing')

        threads = []
        for i in range(numThreads):
            thread = Reader(self._store, self._numReads)
            threads.append(thread)

        for thread in threads:
            thread.start()

        for thread in threads:
            thread.join()

    def testSamples(self):
        """Test samples.

        We do all our necessary testing in testEmpty() so we override
        this method to pass.

        """
        pass
Пример #8
0
 def createStore(self):
     from MiddleKit.Run.MySQLObjectStore import MySQLObjectStore
     self._store = MySQLObjectStore(**storeArgs)
     self._store.readModelFileNamed(self._modelName)
Пример #9
0
class TestThreads(Test):

	def __init__(self):
		Test.__init__(self)
		self._numObjects = 64
		self._numThreads = [1, 2, 4, 8, 16, 32]
		self._numReads   = 32

	def timedMain(self):
		import time
		start = time.time()
		self.main()
		end = time.time()
		duration = end - start
		print
		print 'secs: %0.2f' % duration
		print 'mins: %0.2f' % (duration/60.0)

	def readArgs(self, args):
		self._modelNames = ['MKBasic']

	def testEmpty(self):
		self.createDatabase()
		self.createStore()
		self.createObjects()
		self.testConcurrentReads()

	def createStore(self):
		from MiddleKit.Run.MySQLObjectStore import MySQLObjectStore
		self._store = MySQLObjectStore()
		self._store.readModelFileNamed(self._modelName)

	def createObjects(self):
		from Thing import Thing
		for i in range(self._numObjects):
			t = Thing()
			t.setB(1)
			t.setI(2)
			t.setL(3)
			t.setF(4.0)
			t.setS('five')
			self._store.addObject(t)
		self._store.saveChanges()
		things = self._store.fetchObjectsOfClass('Thing')
		assert len(things)==self._numObjects, '%i, %i' % (len(things), self._numObjects)

	def testConcurrentReads(self):
		for numThreads in self._numThreads:
			print '>> numThreads:', numThreads
			self.testReaderThreads(numThreads)

	def testReaderThreads(self, numThreads):

		class Reader(threading.Thread):

			def __init__(self, store, numReads):
				threading.Thread.__init__(self)
				self._store = store
				self._numReads = numReads

			def run(self):
				store = self._store
				for i in range(self._numReads):
					#print '%x:%03i' % (id(self), i),
					objects = store.fetchObjectsOfClass('Thing')

		threads = []
		for i in range(numThreads):
			thread = Reader(self._store, self._numReads)
			threads.append(thread)

		for thread in threads:
			thread.start()

		for thread in threads:
			thread.join()

	def testSamples(self):
		"""
		We do all our necessary testing in testEmpty() so we override
		this method to pass.
		"""
		pass
Пример #10
0
	def createStore(self):
		from MiddleKit.Run.MySQLObjectStore import MySQLObjectStore
		self._store = MySQLObjectStore()
		self._store.readModelFileNamed(self._modelName)
Пример #11
0
class StorePage(SitePage):


	## Init ##

	def __init__(self):
		SitePage.__init__(self)
		self._store = None


	## Access ##

	def modelFilename(self):
		req = self.request()
		filename = req.field('modelFilename', None)
		if filename:
			self.response().setCookie('modelFilename', filename)
		else:
			filename = req.cookie('modelFilename', None)
		return filename

	def store(self):
		if not self._store:
			self.saveFieldsToCookies()
			modelFilename = self.modelFilename()
			# MK will need access to the Python classes for the model.
			# We expect to find them with the actual model file,
			# so we update sys.path appropriately:
			extraDir = os.path.dirname(modelFilename)
			# We need the parent directory of the model package:
			extraDir = os.path.dirname(extraDir)
			if extraDir not in sys.path:
				sys.path.insert(1, extraDir)
			req = self.request()
			self._store = MySQLObjectStore(host=req.value('host'),
				user=req.value('user'), passwd=req.value('password'))
			self._store.readModelFileNamed(modelFilename)
			self._store.connect()
			recentModels = self.request().cookie('recentModels', [])
			if recentModels:
				recentModels = recentModels.split(';')
			if modelFilename not in recentModels:
				recentModels.append(modelFilename)
				recentModels = ';'.join(recentModels)
				self.response().setCookie('recentModels', ';'.join(recentModels))
		return self._store


	## Writing page parts ##

	def writeTopBar(self):
		names = os.path.split(self.modelFilename())
		self.writeln('<p><a href="SelectModel" class="SelectLink">SELECT</a>'
			' <span class=StatusBar>%s - %s</span></p>' % (names[1], names[0]))
		req = self.request()
		self.writeln('<p><a href="SelectDatabase" class="SelectLink">SELECT</a>'
			' <span class="StatusBar">db=%s, host=%s, user=%s</span></p>'
			% (req.value('database'), req.value('host'), req.value('user')))

	def writeSideBar(self):
		self.writeKlasses()

	def writeKlasses(self):
		# @@ 2000-11-27 ce: move this to MixIns???
		curClassName = self.request().field('class', None)
		klasses = self.store().model().klasses()
		names = klasses.keys()
		names.sort()
		modelFilename = self.urlEncode(self.modelFilename())
		for name in names:
			urlName = self.urlEncode(name)
			if name == curClassName:
				style = 'CurClassLink'
			else:
				style = 'ClassLink'
			self.writeln('<p><a href="BrowseObjects?class=%s" class"=%s">'
				'%s</a></p>' % (name, style, urlName))

	def writeContent(self):
		self.writeln('<p>Woops. Forgot to override writeContent().</p>')
Пример #12
0
class StorePage(SitePage):

    ## Init ##

    def __init__(self):
        SitePage.__init__(self)
        self._store = None

    ## Access ##

    def modelFilename(self):
        req = self.request()
        filename = req.field('modelFilename', None)
        if filename:
            self.response().setCookie('modelFilename', filename)
        else:
            filename = req.cookie('modelFilename', None)
        return filename

    def store(self):
        if not self._store:
            self.saveFieldsToCookies()
            modelFilename = self.modelFilename()
            # MK will need access to the Python classes for the model.
            # We expect to find them with the actual model file,
            # so we update sys.path appropriately:
            extraDir = os.path.dirname(modelFilename)
            # We need the parent directory of the model package:
            extraDir = os.path.dirname(extraDir)
            if extraDir not in sys.path:
                sys.path.insert(1, extraDir)
            req = self.request()
            self._store = MySQLObjectStore(host=req.value('host'),
                                           user=req.value('user'),
                                           passwd=req.value('password'))
            self._store.readModelFileNamed(modelFilename)
            self._store.connect()
            recentModels = self.request().cookie('recentModels', [])
            if recentModels:
                recentModels = recentModels.split(';')
            if modelFilename not in recentModels:
                recentModels.append(modelFilename)
                recentModels = ';'.join(recentModels)
                self.response().setCookie('recentModels',
                                          ';'.join(recentModels))
        return self._store

    ## Writing page parts ##

    def writeTopBar(self):
        names = os.path.split(self.modelFilename())
        self.writeln('<p><a href="SelectModel" class="SelectLink">SELECT</a>'
                     ' <span class=StatusBar>%s - %s</span></p>' %
                     (names[1], names[0]))
        req = self.request()
        self.writeln(
            '<p><a href="SelectDatabase" class="SelectLink">SELECT</a>'
            ' <span class="StatusBar">db=%s, host=%s, user=%s</span></p>' %
            (req.value('database'), req.value('host'), req.value('user')))

    def writeSideBar(self):
        self.writeKlasses()

    def writeKlasses(self):
        # @@ 2000-11-27 ce: move this to MixIns?
        curClassName = self.request().field('class', None)
        klasses = self.store().model().klasses()
        names = sorted(klasses)
        for name in names:
            urlName = self.urlEncode(name)
            style = 'CurClassLink' if name == curClassName else 'ClassLink'
            self.writeln('<p><a href="BrowseObjects?class=%s" class"=%s">'
                         '%s</a></p>' % (name, style, urlName))

    def writeContent(self):
        self.writeln('<p>Woops. Forgot to override writeContent().</p>')