Options.py :  » Development » PyChecker » pychecker-0.8.18 » pychecker2 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyChecker 
PyChecker » pychecker 0.8.18 » pychecker2 » Options.py
class Error(Exception): pass

class Opt:

    def __init__(self, object, longName, description, default):
        self.object = object
        self.longName = longName
        self.description = description
        setattr(object, longName, default)
        self.default = default
        
    def set_value(self, value):
        setattr(self.object, self.longName, value)

    def get_value(self):
        return getattr(self.object, self.longName)

    def get_description(self):
        return self.description

    def is_boolean(self):
        return None
    
    def reset(self):
        setattr(self.object, self.longName, self.default)

class BoolOpt(Opt):

    def __init__(self, object, longName, description, default = None):
        Opt.__init__(self, object, longName, description, default)

    def set_value(self, unused):
        setattr(self.object, self.longName, not self.get_value())
        
    def is_boolean(self):
        return 1


MAJOR = 'Major'
ERROR = 'Error'
MISC = 'Miscellaneous'
Categories = [MAJOR, ERROR, MISC]

class Options:

    def __init__(self):
        self.options = {}
        for c in Categories:
            self.options[c] = []
        self.add(BoolOpt(self, 'verbose', 'turn on verbose messages'), MISC)
        self.add(BoolOpt(self, 'incremental', 'print warnings as they are created'), MISC)
        self.add(BoolOpt(self, 'profile', 'print a profile of pychecker', 0), MISC)

    def add(self, option, category=ERROR):
        self.options[category].append(option)
        
    def process_options(self, args):
        import getopt
        try:
            longopts = {}
            for opts in self.options.values():
                for opt in opts:
                    opt.reset()
                    optname = opt.longName
                    if opt.is_boolean() and opt.get_value():
                        optname = "no-" + opt.longName
                    longopts[optname] = opt
            specs = []
            for k, v in longopts.items():
                if not v.is_boolean():
                    k += '='
                specs.append(k)
            opts, args = getopt.getopt(args, '', specs)
        except getopt.GetoptError, detail:
            raise Error(detail)
        
        for opt, arg in opts:
            longopts[opt[2:]].set_value(arg)

        from pychecker2.File import File
        return [ File(f) for f in args ]

    def usage(self, argv0, stream):
        indent = " "
        over = 20
        print >> stream, "Usage:"
        print >> stream, \
              "%s%s [options] [--] file1.py file2.py ..." % (indent, argv0)
        print >> stream, "available options:"
        for c in Categories:
            if not self.options[c]:
                continue
            print >> stream
            print >> stream, "%s:" % c
            opts = self.options[c]
            opts.sort(lambda x, y: cmp(x.longName, y.longName))
            for opt in opts:
                name = opt.longName
                if opt.is_boolean() and opt.get_value():
                        name = "no-" + name
                print >> stream, "%s--%*s %s" % (
                    indent, -over, name, opt.get_description())
                if not opt.is_boolean():
                    print >> stream, "%s  %*s %s" % (
                        indent, -over, '', opt.get_value())
            
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.