Python twisted.application.service.Application() Examples
The following are 30
code examples of twisted.application.service.Application().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
twisted.application.service
, or try the search function
.
Example #1
Source File: hpe_plugin_service.py From python-hpedockerplugin with Apache License 2.0 | 6 votes |
def start_service(self): hpedockerplugin = HPEDockerPluginService(self._cfg) # this will hold the services that combine to form the poetry server top_service = service.MultiService() hpepluginservice = hpedockerplugin.setupservice() hpepluginservice.setServiceParent(top_service) # this variable has to be named 'application' application = service.Application("hpedockerplugin") # this hooks the collection we made to the application hpeplugin_service = top_service.setServiceParent(application) return top_service
Example #2
Source File: test_twistd.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_applicationRunnerGetsCorrectApplication(self): """ Ensure that a twistd plugin gets used in appropriate ways: it is passed its Options instance, and the service it returns is added to the application. """ arunner = CrippledApplicationRunner(self.config) arunner.run() self.assertIs( self.serviceMaker.options, self.config.subOptions, "ServiceMaker.makeService needs to be passed the correct " "sub Command object.") self.assertIs( self.serviceMaker.service, service.IService(arunner.application).services[0], "ServiceMaker.makeService's result needs to be set as a child " "of the Application.")
Example #3
Source File: test_application.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def testLoadApplication(self): """ Test loading an application file in different dump format. """ a = service.Application("hello") baseconfig = {'file': None, 'source': None, 'python':None} for style in 'source pickle'.split(): config = baseconfig.copy() config[{'pickle': 'file'}.get(style, style)] = 'helloapplication' sob.IPersistable(a).setStyle(style) sob.IPersistable(a).save(filename='helloapplication') a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello") config = baseconfig.copy() config['python'] = 'helloapplication' with open("helloapplication", 'w') as f: f.writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello")
Example #4
Source File: test_twistd.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_loggerComponentBeatsLegacyLoggerComponent(self): """ A L{ILogObserver} takes precedence over a L{LegacyILogObserver} component set on Application. """ nonlogs = [] observer = self._makeObserver() application = Componentized() application.setComponent(ILogObserver, observer) application.setComponent(LegacyILogObserver, nonlogs.append) logger = app.AppLogger({}) logger.start(application) self._checkObserver(observer) self.assertEqual(nonlogs, [])
Example #5
Source File: app.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def getApplication(config, passphrase): s = [(config[t], t) for t in ['python', 'source', 'file'] if config[t]][0] filename, style = s[0], {'file': 'pickle'}.get(s[1], s[1]) try: log.msg("Loading %s..." % filename) application = service.loadApplication(filename, style, passphrase) log.msg("Loaded.") except Exception as e: s = "Failed to load application: %s" % e if isinstance(e, KeyError) and e.args[0] == "application": s += """ Could not find 'application' in the file. To use 'twistd -y', your .tac file must create a suitable object (e.g., by calling service.Application()) and store it in a variable named 'application'. twistd loads your .tac file and scans the global variables for one of this name. Please read the 'Using Application' HOWTO for details. """ traceback.print_exc(file=log.logfile) log.msg(s) log.deferr() sys.exit('\n' + s + '\n') return application
Example #6
Source File: app.py From learn_python3_spider with MIT License | 6 votes |
def getApplication(config, passphrase): s = [(config[t], t) for t in ['python', 'source', 'file'] if config[t]][0] filename, style = s[0], {'file': 'pickle'}.get(s[1], s[1]) try: log.msg("Loading %s..." % filename) application = service.loadApplication(filename, style, passphrase) log.msg("Loaded.") except Exception as e: s = "Failed to load application: %s" % e if isinstance(e, KeyError) and e.args[0] == "application": s += """ Could not find 'application' in the file. To use 'twistd -y', your .tac file must create a suitable object (e.g., by calling service.Application()) and store it in a variable named 'application'. twistd loads your .tac file and scans the global variables for one of this name. Please read the 'Using Application' HOWTO for details. """ traceback.print_exc(file=log.logfile) log.msg(s) log.deferr() sys.exit('\n' + s + '\n') return application
Example #7
Source File: test_application.py From learn_python3_spider with MIT License | 6 votes |
def testLoadApplication(self): """ Test loading an application file in different dump format. """ a = service.Application("hello") baseconfig = {'file': None, 'source': None, 'python':None} for style in 'source pickle'.split(): config = baseconfig.copy() config[{'pickle': 'file'}.get(style, style)] = 'helloapplication' sob.IPersistable(a).setStyle(style) sob.IPersistable(a).save(filename='helloapplication') a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello") config = baseconfig.copy() config['python'] = 'helloapplication' with open("helloapplication", 'w') as f: f.writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello")
Example #8
Source File: test_twistd.py From learn_python3_spider with MIT License | 6 votes |
def test_applicationRunnerGetsCorrectApplication(self): """ Ensure that a twistd plugin gets used in appropriate ways: it is passed its Options instance, and the service it returns is added to the application. """ arunner = CrippledApplicationRunner(self.config) arunner.run() self.assertIs( self.serviceMaker.options, self.config.subOptions, "ServiceMaker.makeService needs to be passed the correct " "sub Command object.") self.assertIs( self.serviceMaker.service, service.IService(arunner.application).services[0], "ServiceMaker.makeService's result needs to be set as a child " "of the Application.")
Example #9
Source File: test_twistd.py From learn_python3_spider with MIT License | 6 votes |
def _setupConfiguredLogger(self, application, extraLogArgs={}, appLogger=app.AppLogger): """ Set up an AppLogger which exercises the C{logger} configuration option. @type application: L{Componentized} @param application: The L{Application} object to pass to L{app.AppLogger.start}. @type extraLogArgs: C{dict} @param extraLogArgs: extra values to pass to AppLogger. @type appLogger: L{AppLogger} class, or a subclass @param appLogger: factory for L{AppLogger} instances. @rtype: C{list} @return: The logs accumulated by the log observer. """ observer = self._makeObserver() logArgs = {"logger": lambda: observer} logArgs.update(extraLogArgs) logger = appLogger(logArgs) logger.start(application) return observer
Example #10
Source File: test_caldav.py From ccs-calendarserver with Apache License 2.0 | 6 votes |
def test_nonAsciiLog(self): """ Make sure that the file based error log can write non ascii data """ logpath = self.mktemp() service = ErrorLoggingMultiService( True, logpath, 10000, 10, False, ) app = Application("non-ascii") service.setServiceParent(app) observer = app.getComponent(ILogObserver, None) self.assertTrue(observer is not None) log = Logger(observer=observer) log.error(u"Couldn\u2019t be wrong") with open(logpath) as f: logentry = f.read() self.assertIn("Couldn\xe2\x80\x99t be wrong", logentry)
Example #11
Source File: socksmon.py From socksmon with BSD 2-Clause "Simplified" License | 6 votes |
def main(): with open('/tmp/server.pem', 'rb') as fp: certData = fp.read() sslcert = ssl.PrivateCertificate.loadPEM(certData) logging.basicConfig(level=logging.INFO) socks = MySOCKSv4Factory("http://127.0.0.1:2357", "http://127.0.0.1:8080", sslcert) socks.protocol = MySOCKSv4 srv = service.MultiService() srv.addService(internet.TCPServer(9050, socks)) srv.addService(internet.TCPServer(2357, server.Site(WebEchoService()))) application = service.Application("Receive Request") srv.setServiceParent(application) srv.startService() reactor.run()
Example #12
Source File: test_application.py From python-for-android with Apache License 2.0 | 6 votes |
def test_simpleStoreAndLoad(self): a = service.Application("hello") p = sob.IPersistable(a) for style in 'source pickle'.split(): p.setStyle(style) p.save() a1 = service.loadApplication("hello.ta"+style[0], style) self.assertEqual(service.IService(a1).name, "hello") f = open("hello.tac", 'w') f.writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) f.close() a1 = service.loadApplication("hello.tac", 'python') self.assertEqual(service.IService(a1).name, "hello")
Example #13
Source File: test_twistd.py From learn_python3_spider with MIT License | 6 votes |
def test_loggerComponentBeatsLegacyLoggerComponent(self): """ A L{ILogObserver} takes precedence over a L{LegacyILogObserver} component set on Application. """ nonlogs = [] observer = self._makeObserver() application = Componentized() application.setComponent(ILogObserver, observer) application.setComponent(LegacyILogObserver, nonlogs.append) logger = app.AppLogger({}) logger.start(application) self._checkObserver(observer) self.assertEqual(nonlogs, [])
Example #14
Source File: server.py From canvas with BSD 3-Clause "New" or "Revised" License | 6 votes |
def CanvasApp(nginx=False): application = service.Application("Canvas") web_server = CanvasTCPServer() web_server.setServiceParent(application) log_file = logfile.LogFile( name="twistd.log", directory="/var/canvas/website/run", maxRotatedFiles=100, ) application.setComponent(log.ILogObserver, log.FileLogObserver(log_file).emit) return application
Example #15
Source File: tkmktap.py From BitTorrent with GNU General Public License v3.0 | 6 votes |
def run(): taps = mktap.loadPlugins() r = Tkinter.Tk() r.withdraw() keyList = taps.keys() keyList.sort() config = TkMkAppFrame(r, None) menu = TkAppMenu( r, config.createApplication, lambda i, d = taps, c = config: c.reset(d[i]), keyList ) config.pack() r['menu'] = menu r.title('Twisted Application Maker ' + version) r.deiconify() tksupport.install(r) reactor.run()
Example #16
Source File: test_application.py From BitTorrent with GNU General Public License v3.0 | 6 votes |
def testSimpleUNIX(self): # XXX - replace this test with one that does the same thing, but # with no web dependencies. if not interfaces.IReactorUNIX(reactor, None): raise unittest.SkipTest, "This reactor does not support UNIX domain sockets" if not gotMicrodom: raise unittest.SkipTest("Need twisted.web to run this test.") s = "(dp0\nS'udpConnectors'\np1\n(lp2\nsS'unixConnectors'\np3\n(lp4\nsS'twisted.internet.app.Application.persistenceVersion'\np5\nI12\nsS'name'\np6\nS'web'\np7\nsS'sslConnectors'\np8\n(lp9\nsS'sslPorts'\np10\n(lp11\nsS'tcpPorts'\np12\n(lp13\nsS'unixPorts'\np14\n(lp15\n(S'/home/moshez/.twistd-web-pb'\np16\n(itwisted.spread.pb\nBrokerFactory\np17\n(dp19\nS'objectToBroker'\np20\n(itwisted.web.distrib\nResourcePublisher\np21\n(dp22\nS'twisted.web.distrib.ResourcePublisher.persistenceVersion'\np23\nI2\nsS'site'\np24\n(itwisted.web.server\nSite\np25\n(dp26\nS'resource'\np27\n(itwisted.web.static\nFile\np28\n(dp29\nS'ignoredExts'\np30\n(lp31\nsS'defaultType'\np32\nS'text/html'\np33\nsS'registry'\np34\n(itwisted.web.static\nRegistry\np35\n(dp36\nS'twisted.web.static.Registry.persistenceVersion'\np37\nI1\nsS'twisted.python.components.Componentized.persistenceVersion'\np38\nI1\nsS'_pathCache'\np39\n(dp40\nsS'_adapterCache'\np41\n(dp42\nS'twisted.internet.interfaces.IServiceCollection'\np43\n(itwisted.internet.app\nApplication\np44\n(dp45\ng1\ng2\nsg3\ng4\nsg5\nI12\nsg6\ng7\nsg8\ng9\nsg10\ng11\nsg12\ng13\nsg14\ng15\nsS'extraPorts'\np46\n(lp47\nsS'gid'\np48\nI1053\nsS'tcpConnectors'\np49\n(lp50\nsS'extraConnectors'\np51\n(lp52\nsS'udpPorts'\np53\n(lp54\nsS'services'\np55\n(dp56\nsS'persistStyle'\np57\nS'pickle'\np58\nsS'delayeds'\np59\n(lp60\nsS'uid'\np61\nI1053\nsbssbsS'encoding'\np62\nNsS'twisted.web.static.File.persistenceVersion'\np63\nI6\nsS'path'\np64\nS'/home/moshez/public_html.twistd'\np65\nsS'type'\np66\ng33\nsS'children'\np67\n(dp68\nsS'processors'\np69\n(dp70\nS'.php3'\np71\nctwisted.web.twcgi\nPHP3Script\np72\nsS'.rpy'\np73\nctwisted.web.script\nResourceScript\np74\nsS'.php'\np75\nctwisted.web.twcgi\nPHPScript\np76\nsS'.cgi'\np77\nctwisted.web.twcgi\nCGIScript\np78\nsS'.epy'\np79\nctwisted.web.script\nPythonScript\np80\nsS'.trp'\np81\nctwisted.web.trp\nResourceUnpickler\np82\nssbsS'logPath'\np83\nNsS'sessions'\np84\n(dp85\nsbsbsS'twisted.spread.pb.BrokerFactory.persistenceVersion'\np86\nI3\nsbI5\nI438\ntp87\nasg55\ng56\nsg48\nI1053\nsg49\ng50\nsg51\ng52\nsg53\ng54\nsg46\ng47\nsg57\ng58\nsg61\nI1053\nsg59\ng60\ns." d = pickle.loads(s) a = Dummy() a.__dict__ = d appl = compat.convert(a) self.assertEqual(service.IProcess(appl).uid, 1053) self.assertEqual(service.IProcess(appl).gid, 1053) self.assertEqual(service.IService(appl).name, "web") services = list(service.IServiceCollection(appl)) self.assertEqual(len(services), 1) s = services[0] self.assertEqual(s.parent, service.IServiceCollection(appl)) self.assert_(s.privileged) self.assert_(isinstance(s, internet.UNIXServer)) args = s.args self.assertEqual(args[0], '/home/moshez/.twistd-web-pb')
Example #17
Source File: test_application.py From BitTorrent with GNU General Public License v3.0 | 6 votes |
def testLoadApplication(self): a = service.Application("hello") baseconfig = {'file': None, 'xml': None, 'source': None, 'python':None} for style in 'source xml pickle'.split(): if style == 'xml' and not gotMicrodom: continue config = baseconfig.copy() config[{'pickle': 'file'}.get(style, style)] = 'helloapplication' sob.IPersistable(a).setStyle(style) sob.IPersistable(a).save(filename='helloapplication') a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello") config = baseconfig.copy() config['python'] = 'helloapplication' open("helloapplication", 'w').writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello")
Example #18
Source File: test_application.py From BitTorrent with GNU General Public License v3.0 | 6 votes |
def test_simpleStoreAndLoad(self): a = service.Application("hello") p = sob.IPersistable(a) for style in 'xml source pickle'.split(): if style == 'xml' and not gotMicrodom: continue p.setStyle(style) p.save() a1 = service.loadApplication("hello.ta"+style[0], style) self.assertEqual(service.IService(a1).name, "hello") open("hello.tac", 'w').writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) a1 = service.loadApplication("hello.tac", 'python') self.assertEqual(service.IService(a1).name, "hello")
Example #19
Source File: app.py From BitTorrent with GNU General Public License v3.0 | 6 votes |
def getApplication(config, passphrase): s = [(config[t], t) for t in ['python', 'xml', 'source', 'file'] if config[t]][0] filename, style = s[0], {'file':'pickle'}.get(s[1],s[1]) try: log.msg("Loading %s..." % filename) application = service.loadApplication(filename, style, passphrase) log.msg("Loaded.") except Exception, e: s = "Failed to load application: %s" % e if isinstance(e, KeyError) and e.args[0] == "application": s += """ Could not find 'application' in the file. To use 'twistd -y', your .tac file must create a suitable object (e.g., by calling service.Application()) and store it in a variable named 'application'. twistd loads your .tac file and scans the global variables for one of this name. Please read the 'Using Application' HOWTO for details. """ traceback.print_exc(file=log.logfile) log.msg(s) log.deferr() sys.exit('\n' + s + '\n')
Example #20
Source File: test_twistd.py From python-for-android with Apache License 2.0 | 6 votes |
def test_applicationRunnerGetsCorrectApplication(self): """ Ensure that a twistd plugin gets used in appropriate ways: it is passed its Options instance, and the service it returns is added to the application. """ arunner = CrippledApplicationRunner(self.config) arunner.run() self.assertIdentical( self.serviceMaker.options, self.config.subOptions, "ServiceMaker.makeService needs to be passed the correct " "sub Command object.") self.assertIdentical( self.serviceMaker.service, service.IService(arunner.application).services[0], "ServiceMaker.makeService's result needs to be set as a child " "of the Application.")
Example #21
Source File: test_application.py From python-for-android with Apache License 2.0 | 6 votes |
def testLoadApplication(self): """ Test loading an application file in different dump format. """ a = service.Application("hello") baseconfig = {'file': None, 'source': None, 'python':None} for style in 'source pickle'.split(): config = baseconfig.copy() config[{'pickle': 'file'}.get(style, style)] = 'helloapplication' sob.IPersistable(a).setStyle(style) sob.IPersistable(a).save(filename='helloapplication') a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello") config = baseconfig.copy() config['python'] = 'helloapplication' f = open("helloapplication", 'w') f.writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) f.close() a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello")
Example #22
Source File: app.py From python-for-android with Apache License 2.0 | 6 votes |
def getApplication(config, passphrase): s = [(config[t], t) for t in ['python', 'source', 'file'] if config[t]][0] filename, style = s[0], {'file':'pickle'}.get(s[1],s[1]) try: log.msg("Loading %s..." % filename) application = service.loadApplication(filename, style, passphrase) log.msg("Loaded.") except Exception, e: s = "Failed to load application: %s" % e if isinstance(e, KeyError) and e.args[0] == "application": s += """ Could not find 'application' in the file. To use 'twistd -y', your .tac file must create a suitable object (e.g., by calling service.Application()) and store it in a variable named 'application'. twistd loads your .tac file and scans the global variables for one of this name. Please read the 'Using Application' HOWTO for details. """ traceback.print_exc(file=log.logfile) log.msg(s) log.deferr() sys.exit('\n' + s + '\n')
Example #23
Source File: test_twistd.py From python-for-android with Apache License 2.0 | 5 votes |
def test_createOrGetApplicationWithTapFile(self): """ Ensure that the createOrGetApplication call that 'twistd -f foo.tap' makes will load the Application out of foo.tap. """ config = twistd.ServerOptions() config.parseOptions(['-f', self.tapfile]) application = CrippledApplicationRunner(config).createOrGetApplication() self.assertEquals(service.IService(application).name, 'Hi!')
Example #24
Source File: test_application.py From BitTorrent with GNU General Public License v3.0 | 5 votes |
def testApplicationAsParent(self): s = service.Service() p = service.Application("") s.setServiceParent(p) self.failUnlessEqual(list(service.IServiceCollection(p)), [s]) self.failUnlessEqual(s.parent, service.IServiceCollection(p))
Example #25
Source File: compat.py From BitTorrent with GNU General Public License v3.0 | 5 votes |
def convert(oldApp): '''Convert an C{i.app.Application} to a C{application.service.Application} @type oldApp: C{twisted.internet.app.Application} @rtype C{twisted.application.service.Application} This function might damage oldApp beyond repair: services that other parts might be depending on might be missing. It is not safe to use oldApp after it has been converted. In case this behaviour is not desirable, pass a deep copy of the old application ''' ret = service.Application(oldApp.name, getattr(oldApp, "uid", None), getattr(oldApp, "gid", None)) c = service.IServiceCollection(ret) service.IProcess(ret).processName = oldApp.processName for (pList, klass) in [(oldApp.extraPorts, internet.GenericServer), (oldApp.extraConnectors, internet.GenericClient),]: for (portType, args, kw) in pList: klass(portType, *args, **kw).setServiceParent(c) for (name, klass) in _mapping: for args in getattr(oldApp, name): klass(*args).setServiceParent(c) for s in c: if hasattr(s, 'privileged'): s.privileged = 1 for s in oldApp.services.values(): if not service.IService.providedBy(s): s.serviceParent = None s = _NewService(s) s.setServiceParent(IOldApplication(c)) else: s.serviceParent = None s.setServiceParent(c) return ret
Example #26
Source File: test_application.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def testPersistableComponent(self): a = service.Application("hello") p = sob.IPersistable(a) self.assertEqual(p.style, 'pickle') self.assertEqual(p.name, 'hello') self.assertIs(p.original, a)
Example #27
Source File: mktap.py From python-for-android with Apache License 2.0 | 5 votes |
def addToApplication(ser, name, append, procname, type, encrypted, uid, gid): if append and os.path.exists(append): a = service.loadApplication(append, 'pickle', None) else: a = service.Application(name, uid, gid) if procname: service.IProcess(a).processName = procname ser.setServiceParent(service.IServiceCollection(a)) sob.IPersistable(a).setStyle(type) passphrase = app.getSavePassphrase(encrypted) if passphrase: append = None sob.IPersistable(a).save(filename=append, passphrase=passphrase)
Example #28
Source File: test_twistd.py From python-for-android with Apache License 2.0 | 5 votes |
def test_setupEnvironment(self): """ L{UnixApplicationRunner.startApplication} calls L{UnixApplicationRunner.setupEnvironment} with the chroot, rundir, nodaemon, umask, and pidfile parameters from the configuration it is constructed with. """ options = twistd.ServerOptions() options.parseOptions([ '--nodaemon', '--umask', '0070', '--chroot', '/foo/chroot', '--rundir', '/foo/rundir', '--pidfile', '/foo/pidfile']) application = service.Application("test_setupEnvironment") self.runner = UnixApplicationRunner(options) args = [] def fakeSetupEnvironment(self, chroot, rundir, nodaemon, umask, pidfile): args.extend((chroot, rundir, nodaemon, umask, pidfile)) # Sanity check self.assertEqual( inspect.getargspec(self.runner.setupEnvironment), inspect.getargspec(fakeSetupEnvironment)) self.patch(UnixApplicationRunner, 'setupEnvironment', fakeSetupEnvironment) self.patch(UnixApplicationRunner, 'shedPrivileges', lambda *a, **kw: None) self.patch(app, 'startApplication', lambda *a, **kw: None) self.runner.startApplication(application) self.assertEqual( args, ['/foo/chroot', '/foo/rundir', True, 56, '/foo/pidfile'])
Example #29
Source File: test_application.py From python-for-android with Apache License 2.0 | 5 votes |
def test_convertStyle(self): appl = service.Application("lala") for instyle in 'source pickle'.split(): for outstyle in 'source pickle'.split(): sob.IPersistable(appl).setStyle(instyle) sob.IPersistable(appl).save(filename="converttest") app.convertStyle("converttest", instyle, None, "converttest.out", outstyle, 0) appl2 = service.loadApplication("converttest.out", outstyle) self.assertEqual(service.IService(appl2).name, "lala")
Example #30
Source File: test_application.py From python-for-android with Apache License 2.0 | 5 votes |
def testPersistableComponent(self): a = service.Application("hello") p = sob.IPersistable(a) self.assertEqual(p.style, 'pickle') self.assertEqual(p.name, 'hello') self.assert_(p.original is a)