Skip to content

Commit

Permalink
#655 - adding support for pyproject.toml (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdewinter authored Apr 23, 2023
1 parent 6090478 commit 55b0cff
Show file tree
Hide file tree
Showing 9 changed files with 607 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ twine = "==4.0.2"
typing-extensions = "==4.5.0"
mypy = "==1.2.0"
columnar = "==1.4.1"
application-properties = "==0.6.0"
coverage = "==7.2.3"
sourcery = "==1.2.0"
application-properties = "==0.7.0"

[requires]
python_version = "3.8"
Expand Down
75 changes: 37 additions & 38 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Added

- None
- [Issue 655](https://github.com/jackdewinter/pymarkdown/issues/655)
- Added support for `tool.pymarkdown` section in `pyproject.toml` for current directory

### Changed

Expand Down
8 changes: 8 additions & 0 deletions clean.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ if ERRORLEVEL 1 (
goto error_end
)

echo {Executing bandit security analyzer on Python code.}
pipenv run bandit -q -r application_properties
if ERRORLEVEL 1 (
echo.
echo {Executing security analyzer on Python code failed.}
goto error_end
)

echo {Executing pylint static analyzer on Python source code.}
set TEST_EXECUTION_FAILED=
pipenv run pylint -j 1 --recursive=y %MY_VERBOSE% %PYTHON_MODULE_NAME%
Expand Down
47 changes: 43 additions & 4 deletions docs/advanced_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ configuration applies to a given item. This theme is that the order of preceden
for interpreting configuration properties goes from the most specific provided configuration
value to the least specific provided configuration value. In order, this is:

- general command line setting
- specific command line setting
- command line configuration file
- default configuration file
- general command line settings (`--add-plugin`, `--log-file`, etc.)
- specific command line setting (`-s log.level=INFO`, etc.)
- command line configuration file (`--config myconfig.json`)
- default configuration file (loading default `.pymarkdown` JSON file)
- alternate project configuration files (`tool.pymarkdown` section of `pyproject.toml` file)
- default value

### General Command Line Settings
Expand Down Expand Up @@ -179,6 +180,19 @@ section. As that file format meets PyMarkdown's requirements for clear item typi
and a hierarchical format, there are no plans to support any other file types for
the primary configuration file.

As an example, the enhanced logging output snippet from the
[Specific Command Line Setting](#specific-command-line-setting) section can be
expressed in JSON format as:

```json
{
"log": {
"level": "INFO",
"stack-trace" : true
}
}
```

### Default Configuration File

As an alternative to a specified configuration file, there are times where there
Expand All @@ -203,6 +217,31 @@ are nested within a given directory structure, some manner of scripting will be
necessary to properly apply the right default configuration file to its corresponding
directory structure.

### Alternate Project Configuration Files

For anyone that has continued to read this far down in the hierarchy of configuration,
there is more! If there is a `pyproject.toml` file in the current directory that
contains a `tool.pymarkdown` section, those values will be used for configuration.
The [TOML file format](https://docs.fileformat.com/programming/toml/) is easy to
use, and is provided as an alternative to the JSON format of the `.pymarkdown`
configuration file.

Note that any items contained within the section must use a key that is expressed
using a full
[flattened format](./advanced_configuration.md#flattened-hierarchical-property-names).
As TOML files provides typing information for the values, care must be taken to
associate the right type with the right value.

As an example, the enhanced logging output snippet from the
[Specific Command Line Setting](#specific-command-line-setting) section can be
expressed in TOML format as:

```toml
[tool.pymarkdown]
log.level = "INFO"
log.stack-trace = true
```

### Default Value

At the bottom of the precedence list is the default value for a configuration
Expand Down
8 changes: 4 additions & 4 deletions publish/coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"projectName": "pymarkdown",
"reportSource": "pytest",
"branchLevel": {
"totalMeasured": 3597,
"totalCovered": 3597
"totalMeasured": 3595,
"totalCovered": 3595
},
"lineLevel": {
"totalMeasured": 14743,
"totalCovered": 14743
"totalMeasured": 14745,
"totalCovered": 14745
}
}

2 changes: 1 addition & 1 deletion publish/test-results.json
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@
},
{
"name": "test.test_main",
"totalTests": 54,
"totalTests": 63,
"failedTests": 0,
"errorTests": 0,
"skippedTests": 0,
Expand Down
46 changes: 31 additions & 15 deletions pymarkdown/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from application_properties import (
ApplicationProperties,
ApplicationPropertiesJsonLoader,
ApplicationPropertiesTomlLoader,
)

from pymarkdown.application_file_scanner import ApplicationFileScanner
Expand Down Expand Up @@ -42,6 +43,8 @@ class PyMarkdownLint:
"""

__default_configuration_file = ".pymarkdown"
__pyproject_toml_file = "pyproject.toml"
__pyproject_section_header = "tool.pymarkdown"

__normal_scan_subcommand = "scan"
__stdin_scan_subcommand = "scan-stdin"
Expand Down Expand Up @@ -278,9 +281,11 @@ def __initialize_plugin_manager(
)
self.__handle_error(formatted_error, this_exception)

def __handle_error(self, formatted_error: str, thrown_error: Exception) -> None:
show_error = self.__logging.show_stack_trace or not isinstance(
thrown_error, ValueError
def __handle_error(
self, formatted_error: str, thrown_error: Optional[Exception]
) -> None:
show_error = self.__logging.show_stack_trace or (
thrown_error and not isinstance(thrown_error, ValueError)
)
LOGGER.warning(formatted_error, exc_info=show_error)

Expand All @@ -298,20 +303,30 @@ def __handle_scan_error(self, next_file: str, this_exception: Exception) -> None
sys.exit(1)

def __apply_configuration_layers(self, args: argparse.Namespace) -> None:
# Look for a "pyproject.toml" file in the current working directory.
project_configuration_file = os.path.abspath(
PyMarkdownLint.__pyproject_toml_file
)
ApplicationPropertiesTomlLoader.load_and_set(
self.__properties,
project_configuration_file,
PyMarkdownLint.__pyproject_section_header,
self.__handle_error,
clear_property_map=False,
check_for_file_presence=True,
)

# Look for a ".pymarkdown" file in the current working directory.
if os.path.exists(
default_configuration_file = os.path.abspath(
PyMarkdownLint.__default_configuration_file
) and os.path.isfile(PyMarkdownLint.__default_configuration_file):
default_configuration_file = os.path.abspath(
PyMarkdownLint.__default_configuration_file
)
LOGGER.debug("Loading configuration file: %s", default_configuration_file)
ApplicationPropertiesJsonLoader.load_and_set(
self.__properties,
default_configuration_file,
self.__handle_error,
clear_property_map=False,
)
)
ApplicationPropertiesJsonLoader.load_and_set(
self.__properties,
default_configuration_file,
self.__handle_error,
clear_property_map=False,
check_for_file_presence=True,
)

# A configuration file specified on the command line has a higher precedence
# than anything except a specific setting applied on the command line.
Expand All @@ -322,6 +337,7 @@ def __apply_configuration_layers(self, args: argparse.Namespace) -> None:
args.configuration_file,
self.__handle_error,
clear_property_map=False,
check_for_file_presence=False,
)

# A specific setting applied on the command line has the highest precedence.
Expand Down
Loading

0 comments on commit 55b0cff

Please sign in to comment.