22 if not os.path.isfile(filename):
23 print(
"Error: No script to support action: ", condition[0],
"Expected: ", filename)
26 actf =
open(filename,
"r")
28 for actline
in actf.readlines():
30 if actline.find(
"## DIALOGCHECK") != -1:
32 elif checkstatus == 1:
33 if actline.find(
"## ENDDIALOGCHECK") != -1:
35 elif actline.find(
"## MINARGS") != -1:
36 num = actline.split()[2]
38 print(
"ERROR: Action definition for script ", filename,
" MINARGS not defined")
41 if len(args)<
int(num):
42 print(
"ERROR: Insufficiant options passed to script ", filename,
"expected ",
int(num),
" recieved ", len(args))
44 elif actline.find(
"## MAXARGS") != -1:
45 num = actline.split()[2]
47 print(
"ERROR: Action definition for script ", filename,
" MAXARGS not defined")
53 if len(args)>
int(num):
54 print(
"ERROR: Too many options passed to script ", filename,
"expected ",
int(num),
" recieved ", len(args))
56 elif actline.find(
"##") != 1:
58 if argnum < len(args):
59 argmatch = re.compile(actline.split()[1])
60 if not argmatch.match(args[argnum]):
61 print(
"ERROR: Argument ", argnum+2,
"of rule: ", condition,
" doesn't match regexp ", actline.split()[1])
65 print(
"Warning: No dialogcheck block for file ", filename,
" Unable to check condition ", condition)
77 f =
open(msgfile,
"rb")
79 print(
"ERROR: Can't open file, ", msgfile)
83 params = cjson.decode(f.read())
85 print(
"ERROR: Failed to parse file, ", msgfile,
"not a valid json file")
88 if "location" in params:
89 if not location ==
'':
90 print(
"Warning: Location defined multiple times in included files")
92 location = params[
"location"]
94 print(
"Warning: no location was specified")
97 for jsonRule
in params[
"rules"]:
104 for action
in jsonRule:
107 for condition
in jsonRule[
"pre"]:
108 action = condition[0]
109 path = os.path.join(
"pre/", action +
".py")
111 print(
"ERROR: verification of action file ", path,
" failed for rule ", rulenumber,
" condition ", condition)
114 elif action ==
"msg":
115 for line
in jsonRule[
"msg"]:
116 if len(line) > MAX_MSG_LENGTH:
119 print(
"WARNING: A Dialog Line for rule", rulenumber,
"is too long. (", len(line),
"characters, maximum is", MAX_MSG_LENGTH,
") \nLine begins:", line[:70])
122 elif action ==
"post":
124 for condition
in jsonRule[
"post"]:
125 action = condition[0]
126 path = os.path.join(
"post/", action +
".py")
128 print(
"ERROR: verification of action file ", path,
" failed for rule ", rulenumber,
" condition ", condition)
131 elif action ==
"match":
133 elif action ==
"replies":
135 elif action ==
"comment":
137 elif action ==
"include":
139 for condition
in jsonRule[
"include"]:
140 if condition[0] ==
"/":
141 inclname = os.path.join(
"../..", condition[1:])
143 inclname = os.path.join(os.path.dirname(msgfile), condition)
144 extrafiles.append(inclname)
146 print(
"Warning: Ignoring unknown rule:", action)
148 if (include == 1
and msg+post+match == 0)
or (msg == 1
and post == 1
and match ==1
and pre == 1):
151 print(
"ERROR: Rule created with an invalid combination of actions, actions are: ",
list(jsonRule.keys()))
157 if len(extrafiles) > 0:
158 for extrapath
in extrafiles:
159 newfiles, newrules, newwarnings, newerrors =
checkdialoguefile(extrapath, location)
160 print(
"checked ", newrules,
"rules from file", extrapath,
"Found ", newerrors,
" errors and ", newwarnings,
"warnings")
161 warnings +=newwarnings
165 return (1+newfiles, rulenumber, warnings, errors)
167 if len(sys.argv) < 2:
168 print(
"usage: python dialog_check.py path/to/dialogfile.msg")
170 for arg
in sys.argv[1:]:
172 print(
"checked ", rulecount,
"rules from file", arg,
"Found ", newerrors,
" errors and ", newwarnings,
"warnings")