Browse Source

Allow a default_extension to be provided

When a file is found without an extension allow a CLI
provided default_extension to be used in these cases
so that files without extensions can be classified as
known file extension types.

Change-Id: I115ccc92ccd51a7018672b98b639ade30869f939
Joshua Harlow 4 years ago
parent
commit
3912dca016
3 changed files with 27 additions and 6 deletions
  1. 7
    2
      README.rst
  2. 13
    1
      doc8/main.py
  3. 7
    3
      doc8/parser.py

+ 7
- 2
README.rst View File

@@ -27,8 +27,8 @@ Command line usage
27 27
     $ doc8  -h
28 28
 
29 29
     usage: doc8 [-h] [--config path] [--allow-long-titles] [--ignore code]
30
-                [--no-sphinx] [--ignore-path path] [--max-line-length int]
31
-                [-e extension] [-v]
30
+                [--no-sphinx] [--ignore-path path] [--default-extension extension]
31
+                [--max-line-length int] [-e extension] [-v] [--version]
32 32
                 [path [path ...]]
33 33
 
34 34
     Check documentation for simple style requirements.
@@ -56,12 +56,16 @@ Command line usage
56 56
       --no-sphinx           do not ignore sphinx specific false positives
57 57
       --ignore-path path    ignore the given directory or file (globs are
58 58
                             supported)
59
+      --default-extension extension
60
+                            Default file extension to use when a file is found
61
+                            without a file extension.
59 62
       --max-line-length int
60 63
                             maximum allowed line length (default: 79)
61 64
       -e extension, --extension extension
62 65
                             check file extensions of the given type (default:
63 66
                             .rst, .txt)
64 67
       -v, --verbose         run in verbose mode
68
+      --version             Show the version and exit.
65 69
 
66 70
 Ini file usage
67 71
 **************
@@ -101,6 +105,7 @@ of conflicts.
101 105
 Option                 Overrides    Merges
102 106
 =====================  ===========  ========
103 107
 ``allow-long-titles``  Yes          No
108
+``default-extension``  Yes          No
104 109
 ``extension``          No           Yes
105 110
 ``ignore-path``        No           Yes
106 111
 ``ignore``             No           Yes

+ 13
- 1
doc8/main.py View File

@@ -109,6 +109,10 @@ def extract_config(args):
109 109
         cfg['verbose'] = parser.getboolean("doc8", "verbose")
110 110
     except (configparser.NoSectionError, configparser.NoOptionError):
111 111
         pass
112
+    try:
113
+        cfg['default_extension'] = parser.get("doc8", "default-extension")
114
+    except (configparser.NoSectionError, configparser.NoOptionError):
115
+        pass
112 116
     try:
113 117
         extensions = parser.get("doc8", "extensions")
114 118
         extensions = extensions.split(",")
@@ -155,13 +159,16 @@ def scan(cfg):
155 159
     files_ignored = 0
156 160
     file_iter = utils.find_files(cfg.get('paths', []),
157 161
                                  cfg.get('extension', []), ignored_paths)
162
+    default_extension = cfg.get('default_extension')
158 163
     for filename, ignoreable in file_iter:
159 164
         if ignoreable:
160 165
             files_ignored += 1
161 166
             if cfg.get('verbose'):
162 167
                 print("  Ignoring '%s'" % (filename))
163 168
         else:
164
-            files.append(file_parser.parse(filename))
169
+            f = file_parser.parse(filename,
170
+                                  default_extension=default_extension)
171
+            files.append(f)
165 172
             if cfg.get('verbose'):
166 173
                 print("  Selecting '%s'" % (filename))
167 174
     return (files, files_ignored)
@@ -263,6 +270,11 @@ def main():
263 270
     parser.add_argument("--ignore-path", action="append", default=[],
264 271
                         help="Ignore the given directory or file (globs"
265 272
                              " are supported).", metavar='path')
273
+    parser.add_argument("--default-extension", action="store",
274
+                        help="Default file extension to use when a file is"
275
+                             " found without a file extension.",
276
+                        default='', dest='default_extension',
277
+                        metavar='extension')
266 278
     parser.add_argument("--max-line-length", action="store", metavar="int",
267 279
                         type=int,
268 280
                         help="Maximum allowed line"

+ 7
- 3
doc8/parser.py View File

@@ -29,7 +29,7 @@ import six
29 29
 class ParsedFile(object):
30 30
     FALLBACK_ENCODING = 'utf-8'
31 31
 
32
-    def __init__(self, filename, encoding=None):
32
+    def __init__(self, filename, encoding=None, default_extension=''):
33 33
         self._filename = filename
34 34
         self._content = None
35 35
         self._raw_content = None
@@ -40,6 +40,8 @@ class ParsedFile(object):
40 40
         self._has_read = False
41 41
         self._extension = os.path.splitext(filename)[1]
42 42
         self._read_lock = threading.Lock()
43
+        if not self._extension:
44
+            self._extension = default_extension
43 45
 
44 46
     @property
45 47
     def errors(self):
@@ -129,7 +131,9 @@ class ParsedFile(object):
129 131
             len(list(self.lines_iter())))
130 132
 
131 133
 
132
-def parse(filename, encoding=None):
134
+def parse(filename, encoding=None, default_extension=''):
133 135
     if not os.path.isfile(filename):
134 136
         raise IOError(errno.ENOENT, 'File not found', filename)
135
-    return ParsedFile(filename, encoding=encoding)
137
+    return ParsedFile(filename,
138
+                      encoding=encoding,
139
+                      default_extension=default_extension)

Loading…
Cancel
Save