summaryrefslogtreecommitdiff
path: root/pykwalify/cli.py
blob: 43838cd4c95d241aebc6a363c3e1ea041ba42cc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-

""" pyKwalify - cli.py """

# python std lib
import logging
import logging.config
import sys

# 3rd party imports
from docopt import docopt


def parse_cli():
    """
    The outline of this function needs to be like this:

    1. parse arguments
    2. validate arguments only, dont go into other logic/code
    3. run application logic
    """

    #
    # 1. parse cli arguments
    #

    __docopt__ = """
usage: pykwalify -d FILE -s FILE ... [-e FILE ...]
       [--strict-rule-validation] [--fix-ruby-style-regex] [--allow-assertions] [--encoding ENCODING]
       [-v ...] [-q]

optional arguments:
  -d FILE, --data-file FILE            the file to be tested
  -e FILE, --extension FILE            file containing python extension
  -s FILE, --schema-file FILE          schema definition file
  --fix-ruby-style-regex               This flag fixes some of the quirks of ruby style regex
                                       that is not compatible with python style regex
  --strict-rule-validation             enables strict validation of all keywords for all
                                       Rule objects to find unsupported keyword usage
  --allow-assertions                   By default assertions is disabled due to security risk.
                                       Error will be raised if assertion is used in schema
                                       but this flag is not used. This option enables assert keyword.
  --encoding ENCODING                  Specify encoding to open data and schema files with.
  -h, --help                           show this help message and exit
  -q, --quiet                          suppress terminal output
  -v, --verbose                        verbose terminal output (multiple -v increases verbosity)
  --version                            display the version number and exit
"""

    # Import pykwalify package
    import pykwalify

    args = docopt(__docopt__, version=pykwalify.__version__)

    pykwalify.init_logging(1 if args["--quiet"] else args["--verbose"])
    log = logging.getLogger(__name__)

    #
    # 2. validate arguments only, dont go into other code/logic
    #

    log.debug("Setting verbose level: %s", args["--verbose"])
    log.debug("Arguments from CLI: %s", args)

    return args


def run(cli_args):
    """
    Split the functionality into 2 methods.

    One for parsing the cli and one that runs the application.
    """
    from .core import Core

    c = Core(
        source_file=cli_args["--data-file"],
        schema_files=cli_args["--schema-file"],
        extensions=cli_args['--extension'],
        strict_rule_validation=cli_args['--strict-rule-validation'],
        fix_ruby_style_regex=cli_args['--fix-ruby-style-regex'],
        allow_assertions=cli_args['--allow-assertions'],
        file_encoding=cli_args['--encoding'],
    )
    c.validate()
    return c


def cli_entrypoint():
    """
    Main entrypoint for script. Used by setup.py to automatically
    create a cli script
    """
    # Check minimum version of Python
    if sys.version_info < (2, 7, 0):
        sys.stderr.write(u"WARNING: pykwalify: It is recommended to run pykwalify on python version 2.7.x or later...\n\n")

    run(parse_cli())