<s> import os , sys , pprint <EOL> try : <EOL> from twisted . python import usage <EOL> except ImportError : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> raise <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> class NoNodeError ( Exception ) : <EOL> def __init__ ( self , basedir ) : <EOL> self . basedir = basedir <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . basedir <EOL> class BasedirParameterMixin : <EOL> optParameters = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:d>" , None , "<STR_LIT>" ) , <EOL> ] <EOL> def postOptions ( self ) : <EOL> if not self [ "<STR_LIT>" ] : <EOL> if self . parent : <EOL> self [ "<STR_LIT>" ] = self . parent [ "<STR_LIT>" ] <EOL> class NoOptions ( BasedirParameterMixin , usage . Options ) : <EOL> pass <EOL> class BasedirArgument : <EOL> def parseArgs ( self , basedir = None ) : <EOL> if basedir is not None : <EOL> self [ "<STR_LIT>" ] = basedir <EOL> class StartArguments ( BasedirArgument ) : <EOL> def parseArgs ( self , basedir = None , * twistd_args ) : <EOL> self . twistd_args = twistd_args <EOL> BasedirArgument . parseArgs ( self , basedir ) <EOL> class CreateNodeOptions ( BasedirParameterMixin , BasedirArgument , usage . Options ) : <EOL> optParameters = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:l>" , None , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:h>" , "<STR_LIT:localhost>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT:port>" , "<STR_LIT:p>" , None , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:r>" , None , <EOL> "<STR_LIT>" ) , <EOL> ] <EOL> optFlags = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:m>" , "<STR_LIT>" ) , <EOL> ] <EOL> class PrintBaseURLOptions ( BasedirParameterMixin , BasedirArgument , usage . Options ) : <EOL> pass <EOL> class StartNodeOptions ( BasedirParameterMixin , StartArguments , usage . Options ) : <EOL> optFlags = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:n>" , "<STR_LIT>" ) , <EOL> ] <EOL> class StopNodeOptions ( BasedirParameterMixin , BasedirArgument , usage . Options ) : <EOL> pass <EOL> class RestartNodeOptions ( BasedirParameterMixin , StartArguments , usage . Options ) : <EOL> def postOptions ( self ) : <EOL> BasedirParameterMixin . postOptions ( self ) <EOL> self [ "<STR_LIT>" ] = False <EOL> class OpenOptions ( BasedirParameterMixin , BasedirArgument , usage . Options ) : <EOL> optFlags = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:n>" , "<STR_LIT>" ) , <EOL> ] <EOL> class SampleOptions ( BasedirParameterMixin , usage . Options ) : <EOL> optFlags = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:o>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT:error>" , "<STR_LIT:e>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:s>" , "<STR_LIT>" ) , <EOL> ] <EOL> def parseArgs ( self , data = "<STR_LIT>" ) : <EOL> self [ "<STR_LIT:data>" ] = data <EOL> class InviteOptions ( BasedirParameterMixin , usage . Options ) : <EOL> optParameters = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:n>" , None , "<STR_LIT>" ) , <EOL> ] <EOL> def parseArgs ( self , code = None ) : <EOL> self [ "<STR_LIT:code>" ] = code <EOL> synopsis = "<STR_LIT>" <EOL> class OfferMailboxOptions ( BasedirParameterMixin , usage . Options ) : <EOL> def parseArgs ( self , petname ) : <EOL> self [ "<STR_LIT>" ] = petname <EOL> synopsis = "<STR_LIT>" <EOL> longdesc = "<STR_LIT>" <EOL> class AcceptMailboxOptions ( BasedirParameterMixin , usage . Options ) : <EOL> optParameters = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:n>" , None , "<STR_LIT>" ) , <EOL> ] <EOL> def parseArgs ( self , code ) : <EOL> self [ "<STR_LIT:code>" ] = code <EOL> synopsis = "<STR_LIT>" <EOL> longdesc = "<STR_LIT>" <EOL> class SendBasicOptions ( BasedirParameterMixin , usage . Options ) : <EOL> def parseArgs ( self , cid , message ) : <EOL> self [ "<STR_LIT>" ] = cid <EOL> self [ "<STR_LIT:message>" ] = message <EOL> synopsis = "<STR_LIT>" <EOL> class TestOptions ( usage . Options ) : <EOL> def parseArgs ( self , * test_args ) : <EOL> if not test_args : <EOL> self . test_args = [ "<STR_LIT>" ] <EOL> else : <EOL> self . test_args = list ( test_args ) <EOL> synopsis = "<STR_LIT>" <EOL> class Options ( usage . Options ) : <EOL> synopsis = "<STR_LIT>" <EOL> optParameters = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:d>" , os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ] <EOL> subCommands = [ ( "<STR_LIT>" , None , CreateNodeOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , PrintBaseURLOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT:start>" , None , StartNodeOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , StopNodeOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , RestartNodeOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , OpenOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , SampleOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , InviteOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , OfferMailboxOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , AcceptMailboxOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , NoOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , SendBasicOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , NoOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , None , NoOptions , "<STR_LIT>" ) , <EOL> ( "<STR_LIT:test>" , None , TestOptions , "<STR_LIT>" ) , <EOL> ] <EOL> def opt_version ( self ) : <EOL> """<STR_LIT>""" <EOL> from . . import __version__ <EOL> print "<STR_LIT>" % __version__ <EOL> import twisted . copyright <EOL> print "<STR_LIT>" % twisted . copyright . version <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def opt_full_version ( self ) : <EOL> """<STR_LIT>""" <EOL> from . . _version import get_versions <EOL> v = get_versions ( ) <EOL> print "<STR_LIT>" % ( v [ "<STR_LIT:version>" ] , <EOL> v [ "<STR_LIT>" ] ) <EOL> import twisted . copyright <EOL> print "<STR_LIT>" % twisted . copyright . version <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def getUsage ( self , ** kwargs ) : <EOL> t = usage . Options . getUsage ( self , ** kwargs ) <EOL> return t + "<STR_LIT>" <EOL> def postOptions ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise usage . UsageError ( "<STR_LIT>" ) <EOL> def create_node ( * args ) : <EOL> from . create_node import create_node <EOL> return create_node ( * args , services = [ "<STR_LIT>" ] ) <EOL> def print_baseurl ( * args ) : <EOL> from . import create_node <EOL> return create_node . print_baseurl ( * args ) <EOL> def start ( * args ) : <EOL> from . startstop import start <EOL> return start ( * args ) <EOL> def stop ( * args ) : <EOL> from . startstop import stop <EOL> return stop ( * args ) <EOL> def restart ( * args ) : <EOL> from . startstop import restart <EOL> return restart ( * args ) <EOL> def open_control_panel ( * args ) : <EOL> from . open import open_control_panel <EOL> return open_control_panel ( * args ) <EOL> def invite ( * args ) : <EOL> from . import invite <EOL> return invite . invite ( * args ) <EOL> def offer_mailbox ( * args ) : <EOL> from . import invite <EOL> return invite . invite ( * args , offer_mailbox = True ) <EOL> def accept_mailbox ( * args ) : <EOL> from . import invite <EOL> return invite . invite ( * args , accept_mailbox = True ) <EOL> def follow_messages ( * args ) : <EOL> from . import messages <EOL> return messages . follow_messages ( * args ) <EOL> petmail_executable = [ ] <EOL> def test ( so , stdout , stderr ) : <EOL> petmail = os . path . abspath ( sys . argv [ <NUM_LIT:0> ] ) <EOL> petmail_executable . append ( petmail ) <EOL> from twisted . scripts import trial as twisted_trial <EOL> sys . argv = [ "<STR_LIT>" ] + so . test_args <EOL> twisted_trial . run ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def render_text ( result ) : <EOL> return result [ "<STR_LIT>" ] + "<STR_LIT:\n>" <EOL> def render_all ( result ) : <EOL> return pprint . pformat ( result ) <EOL> def render_addressbook ( result ) : <EOL> lines = [ ] <EOL> for entry in sorted ( result [ "<STR_LIT>" ] , key = lambda e : e [ "<STR_LIT>" ] ) : <EOL> lines . append ( '<STR_LIT>' % ( entry [ "<STR_LIT>" ] , entry [ "<STR_LIT>" ] ) ) <EOL> lines . append ( "<STR_LIT>" % { False : "<STR_LIT>" , <EOL> True : "<STR_LIT>" } [ entry [ "<STR_LIT>" ] ] ) <EOL> if entry . has_key ( "<STR_LIT>" ) : <EOL> lines . append ( "<STR_LIT>" % entry [ "<STR_LIT>" ] ) <EOL> lines . append ( "<STR_LIT>" % entry [ "<STR_LIT>" ] ) <EOL> return "<STR_LIT:\n>" . join ( lines ) + "<STR_LIT:\n>" <EOL> def render_messages ( result ) : <EOL> lines = [ ] <EOL> for entry in sorted ( result [ "<STR_LIT>" ] , key = lambda e : e [ "<STR_LIT:id>" ] ) : <EOL> lines . append ( '<STR_LIT>' % ( <EOL> entry [ "<STR_LIT:id>" ] , entry [ "<STR_LIT>" ] , entry [ "<STR_LIT>" ] , entry [ "<STR_LIT>" ] ) ) <EOL> lines . append ( str ( entry [ "<STR_LIT>" ] ) ) <EOL> return "<STR_LIT:\n>" . join ( lines ) + "<STR_LIT:\n>" <EOL> def WebCommand ( name , argnames , render = render_text , extra_args = { } ) : <EOL> def _command ( so , out , err ) : <EOL> from . webwait import command <EOL> args = dict ( [ ( argname , so [ argname ] ) for argname in argnames ] ) <EOL> for k , v in extra_args . items ( ) : <EOL> args [ k ] = v <EOL> ok , result = command ( so [ "<STR_LIT>" ] , name , args , err ) <EOL> if ok : <EOL> print >> out , render ( result ) , <EOL> return <NUM_LIT:0> <EOL> else : <EOL> print >> err , result [ "<STR_LIT>" ] , <EOL> return <NUM_LIT:1> <EOL> return _command <EOL> def accept ( * args ) : <EOL> from . accept_invitation import accept_invitation <EOL> return accept_invitation ( * args ) <EOL> DISPATCH = { "<STR_LIT>" : create_node , <EOL> "<STR_LIT>" : print_baseurl , <EOL> "<STR_LIT:start>" : start , <EOL> "<STR_LIT>" : stop , <EOL> "<STR_LIT>" : restart , <EOL> "<STR_LIT>" : open_control_panel , <EOL> "<STR_LIT:test>" : test , <EOL> "<STR_LIT>" : WebCommand ( "<STR_LIT>" , [ "<STR_LIT:data>" , "<STR_LIT>" , <EOL> "<STR_LIT:error>" , "<STR_LIT>" ] ) , <EOL> "<STR_LIT>" : invite , <EOL> "<STR_LIT>" : offer_mailbox , <EOL> "<STR_LIT>" : accept_mailbox , <EOL> "<STR_LIT>" : WebCommand ( "<STR_LIT>" , [ ] , <EOL> render = render_addressbook ) , <EOL> "<STR_LIT>" : WebCommand ( "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT:message>" ] ) , <EOL> "<STR_LIT>" : WebCommand ( "<STR_LIT>" , [ ] , <EOL> render = render_messages ) , <EOL> "<STR_LIT>" : follow_messages , <EOL> "<STR_LIT>" : accept , <EOL> } <EOL> def run ( args , stdout , stderr , petmail = None ) : <EOL> """<STR_LIT>""" <EOL> config = Options ( ) <EOL> try : <EOL> config . parseOptions ( args ) <EOL> except usage . error , e : <EOL> c = config <EOL> while hasattr ( c , '<STR_LIT>' ) : <EOL> c = c . subOptions <EOL> print >> stderr , str ( c ) <EOL> print >> stderr , e . args [ <NUM_LIT:0> ] <EOL> return <NUM_LIT:1> <EOL> command = config . subCommand <EOL> so = config . subOptions <EOL> so [ "<STR_LIT>" ] = petmail <EOL> try : <EOL> rc = DISPATCH [ command ] ( so , stdout , stderr ) <EOL> return rc <EOL> except ImportError , e : <EOL> print >> stderr , "<STR_LIT>" <EOL> print >> stderr , e <EOL> print >> stderr , "<STR_LIT>" <EOL> raise <EOL> return <NUM_LIT:1> <EOL> except NoNodeError , e : <EOL> print >> stderr , e <EOL> return <NUM_LIT:1> <EOL> def entry ( ) : <EOL> """<STR_LIT>""" <EOL> return run ( sys . argv [ <NUM_LIT:1> : ] , sys . stdout , sys . stderr , petmail = sys . argv [ <NUM_LIT:0> ] ) </s>
