Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9793
| Date | 2011-07-18 13:27 +0200 |
|---|---|
| From | Karim <karim.liateni@free.fr> |
| Subject | Re: Argparse, and linking to methods in Subclasses |
| References | <fbe9c830-30a9-4811-b142-b812de1be3d2@glegroupsg2000goo.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1210.1310988509.1164.python-list@python.org> (permalink) |
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<operation> <type_of_server>
>
> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Argparse, and linking to methods in Subclasses Victor Hooi <victorhooi@gmail.com> - 2011-07-17 18:48 -0700 Re: Argparse, and linking to methods in Subclasses Karim <karim.liateni@free.fr> - 2011-07-18 13:27 +0200
csiph-web