File: Twistzilla.py

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (91 lines) | stat: -rw-r--r-- 2,764 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.


from AppKit import *

# import needed classes/functions from Cocoa
from Foundation import *

# import Nib loading functionality from AppKit
from PyObjCTools import AppHelper, NibClassBuilder

from twisted.internet import _threadedselect

_threadedselect.install()

import sys

import urlparse

from twisted.internet import protocol, reactor
from twisted.python import log
from twisted.web import http

# create ObjC classes as defined in MainMenu.nib
NibClassBuilder.extractClasses("MainMenu")


class TwistzillaClient(http.HTTPClient):
    def __init__(self, delegate, urls):
        self.urls = urls
        self.delegate = delegate

    def connectionMade(self):
        self.sendCommand("GET", str(self.urls[2]))
        self.sendHeader("Host", "%s:%d" % (self.urls[0], self.urls[1]))
        self.sendHeader("User-Agent", "CocoaTwistzilla")
        self.endHeaders()

    def handleResponse(self, data):
        self.delegate.gotResponse_(data)


class MyAppDelegate(NibClassBuilder.AutoBaseClass):
    def gotResponse_(self, html):
        s = self.resultTextField.textStorage()
        s.replaceCharactersInRange_withString_((0, s.length()), html)
        self.progressIndicator.stopAnimation_(self)

    def doTwistzillaFetch_(self, sender):
        s = self.resultTextField.textStorage()
        s.deleteCharactersInRange_((0, s.length()))
        self.progressIndicator.startAnimation_(self)
        u = urlparse.urlparse(self.messageTextField.stringValue())
        pos = u[1].find(":")
        if pos == -1:
            host, port = u[1], 80
        else:
            host, port = u[1][:pos], int(u[1][pos + 1 :])
        if u[2] == "":
            fname = "/"
        else:
            fname = u[2]
        host = host.encode("utf8")
        fname = fname.encode("utf8")
        protocol.ClientCreator(
            reactor, TwistzillaClient, self, (host, port, fname)
        ).connectTCP(host, port).addErrback(
            lambda f: self.gotResponse_(f.getBriefTraceback())
        )

    def applicationDidFinishLaunching_(self, aNotification):
        """
        Invoked by NSApplication once the app is done launching and
        immediately before the first pass through the main event
        loop.
        """
        self.messageTextField.setStringValue_("http://www.twistedmatrix.com/")
        reactor.interleave(AppHelper.callAfter)

    def applicationShouldTerminate_(self, sender):
        if reactor.running:
            reactor.addSystemEventTrigger("after", "shutdown", AppHelper.stopEventLoop)
            reactor.stop()
            return False
        return True


if __name__ == "__main__":
    log.startLogging(sys.stdout)
    AppHelper.runEventLoop()