File: testcli.py

package info (click to toggle)
python-activipy 0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 464 kB
  • sloc: python: 1,262; makefile: 171; lisp: 18; sh: 13
file content (130 lines) | stat: -rw-r--r-- 3,039 bytes parent folder | download | duplicates (6)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# coding=utf-8
## Activipy --- ActivityStreams 2.0 implementation and validator for Python
## Copyright © 2015 Christopher Allan Webber <cwebber@dustycloud.org>
##
## This file is part of Activipy, which is GPLv3+ or Apache v2, your option
## (see COPYING); since that means effectively Apache v2 here's those headers
##
## Apache v2 header:
##   Licensed under the Apache License, Version 2.0 (the "License");
##   you may not use this file except in compliance with the License.
##   You may obtain a copy of the License at
##
##       http://www.apache.org/licenses/LICENSE-2.0
##
##   Unless required by applicable law or agreed to in writing, software
##   distributed under the License is distributed on an "AS IS" BASIS,
##   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##   See the License for the specific language governing permissions and
##   limitations under the License.


import json
import sys
import argparse
from collections import namedtuple, OrderedDict

from .vocab import Activity

class InvalidActivity(Exception): pass

class UserError(Exception): pass
class InvalidInput(UserError): pass




# Dump command
# ============

def dump_cli(args):
    try:
        asobj = json.loads(args.asobj)
    except ValueError:
        raise InvalidInput(
            "Not valid json: %s" % args.asobj)

    activity = Activity.from_json(asobj)
    try:
        activity.validate()
    except InvalidActivity as error:
        raise InvalidInput(str(error))


def dump_setup_subparser(subparser):
    subparser.add_argument(
        "asobj",
        help="ActivityStreams object, as json")




# Build command
# ==============

def build_cli():
    pass


def build_setup_subparser(subparser):
    pass




# Testdriver command
# ==================

def testdriver_cli():
    pass


def testdriver_setup_subparser(subparser):
    pass




# Build CLI
# =========

Command = namedtuple("command", ["cli_proc", "setup_subparser"])

SUBCOMMANDS_MAP = OrderedDict([
    ("dump", Command(
        dump_cli, dump_setup_subparser)),
    ("build", Command(
        build_cli, build_setup_subparser)),
    ("testdriver", Command(
        testdriver_cli, testdriver_setup_subparser))])


def main():
    parser = argparse.ArgumentParser(
        # @@: this sucks as a description
        description="Test for activitystreams correctness")

    subparsers = parser.add_subparsers(dest="subparser_name")

    for subcommand_key, subcommand_cmd in SUBCOMMANDS_MAP.items():
        subcmd_parser = subparsers.add_parser(subcommand_key)
        subcommand_cmd.setup_subparser(subcmd_parser)

    args = parser.parse_args()
    if not args.subparser_name:
        parser.print_help()
        sys.exit(1)

    try:
        subcmd_proc = SUBCOMMANDS_MAP[args.subparser_name].cli_proc
        subcmd_proc(args)
    except UserError as error:
        print(error)
        # it's only half evil that the user made this mistake,
        # is my guess
        sys.exit(333)


if __name__ == "__main__":
    main()