ADCT Python
Python client API implementation for publishing ADCT data
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
ldms_blob_file_to_http_post.Readline0 Class Reference
Inheritance diagram for ldms_blob_file_to_http_post.Readline0:
Inheritance graph
[legend]
Collaboration diagram for ldms_blob_file_to_http_post.Readline0:
Collaboration graph
[legend]

Public Member Functions

None __init__ (self, typing.Union[typing.BinaryIO, int] file_, bytes separator, int blocksize)
 
typing.Tuple[typing.List[bytes], bool, bytes] handle_field_pairs (cls, typing.List[typing.Tuple[bytes, bytes]] field_pairs)
 
None get_fields (self)
 
typing.Iterator[bytes] sequence (self)
 

Public Attributes

 file_
 
 blocksize
 
 have_fraction
 
 fraction
 
 separator
 
 yieldno
 
 bang
 
 metapattern
 
 buffer_
 
 pattern
 
 at_eof
 
 fields
 

Detailed Description

Yield a series of blocks, separated by separator.

Constructor & Destructor Documentation

◆ __init__()

None ldms_blob_file_to_http_post.Readline0.__init__ (   self,
typing.Union[typing.BinaryIO, int]  file_,
bytes  separator,
int  blocksize 
)
Initialize.
92 def __init__(self, file_: typing.Union[typing.BinaryIO, int], separator: bytes, blocksize: int) -> None:
93 """Initialize."""
94 self.file_ = file_
95 self.blocksize = blocksize
96
97 self.have_fraction = False
98 self.fraction = b''
99
100 self.separator = separator
101
102 self.fields: typing.List[bytes] = []
103
104 self.yieldno = 0
105
106 self.bang = b'!'
107 self.metapattern = b'([^!]*)!|([^!]+)$'
108 self.buffer_ = b''
109 self.separator = separator
110
111 # bytes objects have a split method, but it doesn't work, at least not in Python 3.1.2. But the re module
112 # works with bytes, so we use that.
113
114 self.pattern = re.sub(self.bang, self.separator, self.metapattern)
115
116 self.at_eof = False
117

Member Function Documentation

◆ get_fields()

None ldms_blob_file_to_http_post.Readline0.get_fields (   self)
Read a block, chop it up into fields - taking into account any leftover partial field.
149 def get_fields(self) -> None:
150 """Read a block, chop it up into fields - taking into account any leftover partial field."""
151 if isinstance(self.file_, int):
152 tail_block: bytes = os.read(self.file_, self.blocksize)
153 else:
154 # assume we have a file-like object
155 tail_block = self.file_.read(self.blocksize)
156
157 if tail_block:
158 self.at_eof = False
159 else:
160 self.at_eof = True
161
162 if self.have_fraction:
163 block = self.fraction + tail_block
164 self.fraction = b''
165 self.have_fraction = False
166 else:
167 block = tail_block
168
169 field_pairs = re.findall(self.pattern, block)
170 regular_fields, self.have_fraction, self.fraction = self.handle_field_pairs(field_pairs)
171
172 # we put the fields in reverse order so we can repeatedly pop efficiently
173 regular_fields.reverse()
174
175 self.fields = regular_fields
176

◆ handle_field_pairs()

typing.Tuple[typing.List[bytes], bool, bytes] ldms_blob_file_to_http_post.Readline0.handle_field_pairs (   cls,
typing.List[typing.Tuple[bytes, bytes]]  field_pairs 
)
Pick apart the pairs from our regex split and return the correct values.
122 ) -> typing.Tuple[typing.List[bytes], bool, bytes]:
123 """Pick apart the pairs from our regex split and return the correct values."""
124 regular_fields = []
125 have_fraction = False
126 fraction = b''
127
128 for field_pair in field_pairs:
129 if field_pair[0]:
130 if field_pair[1]:
131 # They're both not zero length - that's an error
132 raise AssertionError('Both field_pair[0] and field_pair[1] are non-empty')
133 else:
134 # The first is not zero length, the second is zero length
135 regular_fields.append(field_pair[0])
136 else:
137 if field_pair[1]:
138 # the first is zero length, the second is not zero length
139 if have_fraction:
140 raise AssertionError('Already have a fraction')
141 fraction = field_pair[1]
142 have_fraction = True
143 else:
144 # they're both zero length - this is legal for !! - yield one or the other but not both
145 regular_fields.append(field_pair[0])
146
147 return regular_fields, have_fraction, fraction
148

◆ sequence()

typing.Iterator[bytes] ldms_blob_file_to_http_post.Readline0.sequence (   self)
Generate each field (line) in turn.
177 def sequence(self) -> typing.Iterator[bytes]:
178 """Generate each field (line) in turn."""
179 while True:
180 if not self.fields:
181 self.get_fields()
182 while self.fields:
183 yield self.fields.pop()
184 if self.at_eof:
185 if self.have_fraction:
186 yield self.fraction
187 break
188

Member Data Documentation

◆ at_eof

ldms_blob_file_to_http_post.Readline0.at_eof

◆ bang

ldms_blob_file_to_http_post.Readline0.bang

◆ blocksize

ldms_blob_file_to_http_post.Readline0.blocksize

◆ buffer_

ldms_blob_file_to_http_post.Readline0.buffer_

◆ fields

ldms_blob_file_to_http_post.Readline0.fields

◆ file_

ldms_blob_file_to_http_post.Readline0.file_

◆ fraction

ldms_blob_file_to_http_post.Readline0.fraction

◆ have_fraction

ldms_blob_file_to_http_post.Readline0.have_fraction

◆ metapattern

ldms_blob_file_to_http_post.Readline0.metapattern

◆ pattern

ldms_blob_file_to_http_post.Readline0.pattern

◆ separator

ldms_blob_file_to_http_post.Readline0.separator

◆ yieldno

ldms_blob_file_to_http_post.Readline0.yieldno

The documentation for this class was generated from the following file: