summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-09-15 00:07:37 +0000
committerGerrit Code Review <review@openstack.org>2014-09-15 00:07:37 +0000
commit3f7c58cf5a3dc2c433c1b283661c27227b323338 (patch)
tree27d4ffb12b1de3ab0ad108da5db13ab29c16c1cb
parent6baf0132034c877c3e2218c0597245de0f77b4c8 (diff)
parentbeed9aa8b8630d8685c85cf3bd665d4a68340168 (diff)
Merge "Ensure reading is done in one place only"
-rw-r--r--doc8/parser.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/doc8/parser.py b/doc8/parser.py
index 7a88520..16d0304 100644
--- a/doc8/parser.py
+++ b/doc8/parser.py
@@ -16,6 +16,7 @@
16 16
17import errno 17import errno
18import os 18import os
19import threading
19 20
20import chardet 21import chardet
21from docutils import frontend 22from docutils import frontend
@@ -36,7 +37,9 @@ class ParsedFile(object):
36 self._doc = None 37 self._doc = None
37 self._errors = None 38 self._errors = None
38 self._lines = None 39 self._lines = None
40 self._has_read = False
39 self._extension = os.path.splitext(filename)[1] 41 self._extension = os.path.splitext(filename)[1]
42 self._read_lock = threading.Lock()
40 43
41 @property 44 @property
42 def errors(self): 45 def errors(self):
@@ -72,10 +75,19 @@ class ParsedFile(object):
72 self._doc = doc 75 self._doc = doc
73 return self._doc 76 return self._doc
74 77
78 def _read(self):
79 if self._has_read:
80 return
81 with self._read_lock:
82 if not self._has_read:
83 with open(self.filename, 'rb') as fh:
84 self._lines = list(fh)
85 fh.seek(0)
86 self._raw_content = fh.read()
87 self._has_read = True
88
75 def lines_iter(self, remove_trailing_newline=True): 89 def lines_iter(self, remove_trailing_newline=True):
76 if self._lines is None: 90 self._read()
77 with open(self.filename, 'rb') as fh:
78 self._lines = list(fh)
79 for line in self._lines: 91 for line in self._lines:
80 line = six.text_type(line, encoding=self.encoding) 92 line = six.text_type(line, encoding=self.encoding)
81 if remove_trailing_newline and line.endswith("\n"): 93 if remove_trailing_newline and line.endswith("\n"):
@@ -101,9 +113,7 @@ class ParsedFile(object):
101 113
102 @property 114 @property
103 def raw_contents(self): 115 def raw_contents(self):
104 if self._raw_content is None: 116 self._read()
105 with open(self.filename, 'rb') as fh:
106 self._raw_content = fh.read()
107 return self._raw_content 117 return self._raw_content
108 118
109 @property 119 @property