Exemplo n.º 1
0
    def testCanValidateFieldFormatFromPlugin(self):
        spec = ""","Interface for data with plugged field format"
"D","Format","CSV"
"D","Line delimiter","Any"
"D","Item delimiter",","
,
,"Name","Example","Empty","Length","Type","Rule"
"F","first_name","John","X",,"Text"
"F","sirname","Smith","X",,"CapitalizedText"
"""
        _log.info(u"subclasses before=%s", sorted(fields.AbstractFieldFormat.__subclasses__()))  # @UndefinedVariable
        interface.importPlugins(dev_test.getTestPluginsPath())
        _log.info(u"subclasses after=%s", sorted(fields.AbstractFieldFormat.__subclasses__()))  # @UndefinedVariable
        icd = interface.InterfaceControlDocument()
        icd.read(StringIO.StringIO(spec))
        dataText = """First Name,Gender,Date of birth
John,Smith
Bärbel,Müller"""
        dataReadable = StringIO.StringIO(dataText)
        icd.validate(dataReadable)
Exemplo n.º 2
0
    def setOptions(self, argv):
        """Reset options and set them again from argument list such as sys.argv[1:]."""
        assert argv is not None

        usage = u"""
  cutplace [options] ICD-FILE
    validate interface control document in ICD-FILE
  cutplace [options] ICD-FILE DATA-FILE(S)
    validate DATA-FILE(S) according to rules specified in ICD-FILE
  cutplace --web [options]
    launch web server providing a web interface for validation"""

        parser = _NoExitOptionParser(usage=usage, version="%prog " + version.VERSION_NUMBER)
        parser.set_defaults(
            icdEncoding=DEFAULT_ICD_ENCODING,
            isLogTrace=False,
            isOpenBrowser=False,
            logLevel="warning",
            port=_web.DEFAULT_PORT,
        )
        parser.add_option(
            "--list-encodings",
            action="store_true",
            dest="isShowEncodings",
            help="show list of available character encodings and exit",
        )
        validationGroup = optparse.OptionGroup(
            parser, "Validation options", "Specify how to validate data and how to report the results"
        )
        validationGroup.add_option(
            "-e",
            "--icd-encoding",
            metavar="ENCODING",
            dest="icdEncoding",
            help="character encoding to use when reading the ICD (default: %default)",
        )
        validationGroup.add_option(
            "-P",
            "--plugins",
            metavar="FOLDER",
            dest="pluginsFolderPath",
            help="folder to scan for plugins (default: %default)",
        )
        validationGroup.add_option(
            "-s",
            "--split",
            action="store_true",
            dest="isSplit",
            help="split data in a CSV file containing the accepted rows and a raw text file "
            + "containing rejected rows with both using UTF-8 as character encoding",
        )
        parser.add_option_group(validationGroup)
        webGroup = optparse.OptionGroup(
            parser, "Web options", "Provide a  GUI for validation using a simple web server"
        )
        webGroup.add_option("-w", "--web", action="store_true", dest="isWebServer", help="launch web server")
        webGroup.add_option(
            "-p", "--port", metavar="PORT", type="int", dest="port", help="port for web server (default: %default)"
        )
        webGroup.add_option(
            "-b", "--browse", action="store_true", dest="isOpenBrowser", help="open validation page in browser"
        )
        parser.add_option_group(webGroup)
        loggingGroup = optparse.OptionGroup(parser, "Logging options", "Modify the logging output")
        loggingGroup.add_option(
            "--log",
            metavar="LEVEL",
            type="choice",
            choices=_tools.LogLevelNameToLevelMap.keys(),
            dest="logLevel",
            help="set log level to LEVEL (default: %default)",
        )
        loggingGroup.add_option(
            "-t",
            "--trace",
            action="store_true",
            dest="isLogTrace",
            help="include Python stack in error messages related to data",
        )
        parser.add_option_group(loggingGroup)

        (self.options, others) = parser.parse_args(argv[1:])

        self._log.setLevel(_tools.LogLevelNameToLevelMap[self.options.logLevel])
        self.icdEncoding = self.options.icdEncoding
        self.isLogTrace = self.options.isLogTrace
        self.isOpenBrowser = self.options.isOpenBrowser
        self.isShowEncodings = self.options.isShowEncodings
        self.isWebServer = self.options.isWebServer
        self.port = self.options.port
        self.isSplit = self.options.isSplit

        if self.options.pluginsFolderPath is not None:
            interface.importPlugins(self.options.pluginsFolderPath)

        if not self.isShowEncodings and not self.isWebServer:
            if len(others) >= 1:
                icdPath = others[0]
                try:
                    self.setIcdFromFile(icdPath)
                except EnvironmentError, error:
                    raise IOError(u"cannot read ICD file %r: %s" % (icdPath, error))
                if len(others) >= 2:
                    self.dataToValidatePaths = others[1:]
            else:
                parser.error(u"file containing ICD must be specified")
