zephyr-mirror: Style changes

And remove some debugging noise.

(imported from commit 03268673fd9d624ddbde69b9fa2c59268e9c4fe8)
This commit is contained in:
Keegan McAllister 2012-09-07 10:54:28 -04:00
parent 42fcb7789e
commit a102377bff
1 changed files with 29 additions and 28 deletions

View File

@ -1,58 +1,59 @@
#!/usr/bin/python
import mechanize
import re
import urllib
import cgi
import sys
import logging
import zephyr
import sys, logging
logger = logging.getLogger("mechanize")
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)
def browser_login(br):
br.set_handle_robots(False)
browser = mechanize.Browser()
csrf_token = None
def browser_login():
browser.set_handle_robots(False)
## debugging code to consider
# br.set_debug_http(True)
# br.set_debug_responses(True)
# br.set_debug_redirects(True)
# br.set_handle_refresh(False)
br.add_password("https://app.humbughq.com/", "tabbott", "xxxxxxxxxxxxxxxxx", "wiki")
login_info = urllib.urlencode([('username', 'iago'), ('password', 'iago')])
response = br.open("https://app.humbughq.com/")
br.follow_link(text_regex="\s*Log in\s*")
br.select_form(nr=0)
br["username"] = "iago"
br["password"] = "iago"
response2 = br.submit()
# browser.set_debug_http(True)
# browser.set_debug_responses(True)
# browser.set_debug_redirects(True)
# browser.set_handle_refresh(False)
browser.add_password("https://app.humbughq.com/", "tabbott", "xxxxxxxxxxxxxxxxx", "wiki")
browser.open("https://app.humbughq.com/")
browser.follow_link(text_regex="\s*Log in\s*")
browser.select_form(nr=0)
browser["username"] = "iago"
browser["password"] = "iago"
response = browser.submit()
# This is a horrible horrible hack
data = "".join(response2.readlines())
data = "".join(response.readlines())
val = data.index("csrfmiddlewaretoken")
csrf = data[val+28:val+60]
return csrf
global csrf_token
csrf_token = data[val+28:val+60]
# example: send_zephyr("Verona", "Auto2", "test")
def send_zephyr(sender, klass, instance, content):
br.addheaders.append(('X-CSRFToken', csrf))
browser.addheaders.append(('X-CSRFToken', csrf_token))
zephyr_data = urllib.urlencode([('type', 'class'), ('class', klass), ('sender', sender),
('instance', instance), ('new_zephyr', content)])
br.open("https://app.humbughq.com/forge_zephyr/", zephyr_data)
browser.open("https://app.humbughq.com/forge_zephyr/", zephyr_data)
br = mechanize.Browser()
csrf = browser_login(br)
browser_login()
import zephyr
subs = zephyr.Subscriptions()
subs_list = """\
"""
print subs_list
subs = zephyr.Subscriptions()
for sub in subs_list.split():
subs.add((sub, '*', '*'))
print "Starting receive loop"
while True:
notice = zephyr.receive(block=True)
[zsig, body] = notice.message.split("\x00", 1)
sys.stdout.write("received a message on %s from %s..." % (notice.cls, notice.sender))
zsig, body = notice.message.split("\x00", 1)
print "received a message on %s from %s..." % (notice.cls, notice.sender) ,
send_zephyr(cgi.escape(notice.sender), cgi.escape(notice.cls), cgi.escape(notice.instance), cgi.escape(body))
print "forwarded"