[![Version](https://img.shields.io/pypi/v/dir-content-diff)](https://github.com/BlueBrain/dir-content-diff/releases)
[![Build status](https://github.com/BlueBrain/dir-content-diff/actions/workflows/run-tox.yml/badge.svg?branch=main)](https://github.com/BlueBrain/dir-content-diff/actions)
[![Coverage](https://codecov.io/github/BlueBrain/dir-content-diff/coverage.svg?branch=main)](https://codecov.io/github/BlueBrain/dir-content-diff?branch=main)
[![License](https://img.shields.io/badge/License-Apache%202-blue)](https://github.com/BlueBrain/dir-content-diff/blob/main/LICENSE.txt)
[![Documentation status](https://readthedocs.org/projects/dir-content-diff/badge/?version=latest)](https://dir-content-diff.readthedocs.io/)
# Directory Content Difference
This project provides simple tools to compare the content of a directory against a reference
directory.
This is useful to check the results of a process that generate several files, like a luigi
workflow for example.
## Installation
This package should be installed using pip:
```bash
pip install dir-content-diff
```
## Usage
The ``dir-content-diff`` package introduces a framework to compare two directories. A comparator
is associated to each file extension and then each file in the reference directory is compared to
the file with the same relative path in the compared directory. By default, a few comparators are
provided for usual files but others can be associated to new file extensions or can even replace
the default ones. The comparators should be able to report the differences between two files
accurately, reporting which elements are different among the data. When an extension has no
comparator associated, a default comparator is used which just compares the whole binary data of
the files, so it is not able to report which values are different.
### Compare two directories
If one wants to compare two directories with the following structures:
```bash
└── reference_dir
├── sub_dir_1
| ├── sub_file_1.a
| └── sub_file_2.b
└── file_1.c
```
```bash
└── compared_dir
├── sub_dir_1
| ├── sub_file_1.a
| └── sub_file_2.b
| └── sub_file_3.b
└── file_1.c
```
These two directories can be compared with the following code:
```python
import dir_content_diff
dir_content_diff.compare_trees("reference_dir", "compared_dir")
```
This code will return an empty dictionary because no difference was detected.
If ``reference_dir/file_1.c`` is the following JSON-like file:
```json
{
"a": 1,
"b": [1, 2]
}
```
And ``compared_dir/file_1.c`` is the following JSON-like file:
```json
{
"a": 2,
"b": [10, 2, 0]
}
```
The following code registers the ``JsonComparator`` for the file extension ``.c`` and compares the
two directories:
```python
import dir_content_diff
dir_content_diff.register_comparator(".c", dir_content_diff.JsonComparator())
dir_content_diff.compare_trees("reference_dir", "compared_dir")
```
The previous code will output the following dictionary:
```python
{
'file_1.c': (
'The files \'reference_dir/file_1.c\' and \'compared_dir/file_1.c\' are different:\n'
'Added the value(s) \'{"2": 0}\' in the \'[b]\' key.\n'
'Changed the value of \'[a]\' from 1 to 2.\n'
'Changed the value of \'[b][0]\' from 1 to 10.'
)
}
```
It is also possible to check whether the two directories are equal or not with the following code:
```python
import dir_content_diff
dir_content_diff.register_comparator(".c", dir_content_diff.JsonComparator())
dir_content_diff.assert_equal_trees("reference_dir", "compared_dir")
```
Which will output the following ``AssertionError``:
```bash
AssertionError: The files 'reference_dir/file_1.c' and 'compared_dir/file_1.c' are different:
Added the value(s) '{"2": 0}' in the '[b]' key.
Changed the value of '[a]' from 1 to 2.
Changed the value of '[b][0]' from 1 to 10.
```
Finally, the comparators have parameters that can be passed either to be used for all files of a
given extension or only for a specific file:
```python
import dir_content_diff
# Get the default comparators
comparators = dir_content_diff.get_comparators()
# Replace the comparators for JSON files to perform the comparison with a given tolerance
comparators[".json"] = dir_content_diff.JsonComparator(default_diff_kwargs={"tolerance": 0.1})
# Use a specific tolerance for the file ``sub_dir_1/sub_file_1.a``
# In this case, the kwargs are used to compute the difference by default, except the following
# specific kwargs: ``return_raw_diffs``, ``load_kwargs``, ``format_data_kwargs``, ``filter_kwargs``,
# ``format_diff_kwargs``, ``sort_kwargs``, ``concat_kwargs`` and ``report_kwargs``.
specific_args = {"sub_dir_1/sub_file_1.a": {"tolerance": 0.5}}
dir_content_diff.assert_equal_trees(
"reference_dir",
"compared_dir",
comparators=comparators,
specific_args=specific_args,
)
```
Each comparator has different arguments that are detailed in the documentation.
It's also possible to specify a arbitrary comparator for a specific file:
```python
specific_args = {
"sub_dir_1/sub_file_1.a": {
"comparator": dir_content_diff.JsonComparator(),
"tolerance": 0.5,
}
}
```
And last but not least, it's possible to use regular expressions to associate specific arguments to
a set of files:
```python
specific_args = {
"all files with *.a of *.b extensions": {
"patterns": [r".*\.[a,b]$"],
"comparator": dir_content_diff.BaseComparator(),
}
}
```
### Export formatted data
Some comparators have to format the data before comparing them. For example, if one wants to
compare data with file paths inside, it's likely that only a relative part of these paths are
relevant, not the entire absolute paths. To do this, a specific comparator can be defined with a
custom ``format_data()`` method which is automatically called after the data are loaded but before
the data are compared. It is then possible to export the data just after they have been formatted
for check purpose for example. To do this, the ``export_formatted_files`` argument of the
``dir_content_diff.compare_trees`` and ``dir_content_diff.assert_equal_trees`` functions can be set
to ``True``. Thus all the files processed by a comparator with a ``save()`` method will be exported
to a new directory. This new directory is the same as the compared directory to which a suffix is
added. By default, the suffix is `` _FORMATTED ``, but it can be overridden by passing a non-empty
string to the ``export_formatted_files`` argument.
## Pytest plugin
This package can be used as a pytest plugin. When ``pytest`` is run and ``dir-content-diff`` is
installed, it is automatically detected and registered as a plugin. It is then possible to trigger
the export of formatted data with the following ``pytest`` option: ``--dcd-export-formatted-data``.
It is also possible to define a custom suffix for the new directory with the following option:
``--dcd-export-suffix``.
## Funding & Acknowledgment
The development of this software was supported by funding to the Blue Brain Project, a research
center of the École polytechnique fédérale de Lausanne (EPFL), from the Swiss government’s ETH
Board of the Swiss Federal Institutes of Technology.
For license and authors, see `LICENSE.txt` and `AUTHORS.md` respectively.
Copyright © 2021-2023 Blue Brain Project/EPFL
Raw data
{
"_id": null,
"home_page": null,
"name": "dir-content-diff",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": "Blue Brain Project, EPFL",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/36/3b/6c8a8c28f9e1ceb0998f65c60667089dcbf7e630ae248b54d5694ce72399/dir_content_diff-1.12.0.tar.gz",
"platform": null,
"description": "[![Version](https://img.shields.io/pypi/v/dir-content-diff)](https://github.com/BlueBrain/dir-content-diff/releases)\n[![Build status](https://github.com/BlueBrain/dir-content-diff/actions/workflows/run-tox.yml/badge.svg?branch=main)](https://github.com/BlueBrain/dir-content-diff/actions)\n[![Coverage](https://codecov.io/github/BlueBrain/dir-content-diff/coverage.svg?branch=main)](https://codecov.io/github/BlueBrain/dir-content-diff?branch=main)\n[![License](https://img.shields.io/badge/License-Apache%202-blue)](https://github.com/BlueBrain/dir-content-diff/blob/main/LICENSE.txt)\n[![Documentation status](https://readthedocs.org/projects/dir-content-diff/badge/?version=latest)](https://dir-content-diff.readthedocs.io/)\n\n\n# Directory Content Difference\n\nThis project provides simple tools to compare the content of a directory against a reference\ndirectory.\n\nThis is useful to check the results of a process that generate several files, like a luigi\nworkflow for example.\n\n\n## Installation\n\nThis package should be installed using pip:\n\n```bash\npip install dir-content-diff\n```\n\n\n## Usage\n\nThe ``dir-content-diff`` package introduces a framework to compare two directories. A comparator\nis associated to each file extension and then each file in the reference directory is compared to\nthe file with the same relative path in the compared directory. By default, a few comparators are\nprovided for usual files but others can be associated to new file extensions or can even replace\nthe default ones. The comparators should be able to report the differences between two files\naccurately, reporting which elements are different among the data. When an extension has no\ncomparator associated, a default comparator is used which just compares the whole binary data of\nthe files, so it is not able to report which values are different.\n\n### Compare two directories\n\nIf one wants to compare two directories with the following structures:\n\n```bash\n\u2514\u2500\u2500 reference_dir\n \u251c\u2500\u2500 sub_dir_1\n | \u251c\u2500\u2500 sub_file_1.a\n | \u2514\u2500\u2500 sub_file_2.b\n \u2514\u2500\u2500 file_1.c\n```\n\n```bash\n\u2514\u2500\u2500 compared_dir\n \u251c\u2500\u2500 sub_dir_1\n | \u251c\u2500\u2500 sub_file_1.a\n | \u2514\u2500\u2500 sub_file_2.b\n | \u2514\u2500\u2500 sub_file_3.b\n \u2514\u2500\u2500 file_1.c\n```\n\nThese two directories can be compared with the following code:\n\n```python\nimport dir_content_diff\n\ndir_content_diff.compare_trees(\"reference_dir\", \"compared_dir\")\n```\n\nThis code will return an empty dictionary because no difference was detected.\n\nIf ``reference_dir/file_1.c`` is the following JSON-like file:\n\n```json\n{\n \"a\": 1,\n \"b\": [1, 2]\n}\n```\n\nAnd ``compared_dir/file_1.c`` is the following JSON-like file:\n\n```json\n{\n \"a\": 2,\n \"b\": [10, 2, 0]\n}\n```\n\nThe following code registers the ``JsonComparator`` for the file extension ``.c`` and compares the\ntwo directories:\n\n```python\nimport dir_content_diff\n\ndir_content_diff.register_comparator(\".c\", dir_content_diff.JsonComparator())\ndir_content_diff.compare_trees(\"reference_dir\", \"compared_dir\")\n```\n\nThe previous code will output the following dictionary:\n\n```python\n{\n 'file_1.c': (\n 'The files \\'reference_dir/file_1.c\\' and \\'compared_dir/file_1.c\\' are different:\\n'\n 'Added the value(s) \\'{\"2\": 0}\\' in the \\'[b]\\' key.\\n'\n 'Changed the value of \\'[a]\\' from 1 to 2.\\n'\n 'Changed the value of \\'[b][0]\\' from 1 to 10.'\n )\n}\n```\n\nIt is also possible to check whether the two directories are equal or not with the following code:\n\n```python\nimport dir_content_diff\n\ndir_content_diff.register_comparator(\".c\", dir_content_diff.JsonComparator())\ndir_content_diff.assert_equal_trees(\"reference_dir\", \"compared_dir\")\n```\n\nWhich will output the following ``AssertionError``:\n\n```bash\nAssertionError: The files 'reference_dir/file_1.c' and 'compared_dir/file_1.c' are different:\nAdded the value(s) '{\"2\": 0}' in the '[b]' key.\nChanged the value of '[a]' from 1 to 2.\nChanged the value of '[b][0]' from 1 to 10.\n```\n\nFinally, the comparators have parameters that can be passed either to be used for all files of a\ngiven extension or only for a specific file:\n\n```python\nimport dir_content_diff\n\n# Get the default comparators\ncomparators = dir_content_diff.get_comparators()\n\n# Replace the comparators for JSON files to perform the comparison with a given tolerance\ncomparators[\".json\"] = dir_content_diff.JsonComparator(default_diff_kwargs={\"tolerance\": 0.1})\n\n# Use a specific tolerance for the file ``sub_dir_1/sub_file_1.a``\n# In this case, the kwargs are used to compute the difference by default, except the following\n# specific kwargs: ``return_raw_diffs``, ``load_kwargs``, ``format_data_kwargs``, ``filter_kwargs``,\n# ``format_diff_kwargs``, ``sort_kwargs``, ``concat_kwargs`` and ``report_kwargs``.\nspecific_args = {\"sub_dir_1/sub_file_1.a\": {\"tolerance\": 0.5}}\n\ndir_content_diff.assert_equal_trees(\n \"reference_dir\",\n \"compared_dir\",\n comparators=comparators,\n specific_args=specific_args,\n)\n```\n\nEach comparator has different arguments that are detailed in the documentation.\n\nIt's also possible to specify a arbitrary comparator for a specific file:\n\n```python\nspecific_args = {\n \"sub_dir_1/sub_file_1.a\": {\n \"comparator\": dir_content_diff.JsonComparator(),\n \"tolerance\": 0.5,\n }\n}\n```\n\nAnd last but not least, it's possible to use regular expressions to associate specific arguments to\na set of files:\n\n```python\nspecific_args = {\n \"all files with *.a of *.b extensions\": {\n \"patterns\": [r\".*\\.[a,b]$\"],\n \"comparator\": dir_content_diff.BaseComparator(),\n }\n}\n```\n\n\n### Export formatted data\n\nSome comparators have to format the data before comparing them. For example, if one wants to\ncompare data with file paths inside, it's likely that only a relative part of these paths are\nrelevant, not the entire absolute paths. To do this, a specific comparator can be defined with a\ncustom ``format_data()`` method which is automatically called after the data are loaded but before\nthe data are compared. It is then possible to export the data just after they have been formatted\nfor check purpose for example. To do this, the ``export_formatted_files`` argument of the\n``dir_content_diff.compare_trees`` and ``dir_content_diff.assert_equal_trees`` functions can be set\nto ``True``. Thus all the files processed by a comparator with a ``save()`` method will be exported\nto a new directory. This new directory is the same as the compared directory to which a suffix is\nadded. By default, the suffix is `` _FORMATTED ``, but it can be overridden by passing a non-empty\nstring to the ``export_formatted_files`` argument.\n\n## Pytest plugin\n\nThis package can be used as a pytest plugin. When ``pytest`` is run and ``dir-content-diff`` is\ninstalled, it is automatically detected and registered as a plugin. It is then possible to trigger\nthe export of formatted data with the following ``pytest`` option: ``--dcd-export-formatted-data``.\nIt is also possible to define a custom suffix for the new directory with the following option:\n``--dcd-export-suffix``.\n\n\n## Funding & Acknowledgment\n\nThe development of this software was supported by funding to the Blue Brain Project, a research\ncenter of the \u00c9cole polytechnique f\u00e9d\u00e9rale de Lausanne (EPFL), from the Swiss government\u2019s ETH\nBoard of the Swiss Federal Institutes of Technology.\n\nFor license and authors, see `LICENSE.txt` and `AUTHORS.md` respectively.\n\nCopyright \u00a9 2021-2023 Blue Brain Project/EPFL\n",
"bugtrack_url": null,
"license": "Apache License 2.0",
"summary": "Simple tool to compare directory contents.",
"version": "1.12.0",
"project_urls": {
"Homepage": "https://dir-content-diff.readthedocs.io",
"Repository": "https://github.com/BlueBrain/dir-content-diff",
"Tracker": "https://github.com/BlueBrain/dir-content-diff/issues"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "17f082239571adcca20e876e21a28871c6d2f1545fc9baecd72523f98b4223d0",
"md5": "e875cfc91e00e2c73d0edba2aab20e8f",
"sha256": "4578a702f29242f2dbf0500386f216e19a7fb29834504bef22252e8412d5648f"
},
"downloads": -1,
"filename": "dir_content_diff-1.12.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e875cfc91e00e2c73d0edba2aab20e8f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 30806,
"upload_time": "2024-11-18T14:25:11",
"upload_time_iso_8601": "2024-11-18T14:25:11.767714Z",
"url": "https://files.pythonhosted.org/packages/17/f0/82239571adcca20e876e21a28871c6d2f1545fc9baecd72523f98b4223d0/dir_content_diff-1.12.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "363b6c8a8c28f9e1ceb0998f65c60667089dcbf7e630ae248b54d5694ce72399",
"md5": "b5a0c423b68cfa7506a32f3f2ddb5ec0",
"sha256": "15a65b4469666e0f61e9941e3772bf39e5d5c3c65051e4ef22243d5906eb54e7"
},
"downloads": -1,
"filename": "dir_content_diff-1.12.0.tar.gz",
"has_sig": false,
"md5_digest": "b5a0c423b68cfa7506a32f3f2ddb5ec0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 65333,
"upload_time": "2024-11-18T14:25:12",
"upload_time_iso_8601": "2024-11-18T14:25:12.896749Z",
"url": "https://files.pythonhosted.org/packages/36/3b/6c8a8c28f9e1ceb0998f65c60667089dcbf7e630ae248b54d5694ce72399/dir_content_diff-1.12.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-18 14:25:12",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "BlueBrain",
"github_project": "dir-content-diff",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "dir-content-diff"
}