rstcheck_core package¶
Submodules¶
rstcheck_core.checker module¶
- rstcheck_core.checker.check_file(source_file, rstcheck_config, overwrite_with_file_config=True)[source]¶
Check the given file for issues.
On every call docutils’ caches for roles and directives are cleared by reloading their modules.
- Parameters:
source_file (
Path
) – Path to file to checkrstcheck_config (
RstcheckConfig
) – Main configuration of the applicationoverwrite_with_file_config (
bool
) – If the loaded file config should overwrite therstcheck_config
; defaults toTrue
- Return type:
- Returns:
A list of found issues
- rstcheck_core.checker.check_source(source, source_file=None, ignores=None, report_level=ReportLevel.INFO, warn_unknown_settings=False)[source]¶
Check the given rst source for issues.
- Parameters:
source_file (
Optional
[Path
]) – Path to file the source comes from if it comes from a file; defaults toNone
ignores (
Optional
[IgnoreDict
]) – Ignore information; defaults toNone
report_level (
ReportLevel
) – Report level; defaults torstcheck_core.config.DEFAULT_REPORT_LEVEL
warn_unknown_settings (
bool
) – If a warning should be logged for unknown settings in config file; defaults toFalse
source (str) –
- Return type:
- Returns:
- Yield:
Found issues
- class rstcheck_core.checker.CodeBlockChecker(source_origin, ignores=None, report_level=ReportLevel.INFO, warn_unknown_settings=False)[source]¶
Bases:
object
Checker for code blockes with different languages.
Inititalize CodeBlockChecker.
- Parameters:
source_origin (
Union
[Path
,Literal
['<string>'
],Literal
['<stdin>'
]]) – Path to file the source comes fromignores (
Optional
[IgnoreDict
]) – Ignore information; defaults toNone
report_level (
ReportLevel
) – Report level; defaults torstcheck_core.config.DEFAULT_REPORT_LEVEL
warn_unknown_settings (
bool
) – If a warning should be logged for unknown settings in config file; defaults toFalse
- create_checker(source_code, language)[source]¶
Create a checker function for the given source and language.
- check(source_code, language)[source]¶
Call the appropiate checker function for the given langauge to check given source.
- check_doctest(source_code)[source]¶
Check doctest source for syntax errors.
This does not run the test as that would be unsafe. Nor does this check the Python syntax in the doctest. That could be purposely incorrect for testing purposes.
rstcheck_core.config module¶
- rstcheck_core.config.CONFIG_FILES = ['.rstcheck.cfg', 'setup.cfg']¶
Supported default config files.
- class rstcheck_core.config.ReportLevel(value)[source]¶
Bases:
Enum
Report levels supported by docutils.
- INFO = 1¶
- WARNING = 2¶
- ERROR = 3¶
- SEVERE = 4¶
- NONE = 5¶
- rstcheck_core.config.ReportLevelMap = {'error': 3, 'info': 1, 'none': 5, 'severe': 4, 'warning': 2}¶
Map docutils report levels in text form to numbers.
- rstcheck_core.config.DEFAULT_REPORT_LEVEL = ReportLevel.INFO¶
Default report level.
- class rstcheck_core.config.RstcheckConfigFile(**data)[source]¶
Bases:
BaseModel
Rstcheck config file.
- Raises:
ValueError – If setting has incorrect value or type
pydantic.error_wrappers.ValidationError – If setting is not parsable into correct type
- Parameters:
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
-
report_level:
Optional
[ReportLevel
]¶
- classmethod valid_report_level(value)[source]¶
Validate the report_level setting.
- Parameters:
value (
Any
) – Value to validate- Raises:
ValueError – If
value
is not a valid docutils report level- Return type:
- Returns:
Instance of
ReportLevel
or None if emptry string.
- classmethod split_str(value)[source]¶
Validate and parse the following ignore_* settings.
ignore_directives
ignore_roles
ignore_substitutions
ignore_languages
Comma separated strings are split into a list.
- class rstcheck_core.config.RstcheckConfig(**data)[source]¶
Bases:
RstcheckConfigFile
Rstcheck config.
- Raises:
ValueError – If setting has incorrect value or type
pydantic.error_wrappers.ValidationError – If setting is not parsable into correct type
- Parameters:
report_level (Optional[ReportLevel]) –
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
- rstcheck_core.config.load_config_file(file_path, *, log_missing_section_as_warning=True, warn_unknown_settings=False)[source]¶
Load, parse and validate rstcheck config from a file.
Caution
If a TOML file is passed this function need tomli installed for python versions before 3.11! Use toml extra or install manually.
- Parameters:
file_path (
Path
) – File to load config fromlog_missing_section_as_warning (
bool
) – If a missing config section should be logged at WARNING (True
) orINFO
(False
) level; defaults toTrue
warn_unknown_settings (
bool
) – If a warning should be logged for unknown settings in config file; defaults toFalse
- Raises:
FileNotFoundError – If the file is not found
- Return type:
- Returns:
instance of
RstcheckConfigFile
orNone
on missing config section orNONE
is passed as the config path.
- rstcheck_core.config.load_config_file_from_dir(dir_path, *, log_missing_section_as_warning=False, warn_unknown_settings=False)[source]¶
Search, load, parse and validate rstcheck config from a directory.
Searches files from
CONFIG_FILES
in the directory. If a file is found, try to load the config from it. If is has no config, search further.- Parameters:
dir_path (
Path
) – Directory to searchlog_missing_section_as_warning (
bool
) – If a missing config section in a config file should be logged at WARNING (True
) orINFO
(False
) level; defaults toFalse
warn_unknown_settings (
bool
) – If a warning should be logged for unknown settings in config file; defaults toFalse
- Return type:
- Returns:
instance of
RstcheckConfigFile
orNone
if no file is found or no file has a rstcheck section orNONE
is passed as the config path.
- rstcheck_core.config.load_config_file_from_dir_tree(dir_path, *, log_missing_section_as_warning=False, warn_unknown_settings=False)[source]¶
Search, load, parse and validate rstcheck config from a directory tree.
Searches files from
CONFIG_FILES
in the directory. If a file is found, try to load the config from it. If is has no config, search further. If no config is found in the directory search its parents one by one.- Parameters:
dir_path (
Path
) – Directory to searchlog_missing_section_as_warning (
bool
) – If a missing config section in a config file should be logged atWARNING
(True
) orINFO
(False
) level; defaults toFalse
warn_unknown_settings (
bool
) – If a warning should be logged for unknown settings in config file; defaults toFalse
- Return type:
- Returns:
instance of
RstcheckConfigFile
orNone
if no file is found or no file has a rstcheck section orNONE
is passed as the config path.
- rstcheck_core.config.load_config_file_from_path(path, *, search_dir_tree=False, log_missing_section_as_warning_for_file=True, log_missing_section_as_warning_for_dir=False, warn_unknown_settings=False)[source]¶
Analyse the path and call the correct config file loader.
- Parameters:
path (
Path
) – Path to load config file from; can be a file or directorysearch_dir_tree (
bool
) – If the directory tree should be searched; only applies ifpath
is a directory; defaults toFalse
log_missing_section_as_warning_for_file (
bool
) – If a missing config section in a config file should be logged at WARNING (True
) orINFO
(False
) level when the given path is a file; defaults toTrue
log_missing_section_as_warning_for_dir (
bool
) – If a missing config section in a config file should be logged atWARNING
(True
) orINFO
(False
) level when the given file is a direcotry; defaults toFalse
warn_unknown_settings (
bool
) – If a warning should be logged for unknown settings in config file; defaults toFalse
- Raises:
FileNotFoundError – When the passed path is not found.
- Return type:
- Returns:
instance of
RstcheckConfigFile
orNone
if no file is found or no file has a rstcheck section orNONE
is passed as the config path.
- rstcheck_core.config.merge_configs(config_base, config_add, *, config_add_is_dominant=True)[source]¶
Merge two configs into a new one.
- Parameters:
config_base (
RstcheckConfig
) – The base config to merge intoconfig_add (
Union
[RstcheckConfig
,RstcheckConfigFile
]) – The config that is merged into theconfig_base
config_add_is_dominant (
bool
) – If theconfig_add
overwrites values ofconfig_base
; defaults toTrue
- Return type:
- Returns:
New merged config
rstcheck_core.inline_config module¶
- rstcheck_core.inline_config.get_inline_config_from_source(source, source_origin, warn_unknown_settings=False)[source]¶
Get rstcheck inline configs from source.
Unknown configs are ignored.
- Parameters:
- Return type:
- Returns:
A list of inline configs
- rstcheck_core.inline_config.find_ignored_directives(source, source_origin, warn_unknown_settings=False)[source]¶
Search the rst source for rstcheck inline ignore-directives comments.
Directives are ignored via comment.
For example, to ignore directive1, directive2, and directive3:
>>> list(find_ignored_directives(''' ... Example ... ======= ... ... .. rstcheck: ignore-directives=directive1,directive3 ... ... .. rstcheck: ignore-directives=directive2 ... ''', "<string>")) ['directive1', 'directive3', 'directive2']
- rstcheck_core.inline_config.find_ignored_roles(source, source_origin, warn_unknown_settings=False)[source]¶
Search the rst source for rstcheck inline ignore-roles comments.
Roles are ignored via comment.
For example, to ignore role1, role2, and role3:
>>> list(find_ignored_roles(''' ... Example ... ======= ... ... .. rstcheck: ignore-roles=role1,role3 ... ... .. rstcheck: ignore-roles=role2 ... ''', "<string>")) ['role1', 'role3', 'role2']
- rstcheck_core.inline_config.find_ignored_substitutions(source, source_origin, warn_unknown_settings=False)[source]¶
Search the rst source for rstcheck inline ignore-substitutions comments.
Substitutions are ignored via comment.
For example, to ignore substitution1, substitution2, and substitution3:
>>> list(find_ignored_substitutions(''' ... Example ... ======= ... ... .. rstcheck: ignore-substitutions=substitution1,substitution3 ... ... .. rstcheck: ignore-substitutions=substitution2 ... ''', "<string>")) ['substitution1', 'substitution3', 'substitution2']
- rstcheck_core.inline_config.find_ignored_languages(source, source_origin, warn_unknown_settings=False)[source]¶
Search the rst source for rstcheck inline ignore-languages comments.
Languages are ignored via comment.
For example, to ignore C++, JSON, and Python:
>>> list(find_ignored_languages(''' ... Example ... ======= ... ... .. rstcheck: ignore-languages=cpp,json ... ... .. rstcheck: ignore-languages=python ... ''', "<string>")) ['cpp', 'json', 'python']
- rstcheck_core.inline_config.get_inline_flow_control_from_source(source, source_origin, warn_unknown_settings=False)[source]¶
Get rstcheck inline flow control from source.
Unknown flow controls are ignored.
- Parameters:
- Return type:
- Returns:
A list of inline flow controls
rstcheck_core.runner module¶
- class rstcheck_core.runner.RstcheckMainRunner(check_paths, rstcheck_config, overwrite_config=True)[source]¶
Bases:
object
Main runner of rstcheck_core.
Initialize the
RstcheckMainRunner
with a base config.- Parameters:
rstcheck_config (
RstcheckConfig
) – Base configuration config from e.g. the CLI.overwrite_config (
bool
) – If file config overwrites current config; defaults to True
- property files_to_check: List[Path]¶
List of files to check.
This list is updated via the
RstcheckMainRunner.update_file_list()
method.
- property nonexisting_paths: List[Path]¶
List of paths which do not exist.
This list is updated via the
RstcheckMainRunner.update_file_list()
method.
- load_config_file(config_path, warn_unknown_settings=False)[source]¶
Load config from file and merge with current config.
If the loaded file config overwrites the current config depends on the
self.overwrite_config
attribute set on initialization.
- update_file_list()[source]¶
Update file path list with paths specified on initialization.
Uses paths from
RstcheckMainRunner.check_paths
, resolves all file paths and saves them inRstcheckMainRunner.files_to_check
.If a given path does not exist, it is filtered out and saved in
RstcheckMainRunner.files_to_check
.Clear the current file list. Then get the file and directory paths specified with
self.check_paths
attribute set on initialization and search them for rst files to check. Add those files to the file list.- Return type:
- check()[source]¶
Check all files in the file list and save the errors.
Multiple files are run in parallel.
A new call overwrite the old cached errors.
- Return type:
rstcheck_core.types module¶
- rstcheck_core.types.SourceFileOrString¶
Path to source file or if it is a string then ‘<string>’ or ‘<stdin>’.
alias of
Union
[Path
,Literal
[‘<string>’],Literal
[‘<stdin>’]]
- class rstcheck_core.types.LintError(*args, **kwargs)[source]¶
Bases:
dict
Dict with information about an linting error.
- class rstcheck_core.types.IgnoreDict(*args, **kwargs)[source]¶
Bases:
dict
Dict with ignore information.
- rstcheck_core.types.construct_ignore_dict(messages=None, languages=None, directives=None, roles=None, substitutions=None)[source]¶
Create an
IgnoreDict
with passed values or defaults.- Parameters:
messages (
Optional
[Pattern
]) – Value forIgnoreDict.messages
;None
results in an empty list; defaults toNone
directives (
Optional
[List
[str
]]) – Value forIgnoreDict.directives
;None
results in an empty list; defaults toNone
roles (
Optional
[List
[str
]]) – Value forIgnoreDict.roles
;None
results in an empty list; defaults toNone
substitutions (
Optional
[List
[str
]]) – Value forIgnoreDict.substitutions
;None
results in an empty list; defaults toNone
- Return type:
- Returns:
IgnoreDict
with passed values or defaults
- rstcheck_core.types.CheckerRunFunction¶
Function to run checks.
Returned by
rstcheck_core.checker.CodeBlockChecker.create_checker()
.