/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Public Attributes | Private Member Functions
Scraper::base::aparser::AParser Class Reference
Inheritance diagram for Scraper::base::aparser::AParser:
Inheritance graph
[legend]
Collaboration diagram for Scraper::base::aparser::AParser:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __call__

Public Attributes

 loglevel
 parent parser
 pp
 pvars
 argv
 conf
 hold on to unparsed args for subsequent parsing

Private Member Functions

def _read_conf

Detailed Description

Primes an argparser with defaults read from a section of an
ConfigParser style config file and sets up logging 

Operates via 2-stage parsing

Usage::

    parser = AParser(defpath="~/.scraper.cfg",defsect="default")
    parser.add_argument( '-m','--maxiter', help="maximum iterations, or 0 for no limit")
    parser.set_defaults( maxiter=0 )
    args = parser() 
    print args

Draws upon:

* http://blog.vwelch.com/2011/04/combining-configparser-and-argparse.html
* http://www.doughellmann.com/PyMOTW/argparse/

Definition at line 13 of file aparser.py.


Constructor & Destructor Documentation

def Scraper::base::aparser::AParser::__init__ (   self,
  args,
  kwargs 
)

Definition at line 70 of file aparser.py.

00071                                         :
00072          defpath = kwargs.pop('defpath')  # mandatory
00073          defsect = kwargs.pop('defsect')  # mandatory  
00074        
00075 
00076          self._read_conf(defpath=defpath, defsect=defsect)
00077 
00078          description = kwargs.pop('description', __doc__ )
00079 
00080          ArgumentParser.__init__( self, parents=[self.pp], description=description, 
00081                     formatter_class=RawDescriptionHelpFormatter )  # Don't mess with format of description
00082          
00083          d = self.pvars
00084          d.update( self.conf )
00085          self.set_defaults( **d )
00086          #log.debug("ArgumentParser primed with defaults %r %r  " % ( self.pvars, self.conf ) )


Member Function Documentation

def Scraper::base::aparser::AParser::_read_conf (   self,
  defpath,
  defsect 
) [private]
Note drawback of this have-your-cake-and-eat-it approach that combines
argparsing with ConfigParser 
for `--help` to work must specify an existing default config section  

Definition at line 34 of file aparser.py.

00035                                            :
00036         """
00037         Note drawback of this have-your-cake-and-eat-it approach that combines
00038         argparsing with ConfigParser 
00039         for `--help` to work must specify an existing default config section  
00040         """ 
00041         pp = ArgumentParser( add_help=False)  ## parent parser
00042 
00043         pp.add_argument("-l","--loglevel", help="logging level INFO, DEBUG, WARN,...  Default: %(default)s ")
00044         pp.add_argument("-c","--cfg",      help="Path to config file. Default: %(default)s" )
00045         pp.add_argument("-s","--sect",     help="Section in config file. Default: %(default)s" )
00046         pp.set_defaults( cfg=defpath , loglevel="INFO" , sect=defsect )
00047         pargs, argv = pp.parse_known_args()   ## unparsed args are returned
00048 
00049         if pargs.loglevel:
00050             level = getattr( logging , pargs.loglevel.upper() )
00051             logging.basicConfig(level=level )
00052        
00053         if pargs.cfg:
00054             rcp = RawConfigParser()
00055             rcp.read([os.path.expandvars(os.path.expanduser(pargs.cfg))])
00056             sect = getattr(pargs, 'sect', "Default" )
00057             try:
00058                 conf = dict(rcp.items(sect))
00059             except NoSectionError:
00060                 log.warn("no config file at %s or no section %s " % ( pargs.cfg, sect ) ) 
00061                 sys.exit(1) 
00062             log.info( "base config read from %s:%s " % (pargs.cfg, sect,)  )
00063             log.debug( "giving %s " % (pformat(conf))  )
00064 
00065         self.loglevel = pargs.loglevel  
00066         self.pp = pp
00067         self.pvars = vars(pargs)
00068         self.argv = argv           ## hold on to unparsed args for subsequent parsing 
00069         self.conf = conf

def Scraper::base::aparser::AParser::__call__ (   self,
  args,
  kwa 
)

Definition at line 87 of file aparser.py.

00088                                      :
00089          noargs = kwa.get('noargs',False)
00090          args = [] if noargs else self.argv 
00091          d = vars( self.parse_args( args ) )  
00092          return dict(d, loglevel=self.loglevel )
00093 
00094 


Member Data Documentation

parent parser

unparsed args are returned

Definition at line 40 of file aparser.py.

Definition at line 40 of file aparser.py.

Definition at line 40 of file aparser.py.

Definition at line 40 of file aparser.py.

hold on to unparsed args for subsequent parsing

Definition at line 41 of file aparser.py.


The documentation for this class was generated from the following file:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:50:03 for Scraper by doxygen 1.7.4