forked from Netflix/hubcommander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_functions.py
160 lines (121 loc) · 5.22 KB
/
parse_functions.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
.. module: hubcommander.bot_components.parse_functions
:platform: Unix
:copyright: (c) 2017 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
.. moduleauthor:: Mike Grima <mgrima@netflix.com>
"""
import warnings
TOGGLE_ON_VALUES = ["on", "true", "enable", "enabled"]
TOGGLE_OFF_VALUES = ["off", "false", "disable", "disabled"]
class ParseException(Exception):
"""
Exception specific to parsing logic, where improper data was fed in.
"""
def __init__(self, arg_type, proper_values):
super(ParseException, self).__init__("Parse Error")
self.arg_type = arg_type
self.proper_values = proper_values
def format_proper_usage(self, user):
usage_text = "@{user}: Invalid argument passed for `{arg_type}`.\n\n" \
"{proper_values}"
return usage_text.format(user=user, arg_type=self.arg_type, proper_values=self.proper_values)
def preformat_args(text):
"""
THIS METHOD IS DEPRECATED! USE THE DECORATORS FOR PARSING!!
:param text:
:return:
"""
warnings.simplefilter('always', DeprecationWarning)
warnings.warn("The function: 'preformat_args' is deprecated. Please use the decorators for "
"argument parsing.", DeprecationWarning)
return text.lower() \
.replace('[', '').replace(']', '') \
.replace('{', '').replace('}', '') \
.split(" ")[1:]
def preformat_args_with_spaces(text, num_quoted):
"""
THIS METHOD IS DEPRECATED! USE THE DECORATORS FOR PARSING!!
This method will not only strip out the things that need to be stripped out, but it will also
ensure that double-quoted objects are extracted as independent arguments.
For example, if the text passed in was:
!SetDescription Netflix HubCommander "A Slack bot for GitHub management", we would want to get back:
a list that contains: ["netflix", "hubcommander", '"A Slack bot for GitHub management"']
The `num_quoted` param refers to the number of double-quoted parameters are required for the command.
For the example above, there is one double-quoted parameter required for the command.
:param text:
:param num_quoted:
:return:
"""
warnings.simplefilter('always', DeprecationWarning)
warnings.warn("The function: 'preformat_args_with_spaces' is deprecated. Please use the decorators for "
"argument parsing.", DeprecationWarning)
working = text.replace('[', '').replace(']', '') \
.replace('{', '').replace('}', '') \
.replace(u'\u201C', "\"").replace(u'\u201D', "\"") \
.replace(u'\u2018', "\'").replace(u'\u2019', "\'") # macOS's Bullshit around quotes..
# Check if there are 0 or an un-even number of quotation marks. If so, then exit:
if working.count('\"') < 2:
raise SystemExit()
if working.count('\"') % 2 != 0:
raise SystemExit()
# Get all the quoted things: (We only really care about the double-quotes, since they are related to command
# syntax.)
quotes = working.split('"')[1::2]
if len(quotes) != num_quoted:
raise SystemExit()
# Remove them from the working string:
working = working.replace("\"", '')
for quote in quotes:
working = working.replace(quote, '')
# Get the space delimited commands:
working = working.lower()
# Remove extra dangling whitespaces (there must be 1 dangling space at the end for the split operation to operate):
if num_quoted > 1:
working = working[0:-(num_quoted - 1)]
space_delimited = working.split(" ")[1:-1]
# The -1 above is needed, because there will be additional empty items on the list due to the space
# after the other positional arguments :/
return space_delimited + quotes
def extract_repo_name(plugin_obj, reponame, **kwargs):
"""
Reponames can be FQDN's. Slack has an annoying habit of sending over URL's like so:
<http://www.foo.com|www.foo.com>
^^ Need to pull out the URL. In our case, we care only about the label, which is the last part between | and >
:param plugin_obj: Not used
:param reponame:
:return:
"""
if "|" not in reponame:
return reponame
split_repo = reponame.split("|")[1]
return split_repo.replace(">", "")
def extract_multiple_repo_names(plugin_obj, repos, **kwargs):
"""
Does what the above does, but does it for a comma separated list of repos.
:param plugin_obj:
:param repos:
:param kwargs:
:return:
"""
repo_list = repos.split(",")
parsed_repos = []
for repo in repo_list:
parsed_repos.append(extract_repo_name(plugin_obj, repo, **kwargs))
return parsed_repos
def parse_toggles(plugin_obj, toggle, toggle_type="toggle", **kwargs):
"""
Parses typical toggle values, like off, on, enabled, disabled, true, false, etc.
:param plugin_obj: Not used
:param toggle_type:
:param toggle:
:return:
"""
toggle = toggle.lower()
if toggle in TOGGLE_ON_VALUES:
return True
elif toggle in TOGGLE_OFF_VALUES:
return False
raise ParseException(toggle_type, "Acceptable values are: `{on}, {off}`".format(
on=", ".join(TOGGLE_ON_VALUES), off=", ".join(TOGGLE_OFF_VALUES)
))