nothing in particular

A place for things I don't want to forget

Wednesday, October 28, 2009

take #2


from twisted.web import server, xmlrpc, http
from twisted.web.server import Site
from twisted.web.static import File
from twisted.web.resource import Resource
from twisted.internet.task import deferLater
from twisted.enterprise import adbapi
from twisted.internet import reactor, threads, defer



import sys
import time
import random
import hashlib
import memcache

def sendToThread(kwargs):
''' this is a convenience method that will be used to run blocking
methods in a thread. That way twisted will not block waiting for them.
It takes a dictionary as a parameter. This dictionary is REQUIRED to have
key,values for a) func, b) cb, c) err_cb (function to run, callback and
error callback).

Also for each of these keys it looks for X_args and X_kwargs (where X
is one of a),b),c) above.
It returns the deferred object.
'''

func = kwargs['func']
func_args = kwargs.get('func_args', ())
func_kwargs = kwargs.get('func_kwargs', {})

cb = kwargs['cb']
cb_args = kwargs.get('cb_args', ())
cb_kwargs = kwargs.get('cb_kwargs', {})

err_cb = kwargs['err_cb']
err_cb_args = kwargs.get('err_cb_args', ())
err_cb_kwargs = kwargs.get('err_db_kwargs', {})
d = threads.deferToThread(func, *func_args, **func_kwargs)
d.addCallback(cb, *cb_args, **cb_kwargs)
d.addErrback(err_cb, *err_cb_args, **err_cb_kwargs)

return d

class CodeHandler(Resource):
def __init__(self, path):
self.children = {}
self.path = path

def authRequest(self, request):
'''
This method should authorize incoming requests for code download.
It should return a deferred object and the callback should either
do all the magic stuff or send a 403 back

For now let's just return true/false.
'''
res = request.getHeader('lol')
if (res == '1234'):
return (True, 'OK')
else:
return (False, '''Just because I don't like you''')

def calcSig(self, path, request):
val = mc.get(path)
print val
if not val:
hash = hashlib.sha1()
f = open(path, 'r')
hash.update(f.read())
f.close()
h = hash.hexdigest()
mc.set(path,h)
else:
h = val
request.setHeader('sig',h)
return (request, path)


def putResults(self, parms):
request = parms[0]
path = parms[1]
return File(path).render(request)

def _catchFail(self, err):
''' cleanup stuff in case connection is closed on us goes here '''
print "client disconnected: %s" % err

def genericFail(self, *args):
args[0].printDetailedTraceback()

def render(self, request):
self.id = random.random()
ret = self.authRequest(request)
if ret[0]:
if request.getHeader('sleep'):
dfrdict = {'func':self.calcSig,
'cb': self.putResults,
'err_cb': self.genericFail,
'func_args': [self.path, request]}
d = sendToThread(dfrdict)
request.notifyFinish().addErrback(self._catchFail)
return server.NOT_DONE_YET
request.setHeader('omg', 'hi')
return File(self.path).render(request)
else:
request.setResponseCode(http.FORBIDDEN)
return "

Access Denied


%s" % ret[1]

def getChild(self, path, request):
return self

if __name__ == '__main__':
mc = memcache.Client(['127.0.0.1:11211'], debug=0)
root = Resource()
#root.putChild('RPC2', TestRPC())
root.putChild('modules', CodeHandler('/tmp/lol.sql3'))
factory = Site(root)
dbpool = adbapi.ConnectionPool('sqlite3','/tmp/lol.sql3')
reactor.listenTCP(8888, factory)
reactor.run()

Tuesday, October 20, 2009


from twisted.web import server, xmlrpc, http
from twisted.web.server import Site
from twisted.web.static import File
from twisted.web.resource import Resource
from twisted.internet.task import deferLater
from twisted.enterprise import adbapi
from twisted.internet import reactor, threads, defer

import sys
import time
import random
class TestRPC(xmlrpc.XMLRPC):
def __init__(self):
self.allowNone = True

def testDelay(self, results):
print type(results)
return results

def xmlrpc_helloFast(self, test):
return test

def xmlrpc_helloSlow(self, test):
ret = dbpool.runQuery("SELECT tst from test");
ret.addCallback(self.testDelay)
return ret

def insertTst(self, txn, test):
txn.execute("INSERT INTO test (tst) VALUES (?) ", (test,))


