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 errno
|
||||||
import os
|
import os
|
||||||
|
import threading
|
||||||
|
|
||||||
import chardet
|
import chardet
|
||||||
from docutils import frontend
|
from docutils import frontend
|
||||||
|
@ -36,7 +37,9 @@ class ParsedFile(object):
|
||||||
self._doc = None
|
self._doc = None
|
||||||
self._errors = None
|
self._errors = None
|
||||||
self._lines = None
|
self._lines = None
|
||||||
|
self._has_read = False
|
||||||
self._extension = os.path.splitext(filename)[1]
|
self._extension = os.path.splitext(filename)[1]
|
||||||
|
self._read_lock = threading.Lock()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def errors(self):
|
def errors(self):
|
||||||
|
@ -72,10 +75,19 @@ class ParsedFile(object):
|
||||||
self._doc = doc
|
self._doc = doc
|
||||||
return self._doc
|
return self._doc
|
||||||
|
|
||||||
|
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):
|
def lines_iter(self, remove_trailing_newline=True):
|
||||||
if self._lines is None:
|
self._read()
|
||||||
with open(self.filename, 'rb') as fh:
|
|
||||||
self._lines = list(fh)
|
|
||||||
for line in self._lines:
|
for line in self._lines:
|
||||||
line = six.text_type(line, encoding=self.encoding)
|
line = six.text_type(line, encoding=self.encoding)
|
||||||
if remove_trailing_newline and line.endswith("\n"):
|
if remove_trailing_newline and line.endswith("\n"):
|
||||||
|
@ -101,9 +113,7 @@ class ParsedFile(object):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def raw_contents(self):
|
def raw_contents(self):
|
||||||
if self._raw_content is None:
|
self._read()
|
||||||
with open(self.filename, 'rb') as fh:
|
|
||||||
self._raw_content = fh.read()
|
|
||||||
return self._raw_content
|
return self._raw_content
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
Loading…
Reference in New Issue