Exemplo n.º 3
0
    def setOptions(self, argv):
        """Reset options and set them again from argument list such as sys.argv[1:]."""
        assert argv is not None

        usage = u"""
  cutplace [options] ICD-FILE
    validate interface control document in ICD-FILE
  cutplace [options] ICD-FILE DATA-FILE(S)
    validate DATA-FILE(S) according to rules specified in ICD-FILE
  cutplace --web [options]
    launch web server providing a web interface for validation"""

        parser = _NoExitOptionParser(usage=usage,
                                     version="%prog " + version.VERSION_NUMBER)
        parser.set_defaults(icdEncoding=DEFAULT_ICD_ENCODING,
                            isLogTrace=False,
                            isOpenBrowser=False,
                            logLevel="warning",
                            port=_web.DEFAULT_PORT)
        parser.add_option(
            "--list-encodings",
            action="store_true",
            dest="isShowEncodings",
            help="show list of available character encodings and exit")
        validationGroup = optparse.OptionGroup(
            parser, "Validation options",
            "Specify how to validate data and how to report the results")
        validationGroup.add_option(
            "-e",
            "--icd-encoding",
            metavar="ENCODING",
            dest="icdEncoding",
            help=
            "character encoding to use when reading the ICD (default: %default)"
        )
        validationGroup.add_option(
            "-P",
            "--plugins",
            metavar="FOLDER",
            dest="pluginsFolderPath",
            help="folder to scan for plugins (default: %default)")
        validationGroup.add_option(
            "-s",
            "--split",
            action="store_true",
            dest="isSplit",
            help=
            "split data in a CSV file containing the accepted rows and a raw text file "
            +
            "containing rejected rows with both using UTF-8 as character encoding"
        )
        parser.add_option_group(validationGroup)
        webGroup = optparse.OptionGroup(
            parser, "Web options",
            "Provide a  GUI for validation using a simple web server")
        webGroup.add_option("-w",
                            "--web",
                            action="store_true",
                            dest="isWebServer",
                            help="launch web server")
        webGroup.add_option("-p",
                            "--port",
                            metavar="PORT",
                            type="int",
                            dest="port",
                            help="port for web server (default: %default)")
        webGroup.add_option("-b",
                            "--browse",
                            action="store_true",
                            dest="isOpenBrowser",
                            help="open validation page in browser")
        parser.add_option_group(webGroup)
        loggingGroup = optparse.OptionGroup(parser, "Logging options",
                                            "Modify the logging output")
        loggingGroup.add_option(
            "--log",
            metavar="LEVEL",
            type="choice",
            choices=_tools.LogLevelNameToLevelMap.keys(),
            dest="logLevel",
            help="set log level to LEVEL (default: %default)")
        loggingGroup.add_option(
            "-t",
            "--trace",
            action="store_true",
            dest="isLogTrace",
            help="include Python stack in error messages related to data")
        parser.add_option_group(loggingGroup)

        (self.options, others) = parser.parse_args(argv[1:])

        self._log.setLevel(
            _tools.LogLevelNameToLevelMap[self.options.logLevel])
        self.icdEncoding = self.options.icdEncoding
        self.isLogTrace = self.options.isLogTrace
        self.isOpenBrowser = self.options.isOpenBrowser
        self.isShowEncodings = self.options.isShowEncodings
        self.isWebServer = self.options.isWebServer
        self.port = self.options.port
        self.isSplit = self.options.isSplit

        if self.options.pluginsFolderPath is not None:
            interface.importPlugins(self.options.pluginsFolderPath)

        if not self.isShowEncodings and not self.isWebServer:
            if len(others) >= 1:
                icdPath = others[0]
                try:
                    self.setIcdFromFile(icdPath)
                except EnvironmentError, error:
                    raise IOError(u"cannot read ICD file %r: %s" %
                                  (icdPath, error))
                if len(others) >= 2:
                    self.dataToValidatePaths = others[1:]
            else:
                parser.error(u"file containing ICD must be specified")