def xmlrpc_helloTxn(self, test):
something = dbpool.runInteraction(self.insertTst, test)
return something

class CodeHandler(Resource):
def __init__(self, path):
#self.children = {'test':self.hello()}
self.children = {}
self.path = '/tmp/lol.sql3'

def hello(self):
return(File('/tmp/'))

def authRequest(self, request):
'''
This method should authorize incoming requests for code download.
It should return a deferred object and the callback should either
do all the magic stuff or send a 403 back

For now let's just return true/false.
'''
res = request.getHeader('lol')
if (res == '1234'):
return (True, 'OK')
else:
return (False, '''Just because I don't like you''')

def calcSig(self, path, sleep, request):
time.sleep(random.randint(0,20))
request.setHeader('sig','12983ty12gfoiu12fhn2fy12pf2q')
return (request, path)

def putResults(self, parms):
request = parms[0]
path = parms[1]
print "Served %s" % request.uri
return File(path).render(request)

def _catchFail(self, err):
''' cleanup stuff in case connection is closed on us goes here '''
print "client disconnected: %s" % err


def render(self, request):
self.id = random.random()
print "Serving %s" % request.uri
ret = self.authRequest(request)
if ret[0]:
if request.getHeader('sleep'):
d = threads.deferToThread(self.calcSig, self.path, True, request)
d.addCallback(self.putResults)
request.notifyFinish().addErrback(self._catchFail)
return server.NOT_DONE_YET
request.setHeader('omg', 'hi')
print "Served %s" % request.uri
return File(self.path).render(request)
else:
request.setResponseCode(http.FORBIDDEN)
return "

Access Denied


%s" % ret[1]

def getChild(self, path, request):
return self

if __name__ == '__main__':
root = Resource()
root.putChild('RPC2', TestRPC())
root.putChild('modules', CodeHandler('/tmp'))
factory = Site(root)
dbpool = adbapi.ConnectionPool('sqlite3','/tmp/lol.sql3')
reactor.listenTCP(8888, factory)
reactor.run()

Thursday, January 4, 2007

Nokia 6288 and Mac OS X

My latest mobile phone is Nokia 6288. I've been using it for the last few days and it looks to be working properly, for now. The only problem I had was that it was not supported by Apple's iSync software so I could not synchronise the contacts with my Mac's Address Book.

I could not find any directions for this specific model but after broadening the query a bit I found this.

Fortunately Nokia 6288 is almost identical to 6280, which is supported by iSync .

As the article says... First open /Applications: iSync.app: (Control-click and choose Show Package Contents): Contents: PlugIns: ApplePhoneConduit.syncdevice: Contents: PlugIns: PhoneModelsSync.phoneplugin: Contents: Resources: MetaClasses.plist file in TextEdit, and add the following XML lines:


<key>com.nokia.6288</key>
<dict>
<key>Identification</key>
<dict>
<key>com.apple.cgmi+cgmm</key>
<string>Nokia+Nokia 6288</string>
<key>com.apple.gmi+gmm</key>
<string>Nokia+Nokia 6288</string>
<key>com.apple.usb.vendorid-modelid</key>
<string>0x0421/0x045A</string>
</dict>
<key>InheritsFrom</key>
<array>
<string>family.com.nokia.series40.3rdEd.bus.usb-bt</string>
</array>
<key>Services</key>
<array>
<dict>
<key>ServiceName</key>
<string>com.apple.model</string>
<key>ServiceProperties</key>
<dict>
<key>ModelIcon</key>
<string>NOK6280.tiff</string>
<key>ModelName</key>
<string>6288</string>
</dict>
</dict>
<dict>
<key>ServiceName</key>
<string>com.apple.synchro</string>
<key>ServiceProperties</key>
<dict>
<key>MaxCityLength</key>
<integer>50</integer>
<key>MaxEMailLength</key>
<integer>60</integer>
<key>MaxEventLocationLength</key>
<integer>150</integer>
<key>MaxPhoneNumberLength</key>
<integer>48</integer>
<key>MaxPostalCodeLength</key>
<integer>50</integer>
<key>MaxStateLength</key>
<integer>50</integer>
<key>MaxStreetLength</key>
<integer>50</integer>
<key>MaxURLLength</key>
<integer>60</integer>
</dict>
</dict>
</array>
</dict>


After that just pair the device via bluetooth and run isync. Business as usual