🚶 stroll: a better os.path.walk 🚶
-------------------------------------
``stroll`` is a drop-in substitute for ``os.path.walk()`` with more features:
* Unix-style globs or "star notation" like \*.py
* Walks over multiple roots
* Calls expanduser to handle paths like ``~/foo.txt``
* Yields ``pathlib.Path()`` instead of ``str``
* Yields full absolute paths by default
* Can exclude or include files flexibly by pattern or function
* Raises ``FileNotFoundError`` if a root directory doesn't exist, instead
of silently doing nothing like ``os.walk`` does
* Excludes dotfiles by default
* Includes two functions for ignoring generated files in a Python project:
* The Python build, test and release cycle tend to leave generated files in
places like ``build/`` or ``__pycache__/``, and usually you want to ignore
these
* ``stroll.python_source()`` iterates over Python source files
* ``stroll.python()`` iterates over all source files in a Python project
* The files and directories that are ignored are:
* files or directories that start with a ``.``
* ``.egg-info/`` and ``__pycache__/``
* ``build/``, ``dist/`` and ``htmlcov/`` at the top level only
API
===
``stroll()``
~~~~~~~~~~~~
.. code-block:: python
stroll(
roots='.',
topdown=True,
onerror=None,
followlinks=False,
include=None,
exclude=<function dotfile at 0x10c6e47b8>,
directories=False,
relative=False,
with_root=None,
sort=True,
suffix=None,
separator=',',
ignore_missing_roots=False,
)
(`stroll.py, 59-228 <https://github.com/rec/stroll/blob/master/stroll.py#L59-L228>`_)
Directory walker that improves on ``os.walk()``.
For each directory in ``roots``, walk through each file in each
subdirectory and yield a Path to that file. Ignores dotfiles by default.
EXAMPLE
.. code-block:: python
import stroll
for f in stroll('~/foo:~/bar'):
if f.suffix == '.txt':
print(f)
for f in stroll.python_source('/code/project'):
assert f.suffix == '.py'
ARGUMENTS
roots
Either a list or tuple of strings, or a single string that is split
using ``separator`` (defaults to ``,``, the comma).
topdown (argument to ``os.walk``)
If optional arg ``topdown`` is true or not specified, the ``Path`` to a
directory is generated before any of its subdirectories - directories
are generated top-down.
If ``topdown`` is false, the Path to a directory is generated after all
of its subdirectories - directories are generated bottom up.
onerror (argument to ``os.walk``)
By default errors from the ``os.scandir()`` call are ignored. If
optional arg ``onerror`` is specified, it should be a function; it
will be called with one argument, an OSError instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename attribute of the exception object.
followlinks (argument to ``os.walk``)
By default, ``os.walk()`` does not follow symbolic links to
subdirectories on systems that support them. In order to get this
functionality, set the optional argument ``followlinks`` to true.
Caution: if you pass a relative pathname for top, don't change the
current working directory between resumptions of walk. ``os.walk()``
never changes the current directory, and assumes that the client
doesn't either.
include
A list of patterns that files must match.
Patterns can either be a Unix-style match string,
or a Python callable which returns ``True`` if the file matches
exclude
A list of patterns that files cannot match (and will skip).
Patterns can either be a Unix-style match string,
or a Python callable which returns ``True`` if the file matches.
directories
If true, both files and directories are yielded.
If false, the default, only files are yielded
relative
If true, file paths are relative to the root they were found in.
If false, the default, absolute paths are generated.
with_root
If true, pairs looking like (root, filepath) are generated.
If ``False``, just file paths are generated.
If ``None``, the default, pairs are generated only if there is more than
one root *and* relative paths are selected.
sort
If true, files or subdirectories are generated in sorted order.
If false, the default, files or subdirectories are generated in
whatever order the operating system gives them, which might be
sorted anyway
suffix
If ``None``, the default, there is no suffix matching. Note that
``include`` and ``exclude`` might match suffixes independently.
ignore_missing_roots
If true, root directories that do not exist are silently skipped.
If false, the default, all roots are checked for existence before
any files are generated.
``stroll.python()``
~~~~~~~~~~~~~~~~~~~
.. code-block:: python
stroll.python(
roots,
topdown=True,
onerror=None,
followlinks=False,
include=None,
exclude=(<function dotfile at 0x10c6e47b8>, <function match_root at 0x10c754400>, <function match_suffix at 0x10c754488>, <function match at 0x10c754510>),
directories=False,
relative=False,
with_root=None,
sort=True,
suffix=None,
separator=',',
ignore_missing_roots=False,
)
Iterate over a Python project, skipping generated files
``stroll.python_source()``
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
stroll.python_source(
roots,
topdown=True,
onerror=None,
followlinks=False,
include='*.py',
exclude=(<function dotfile at 0x10c6e47b8>, <function match_root at 0x10c754400>, <function match_suffix at 0x10c754488>, <function match at 0x10c754510>),
directories=False,
relative=False,
with_root=None,
sort=True,
suffix=None,
separator=',',
ignore_missing_roots=False,
)
Iterate over \*.py files in a Python project, skipping generated files
(automatically generated by `doks <https://github.com/rec/doks/>`_ on 2020-11-21T15:09:32.268025)
Raw data
{
"_id": null,
"home_page": "https://github.com/rec/stroll",
"name": "stroll",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "os.walk",
"author": "Tom Ritchford",
"author_email": "tom@swirly.com",
"download_url": "https://files.pythonhosted.org/packages/4d/bd/1b76ba6d1806022f430c1edba3bc74f6651c5fb7b79a0e01569d7e202f38/stroll-1.1.0.tar.gz",
"platform": "",
"description": "\ud83d\udeb6 stroll: a better os.path.walk \ud83d\udeb6\n-------------------------------------\n\n``stroll`` is a drop-in substitute for ``os.path.walk()`` with more features:\n\n* Unix-style globs or \"star notation\" like \\*.py\n\n* Walks over multiple roots\n\n* Calls expanduser to handle paths like ``~/foo.txt``\n\n* Yields ``pathlib.Path()`` instead of ``str``\n\n* Yields full absolute paths by default\n\n* Can exclude or include files flexibly by pattern or function\n\n* Raises ``FileNotFoundError`` if a root directory doesn't exist, instead\n of silently doing nothing like ``os.walk`` does\n\n* Excludes dotfiles by default\n\n* Includes two functions for ignoring generated files in a Python project:\n\n * The Python build, test and release cycle tend to leave generated files in\n places like ``build/`` or ``__pycache__/``, and usually you want to ignore\n these\n\n * ``stroll.python_source()`` iterates over Python source files\n\n * ``stroll.python()`` iterates over all source files in a Python project\n\n * The files and directories that are ignored are:\n * files or directories that start with a ``.``\n * ``.egg-info/`` and ``__pycache__/``\n * ``build/``, ``dist/`` and ``htmlcov/`` at the top level only\n\nAPI\n===\n\n``stroll()``\n~~~~~~~~~~~~\n\n.. code-block:: python\n\n stroll(\n roots='.',\n topdown=True,\n onerror=None,\n followlinks=False,\n include=None,\n exclude=<function dotfile at 0x10c6e47b8>,\n directories=False,\n relative=False,\n with_root=None,\n sort=True,\n suffix=None,\n separator=',',\n ignore_missing_roots=False,\n )\n\n(`stroll.py, 59-228 <https://github.com/rec/stroll/blob/master/stroll.py#L59-L228>`_)\n\nDirectory walker that improves on ``os.walk()``.\n\nFor each directory in ``roots``, walk through each file in each\nsubdirectory and yield a Path to that file. Ignores dotfiles by default.\n\nEXAMPLE\n\n.. code-block:: python\n\n import stroll\n\n for f in stroll('~/foo:~/bar'):\n if f.suffix == '.txt':\n print(f)\n\n for f in stroll.python_source('/code/project'):\n assert f.suffix == '.py'\n\nARGUMENTS\n roots\n Either a list or tuple of strings, or a single string that is split\n using ``separator`` (defaults to ``,``, the comma).\n\n topdown (argument to ``os.walk``)\n If optional arg ``topdown`` is true or not specified, the ``Path`` to a\n directory is generated before any of its subdirectories - directories\n are generated top-down.\n\n If ``topdown`` is false, the Path to a directory is generated after all\n of its subdirectories - directories are generated bottom up.\n\n onerror (argument to ``os.walk``)\n By default errors from the ``os.scandir()`` call are ignored. If\n optional arg ``onerror`` is specified, it should be a function; it\n will be called with one argument, an OSError instance. It can\n report the error to continue with the walk, or raise the exception\n to abort the walk. Note that the filename is available as the\n filename attribute of the exception object.\n\n followlinks (argument to ``os.walk``)\n By default, ``os.walk()`` does not follow symbolic links to\n subdirectories on systems that support them. In order to get this\n functionality, set the optional argument ``followlinks`` to true.\n\n Caution: if you pass a relative pathname for top, don't change the\n current working directory between resumptions of walk. ``os.walk()``\n never changes the current directory, and assumes that the client\n doesn't either.\n\n include\n A list of patterns that files must match.\n\n Patterns can either be a Unix-style match string,\n or a Python callable which returns ``True`` if the file matches\n\n exclude\n A list of patterns that files cannot match (and will skip).\n\n Patterns can either be a Unix-style match string,\n or a Python callable which returns ``True`` if the file matches.\n\n directories\n If true, both files and directories are yielded.\n If false, the default, only files are yielded\n\n relative\n If true, file paths are relative to the root they were found in.\n If false, the default, absolute paths are generated.\n\n with_root\n If true, pairs looking like (root, filepath) are generated.\n If ``False``, just file paths are generated.\n If ``None``, the default, pairs are generated only if there is more than\n one root *and* relative paths are selected.\n\n sort\n If true, files or subdirectories are generated in sorted order.\n If false, the default, files or subdirectories are generated in\n whatever order the operating system gives them, which might be\n sorted anyway\n\n suffix\n If ``None``, the default, there is no suffix matching. Note that\n ``include`` and ``exclude`` might match suffixes independently.\n\n ignore_missing_roots\n If true, root directories that do not exist are silently skipped.\n If false, the default, all roots are checked for existence before\n any files are generated.\n\n``stroll.python()``\n~~~~~~~~~~~~~~~~~~~\n\n.. code-block:: python\n\n stroll.python(\n roots,\n topdown=True,\n onerror=None,\n followlinks=False,\n include=None,\n exclude=(<function dotfile at 0x10c6e47b8>, <function match_root at 0x10c754400>, <function match_suffix at 0x10c754488>, <function match at 0x10c754510>),\n directories=False,\n relative=False,\n with_root=None,\n sort=True,\n suffix=None,\n separator=',',\n ignore_missing_roots=False,\n )\n\nIterate over a Python project, skipping generated files\n\n``stroll.python_source()``\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code-block:: python\n\n stroll.python_source(\n roots,\n topdown=True,\n onerror=None,\n followlinks=False,\n include='*.py',\n exclude=(<function dotfile at 0x10c6e47b8>, <function match_root at 0x10c754400>, <function match_suffix at 0x10c754488>, <function match at 0x10c754510>),\n directories=False,\n relative=False,\n with_root=None,\n sort=True,\n suffix=None,\n separator=',',\n ignore_missing_roots=False,\n )\n\nIterate over \\*.py files in a Python project, skipping generated files\n\n(automatically generated by `doks <https://github.com/rec/doks/>`_ on 2020-11-21T15:09:32.268025)\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Better os.walk",
"version": "1.1.0",
"split_keywords": [
"os.walk"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "c089d4f22c3dcb5252776f71cf17f727",
"sha256": "9b4c48cc84727f7bb9e0aae1eb3fcfc195ea4d9aaf81cb47dbf9cde6756f3476"
},
"downloads": -1,
"filename": "stroll-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c089d4f22c3dcb5252776f71cf17f727",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7749,
"upload_time": "2021-08-17T15:41:30",
"upload_time_iso_8601": "2021-08-17T15:41:30.037989Z",
"url": "https://files.pythonhosted.org/packages/89/27/bb7228da50750113278f4aa1be720584968f72026a2356d46be98e0d5459/stroll-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "79bb4c1e5114b8398178d2182cedd02f",
"sha256": "778d23c31eaf49415b1c430b3b26b5498a60222e0f9f3338099af64a9e77f361"
},
"downloads": -1,
"filename": "stroll-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "79bb4c1e5114b8398178d2182cedd02f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8288,
"upload_time": "2021-08-17T15:41:31",
"upload_time_iso_8601": "2021-08-17T15:41:31.510013Z",
"url": "https://files.pythonhosted.org/packages/4d/bd/1b76ba6d1806022f430c1edba3bc74f6651c5fb7b79a0e01569d7e202f38/stroll-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-08-17 15:41:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "rec",
"github_project": "stroll",
"travis_ci": true,
"coveralls": true,
"github_actions": false,
"requirements": [
{
"name": "xmod",
"specs": []
}
],
"test_requirements": [
{
"name": "coverage",
"specs": []
},
{
"name": "doks",
"specs": []
},
{
"name": "flake8",
"specs": []
},
{
"name": "pytest",
"specs": []
},
{
"name": "readme-renderer",
"specs": []
},
{
"name": "tdir",
"specs": []
}
],
"tox": true,
"lcname": "stroll"
}