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