-
Notifications
You must be signed in to change notification settings - Fork 0
/
automatedautomod.py
65 lines (55 loc) · 1.97 KB
/
automatedautomod.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
from datetime import datetime
def automod_text_from_file(file_name):
with open(file_name) as file:
return file.read()
def rule_list_from_line(line):
rule_list = line.split()
rule_list.remove("rule-start")
rule_list.remove("#")
return rule_list
def utc_weekday_num():
return datetime.utcnow().weekday()
def utc_weekday_str():
return datetime.utcnow().strftime('%A')
def day_rule_text_from_weekday_num(day_num):
day_rule_names = ["mon-off", "tue-off", "wed-off", "thu-off", "fri-off", "sat-off", "sun-off"]
return day_rule_names[day_num]
def disable_rule(rule, automod_lines):
i = rule["start_line"] - 1
while i < rule["end_line"]:
if not automod_lines[i].strip().startswith("#"):
automod_lines[i] = "#" + automod_lines[i]
i = i + 1
return automod_lines
def enable_rule(rule, automod_lines):
i = rule["start_line"] - 1
while i < rule["end_line"]:
if automod_lines[i].strip().startswith("#"):
automod_lines[i] = automod_lines[i].strip("#")
i = i + 1
return automod_lines
def create_rule_blocks(lines):
rule_blocks = []
i = 0
start_line = 0
end_line = 0
rule_list = []
for line in lines:
if "rule-start" in line:
start_line = i
rule_list = rule_list_from_line(line)
if "rule-end" in line:
end_line = i
rule_blocks.append({"rule_list": rule_list, "start_line": start_line + 2, "end_line": end_line})
i = i + 1
return rule_blocks
def apply_rules_to_automod_lines(rule_blocks, automod_lines):
for rule in rule_blocks:
if day_rule_text_from_weekday_num(utc_weekday_num()) in rule["rule_list"]:
automod_lines = disable_rule(rule, automod_lines)
else:
automod_lines = enable_rule(rule, automod_lines)
return automod_lines
def write_automod_to_file(automod, file_name):
with open(file_name, "w") as file:
file.write(automod)