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 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 options 
 23  """ 
 24   
 25  from flumotion.common import common 
 26  from flumotion.common import log 
 27   
 28   
29 -def OptparseOptionParserClass():
30 from optparse import OptionParser as BaseOP 31 class OptionParser(BaseOP): 32 def __init__(self, usage, description, domain): 33 self.domain = domain 34 BaseOP.__init__(self, usage=usage, description=description)
35 return OptionParser 36
37 -def OptparseOptionGroupClass():
38 from optparse import OptionGroup as BaseOG 39 class OptionGroup(BaseOG): 40 def __init__(self, parser, title, description=None, **kwargs): 41 BaseOG.__init__(self, parser, title, description, 42 **kwargs)
43 return OptionGroup 44
45 -def GOptionOptionParserClass(use_gst):
46 from goption.option import OptionParser as BaseOP 47 class OptionParser(BaseOP): 48 def __init__(self, usage, description, domain): 49 self.domain = domain 50 BaseOP.__init__(self, usage=usage, description=description) 51 if use_gst: 52 try: 53 import pygst 54 pygst.require('0.10') 55 import gstoption 56 self.add_option_group(gstoption.get_group()) 57 except ImportError: 58 pass
59 return OptionParser 60
61 -def GOptionOptionGroupClass():
62 from goption.option import OptionGroup as BaseOG 63 class OptionGroup(BaseOG): 64 def __init__(self, parser, title, description=None, **kwargs): 65 if not description: 66 description = title.capitalize() + " options" 67 BaseOG.__init__(self, title, description, 68 option_list=[], **kwargs)
69 return OptionGroup 70
71 -def OptionParser(usage="", description="", domain=""):
72 """I have two responsibilities: 73 - provide a generic interface to OptionParser on top of the optparse 74 implementation and the GOption variant. 75 - abstract the common command line arguments used by all flumotion 76 binaries 77 """ 78 from flumotion.common.boot import USE_GOPTION_PARSER, USE_GST 79 if USE_GOPTION_PARSER: 80 OptionParser = GOptionOptionParserClass(USE_GST) 81 else: 82 OptionParser = OptparseOptionParserClass() 83 84 class FOptionParser(OptionParser): 85 def __init__(self, usage, description, domain): 86 OptionParser.__init__(self, usage, description, domain) 87 self._add_common_options()
88 89 def _add_common_options(self): 90 self.add_option('-d', '--debug', 91 action="store", type="string", dest="debug", 92 help="set debug levels") 93 self.add_option('-v', '--verbose', 94 action="store_true", dest="verbose", 95 help="be verbose") 96 self.add_option('', '--version', 97 action="store_true", dest="version", 98 default=False, 99 help="show version information") 100 101 def parse_args(self, args): 102 options, args = OptionParser.parse_args(self, args) 103 104 if options.verbose: 105 log.setFluDebug("*:3") 106 107 if options.version: 108 print common.version(self.domain) 109 import sys 110 sys.exit(0) 111 112 if options.debug: 113 log.setFluDebug(options.debug) 114 115 return options, args 116 117 return FOptionParser(usage, description, domain) 118 119
120 -def OptionGroup(parser, title, description=None, **kwargs):
121 from flumotion.common.boot import USE_GOPTION_PARSER 122 if USE_GOPTION_PARSER: 123 OptionGroup = GOptionOptionGroupClass() 124 else: 125 OptionGroup = OptparseOptionGroupClass() 126 127 class FOptionGroup(OptionGroup): 128 pass
129 return FOptionGroup(parser, title, description, **kwargs) 130