Browse Source

Merge "Allow overriding file encoding"

Jenkins 4 years ago
parent
commit
00a13a3239
3 changed files with 35 additions and 1 deletions
  1. 5
    0
      README.rst
  2. 14
    1
      doc8/main.py
  3. 16
    0
      doc8/tests/test_checks.py

+ 5
- 0
README.rst View File

@@ -60,6 +60,10 @@ Command line usage
60 60
       --default-extension extension
61 61
                             Default file extension to use when a file is found
62 62
                             without a file extension.
63
+      --file-encoding encoding
64
+                            Override encoding to use when attempting to determine
65
+                            an input files text encoding (providing this avoids
66
+                            using `chardet` to automatically detect encoding/s)
63 67
       --max-line-length int
64 68
                             maximum allowed line length (default: 79)
65 69
       -e extension, --extension extension
@@ -111,6 +115,7 @@ Option                 Overrides    Merges
111 115
 ``ignore-path``        No           Yes
112 116
 ``ignore``             No           Yes
113 117
 ``max-line-length``    Yes          No
118
+``file-encoding``      Yes          No
114 119
 ``sphinx``             Yes          No
115 120
 =====================  ===========  ========
116 121
 

+ 14
- 1
doc8/main.py View File

@@ -110,6 +110,10 @@ def extract_config(args):
110 110
         cfg['verbose'] = parser.getboolean("doc8", "verbose")
111 111
     except (configparser.NoSectionError, configparser.NoOptionError):
112 112
         pass
113
+    try:
114
+        cfg['file_encoding'] = parser.get("doc8", "file-encoding")
115
+    except (configparser.NoSectionError, configparser.NoOptionError):
116
+        pass
113 117
     try:
114 118
         cfg['default_extension'] = parser.get("doc8", "default-extension")
115 119
     except (configparser.NoSectionError, configparser.NoOptionError):
@@ -162,6 +166,7 @@ def scan(cfg):
162 166
     file_iter = utils.find_files(cfg.get('paths', []),
163 167
                                  cfg.get('extension', []), ignored_paths)
164 168
     default_extension = cfg.get('default_extension')
169
+    file_encoding = cfg.get('file_encoding')
165 170
     for filename, ignoreable in file_iter:
166 171
         if ignoreable:
167 172
             files_ignored += 1
@@ -169,7 +174,8 @@ def scan(cfg):
169 174
                 print("  Ignoring '%s'" % (filename))
170 175
         else:
171 176
             f = file_parser.parse(filename,
172
-                                  default_extension=default_extension)
177
+                                  default_extension=default_extension,
178
+                                  encoding=file_encoding)
173 179
             files.append(f)
174 180
             if cfg.get('verbose'):
175 181
                 print("  Selecting '%s'" % (filename))
@@ -277,6 +283,13 @@ def main():
277 283
                              " found without a file extension.",
278 284
                         default='', dest='default_extension',
279 285
                         metavar='extension')
286
+    parser.add_argument("--file-encoding", action="store",
287
+                        help="Override encoding to use when attempting"
288
+                             " to determine an input files text encoding "
289
+                             "(providing this avoids using `chardet` to"
290
+                             " automatically detect encoding/s)",
291
+                        default='', dest='file_encoding',
292
+                        metavar='encoding')
280 293
     parser.add_argument("--max-line-length", action="store", metavar="int",
281 294
                         type=int,
282 295
                         help="Maximum allowed line"

+ 16
- 0
doc8/tests/test_checks.py View File

@@ -89,6 +89,22 @@ test
89 89
                 (line, code, msg) = errors[0]
90 90
                 self.assertIn(code, check.REPORTS)
91 91
 
92
+    def test_correct_length(self):
93
+        conf = {
94
+            'max_line_length': 79,
95
+            'allow_long_titles': True,
96
+        }
97
+        with tempfile.NamedTemporaryFile(suffix='.rst') as fh:
98
+            fh.write(b'known exploit in the wild, for example'
99
+                     ' \xe2\x80\x93 the time'
100
+                     ' between advance notification')
101
+            fh.flush()
102
+
103
+            parsed_file = parser.ParsedFile(fh.name, encoding='utf-8')
104
+            check = checks.CheckMaxLineLength(conf)
105
+            errors = list(check.report_iter(parsed_file))
106
+            self.assertEqual(0, len(errors))
107
+
92 108
     def test_unsplittable_length(self):
93 109
         content = """
94 110
 ===

Loading…
Cancel
Save