<s> import shelve <EOL> from twisted . internet . defer import inlineCallbacks <EOL> from autobahn import wamp <EOL> from autobahn . twisted . wamp import ApplicationSession <EOL> class KeyValueStore ( ApplicationSession ) : <EOL> """<STR_LIT>""" <EOL> @ inlineCallbacks <EOL> def onJoin ( self , details ) : <EOL> self . store = shelve . open ( "<STR_LIT>" , flag = '<STR_LIT:c>' , writeback = False ) <EOL> yield self . register ( self ) <EOL> print ( "<STR_LIT>" ) <EOL> @ wamp . register ( u"<STR_LIT>" ) <EOL> def set ( self , key = None , value = None ) : <EOL> if key is not None : <EOL> k = str ( key ) <EOL> if value is not None : <EOL> self . store [ k ] = value <EOL> else : <EOL> if k in self . store : <EOL> del self . store [ k ] <EOL> else : <EOL> self . store . clear ( ) <EOL> self . store . sync ( ) <EOL> @ wamp . register ( u"<STR_LIT>" ) <EOL> def get ( self , key = None ) : <EOL> if key is None : <EOL> res = { } <EOL> for key , value in self . store . items ( ) : <EOL> res [ key ] = value <EOL> return res <EOL> else : <EOL> return self . store . get ( str ( key ) , None ) <EOL> @ wamp . register ( u"<STR_LIT>" ) <EOL> def keys ( self ) : <EOL> return self . store . keys ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> import argparse <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( "<STR_LIT>" , type = int , default = <NUM_LIT> , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( "<STR_LIT>" , type = str , default = None , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> from twisted . python import log <EOL> log . startLogging ( sys . stdout ) <EOL> from twisted . internet import reactor <EOL> print ( "<STR_LIT>" . format ( reactor . __class__ ) ) <EOL> if args . web : <EOL> from twisted . web . server import Site <EOL> from twisted . web . static import File <EOL> reactor . listenTCP ( args . web , Site ( File ( "<STR_LIT:.>" ) ) ) <EOL> from autobahn . twisted . wamp import ApplicationRunner <EOL> router = args . router or u'<STR_LIT>' <EOL> runner = ApplicationRunner ( router , u"<STR_LIT>" , standalone = not args . router ) <EOL> runner . run ( KeyValueStore ) </s>
