aboutsummaryrefslogtreecommitdiff
path: root/textoutpc/_stream.py
blob: 39867751556199a8cab145ed4c6c5609d90b9681 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
""" Layer on top of the character stream.

	See the `TextoutStream` class description for more information.
"""

import io as _io
import regex as _re

__all__ = ["TextoutStream", "TextoutUnit"]

# ---
# Class definitions.
# ---

class TextoutUnit:
	""" Raw textout stream unit. """

	BEGIN   = 1
	END     = 2
	SPECIAL = 3
	NEWLINE = 4
	PARSEP  = 5

	def __init__(self, *args):
		if len(args) > 1:
			self.type, self.name, self.value, *_ = args + (None,)
			return

		result = args[0]
		gr = result.groupdict()

		self.name = None
		self.value = None

		if   gr['sname'] == '\n':
			self.type = self.NEWLINE
		elif gr['parsep'] != None:
			self.type = self.PARSEP
		elif gr['bname'] != None:
			self.type = self.BEGIN
			self.name = gr['bname']
			self.value = gr['value']

			self.full = "[{}{}]".format(self.name,
				"=" + self.value if self.value != None else "")
		elif gr['ename'] != None:
			self.type = self.END
			self.name = gr['ename']
			self.full = "[/" + self.name + "]"
		else:
			self.type = self.SPECIAL
			self.name = gr['sname']

			self.full = self.name

		if self.name != None:
			self.name = self.name.lower()
			if self.type != self.SPECIAL:
				self.name = "[{}]".format(self.name)

	def __repr__(self):
		typetab = {self.BEGIN: "begin", self.END: "end",
			self.SPECIAL: "special", self.NEWLINE: "newline"}
		return '_TextoutUnit(type={}{}{})'.format(\
			typetab[self.type],
			', name=' + repr(self.name) if self.name != None else "",
			', value=' + repr(self.value) if self.value != None else "")

	def __equ__(self, other):
		if not isinstance(other, TextoutUnit):
			return False
		if self.type == other.type \
		and (self.type == self.NEWLINE or self.name == other.name) \
		and (self.type != self.BEGIN or self.value == other.value):
			return False
		return True

class TextoutStream:
	""" Textout stream, for easier stream processing.

		The idea behind this stream is that it will provide more suitable
		(therefore easier to process) data for the applications above,
		with raw text and tags. """

	# A tag can basically be one of the following things:
	# - a starting tag, looking like [<name>] or [<name>=<attribute>]
	# - an ending tag, looking like [/<name>]
	# - a special tag (starting or ending), usually one-char (the only
	#   one currently available is the ` tag).
	#
	# A tag name is 32 chars at most (at least 1 char).
	# A closing tag can have no name, which means that it will close the
	# last opened tag automatically.
	# A tag attribute is 256 chars at most.
	#
	# FIXME: check the sizes? it seems that it stopped working…

	_Tag = _re.compile("""\
		\[\s?
		(?P<bname>
			(?P<bname_e>[^\/\[\]\=][^\[\]\=]* (\[(?&bname_e)\]?)*)*
		)
		(\s?=\s?(?P<value>
			(?P<value_e>[^\[\]]* (\[(?&value_e)\]?)*)*
		))?
		\s?\]
	|
		\[[\\\/]\s?(?P<ename>
			(?P<ename_e>[^\/\[\]\=][^\[\]\=]* (\[(?&ename_e)\]?)*)*
		)\s?\]
	|
		(?P<parsep>[\n]{2,})
	|
		(?P<sname>`|[\n])
	""", _re.VERBOSE | _re.DOTALL | _re.MULTILINE)

	# Keep this buffer size above the maximum size of a tag (387)
	# for this class to work alright. Anything above 512 should work great.

	BUFFER_SIZE = 1024

	def __init__(self, stream):
		# If the 'stream' is a string, we want to use standard stream
		# functions, so we're gonna enforce them using the `StringIO` class.

		if isinstance(stream, str):
			stream = _io.StringIO(stream)

		# Buffer management.

		self.stream = stream
		self.buf = ""

		# Management of the last tag match.

		self.result = None
		self.last = None

		# Error position.

		self.pos = 0
		self.line = 0
		self.col = 0

	def __iter__(self):
		# This class is (obviously) iterable.
		# We want to use this class as the iterator as well.

		return self

	def __next__(self):
		# If we have a result, process it.

		if self.result:
			data, self.result = TextoutUnit(self.result), None
			self.last = data
			return data

		# Make sure to have enough data to read.

		self.buf += self.stream.read(self.BUFFER_SIZE - len(self.buf))
		if not self.buf:
			self.last = None
			raise StopIteration

		# Check that we have a result.

		result = self._Tag.search(self.buf, partial = True)
		if not result:
			text = self.buf
			self.buf = ''
			self.last = text
			return text

		# If there is some text, return it.
		# Eventually store the result so we can process it later.

		if result.start() > 0:
			ret = self.buf[:result.start()]
			self.buf = self.buf[result.end():]
			if not result.partial:
				self.result = result
			self.last = ret
			return ret

		# Process the result now!

		self.buf = self.buf[result.end():]
		data = TextoutUnit(result)
		self.last = data
		return data

# End of file.