Ensure reading is done in one place only
Instead of having reading being split across the lines_iter method and the raw_content property just have it occur in a common function so that it can be done once and only once. Change-Id: I603bc4e85ad489d5397a78d6c987e15a0515f7ea
This commit is contained in:
parent
89779ed115
commit
beed9aa8b8
|
@ -16,6 +16,7 @@
|
|||
|
||||
import errno
|
||||
import os
|
||||
import threading
|
||||
|
||||
import chardet
|
||||
from docutils import frontend
|
||||
|
@ -36,7 +37,9 @@ class ParsedFile(object):
|
|||
self._doc = None
|
||||
self._errors = None
|
||||
self._lines = None
|
||||
self._has_read = False
|
||||
self._extension = os.path.splitext(filename)[1]
|
||||
self._read_lock = threading.Lock()
|
||||
|
||||
@property
|
||||
def errors(self):
|
||||
|
@ -72,10 +75,19 @@ class ParsedFile(object):
|
|||
self._doc = doc
|
||||
return self._doc
|
||||
|
||||
def lines_iter(self, remove_trailing_newline=True):
|
||||
if self._lines is None:
|
||||
def _read(self):
|
||||
if self._has_read:
|
||||
return
|
||||
with self._read_lock:
|
||||
if not self._has_read:
|
||||
with open(self.filename, 'rb') as fh:
|
||||
self._lines = list(fh)
|
||||
fh.seek(0)
|
||||
self._raw_content = fh.read()
|
||||
self._has_read = True
|
||||
|
||||
def lines_iter(self, remove_trailing_newline=True):
|
||||
self._read()
|
||||
for line in self._lines:
|
||||
line = six.text_type(line, encoding=self.encoding)
|
||||
if remove_trailing_newline and line.endswith("\n"):
|
||||
|
@ -101,9 +113,7 @@ class ParsedFile(object):
|
|||
|
||||
@property
|
||||
def raw_contents(self):
|
||||
if self._raw_content is None:
|
||||
with open(self.filename, 'rb') as fh:
|
||||
self._raw_content = fh.read()
|
||||
self._read()
|
||||
return self._raw_content
|
||||
|
||||
@property
|
||||
|
|
Loading…
Reference in New Issue