Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:p 40': 0.04; 'instance': 0.05; 'backwards': 0.07; 'server:': 0.07; 'python': 0.08; 'configure,': 0.09; 'header:In-reply-to:1': 0.09; 'hostname': 0.09; 'normally,': 0.09; 'revision': 0.09; 'servers?': 0.09; 'am,': 0.13; 'wrote:': 0.15; 'above?': 0.16; 'argparse': 0.16; 'from:addr:free.fr': 0.16; 'originally,': 0.16; 'argument': 0.16; 'class,': 0.16; 'def': 0.16; 'thanks,': 0.17; 'issue.': 0.19; 'slightly': 0.19; 'appropriate': 0.20; 'pass': 0.28; 'skip:p 30': 0.28; 'server': 0.29; 'script': 0.29; 'generic': 0.29; 'build,': 0.30; 'deploy,': 0.30; 'pattern': 0.30; 'class': 0.31; 'hi,': 0.31; "skip:' 10": 0.32; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'however,': 0.34; '...': 0.34; 'things': 0.34; 'none': 0.35; 'question': 0.35; 'operations': 0.36; 'perform': 0.37; 'could': 0.37; 'using': 0.37; 'another': 0.38; 'subject:: ': 0.38; 'execution': 0.38; 'run': 0.39; 'perhaps': 0.39; 'correctly': 0.39; 'to:addr:python.org': 0.39; 'skip:d 20': 0.40; 'setup': 0.40; 'your': 0.60; 'link': 0.63; 'due': 0.66; 'received:62': 0.67; 'wish': 0.70; 'servers:': 0.84; 'victor': 0.91 Date: Mon, 18 Jul 2011 13:27:18 +0200 From: Karim Subject: Re: Argparse, and linking to methods in Subclasses In-reply-to: To: python-list@python.org MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7bit References: User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1310988509 news.xs4all.nl 23939 [2001:888:2000:d::a6]:47273 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9793 with global: SERVER = None A the end of Argparse declarations: parser_check.set_defaults(action=do_the_check) parser_build.set_defaults(action=do_the_build) Then declare the action functions: def do_the_check(namespace_args): if not SERVER: SERVER = Server(namespace_arg.type_of_server) SERVER.check() def do_the_build(namespace_args): if not SERVER: SERVER = Server(namespace_arg.type_of_server) SERVER.build() If I correctly understood your issue. Regards Karim On 07/18/2011 03:48 AM, Victor Hooi wrote: > Hi, > > I have a simple Python script to perform operations on various types on in-house servers: > > manage_servers.py > > Operations are things like check, build, deploy, configure, verify etc. > > Types of server are just different types of inhouse servers we use. > > We have a generic server class, then specific types that inherit from that: > > class Server > def configure_logging(self, loggin_file): > ... > def check(self): > ... > def deploy(self): > ... > def configure(self): > ... > def __init__(self, hostname): > self.hostname = hostname > logging = self.configure_logging(LOG_FILENAME) > class SpamServer(Server): > def check(self): > ... > class HamServer(Server): > def deploy(self): > ... > > My question is how to link that all up to argparse? > > Originally, I was using argparse subparses for the operations (check, build, deploy) and another argument for the type. > > subparsers = parser.add_subparsers(help='The operation that you want to run on the server.') > parser_check = subparsers.add_parser('check', help='Check that the server has been setup correctly.') > parser_build = subparsers.add_parser('build', help='Download and build a copy of the execution stack.') > parser_build.add_argument('-r', '--revision', help='SVN revision to build from.') > ... > parser.add_argument('type_of_server', action='store', choices=types_of_servers, > help='The type of server you wish to create.') > > Normally, you'd link each subparse to a method - and then pass in the type_of_server as an argument. However, that's slightly backwards due to the classes- I need to create an instance of the appropriate Server class, then call the operation method inside of that. > > Any ideas of how I could achieve the above? Perhaps a different design pattern for Servers? Or a way to use argparse in this situation? > > Thanks, > Victor