-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.py
More file actions
130 lines (102 loc) · 3.46 KB
/
Copy pathreader.py
File metadata and controls
130 lines (102 loc) · 3.46 KB
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
#
# Neodym
#
# Copyright (C) by Andreas Zoglauer and contributors
# Please see the license file for more details.
#
# -----------------------------------------------------------------------------------
# Import external files
import os
# Import neodym files
# -----------------------------------------------------------------------------------
# This class reads the file line-by-line and stores it in mFileContent.
# if finds the configuration and reads it into the dictionary
class ZReader:
def __init__(self):
# The file name without path for href's in the webpage
self.mFileName = ""
# The path to the file
self.mFilePath = ""
# The complete file content - line by line
self.mFileContent = []
# The main content without the configurations
self.mMainContent = ""
# The dictionary content
self.mDictionary = {}
# Read the file
def read(self, FileName):
self.mFileName = os.path.basename(FileName)
self.mFilePath = os.path.dirname(FileName)
print(self.mFilePath)
print(FileName)
with open(FileName) as File:
self.mFileContent = File.readlines()
self.mFileContent = [X.strip() for X in self.mFileContent]
InConfig = False
for Line in self.mFileContent:
if "<!-- neodym-config" in Line:
InConfig = True
continue
if InConfig == True and "-->" in Line:
InConfig = False
continue
if InConfig == True:
if Line.find(":") != -1:
Split = Line.split(":", maxsplit=2)
if len(Split) == 2 and Split[0].strip() != "" and Split[1].strip() != "":
self.mDictionary[Split[0].strip()] = Split[1].strip()
print("Added to dictionary: " + Split[0].strip() + ", " + Split[1].strip())
else:
self.mMainContent += Line
self.mMainContent += "\n"
return True
# Copy data into this reader
def assimilate(self, Reader):
self.mFileName = Reader.mFileName
self.mFilePath = Reader.mFilePath
self.mFileContent = Reader.mFileContent
self.mMainContent = Reader.mMainContent
self.mDictionary = Reader.mDictionary
# Check if the configuration has a key
def has(self, Key, Value):
if Key in self.mDictionary:
return self.mDictionary[Key] == Value
else:
return False
# Extract a single Tag from the file content
def extractSingleTag(self, Name):
print("Extract")
Start = "<" + Name + ">"
End = "</" + Name + ">"
Found = False
InTag = False
Tag = ""
# Extract all the lines from where the tag appears, the first time including the line where it ends
for Line in self.mFileContent:
if Found == True:
if InTag == True:
Tag += Line
Tag += "\n"
if End in Line:
InTag = False
else:
if Start in Line:
print("ERROR: Tag appears multiple times")
return ""
else:
if Start in Line:
Found = True
InTag = True
Tag += Line
Tag += "\n"
if InTag == True:
print("ERROR: Tag not closed")
return ""
if Found == False:
print("Nothing found")
return ""
# Remove everything to the Start and everything after the End
StartIndex = Tag.index(Start) + len(Start)
EndIndex = Tag.index(End, StartIndex)
return Tag[StartIndex:EndIndex]
# -----------------------------------------------------------------------------------