Package flumotion :: Package common :: Module options
[hide private]

Source Code for Module flumotion.common.options

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_options -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3   
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  """command-line parsing and options 
 19  """ 
 20   
 21  from flumotion.common import common 
 22  from flumotion.common import log 
 23   
 24  # Disable optionparser, it needs more upstream 
 25  # changes to work properly 
 26  from flumotion.common import boot 
 27   
 28  __version__ = "$Rev$" 
 29   
 30  boot.USE_GOPTION_PARSER = False 
 31   
 32   
33 -def OptparseOptionParserClass():
34 from optparse import OptionParser as BaseOP 35 36 class OptionParser(BaseOP): 37 38 def __init__(self, usage, description, domain): 39 self.domain = domain 40 BaseOP.__init__(self, usage=usage, description=description)
41 return OptionParser 42 43
44 -def OptparseOptionGroupClass():
45 from optparse import OptionGroup as BaseOG 46 47 class OptionGroup(BaseOG): 48 49 def __init__(self, parser, title, description=None, **kwargs): 50 BaseOG.__init__(self, parser, title, description, 51 **kwargs)
52 return OptionGroup 53 54
55 -def GOptionOptionParserClass(use_gst):
56 from gobject.option import OptionParser as BaseOP 57 58 class OptionParser(BaseOP): 59 60 def __init__(self, usage, description, domain): 61 self.domain = domain 62 BaseOP.__init__(self, usage=usage, description=description) 63 if use_gst: 64 try: 65 import pygst 66 pygst.require('0.10') 67 import gstoption 68 self.add_option_group(gstoption.get_group()) 69 except ImportError: 70 pass
71 return OptionParser 72 73
74 -def GOptionOptionGroupClass():
75 from goption.option import OptionGroup as BaseOG 76 77 class OptionGroup(BaseOG): 78 79 def __init__(self, parser, title, description=None, **kwargs): 80 if not description: 81 description = title.capitalize() + " options" 82 BaseOG.__init__(self, title, description, 83 option_list=[], **kwargs)
84 return OptionGroup 85 86
87 -def OptionParser(usage="", description="", domain=""):
88 """I have two responsibilities: 89 - provide a generic interface to OptionParser on top of the optparse 90 implementation and the GOption variant. 91 - abstract the common command line arguments used by all flumotion 92 binaries 93 """ 94 from flumotion.common.boot import USE_GOPTION_PARSER, USE_GST 95 if USE_GOPTION_PARSER: 96 OptionParser = GOptionOptionParserClass(USE_GST) 97 else: 98 OptionParser = OptparseOptionParserClass() 99 100 class FOptionParser(OptionParser): 101 102 def __init__(self, usage, description, domain): 103 OptionParser.__init__(self, usage, description, domain) 104 self._add_common_options()
105 106 def _add_common_options(self): 107 self.add_option('-d', '--debug', 108 action="store", type="string", dest="debug", 109 help="set debug levels") 110 self.add_option('-v', '--verbose', 111 action="store_true", dest="verbose", 112 help="be verbose") 113 self.add_option('', '--version', 114 action="store_true", dest="version", 115 default=False, 116 help="show version information") 117 118 def parse_args(self, args): 119 options, args = OptionParser.parse_args(self, args) 120 121 if options.verbose: 122 log.setFluDebug("*:3") 123 124 if options.version: 125 print common.version(self.domain) 126 import sys 127 sys.exit(0) 128 129 if options.debug: 130 log.setFluDebug(options.debug) 131 132 return options, args 133 134 return FOptionParser(usage, description, domain) 135 136
137 -def OptionGroup(parser, title, description=None, **kwargs):
138 from flumotion.common.boot import USE_GOPTION_PARSER 139 if USE_GOPTION_PARSER: 140 OptionGroup = GOptionOptionGroupClass() 141 else: 142 OptionGroup = OptparseOptionGroupClass() 143 144 class FOptionGroup(OptionGroup): 145 pass
146 return FOptionGroup(parser, title, description, **kwargs) 147