-
Notifications
You must be signed in to change notification settings - Fork 0
/
etl.py
100 lines (84 loc) · 3.17 KB
/
etl.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
import os;
STAY = "STAY";
INNER = "INNER";
NEXT = "NEXT";
DONE = "DONE";
DONE_AND_CONSUME = "DONE_AND_CONSUME";
def processSection(state, text):
#print "Section: " + text;
if (text == '_______________________________________________________________________________\n'):
return (DONE, [], state)
return (STAY, [], state);
def processMonsterListing(state, text):
print "Walkthrough Monsters: " + text
if (text == '_________________________________________________\n'):
return (DONE_AND_CONSUME, [], state)
return (STAY, [], state)
def processWalkthroughSectionTitle(state, text):
print "Walkthrough SectionTitle: " + text;
if (text == '_________________________________________________\n'):
return (DONE_AND_CONSUME, processWalkthroughSection, state)
stripped = text.strip();
if (stripped.startswith('[')):
state['walkthrough_subsection'] = stripped
print stripped
elif (stripped == 'Pokemon Type Version Rate Level'):
return (NEXT, processMonsterListing, state);
return (STAY, [], state);
def processTypeShorthand(state, text):
#print "Type Shorthand: " + text;
if (text == '_________________________________________________\n'):
return (DONE, [], state);
t = text.split();
state['types'][t[0]] = t[1];
return (STAY, [], state);
def processWalkthroughSection(state, text):
print "Walkthrough: " + text;
if (text == '_________________________________________________\n'):
return (INNER, processWalkthroughSectionTitle, state)
if (text == '--------------\n' and state['previous'] == 'Type Shorthand\n'):
return (INNER, processTypeShorthand, state)
return (STAY, [], state);
def processSectionTitle(state, text):
#print "Title: " + text;
if (text == '_______________________________________________________________________________\n'):
if (state['title'] == "Table of Contents"):
return (NEXT, processSection, state)
elif (state['title'] == "I. The Walkthrough"):
return (NEXT, processWalkthroughSection, state)
elif (len(text.strip()) > 0):
state['title'] = text.strip();
return (STAY, [], state);
def baseProcessor(state, text):
#print "Base: " + text;
if (text == '_______________________________________________________________________________\n'):
return (INNER, processSectionTitle, state)
return (STAY, [], state);
def processZerokidFaq():
fileName = "/home/local/ANT/rafkindd/Downloads/Pokemen_Red_Faq_by_zerokid.txt";
processorStack = [];
processorStack.append(baseProcessor);
transition = "";
newProc = [];
state = {};
state['types'] = {};
state['areas'] = {};
with open(fileName, "r") as inFile:
for line in inFile:
iterations = 1;
while (iterations > 0):
currentProcessor = processorStack[len(processorStack)-1]
(transition, newProc, state) = currentProcessor(state, line);
iterations -= 1;
if (transition == INNER):
processorStack.append(newProc);
elif (transition == NEXT):
processorStack[len(processorStack)-1] = newProc;
elif (transition == DONE):
currentProcessor = processorStack.pop();
iterations += 1;
elif (transition == DONE_AND_CONSUME):
currentProcessor = processorStack.pop();
state['previous'] = line
if __name__ == "__main__":
processZerokidFaq();