2323# SOFTWARE.
2424"""This module provides docformatter's Configurater class."""
2525
26+
2627# Standard Library Imports
2728import argparse
29+ import contextlib
2830import os
31+ import sys
2932from configparser import ConfigParser
3033from typing import Dict , List , Union
3134
32- try :
33- # Third Party Imports
34- import tomli
35+ TOMLLIB_INSTALLED = False
36+ TOMLI_INSTALLED = False
37+ with contextlib .suppress (ImportError ):
38+ if sys .version_info >= (3 , 11 ):
39+ # Standard Library Imports
40+ import tomllib
41+
42+ TOMLLIB_INSTALLED = True
43+ else :
44+ # Third Party Imports
45+ import tomli
3546
36- TOMLI_INSTALLED = True
37- except ImportError :
38- TOMLI_INSTALLED = False
47+ TOMLI_INSTALLED = True
3948
4049# docformatter Package Imports
4150from docformatter import __pkginfo__
4453class Configurater :
4554 """Read and store all the docformatter configuration information."""
4655
47- parser = None
56+ parser : argparse . ArgumentParser = argparse . ArgumentParser ()
4857 """Parser object."""
4958
5059 flargs_dct : Dict [str , Union [bool , float , int , str ]] = {}
@@ -57,7 +66,7 @@ class Configurater:
5766 ]
5867 """List of supported configuration files."""
5968
60- args : argparse .Namespace = None
69+ args : argparse .Namespace = argparse . Namespace ()
6170
6271 def __init__ (self , args : List [Union [bool , int , str ]]) -> None :
6372 """Initialize a Configurater class instance.
@@ -75,9 +84,7 @@ def __init__(self, args: List[Union[bool, int, str]]) -> None:
7584 )
7685
7786 try :
78- self .config_file = self .args_lst [
79- self .args_lst .index ("--config" ) + 1
80- ]
87+ self .config_file = self .args_lst [self .args_lst .index ("--config" ) + 1 ]
8188 except ValueError :
8289 for _configuration_file in self .configuration_file_lst :
8390 if os .path .isfile (_configuration_file ):
@@ -116,8 +123,7 @@ def do_parse_arguments(self) -> None:
116123 "-r" ,
117124 "--recursive" ,
118125 action = "store_true" ,
119- default = self .flargs_dct .get ("recursive" , "false" ).lower ()
120- == "true" ,
126+ default = self .flargs_dct .get ("recursive" , "false" ).lower () == "true" ,
121127 help = "drill down directories recursively" ,
122128 )
123129 self .parser .add_argument (
@@ -163,13 +169,11 @@ def do_parse_arguments(self) -> None:
163169 "--style" ,
164170 default = self .flargs_dct .get ("style" , "sphinx" ),
165171 help = "name of the docstring style to use when formatting "
166- "parameter lists (default: sphinx)" ,
172+ "parameter lists (default: sphinx)" ,
167173 )
168174 self .parser .add_argument (
169175 "--wrap-summaries" ,
170- default = int (
171- self .flargs_dct .get ("wrap-summaries" , _default_wrap_summaries )
172- ),
176+ default = int (self .flargs_dct .get ("wrap-summaries" , _default_wrap_summaries )),
173177 type = int ,
174178 metavar = "length" ,
175179 help = "wrap long summary lines at this length; "
@@ -179,9 +183,7 @@ def do_parse_arguments(self) -> None:
179183 self .parser .add_argument (
180184 "--wrap-descriptions" ,
181185 default = int (
182- self .flargs_dct .get (
183- "wrap-descriptions" , _default_wrap_descriptions
184- )
186+ self .flargs_dct .get ("wrap-descriptions" , _default_wrap_descriptions )
185187 ),
186188 type = int ,
187189 metavar = "length" ,
@@ -192,8 +194,7 @@ def do_parse_arguments(self) -> None:
192194 self .parser .add_argument (
193195 "--force-wrap" ,
194196 action = "store_true" ,
195- default = self .flargs_dct .get ("force-wrap" , "false" ).lower ()
196- == "true" ,
197+ default = self .flargs_dct .get ("force-wrap" , "false" ).lower () == "true" ,
197198 help = "force descriptions to be wrapped even if it may "
198199 "result in a mess (default: False)" ,
199200 )
@@ -228,25 +229,20 @@ def do_parse_arguments(self) -> None:
228229 "pre-summary-space" , _default_pre_summary_space
229230 ).lower ()
230231 == "true" ,
231- help = "add a space after the opening triple quotes "
232- "(default: False)" ,
232+ help = "add a space after the opening triple quotes " "(default: False)" ,
233233 )
234234 self .parser .add_argument (
235235 "--make-summary-multi-line" ,
236236 action = "store_true" ,
237- default = self .flargs_dct .get (
238- "make-summary-multi-line" , "false"
239- ).lower ()
237+ default = self .flargs_dct .get ("make-summary-multi-line" , "false" ).lower ()
240238 == "true" ,
241239 help = "add a newline before and after the summary of a one-line "
242240 "docstring (default: False)" ,
243241 )
244242 self .parser .add_argument (
245243 "--close-quotes-on-newline" ,
246244 action = "store_true" ,
247- default = self .flargs_dct .get (
248- "close-quotes-on-newline" , "false"
249- ).lower ()
245+ default = self .flargs_dct .get ("close-quotes-on-newline" , "false" ).lower ()
250246 == "true" ,
251247 help = "place closing triple quotes on a new-line when a "
252248 "one-line docstring wraps to two or more lines "
@@ -275,8 +271,7 @@ def do_parse_arguments(self) -> None:
275271 self .parser .add_argument (
276272 "--non-strict" ,
277273 action = "store_true" ,
278- default = self .flargs_dct .get ("non-strict" , "false" ).lower ()
279- == "true" ,
274+ default = self .flargs_dct .get ("non-strict" , "false" ).lower () == "true" ,
280275 help = "don't strictly follow reST syntax to identify lists (see "
281276 "issue #67) (default: False)" ,
282277 )
@@ -309,9 +304,7 @@ def do_parse_arguments(self) -> None:
309304
310305 if self .args .length_range :
311306 if self .args .length_range [0 ] <= 0 :
312- self .parser .error (
313- "--docstring-length must be positive numbers"
314- )
307+ self .parser .error ("--docstring-length must be positive numbers" )
315308 if self .args .length_range [0 ] > self .args .length_range [1 ]:
316309 self .parser .error (
317310 "First value of --docstring-length should be less "
@@ -328,7 +321,11 @@ def _do_read_configuration_file(self) -> None:
328321 fullpath , ext = os .path .splitext (self .config_file )
329322 filename = os .path .basename (fullpath )
330323
331- if ext == ".toml" and TOMLI_INSTALLED and filename == "pyproject" :
324+ if (
325+ ext == ".toml"
326+ and (TOMLI_INSTALLED or TOMLLIB_INSTALLED )
327+ and filename == "pyproject"
328+ ):
332329 self ._do_read_toml_configuration ()
333330
334331 if (ext == ".cfg" and filename == "setup" ) or (
@@ -339,13 +336,15 @@ def _do_read_configuration_file(self) -> None:
339336 def _do_read_toml_configuration (self ) -> None :
340337 """Load configuration information from a *.toml file."""
341338 with open (self .config_file , "rb" ) as f :
342- config = tomli .load (f )
339+ if TOMLI_INSTALLED :
340+ config = tomli .load (f )
341+ elif TOMLLIB_INSTALLED :
342+ config = tomllib .load (f )
343343
344344 result = config .get ("tool" , {}).get ("docformatter" , None )
345345 if result is not None :
346346 self .flargs_dct = {
347- k : v if isinstance (v , list ) else str (v )
348- for k , v in result .items ()
347+ k : v if isinstance (v , list ) else str (v ) for k , v in result .items ()
349348 }
350349
351350 def _do_read_parser_configuration (self ) -> None :
0 commit comments