This repository has been archived by the owner on Dec 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawootp.py
executable file
·114 lines (94 loc) · 2.4 KB
/
awootp.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
#!/usr/bin/env python3
from sys import argv
from time import strftime
from os import path
nl = None
hl = None
variables = {}
line = 1
class Line:
def __init__(self, value):
self.value = value
self.next = None
def print(self):
if self.value == "":
print ("")
else:
A = eval_line(self.value)
if A:
print (A)
if self.next != None:
self.next.print()
def unknown_var(var_name):
global line
return '[[[' + str(line) + ': UNKNOWN VARIABLE: "' + var_name +'"]]]'
def make_env():
global variables
variables["DATE"] = strftime("%d-%b %Y")
variables["SPACE"] = " "
variables["NEWLINE"] = "\n"
variables["COLON"] = ":"
def eval_line(line):
if line == "":
return "";
if line[0] == ":":
return process_command(line[1:])
else:
return line
def process_command(full_command):
global variables
global hl
global nl
if full_command == "" or full_command[0] == "#":
return None
parts = full_command.split()
command = parts[0]
if len(parts) > 1:
args = ' '.join(parts[1:])
if command == "APPEND":
process_line(eval_line(args))
return None
elif command == "VAR":
var_name = eval_line(args).upper()
return variables.get(var_name, unknown_var(var_name))
elif command == "SET":
variables[parts[1].upper()] = eval_line(' '.join(parts[2:]))
return None
elif command == "INCLUDE":
no = nl
ho = hl
nl = None
hl = None
process_file(eval_line(args))
print_file()
nl = no
hl = ho
return None
elif command == "JOIN":
result = []
for var_request in parts[1:]:
result.append(variables.get(var_request, unknown_var(var_request)))
return ''.join(result)
else:
return "UNKNOWN COMMAND"
def process_line(line):
global nl, hl
newl = Line(line)
if nl != None:
nl.next = newl
else:
hl = newl
nl = newl
def process_file(file_name):
with open(path.expanduser(file_name)) as f:
for line in f:
process_line(line.rstrip("\n"))
def print_file():
hl.print()
if len(argv) > 1:
make_env()
process_file("~/AwooTP/default.awoodoc")
for f in argv[1:]:
process_file(f)
line+=1
print_file()