-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathoptionparser.py
121 lines (100 loc) · 3.65 KB
/
optionparser.py
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
# -*- coding: utf-8 -*-
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
""" Option parser
This exports:
- parse is a function that parses all options and
delegate them to those enhance parsers.
- OptionParser is a class, it's an abstract class
and defines the interfaces. All of option parsers need
inherit this class.
Option parser of pywb to improve or modify the action which
is bound to option.
"""
__all__ = [
"parse",
"OptionParser",
]
import os
import sys
import abc
import pywbutil
def parse(options, enhance_options):
""" Parse all options and delegate them to those enhance parsers
Arguments:
- options: a list, the command arguments of pywb
- enhance_options: a dict that key is option and
the value is option parser of processing arguments
"""
acceptable_wb_options = "n:c:t:s:b:T:p:u:v:lrkVhwiIx:"\
"y:z:C:H:P:A:g:X:de:SqB:m:Z:f:"\
"Y:a:o:F:j:J:O:R:D:U:Y:W:E:G:Q:"\
"K012:3456789"
# Anonymous_options are those options without prefix dash.
# They were not defined at acceptable_wb_option.
# e.g. destination hostname
anonymous_options = []
# Defined_options are those options begin with dash.
# They were defined at acceptable_wb_option.
# e.g. -c, -t...
defined_options = []
i = 0
while i < len(options):
option = options[i]
i += 1
if option in enhance_options: # enhance options
i += enhance_options[option].load(options[i:])
continue
if not option.startswith("-"): # single option
anonymous_options.append(option)
continue
position = acceptable_wb_options.find(option[1])
if len(option) != 2 or position == -1: # invalid option
raise ValueError("unsupported argument [" + option + "]")
if position < len(acceptable_wb_options)\
and acceptable_wb_options[position + 1] == ":":
# double option
defined_options.append(option)
if i >= len(options):
raise ValueError("option [" + option + "] need an argument")
option = options[i]
i += 1
defined_options.append(option)
continue
else: # single option
defined_options.insert(0, option)
continue
# combine all options
options = [pywbutil.get_wb_path()]
for _, trigger in enhance_options.items():
options += trigger.dump()
options += defined_options
options += anonymous_options
return options
class OptionParser(object):
""" OptionParser is an abstract class and defines the interfaces.
All of option parser need inherit this class.
"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def load(self, arguments):
""" load the arguments
Arguments:
- arguments: a list, the arguments what this action need
Return is a interger that means the number of this action need
"""
return 0
@abc.abstractmethod
def dump(self):
""" Dump the new options for wb
Return a list of string, the options that will be passed to wb
it's a parameters list. if the space-separated string is inserted
into the return list, it'll be as just one parameter to pass to wb
"""
return []
@abc.abstractmethod
def help(self):
""" Help document for this action
Return is a string of help document for option bound by this instance
"""
return " "