├── .editorconfig ├── .github ├── ISSUE_TEMPLATE.md └── workflows │ └── test.yaml ├── .gitignore ├── .readthedocs.yaml ├── AUTHORS.rst ├── CONTRIBUTING.rst ├── HISTORY.rst ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.rst ├── docs ├── Makefile ├── authors.rst ├── conf.py ├── contributing.rst ├── file_read_backwards.rst ├── history.rst ├── index.rst ├── installation.rst ├── make.bat ├── modules.rst ├── readme.rst └── usage.rst ├── file_read_backwards ├── __init__.py ├── buffer_work_space.py └── file_read_backwards.py ├── requirements_dev.txt ├── setup.cfg ├── setup.py ├── tests ├── __init__.py ├── test_buffer_work_space.py └── test_file_read_backwards.py └── tox.ini /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_style = space 7 | indent_size = 4 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | charset = utf-8 11 | end_of_line = lf 12 | 13 | [*.bat] 14 | indent_style = tab 15 | end_of_line = crlf 16 | 17 | [LICENSE] 18 | insert_final_newline = false 19 | 20 | [Makefile] 21 | indent_style = tab 22 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | * file_read_backwards version: 2 | * Python version: 3 | * Operating System: 4 | 5 | ### Description 6 | 7 | Describe what you were trying to get done. 8 | Tell us what happened, what went wrong, and what you expected to happen. 9 | 10 | ### What I Did 11 | 12 | ``` 13 | Paste the command(s) you ran and the output. 14 | If there was a crash, please include the traceback here. 15 | ``` 16 | -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: Python package 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-22.04 # 22.04 has python 3.7, see https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json 9 | strategy: 10 | matrix: 11 | python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] 12 | 13 | steps: 14 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 15 | - name: Set up Python ${{ matrix.python-version }} 16 | uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5.0 17 | with: 18 | python-version: ${{ matrix.python-version }} 19 | - name: Run test 20 | run: | 21 | pip install -r requirements_dev.txt 22 | make test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | # pyenv python configuration file 62 | .python-version 63 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | # Read the Docs configuration file for Sphinx projects 3 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 4 | version: 2 5 | build: 6 | os: ubuntu-22.04 7 | tools: 8 | python: "3.11" 9 | # Build documentation in the "docs/" directory with Sphinx 10 | sphinx: 11 | configuration: docs/conf.py 12 | -------------------------------------------------------------------------------- /AUTHORS.rst: -------------------------------------------------------------------------------- 1 | ======= 2 | Credits 3 | ======= 4 | 5 | * Robin Robin 6 | * John Leslie 7 | * Samuel Giffard 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.rst: -------------------------------------------------------------------------------- 1 | .. highlight:: shell 2 | 3 | ============ 4 | Contributing 5 | ============ 6 | 7 | Contributions are welcome, and they are greatly appreciated! Every 8 | little bit helps, and credit will always be given. 9 | 10 | You can contribute in many ways: 11 | 12 | Types of Contributions 13 | ---------------------- 14 | 15 | Report Bugs 16 | ~~~~~~~~~~~ 17 | 18 | Report bugs at https://github.com/RobinNil/file_read_backwards/issues. 19 | 20 | If you are reporting a bug, please include: 21 | 22 | * Your operating system name and version. 23 | * Any details about your local setup that might be helpful in troubleshooting. 24 | * Detailed steps to reproduce the bug. 25 | 26 | Fix Bugs 27 | ~~~~~~~~ 28 | 29 | Look through the GitHub issues for bugs. Anything tagged with "bug" 30 | and "help wanted" is open to whoever wants to implement it. 31 | 32 | Implement Features 33 | ~~~~~~~~~~~~~~~~~~ 34 | 35 | Look through the GitHub issues for features. Anything tagged with "enhancement" 36 | and "help wanted" is open to whoever wants to implement it. 37 | 38 | Write Documentation 39 | ~~~~~~~~~~~~~~~~~~~ 40 | 41 | file_read_backwards could always use more documentation, whether as part of the 42 | official file_read_backwards docs, in docstrings, or even on the web in blog posts, 43 | articles, and such. 44 | 45 | Submit Feedback 46 | ~~~~~~~~~~~~~~~ 47 | 48 | The best way to send feedback is to file an issue at https://github.com/robin81/file_read_backwards/issues. 49 | 50 | If you are proposing a feature: 51 | 52 | * Explain in detail how it would work. 53 | * Keep the scope as narrow as possible, to make it easier to implement. 54 | * Remember that this is a volunteer-driven project, and that contributions 55 | are welcome :) 56 | 57 | Get Started! 58 | ------------ 59 | 60 | Ready to contribute? Here's how to set up `file_read_backwards` for local development. 61 | 62 | 1. Fork the `file_read_backwards` repo on GitHub. 63 | 2. Clone your fork locally:: 64 | 65 | $ git clone git@github.com:your_name_here/file_read_backwards.git 66 | 67 | 3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:: 68 | 69 | $ mkvirtualenv file_read_backwards 70 | $ cd file_read_backwards/ 71 | $ python setup.py develop 72 | 73 | 4. Create a branch for local development:: 74 | 75 | $ git checkout -b name-of-your-bugfix-or-feature 76 | 77 | Now you can make your changes locally. 78 | 79 | 5. When you're done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:: 80 | 81 | $ flake8 file_read_backwards tests 82 | $ python setup.py test or py.test 83 | $ tox 84 | 85 | To get flake8 and tox, just pip install them into your virtualenv. 86 | 87 | 6. Commit your changes and push your branch to GitHub:: 88 | 89 | $ git add . 90 | $ git commit -m "Your detailed description of your changes." 91 | $ git push origin name-of-your-bugfix-or-feature 92 | 93 | 7. Submit a pull request through the GitHub website. 94 | 95 | Pull Request Guidelines 96 | ----------------------- 97 | 98 | Before you submit a pull request, check that it meets these guidelines: 99 | 100 | 1. The pull request should include tests. 101 | 2. If the pull request adds functionality, the docs should be updated. Put 102 | your new functionality into a function with a docstring, and add the 103 | feature to the list in README.rst. 104 | 105 | Tips 106 | ---- 107 | 108 | To run a subset of tests:: 109 | 110 | 111 | $ python -m unittest tests.test_file_read_backwards 112 | -------------------------------------------------------------------------------- /HISTORY.rst: -------------------------------------------------------------------------------- 1 | ======= 2 | History 3 | ======= 4 | 5 | 1.0.0 (2016-12-18) 6 | ------------------ 7 | 8 | * First release on PyPI. 9 | 10 | 1.1.0 (2016-12-31) 11 | ------------------ 12 | 13 | * Added support for "latin-1". 14 | * Marked the package "Production/Stable". 15 | 16 | 1.1.1 (2017-01-09) 17 | ------------------ 18 | 19 | * Updated README.rst for more clarity around encoding support and Python 2.7 and 3 support. 20 | 21 | 1.1.2 (2017-01-11) 22 | ------------------ 23 | 24 | * Documentation re-arrangement. Usage examples are now in README.rst 25 | * Minor refactoring 26 | 27 | 1.2.0 (2017-09-01) 28 | ------------------ 29 | 30 | * Include context manager style as it provides cleaner/automatic close functionality 31 | 32 | 1.2.1 (2017-09-02) 33 | ------------------ 34 | 35 | * Made doc strings consistent to Google style and some code linting 36 | 37 | 38 | 1.2.2 (2017-11-19) 39 | ------------------ 40 | 41 | * Re-release of 1.2.1 for ease of updating pypi page for updated travis & pyup. 42 | 43 | 2.0.0 (2018-03-23) 44 | ------------------ 45 | 46 | Mimicing Python file object behavior. 47 | 48 | * FileReadBackwards no longer creates multiple iterators (a change of behavior from 1.x.y version) 49 | * Adding readline() function retuns one line at a time with a trailing new line and empty string when it reaches end of file. 50 | The fine print: the trailing new line will be `os.linesep` (rather than whichever new line type in the file). 51 | 52 | 3.0.0 (2023-03-29) 53 | ------------------ 54 | 55 | * Officially support Python 3.7 - 3.11. 56 | 57 | 3.1.0 (2024-05-02) 58 | ------------------ 59 | 60 | * Officially support Python 3.7 - 3.12 61 | 62 | 3.2.0 (2025-04-21) 63 | ------------------ 64 | 65 | * Officially support Python 3.13. 66 | 67 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | MIT License 3 | 4 | Copyright (c) 2016, Robin Robin 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 | 12 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | 2 | include AUTHORS.rst 3 | 4 | include CONTRIBUTING.rst 5 | include HISTORY.rst 6 | include LICENSE 7 | include README.rst 8 | 9 | recursive-include tests * 10 | recursive-exclude * __pycache__ 11 | recursive-exclude * *.py[co] 12 | 13 | recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif 14 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean clean-test clean-pyc clean-build docs help 2 | .DEFAULT_GOAL := help 3 | define BROWSER_PYSCRIPT 4 | import os, webbrowser, sys 5 | try: 6 | from urllib import pathname2url 7 | except: 8 | from urllib.request import pathname2url 9 | 10 | webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) 11 | endef 12 | export BROWSER_PYSCRIPT 13 | 14 | define PRINT_HELP_PYSCRIPT 15 | import re, sys 16 | 17 | for line in sys.stdin: 18 | match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) 19 | if match: 20 | target, help = match.groups() 21 | print("%-20s %s" % (target, help)) 22 | endef 23 | export PRINT_HELP_PYSCRIPT 24 | BROWSER := python -c "$$BROWSER_PYSCRIPT" 25 | 26 | help: 27 | @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) 28 | 29 | clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts 30 | 31 | 32 | clean-build: ## remove build artifacts 33 | rm -fr build/ 34 | rm -fr dist/ 35 | rm -fr .eggs/ 36 | find . -name '*.egg-info' -exec rm -fr {} + 37 | find . -name '*.egg' -exec rm -f {} + 38 | 39 | clean-pyc: ## remove Python file artifacts 40 | find . -name '*.pyc' -exec rm -f {} + 41 | find . -name '*.pyo' -exec rm -f {} + 42 | find . -name '*~' -exec rm -f {} + 43 | find . -name '__pycache__' -exec rm -fr {} + 44 | 45 | clean-test: ## remove test and coverage artifacts 46 | rm -fr .tox/ 47 | rm -f .coverage 48 | rm -fr htmlcov/ 49 | 50 | lint: ## check style with flake8 51 | flake8 file_read_backwards tests 52 | 53 | test: ## run tests quickly with the default Python 54 | pytest tests 55 | 56 | test-all: ## run tests on every Python version with tox 57 | tox 58 | 59 | coverage: ## check code coverage quickly with the default Python 60 | pytest --cov=file_read_backwards tests 61 | coverage html 62 | $(BROWSER) htmlcov/index.html 63 | 64 | docs: ## generate Sphinx HTML documentation, including API docs 65 | rm -f docs/file_read_backwards.rst 66 | rm -f docs/modules.rst 67 | sphinx-apidoc -o docs/ file_read_backwards 68 | $(MAKE) -C docs clean 69 | $(MAKE) -C docs html 70 | $(BROWSER) docs/_build/html/index.html 71 | 72 | servedocs: docs ## compile the docs watching for changes 73 | watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D . 74 | 75 | release: clean ## package and upload a release 76 | python setup.py sdist upload 77 | python setup.py bdist_wheel upload 78 | 79 | dist: clean ## builds source and wheel package 80 | python setup.py sdist 81 | python setup.py bdist_wheel 82 | ls -l dist 83 | 84 | install: clean ## install the package to the active Python's site-packages 85 | python setup.py install 86 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | =============================== 2 | file_read_backwards 3 | =============================== 4 | 5 | 6 | .. image:: https://img.shields.io/pypi/v/file_read_backwards.svg 7 | :target: https://pypi.python.org/pypi/file_read_backwards 8 | 9 | .. image:: https://readthedocs.org/projects/file-read-backwards/badge/?version=latest 10 | :target: https://file-read-backwards.readthedocs.io/en/latest/?badge=latest 11 | :alt: Documentation Status 12 | 13 | .. image:: https://pyup.io/repos/github/RobinNil/file_read_backwards/shield.svg 14 | :target: https://pyup.io/repos/github/RobinNil/file_read_backwards/ 15 | :alt: Updates 16 | 17 | 18 | Memory efficient way of reading files line-by-line from the end of file 19 | 20 | 21 | * Free software: MIT license 22 | * Documentation: https://file-read-backwards.readthedocs.io. 23 | 24 | 25 | Features 26 | -------- 27 | 28 | This package is for reading file backward line by line as unicode in a memory efficient manner for both Python 2.7 and Python 3. 29 | 30 | It currently supports ascii, latin-1, and utf-8 encodings. 31 | 32 | It supports "\\r", "\\r\\n", and "\\n" as new lines. 33 | 34 | Usage Examples 35 | -------------- 36 | 37 | Another example using `python3.11`:: 38 | 39 | from file_read_backwards import FileReadBackwards 40 | 41 | with FileReadBackwards("/tmp/file", encoding="utf-8") as frb: 42 | 43 | # getting lines by lines starting from the last line up 44 | for l in frb: 45 | print(l) 46 | 47 | 48 | Another way to consume the file is via `readline()`, in `python3.11`:: 49 | 50 | from file_read_backwards import FileReadBackwards 51 | 52 | with FileReadBackwards("/tmp/file", encoding="utf-8") as frb: 53 | 54 | while True: 55 | l = frb.readline() 56 | if not l: 57 | break 58 | print(l, end="") 59 | 60 | Credits 61 | --------- 62 | 63 | This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template. 64 | 65 | .. _Cookiecutter: https://github.com/audreyr/cookiecutter 66 | .. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage 67 | 68 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/file_read_backwards.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/file_read_backwards.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/file_read_backwards" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/file_read_backwards" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 178 | -------------------------------------------------------------------------------- /docs/authors.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../AUTHORS.rst 2 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # file_read_backwards documentation build configuration file, created by 5 | # sphinx-quickstart on Tue Jul 9 22:26:36 2013. 6 | # 7 | # This file is execfile()d with the current directory set to its 8 | # containing dir. 9 | # 10 | # Note that not all possible configuration values are present in this 11 | # autogenerated file. 12 | # 13 | # All configuration values have a default; values that are commented out 14 | # serve to show the default. 15 | 16 | import sys 17 | import os 18 | 19 | # If extensions (or modules to document with autodoc) are in another 20 | # directory, add these directories to sys.path here. If the directory is 21 | # relative to the documentation root, use os.path.abspath to make it 22 | # absolute, like shown here. 23 | #sys.path.insert(0, os.path.abspath('.')) 24 | 25 | # Get the project root dir, which is the parent dir of this 26 | cwd = os.getcwd() 27 | project_root = os.path.dirname(cwd) 28 | 29 | # Insert the project root dir as the first element in the PYTHONPATH. 30 | # This lets us ensure that the source package is imported, and that its 31 | # version is used. 32 | sys.path.insert(0, project_root) 33 | 34 | import file_read_backwards 35 | 36 | # -- General configuration --------------------------------------------- 37 | 38 | # If your documentation needs a minimal Sphinx version, state it here. 39 | #needs_sphinx = '1.0' 40 | 41 | # Add any Sphinx extension module names here, as strings. They can be 42 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 43 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] 44 | 45 | # Add any paths that contain templates here, relative to this directory. 46 | templates_path = ['_templates'] 47 | 48 | # The suffix of source filenames. 49 | source_suffix = '.rst' 50 | 51 | # The encoding of source files. 52 | #source_encoding = 'utf-8-sig' 53 | 54 | # The master toctree document. 55 | master_doc = 'index' 56 | 57 | # General information about the project. 58 | project = u'file_read_backwards' 59 | copyright = u"2016, Robin Robin" 60 | 61 | # The version info for the project you're documenting, acts as replacement 62 | # for |version| and |release|, also used in various other places throughout 63 | # the built documents. 64 | # 65 | # The short X.Y version. 66 | version = file_read_backwards.__version__ 67 | # The full version, including alpha/beta/rc tags. 68 | release = file_read_backwards.__version__ 69 | 70 | # The language for content autogenerated by Sphinx. Refer to documentation 71 | # for a list of supported languages. 72 | #language = None 73 | 74 | # There are two options for replacing |today|: either, you set today to 75 | # some non-false value, then it is used: 76 | #today = '' 77 | # Else, today_fmt is used as the format for a strftime call. 78 | #today_fmt = '%B %d, %Y' 79 | 80 | # List of patterns, relative to source directory, that match files and 81 | # directories to ignore when looking for source files. 82 | exclude_patterns = ['_build'] 83 | 84 | # The reST default role (used for this markup: `text`) to use for all 85 | # documents. 86 | #default_role = None 87 | 88 | # If true, '()' will be appended to :func: etc. cross-reference text. 89 | #add_function_parentheses = True 90 | 91 | # If true, the current module name will be prepended to all description 92 | # unit titles (such as .. function::). 93 | #add_module_names = True 94 | 95 | # If true, sectionauthor and moduleauthor directives will be shown in the 96 | # output. They are ignored by default. 97 | #show_authors = False 98 | 99 | # The name of the Pygments (syntax highlighting) style to use. 100 | pygments_style = 'sphinx' 101 | 102 | # A list of ignored prefixes for module index sorting. 103 | #modindex_common_prefix = [] 104 | 105 | # If true, keep warnings as "system message" paragraphs in the built 106 | # documents. 107 | #keep_warnings = False 108 | 109 | 110 | # -- Options for HTML output ------------------------------------------- 111 | 112 | # The theme to use for HTML and HTML Help pages. See the documentation for 113 | # a list of builtin themes. 114 | html_theme = 'default' 115 | 116 | # Theme options are theme-specific and customize the look and feel of a 117 | # theme further. For a list of options available for each theme, see the 118 | # documentation. 119 | #html_theme_options = {} 120 | 121 | # Add any paths that contain custom themes here, relative to this directory. 122 | #html_theme_path = [] 123 | 124 | # The name for this set of Sphinx documents. If None, it defaults to 125 | # " v documentation". 126 | #html_title = None 127 | 128 | # A shorter title for the navigation bar. Default is the same as 129 | # html_title. 130 | #html_short_title = None 131 | 132 | # The name of an image file (relative to this directory) to place at the 133 | # top of the sidebar. 134 | #html_logo = None 135 | 136 | # The name of an image file (within the static path) to use as favicon 137 | # of the docs. This file should be a Windows icon file (.ico) being 138 | # 16x16 or 32x32 pixels large. 139 | #html_favicon = None 140 | 141 | # Add any paths that contain custom static files (such as style sheets) 142 | # here, relative to this directory. They are copied after the builtin 143 | # static files, so a file named "default.css" will overwrite the builtin 144 | # "default.css". 145 | html_static_path = ['_static'] 146 | 147 | # If not '', a 'Last updated on:' timestamp is inserted at every page 148 | # bottom, using the given strftime format. 149 | #html_last_updated_fmt = '%b %d, %Y' 150 | 151 | # If true, SmartyPants will be used to convert quotes and dashes to 152 | # typographically correct entities. 153 | #html_use_smartypants = True 154 | 155 | # Custom sidebar templates, maps document names to template names. 156 | #html_sidebars = {} 157 | 158 | # Additional templates that should be rendered to pages, maps page names 159 | # to template names. 160 | #html_additional_pages = {} 161 | 162 | # If false, no module index is generated. 163 | #html_domain_indices = True 164 | 165 | # If false, no index is generated. 166 | #html_use_index = True 167 | 168 | # If true, the index is split into individual pages for each letter. 169 | #html_split_index = False 170 | 171 | # If true, links to the reST sources are added to the pages. 172 | #html_show_sourcelink = True 173 | 174 | # If true, "Created using Sphinx" is shown in the HTML footer. 175 | # Default is True. 176 | #html_show_sphinx = True 177 | 178 | # If true, "(C) Copyright ..." is shown in the HTML footer. 179 | # Default is True. 180 | #html_show_copyright = True 181 | 182 | # If true, an OpenSearch description file will be output, and all pages 183 | # will contain a tag referring to it. The value of this option 184 | # must be the base URL from which the finished HTML is served. 185 | #html_use_opensearch = '' 186 | 187 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 188 | #html_file_suffix = None 189 | 190 | # Output file base name for HTML help builder. 191 | htmlhelp_basename = 'file_read_backwardsdoc' 192 | 193 | 194 | # -- Options for LaTeX output ------------------------------------------ 195 | 196 | latex_elements = { 197 | # The paper size ('letterpaper' or 'a4paper'). 198 | #'papersize': 'letterpaper', 199 | 200 | # The font size ('10pt', '11pt' or '12pt'). 201 | #'pointsize': '10pt', 202 | 203 | # Additional stuff for the LaTeX preamble. 204 | #'preamble': '', 205 | } 206 | 207 | # Grouping the document tree into LaTeX files. List of tuples 208 | # (source start file, target name, title, author, documentclass 209 | # [howto/manual]). 210 | latex_documents = [ 211 | ('index', 'file_read_backwards.tex', 212 | u'file_read_backwards Documentation', 213 | u'Robin Robin', 'manual'), 214 | ] 215 | 216 | # The name of an image file (relative to this directory) to place at 217 | # the top of the title page. 218 | #latex_logo = None 219 | 220 | # For "manual" documents, if this is true, then toplevel headings 221 | # are parts, not chapters. 222 | #latex_use_parts = False 223 | 224 | # If true, show page references after internal links. 225 | #latex_show_pagerefs = False 226 | 227 | # If true, show URL addresses after external links. 228 | #latex_show_urls = False 229 | 230 | # Documents to append as an appendix to all manuals. 231 | #latex_appendices = [] 232 | 233 | # If false, no module index is generated. 234 | #latex_domain_indices = True 235 | 236 | 237 | # -- Options for manual page output ------------------------------------ 238 | 239 | # One entry per manual page. List of tuples 240 | # (source start file, name, description, authors, manual section). 241 | man_pages = [ 242 | ('index', 'file_read_backwards', 243 | u'file_read_backwards Documentation', 244 | [u'Robin Robin'], 1) 245 | ] 246 | 247 | # If true, show URL addresses after external links. 248 | #man_show_urls = False 249 | 250 | 251 | # -- Options for Texinfo output ---------------------------------------- 252 | 253 | # Grouping the document tree into Texinfo files. List of tuples 254 | # (source start file, target name, title, author, 255 | # dir menu entry, description, category) 256 | texinfo_documents = [ 257 | ('index', 'file_read_backwards', 258 | u'file_read_backwards Documentation', 259 | u'Robin Robin', 260 | 'file_read_backwards', 261 | 'One line description of project.', 262 | 'Miscellaneous'), 263 | ] 264 | 265 | # Documents to append as an appendix to all manuals. 266 | #texinfo_appendices = [] 267 | 268 | # If false, no module index is generated. 269 | #texinfo_domain_indices = True 270 | 271 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 272 | #texinfo_show_urls = 'footnote' 273 | 274 | # If true, do not generate a @detailmenu in the "Top" node's menu. 275 | #texinfo_no_detailmenu = False 276 | -------------------------------------------------------------------------------- /docs/contributing.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../CONTRIBUTING.rst 2 | -------------------------------------------------------------------------------- /docs/file_read_backwards.rst: -------------------------------------------------------------------------------- 1 | file\_read\_backwards package 2 | ============================= 3 | 4 | Submodules 5 | ---------- 6 | 7 | file\_read\_backwards.buffer\_work\_space module 8 | ------------------------------------------------ 9 | 10 | .. automodule:: file_read_backwards.buffer_work_space 11 | :members: 12 | :undoc-members: 13 | :show-inheritance: 14 | 15 | file\_read\_backwards.file\_read\_backwards module 16 | -------------------------------------------------- 17 | 18 | .. automodule:: file_read_backwards.file_read_backwards 19 | :members: 20 | :undoc-members: 21 | :show-inheritance: 22 | 23 | Module contents 24 | --------------- 25 | 26 | .. automodule:: file_read_backwards 27 | :members: 28 | :undoc-members: 29 | :show-inheritance: 30 | -------------------------------------------------------------------------------- /docs/history.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../HISTORY.rst 2 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | Welcome to file_read_backwards's documentation! 2 | ====================================== 3 | 4 | Contents: 5 | 6 | .. toctree:: 7 | :maxdepth: 2 8 | 9 | readme 10 | installation 11 | usage 12 | contributing 13 | authorshistory 14 | 15 | Indices and tables 16 | ================== 17 | 18 | * :ref:`genindex` 19 | * :ref:`modindex` 20 | * :ref:`search` 21 | -------------------------------------------------------------------------------- /docs/installation.rst: -------------------------------------------------------------------------------- 1 | .. highlight:: shell 2 | 3 | ============ 4 | Installation 5 | ============ 6 | 7 | 8 | Stable release 9 | -------------- 10 | 11 | To install file_read_backwards, run this command in your terminal: 12 | 13 | .. code-block:: console 14 | 15 | $ pip install file_read_backwards 16 | 17 | This is the preferred method to install file_read_backwards, as it will always install the most recent stable release. 18 | 19 | If you don't have `pip`_ installed, this `Python installation guide`_ can guide 20 | you through the process. 21 | 22 | .. _pip: https://pip.pypa.io 23 | .. _Python installation guide: http://docs.python-guide.org/en/latest/starting/installation/ 24 | 25 | 26 | From sources 27 | ------------ 28 | 29 | The sources for file_read_backwards can be downloaded from the `Github repo`_. 30 | 31 | You can either clone the public repository: 32 | 33 | .. code-block:: console 34 | 35 | $ git clone git://github.com/RobinNil/file_read_backwards 36 | 37 | Or download the `tarball`_: 38 | 39 | .. code-block:: console 40 | 41 | $ curl -OL https://github.com/RobinNil/file_read_backwards/tarball/master 42 | 43 | Once you have a copy of the source, you can install it with: 44 | 45 | .. code-block:: console 46 | 47 | $ python setup.py install 48 | 49 | 50 | .. _Github repo: https://github.com/RobinNil/file_read_backwards 51 | .. _tarball: https://github.com/RobinNil/file_read_backwards/tarball/master 52 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | set I18NSPHINXOPTS=%SPHINXOPTS% . 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. xml to make Docutils-native XML files 37 | echo. pseudoxml to make pseudoxml-XML files for display purposes 38 | echo. linkcheck to check all external links for integrity 39 | echo. doctest to run all doctests embedded in the documentation if enabled 40 | goto end 41 | ) 42 | 43 | if "%1" == "clean" ( 44 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 45 | del /q /s %BUILDDIR%\* 46 | goto end 47 | ) 48 | 49 | 50 | %SPHINXBUILD% 2> nul 51 | if errorlevel 9009 ( 52 | echo. 53 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 54 | echo.installed, then set the SPHINXBUILD environment variable to point 55 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 56 | echo.may add the Sphinx directory to PATH. 57 | echo. 58 | echo.If you don't have Sphinx installed, grab it from 59 | echo.http://sphinx-doc.org/ 60 | exit /b 1 61 | ) 62 | 63 | if "%1" == "html" ( 64 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 65 | if errorlevel 1 exit /b 1 66 | echo. 67 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 68 | goto end 69 | ) 70 | 71 | if "%1" == "dirhtml" ( 72 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 73 | if errorlevel 1 exit /b 1 74 | echo. 75 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 76 | goto end 77 | ) 78 | 79 | if "%1" == "singlehtml" ( 80 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 81 | if errorlevel 1 exit /b 1 82 | echo. 83 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 84 | goto end 85 | ) 86 | 87 | if "%1" == "pickle" ( 88 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 89 | if errorlevel 1 exit /b 1 90 | echo. 91 | echo.Build finished; now you can process the pickle files. 92 | goto end 93 | ) 94 | 95 | if "%1" == "json" ( 96 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 97 | if errorlevel 1 exit /b 1 98 | echo. 99 | echo.Build finished; now you can process the JSON files. 100 | goto end 101 | ) 102 | 103 | if "%1" == "htmlhelp" ( 104 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 105 | if errorlevel 1 exit /b 1 106 | echo. 107 | echo.Build finished; now you can run HTML Help Workshop with the ^ 108 | .hhp project file in %BUILDDIR%/htmlhelp. 109 | goto end 110 | ) 111 | 112 | if "%1" == "qthelp" ( 113 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 114 | if errorlevel 1 exit /b 1 115 | echo. 116 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 117 | .qhcp project file in %BUILDDIR%/qthelp, like this: 118 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\file_read_backwards.qhcp 119 | echo.To view the help file: 120 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\file_read_backwards.ghc 121 | goto end 122 | ) 123 | 124 | if "%1" == "devhelp" ( 125 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished. 129 | goto end 130 | ) 131 | 132 | if "%1" == "epub" ( 133 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 134 | if errorlevel 1 exit /b 1 135 | echo. 136 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 137 | goto end 138 | ) 139 | 140 | if "%1" == "latex" ( 141 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 142 | if errorlevel 1 exit /b 1 143 | echo. 144 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 145 | goto end 146 | ) 147 | 148 | if "%1" == "latexpdf" ( 149 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 150 | cd %BUILDDIR%/latex 151 | make all-pdf 152 | cd %BUILDDIR%/.. 153 | echo. 154 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 155 | goto end 156 | ) 157 | 158 | if "%1" == "latexpdfja" ( 159 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 160 | cd %BUILDDIR%/latex 161 | make all-pdf-ja 162 | cd %BUILDDIR%/.. 163 | echo. 164 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 165 | goto end 166 | ) 167 | 168 | if "%1" == "text" ( 169 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 170 | if errorlevel 1 exit /b 1 171 | echo. 172 | echo.Build finished. The text files are in %BUILDDIR%/text. 173 | goto end 174 | ) 175 | 176 | if "%1" == "man" ( 177 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 178 | if errorlevel 1 exit /b 1 179 | echo. 180 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 181 | goto end 182 | ) 183 | 184 | if "%1" == "texinfo" ( 185 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 186 | if errorlevel 1 exit /b 1 187 | echo. 188 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 189 | goto end 190 | ) 191 | 192 | if "%1" == "gettext" ( 193 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 194 | if errorlevel 1 exit /b 1 195 | echo. 196 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 197 | goto end 198 | ) 199 | 200 | if "%1" == "changes" ( 201 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 202 | if errorlevel 1 exit /b 1 203 | echo. 204 | echo.The overview file is in %BUILDDIR%/changes. 205 | goto end 206 | ) 207 | 208 | if "%1" == "linkcheck" ( 209 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 210 | if errorlevel 1 exit /b 1 211 | echo. 212 | echo.Link check complete; look for any errors in the above output ^ 213 | or in %BUILDDIR%/linkcheck/output.txt. 214 | goto end 215 | ) 216 | 217 | if "%1" == "doctest" ( 218 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 219 | if errorlevel 1 exit /b 1 220 | echo. 221 | echo.Testing of doctests in the sources finished, look at the ^ 222 | results in %BUILDDIR%/doctest/output.txt. 223 | goto end 224 | ) 225 | 226 | if "%1" == "xml" ( 227 | %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml 228 | if errorlevel 1 exit /b 1 229 | echo. 230 | echo.Build finished. The XML files are in %BUILDDIR%/xml. 231 | goto end 232 | ) 233 | 234 | if "%1" == "pseudoxml" ( 235 | %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml 236 | if errorlevel 1 exit /b 1 237 | echo. 238 | echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. 239 | goto end 240 | ) 241 | 242 | :end 243 | -------------------------------------------------------------------------------- /docs/modules.rst: -------------------------------------------------------------------------------- 1 | file_read_backwards 2 | =================== 3 | 4 | .. toctree:: 5 | :maxdepth: 4 6 | 7 | file_read_backwards 8 | -------------------------------------------------------------------------------- /docs/readme.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../README.rst 2 | -------------------------------------------------------------------------------- /docs/usage.rst: -------------------------------------------------------------------------------- 1 | ===== 2 | Usage 3 | ===== 4 | 5 | Please see :doc:`readme`. 6 | -------------------------------------------------------------------------------- /file_read_backwards/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from .file_read_backwards import FileReadBackwards # noqa: F401 4 | 5 | __author__ = """Robin Robin""" 6 | __email__ = 'robinsquare42@gmail.com' 7 | __version__ = '3.2.0' 8 | -------------------------------------------------------------------------------- /file_read_backwards/buffer_work_space.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """BufferWorkSpace module.""" 5 | 6 | import os 7 | 8 | new_lines = ["\r\n", "\n", "\r"] 9 | new_lines_bytes = [n.encode("ascii") for n in new_lines] # we only support encodings that's backward compat with ascii 10 | 11 | 12 | class BufferWorkSpace: 13 | 14 | """It is a helper module for FileReadBackwards.""" 15 | 16 | def __init__(self, fp, chunk_size): 17 | """Convention for the data. 18 | 19 | When read_buffer is not None, it represents contents of the file from `read_position` onwards 20 | that has not been processed/returned. 21 | read_position represents the file pointer position that has been read into read_buffer 22 | initialized to be just past the end of file. 23 | """ 24 | self.fp = fp 25 | self.read_position = _get_file_size(self.fp) # set the previously read position to the 26 | self.read_buffer = None 27 | self.chunk_size = chunk_size 28 | 29 | def add_to_buffer(self, content, read_position): 30 | """Add additional bytes content as read from the read_position. 31 | 32 | Args: 33 | content (bytes): data to be added to buffer working BufferWorkSpac. 34 | read_position (int): where in the file pointer the data was read from. 35 | """ 36 | self.read_position = read_position 37 | if self.read_buffer is None: 38 | self.read_buffer = content 39 | else: 40 | self.read_buffer = content + self.read_buffer 41 | 42 | def yieldable(self): 43 | """Return True if there is a line that the buffer can return, False otherwise.""" 44 | if self.read_buffer is None: 45 | return False 46 | 47 | t = _remove_trailing_new_line(self.read_buffer) 48 | n = _find_furthest_new_line(t) 49 | if n >= 0: 50 | return True 51 | 52 | # we have read in entire file and have some unprocessed lines 53 | if self.read_position == 0 and self.read_buffer is not None: 54 | return True 55 | return False 56 | 57 | def return_line(self): 58 | """Return a new line if it is available. 59 | 60 | Precondition: self.yieldable() must be True 61 | """ 62 | assert(self.yieldable()) # noqa: E275 63 | 64 | t = _remove_trailing_new_line(self.read_buffer) 65 | i = _find_furthest_new_line(t) 66 | 67 | if i >= 0: 68 | delimiter = i + 1 69 | after_new_line = slice(delimiter, None) 70 | up_to_include_new_line = slice(0, delimiter) 71 | r = t[after_new_line] 72 | self.read_buffer = t[up_to_include_new_line] 73 | else: # the case where we have read in entire file and at the "last" line 74 | r = t 75 | self.read_buffer = None 76 | return r 77 | 78 | def read_until_yieldable(self): 79 | """Read in additional chunks until it is yieldable.""" 80 | while not self.yieldable(): 81 | read_content, read_position = _get_next_chunk(self.fp, self.read_position, self.chunk_size) 82 | self.add_to_buffer(read_content, read_position) 83 | 84 | def has_returned_every_line(self): 85 | """Return True if every single line in the file has been returned, False otherwise.""" 86 | if self.read_position == 0 and self.read_buffer is None: 87 | return True 88 | return False 89 | 90 | 91 | def _get_file_size(fp): 92 | return os.fstat(fp.fileno()).st_size 93 | 94 | 95 | def _get_next_chunk(fp, previously_read_position, chunk_size): 96 | """Return next chunk of data that we would from the file pointer. 97 | 98 | Args: 99 | fp: file-like object 100 | previously_read_position: file pointer position that we have read from 101 | chunk_size: desired read chunk_size 102 | 103 | Returns: 104 | (bytestring, int): data that has been read in, the file pointer position where the data has been read from 105 | """ 106 | seek_position, read_size = _get_what_to_read_next(fp, previously_read_position, chunk_size) 107 | fp.seek(seek_position) 108 | read_content = fp.read(read_size) 109 | read_position = seek_position 110 | return read_content, read_position 111 | 112 | 113 | def _get_what_to_read_next(fp, previously_read_position, chunk_size): 114 | """Return information on which file pointer position to read from and how many bytes. 115 | 116 | Args: 117 | fp 118 | past_read_positon (int): The file pointer position that has been read previously 119 | chunk_size(int): ideal io chunk_size 120 | 121 | Returns: 122 | (int, int): The next seek position, how many bytes to read next 123 | """ 124 | seek_position = max(previously_read_position - chunk_size, 0) 125 | read_size = chunk_size 126 | 127 | # examples: say, our new_lines are potentially "\r\n", "\n", "\r" 128 | # find a reading point where it is not "\n", rewind further if necessary 129 | # if we have "\r\n" and we read in "\n", 130 | # the next iteration would treat "\r" as a different new line. 131 | # Q: why don't I just check if it is b"\n", but use a function ? 132 | # A: so that we can potentially expand this into generic sets of separators, later on. 133 | while seek_position > 0: 134 | fp.seek(seek_position) 135 | if _is_partially_read_new_line(fp.read(1)): 136 | seek_position -= 1 137 | read_size += 1 # as we rewind further, let's make sure we read more to compensate 138 | else: 139 | break 140 | 141 | # take care of special case when we are back to the beginnin of the file 142 | read_size = min(previously_read_position - seek_position, read_size) 143 | return seek_position, read_size 144 | 145 | 146 | def _remove_trailing_new_line(line): 147 | """Remove a single instance of new line at the end of line if it exists. 148 | 149 | Returns: 150 | bytestring 151 | """ 152 | # replace only 1 instance of newline 153 | # match longest line first (hence the reverse=True), we want to match "\r\n" rather than "\n" if we can 154 | for n in sorted(new_lines_bytes, key=lambda x: len(x), reverse=True): 155 | if line.endswith(n): 156 | remove_new_line = slice(None, -len(n)) 157 | return line[remove_new_line] 158 | return line 159 | 160 | 161 | def _find_furthest_new_line(read_buffer): 162 | """Return -1 if read_buffer does not contain new line otherwise the position of the rightmost newline. 163 | 164 | Args: 165 | read_buffer (bytestring) 166 | 167 | Returns: 168 | int: The right most position of new line character in read_buffer if found, else -1 169 | """ 170 | new_line_positions = [read_buffer.rfind(n) for n in new_lines_bytes] 171 | return max(new_line_positions) 172 | 173 | 174 | def _is_partially_read_new_line(b): 175 | """Return True when b is part of a new line separator found at index >= 1, False otherwise. 176 | 177 | Args: 178 | b (bytestring) 179 | 180 | Returns: 181 | bool 182 | """ 183 | for n in new_lines_bytes: 184 | if n.find(b) >= 1: 185 | return True 186 | return False 187 | -------------------------------------------------------------------------------- /file_read_backwards/file_read_backwards.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """FileReadBackwards module.""" 5 | 6 | import io 7 | import os 8 | 9 | from .buffer_work_space import BufferWorkSpace 10 | 11 | supported_encodings = ["utf-8", "ascii", "latin-1"] # any encodings that are backward compatible with ascii should work 12 | 13 | 14 | class FileReadBackwards: 15 | 16 | """Class definition for `FileReadBackwards`. 17 | 18 | A `FileReadBackwards` will spawn a `FileReadBackwardsIterator` and keep an opened file handler. 19 | 20 | It can be used as a Context Manager. If done so, when exited, it will close its file handler. 21 | 22 | In any mode, `close()` can be called to close the file handler.. 23 | """ 24 | 25 | def __init__(self, path, encoding="utf-8", chunk_size=io.DEFAULT_BUFFER_SIZE): 26 | """Constructor for FileReadBackwards. 27 | 28 | Args: 29 | path: Path to the file to be read 30 | encoding (str): Encoding 31 | chunk_size (int): How many bytes to read at a time 32 | """ 33 | if encoding.lower() not in supported_encodings: 34 | error_message = "{0} encoding was not supported/tested.".format(encoding) 35 | error_message += "Supported encodings are '{0}'".format(",".join(supported_encodings)) 36 | raise NotImplementedError(error_message) 37 | 38 | self.path = path 39 | self.encoding = encoding.lower() 40 | self.chunk_size = chunk_size 41 | self.iterator = FileReadBackwardsIterator(io.open(self.path, mode="rb"), self.encoding, self.chunk_size) 42 | 43 | def __iter__(self): 44 | """Return its iterator.""" 45 | return self.iterator 46 | 47 | def __enter__(self): 48 | return self 49 | 50 | def __exit__(self, exc_type, exc_val, exc_tb): 51 | """Closes all opened its file handler and propagates all exceptions on exit.""" 52 | self.close() 53 | return False 54 | 55 | def close(self): 56 | """Closes all opened it s file handler.""" 57 | self.iterator.close() 58 | 59 | def readline(self): 60 | """Return a line content (with a trailing newline) if there are content. Return '' otherwise.""" 61 | 62 | try: 63 | r = next(self.iterator) + os.linesep 64 | return r 65 | except StopIteration: 66 | return "" 67 | 68 | 69 | class FileReadBackwardsIterator: 70 | """Iterator for `FileReadBackwards`. 71 | 72 | This will read backwards line by line a file. It holds an opened file handler. 73 | """ 74 | def __init__(self, fp, encoding, chunk_size): 75 | """Constructor for FileReadBackwardsIterator 76 | 77 | Args: 78 | fp (File): A file that we wish to start reading backwards from 79 | encoding (str): Encoding of the file 80 | chunk_size (int): How many bytes to read at a time 81 | """ 82 | self.path = fp.name 83 | self.encoding = encoding 84 | self.chunk_size = chunk_size 85 | self.__fp = fp 86 | self.__buf = BufferWorkSpace(self.__fp, self.chunk_size) 87 | 88 | def __iter__(self): 89 | return self 90 | 91 | def next(self): 92 | """Returns unicode string from the last line until the beginning of file. 93 | 94 | Gets exhausted if:: 95 | 96 | * already reached the beginning of the file on previous iteration 97 | * the file got closed 98 | 99 | When it gets exhausted, it closes the file handler. 100 | """ 101 | # Using binary mode, because some encodings such as "utf-8" use variable number of 102 | # bytes to encode different Unicode points. 103 | # Without using binary mode, we would probably need to understand each encoding more 104 | # and do the seek operations to find the proper boundary before issuing read 105 | if self.closed: 106 | raise StopIteration 107 | if self.__buf.has_returned_every_line(): 108 | self.close() 109 | raise StopIteration 110 | self.__buf.read_until_yieldable() 111 | r = self.__buf.return_line() 112 | return r.decode(self.encoding) 113 | 114 | __next__ = next 115 | 116 | @property 117 | def closed(self): 118 | """The status of the file handler. 119 | 120 | :return: True if the file handler is still opened. False otherwise. 121 | """ 122 | return self.__fp.closed 123 | 124 | def close(self): 125 | """Closes the file handler.""" 126 | self.__fp.close() 127 | -------------------------------------------------------------------------------- /requirements_dev.txt: -------------------------------------------------------------------------------- 1 | bumpversion==0.6.0 2 | coverage==7.6.9 3 | cryptography==44.0.1 4 | flake8==5.0.4 # pyup: ignore, latest version does not with python 3.7 5 | PyYAML==6.0.1 6 | pytest==7.4.0 7 | pytest-mock==3.10.0 8 | pytest-cov==4.1.0 9 | Sphinx==4.3.2 # pyup: ignore, latest version does not work with python 3.7 10 | tox==3.28.0 # pyup: ignore, latest version does not work with python 3.7 11 | watchdog==3.0.0 12 | wheel==0.45.1 13 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [bumpversion] 2 | current_version = 3.2.0 3 | commit = True 4 | tag = True 5 | 6 | [bumpversion:file:setup.py] 7 | search = version='{current_version}' 8 | replace = version='{new_version}' 9 | 10 | [bumpversion:file:file_read_backwards/__init__.py] 11 | search = __version__ = '{current_version}' 12 | replace = __version__ = '{new_version}' 13 | 14 | [bdist_wheel] 15 | universal = 1 16 | 17 | [flake8] 18 | exclude = docs 19 | max-line-length = 120 20 | ignore = D100 21 | 22 | [tool:pytest] 23 | testpaths = tests 24 | addopts = --verbose 25 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from setuptools import setup 5 | 6 | with open('README.rst') as readme_file: 7 | readme = readme_file.read() 8 | 9 | with open('HISTORY.rst') as history_file: 10 | history = history_file.read() 11 | 12 | requirements = [ 13 | ] 14 | 15 | test_requirements = [ 16 | "mock", 17 | ] 18 | 19 | setup( 20 | name='file_read_backwards', 21 | version='3.2.0', 22 | description="Memory efficient way of reading files line-by-line from the end of file", 23 | long_description=readme + '\n\n' + history, 24 | author="Robin Robin", 25 | author_email='robinsquare42@gmail.com', 26 | url='https://github.com/RobinNil/file_read_backwards', 27 | packages=[ 28 | 'file_read_backwards', 29 | ], 30 | package_dir={'file_read_backwards': 31 | 'file_read_backwards'}, 32 | include_package_data=True, 33 | install_requires=requirements, 34 | license="MIT", 35 | zip_safe=False, 36 | keywords='file_read_backwards', 37 | classifiers=[ 38 | 'Development Status :: 5 - Production/Stable', 39 | 'Intended Audience :: Developers', 40 | 'Natural Language :: English', 41 | 'Programming Language :: Python :: 3', 42 | 'Programming Language :: Python :: 3.9', 43 | 'Programming Language :: Python :: 3.10', 44 | 'Programming Language :: Python :: 3.11', 45 | 'Programming Language :: Python :: 3.12', 46 | 'Programming Language :: Python :: 3.13', 47 | ], 48 | test_suite='tests', 49 | tests_require=test_requirements 50 | ) 51 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | -------------------------------------------------------------------------------- /tests/test_buffer_work_space.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """Tests for `buffer_work_space` module.""" 4 | 5 | import io 6 | import os 7 | import tempfile 8 | import pytest 9 | from pytest_mock import MockerFixture 10 | from file_read_backwards.buffer_work_space import BufferWorkSpace 11 | from file_read_backwards.buffer_work_space import new_lines_bytes 12 | from file_read_backwards.buffer_work_space import _find_furthest_new_line 13 | from file_read_backwards.buffer_work_space import _remove_trailing_new_line 14 | from file_read_backwards.buffer_work_space import _get_file_size 15 | from file_read_backwards.buffer_work_space import _is_partially_read_new_line 16 | from file_read_backwards.buffer_work_space import _get_what_to_read_next 17 | from file_read_backwards.buffer_work_space import _get_next_chunk 18 | 19 | 20 | class TestFindFurthestNewLine: 21 | def test_find_furthest_new_line_with_no_new_line_in_empty_byte_string(self): 22 | test_string = b"" 23 | r = _find_furthest_new_line(test_string) 24 | assert r == -1 25 | 26 | def test_find_furthest_new_line_with_no_new_line_in_non_empty_byte_string(self): 27 | test_string = b"SomeRandomCharacters" 28 | r = _find_furthest_new_line(test_string) 29 | assert r == -1 30 | 31 | def test_find_furthest_new_line_with_bytestring_with_new_line_at_the_end(self): 32 | base_string = b"SomeRandomCharacters" 33 | for n in new_lines_bytes: 34 | test_string = base_string + n 35 | expected_value = len(test_string) - 1 36 | r = _find_furthest_new_line(test_string) 37 | assert r == expected_value 38 | 39 | def test_find_furthest_new_line_with_bytestring_with_new_line_in_the_middle(self): 40 | base_string = b"SomeRandomCharacters" 41 | for n in new_lines_bytes: 42 | test_string = base_string + n + base_string 43 | expected_value = len(base_string) + len(n) - 1 44 | r = _find_furthest_new_line(test_string) 45 | assert r == expected_value 46 | 47 | def test_find_furthest_new_line_with_bytestring_with_new_line_in_the_middle_and_end(self): 48 | base_string = b"SomeRandomCharacters" 49 | for n in new_lines_bytes: 50 | test_string = base_string + n + base_string + n 51 | expected_value = len(test_string) - 1 52 | r = _find_furthest_new_line(test_string) 53 | assert r == expected_value 54 | 55 | 56 | class TestRemoveTrailingNewLine: 57 | def test_remove_trailing_new_line_with_empty_byte_string(self): 58 | test_string = b"" 59 | expected_string = test_string 60 | r = _remove_trailing_new_line(test_string) 61 | assert r == expected_string 62 | 63 | def test_remove_trailing_new_line_with_non_empty_byte_string_with_no_new_line(self): 64 | test_string = b"Something" 65 | expected_string = test_string 66 | r = _remove_trailing_new_line(test_string) 67 | assert r == expected_string 68 | 69 | def test_remove_trailing_new_line_with_non_empty_byte_string_with_variety_of_new_lines(self): 70 | expected_str = b"Something" 71 | for n in new_lines_bytes: 72 | test_string = expected_str + n 73 | r = _remove_trailing_new_line(test_string) 74 | assert r == expected_str 75 | 76 | def test_remove_trailing_new_line_with_non_empty_byte_string_with_variety_of_new_lines_in_the_middle(self): 77 | base_string = b"Something" 78 | for n in new_lines_bytes: 79 | test_string = base_string + n + base_string 80 | expected_string = test_string 81 | r = _remove_trailing_new_line(test_string) 82 | assert r == expected_string 83 | 84 | 85 | class TestGetFileSize: 86 | def test_empty_file(self): 87 | with tempfile.NamedTemporaryFile(delete=False) as t: 88 | pass 89 | expected_value = 0 90 | with io.open(t.name, mode="rb") as fp: 91 | r = _get_file_size(fp) 92 | assert r == expected_value 93 | os.unlink(t.name) 94 | 95 | def test_file_with_eight_bytes(self): 96 | with tempfile.NamedTemporaryFile(delete=False) as t: 97 | t.write(b"a" * 8) 98 | expected_value = 8 99 | with io.open(t.name, mode="rb") as fp: 100 | r = _get_file_size(fp) 101 | assert r == expected_value 102 | os.unlink(t.name) 103 | 104 | 105 | class TestIsPartiallyReadNewLine: 106 | def test_when_we_have_a_partially_read_new_line(self): 107 | for n in new_lines_bytes: 108 | if len(n) > 1: 109 | b = n[-1] 110 | r = _is_partially_read_new_line(b) 111 | assert r 112 | 113 | 114 | class TestGetWhatToReadNext: 115 | def test_with_empty_file(self): 116 | with tempfile.NamedTemporaryFile(delete=False) as t: 117 | pass 118 | expected_result = (0, 0) 119 | with io.open(t.name, mode="rb") as fp: 120 | r = _get_what_to_read_next(fp, previously_read_position=0, chunk_size=3) 121 | assert r == expected_result 122 | os.unlink(t.name) 123 | 124 | def test_with_file_with_seven_bytes_of_alphanumeric(self): 125 | with tempfile.NamedTemporaryFile(delete=False) as t: 126 | t.write(b"abcdefg") 127 | expected_result = (4, 3) 128 | with io.open(t.name, mode="rb") as fp: 129 | r = _get_what_to_read_next(fp, previously_read_position=7, chunk_size=3) 130 | assert r == expected_result 131 | os.unlink(t.name) 132 | 133 | def test_with_file_with_single_new_line(self): 134 | for n in new_lines_bytes: 135 | with tempfile.NamedTemporaryFile(delete=False) as t: 136 | t.write(n) 137 | expected_result = (0, len(n)) 138 | chunk_size = len(n) + 1 139 | with io.open(t.name, mode="rb") as fp: 140 | r = _get_what_to_read_next(fp, previously_read_position=len(n), chunk_size=chunk_size) 141 | assert r == expected_result 142 | os.unlink(t.name) 143 | 144 | def test_with_file_where_we_need_to_read_more_than_chunk_size(self): 145 | with tempfile.NamedTemporaryFile(delete=False) as t: 146 | t.write(b"abcd\nfg") 147 | expected_result = (3, 4) 148 | with io.open(t.name, mode="rb") as fp: 149 | r = _get_what_to_read_next(fp, previously_read_position=7, chunk_size=3) 150 | assert r == expected_result 151 | os.unlink(t.name) 152 | 153 | 154 | class TestGetNextChunk: 155 | def test_with_empty_file(self): 156 | with tempfile.NamedTemporaryFile(delete=False) as t: 157 | pass 158 | expected_result = (b"", 0) 159 | with io.open(t.name, mode="rb") as fp: 160 | r = _get_next_chunk(fp, previously_read_position=0, chunk_size=3) 161 | assert r == expected_result 162 | os.unlink(t.name) 163 | 164 | def test_with_non_empty_file(self): 165 | with tempfile.NamedTemporaryFile(delete=False) as t: 166 | t.write(b"abcdefg") 167 | expected_result = (b"efg", 4) 168 | with io.open(t.name, mode="rb") as fp: 169 | r = _get_next_chunk(fp, previously_read_position=7, chunk_size=3) 170 | assert r == expected_result 171 | os.unlink(t.name) 172 | 173 | def test_with_non_empty_file_where_we_read_more_than_chunk_size(self): 174 | with tempfile.NamedTemporaryFile(delete=False) as t: 175 | t.write(b"abcd\nfg") 176 | expected_result = (b"d\nfg", 3) 177 | with io.open(t.name, mode="rb") as fp: 178 | r = _get_next_chunk(fp, previously_read_position=7, chunk_size=3) 179 | assert r == expected_result 180 | os.unlink(t.name) 181 | 182 | 183 | class TestBufferWorkSpace: 184 | def test_add_to_empty_buffer_work_space(self, mocker: MockerFixture): 185 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 186 | fp_mock = mocker.Mock() 187 | _get_file_size_mock.return_value = 1024 188 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 189 | b.add_to_buffer(content=b"aaa", read_position=1021) 190 | assert b.read_buffer == b"aaa" 191 | assert b.read_position == 1021 192 | 193 | def test_add_to_non_empty_buffer_work_space(self, mocker: MockerFixture): 194 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 195 | fp_mock = mocker.Mock() 196 | _get_file_size_mock.return_value = 1024 197 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 198 | b.add_to_buffer(content=b"aaa", read_position=1021) 199 | b.add_to_buffer(content=b"bbb", read_position=1018) 200 | assert b.read_buffer == b"bbbaaa" 201 | assert b.read_position == 1018 202 | 203 | def test_yieldable_for_new_initialized_buffer_work_space(self): 204 | with tempfile.NamedTemporaryFile(delete=False) as t: 205 | with io.open(t.name, mode="rb") as fp: 206 | b = BufferWorkSpace(fp, chunk_size=io.DEFAULT_BUFFER_SIZE) 207 | r = b.yieldable() 208 | assert not r 209 | os.unlink(t.name) 210 | 211 | def test_yieldable_for_unexhausted_buffer_space_with_single_new_line(self, mocker: MockerFixture): 212 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 213 | fp_mock = mocker.Mock() 214 | _get_file_size_mock.return_value = 1024 215 | 216 | for n in new_lines_bytes: 217 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 218 | b.read_position = 1024 - len(n) 219 | b.read_buffer = n 220 | expected_result = False 221 | r = b.yieldable() 222 | assert r == expected_result 223 | 224 | def test_yieldable_for_buffer_space_with_two_new_lines(self, mocker: MockerFixture): 225 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 226 | fp_mock = mocker.Mock() 227 | _get_file_size_mock.return_value = 1024 228 | 229 | for n in new_lines_bytes: 230 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 231 | b.read_position = 1024 - (len(n) * 2) 232 | b.read_buffer = n * 2 233 | expected_result = True 234 | r = b.yieldable() 235 | assert r == expected_result 236 | 237 | def test_yieldable_for_fully_read_with_unreturned_contents_in_buffer_space(self, mocker: MockerFixture): 238 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 239 | fp_mock = mocker.Mock() 240 | _get_file_size_mock.return_value = 1024 241 | 242 | for n in new_lines_bytes: 243 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 244 | b.read_position = 0 245 | b.read_buffer = b"" 246 | expected_result = True 247 | r = b.yieldable() 248 | assert r == expected_result 249 | 250 | def test_yieldable_for_fully_read_and_returned_contents_in_buffer_space(self, mocker: MockerFixture): 251 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 252 | fp_mock = mocker.Mock() 253 | _get_file_size_mock.return_value = 1024 254 | 255 | for n in new_lines_bytes: 256 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 257 | b.read_position = 0 258 | b.read_buffer = None 259 | r = b.yieldable() 260 | assert not r 261 | 262 | def test_return_line_with_buffer_space_with_two_new_lines(self, mocker: MockerFixture): 263 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 264 | fp_mock = mocker.Mock() 265 | _get_file_size_mock.return_value = 1024 266 | 267 | for n in new_lines_bytes: 268 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 269 | b.read_position = 1024 - (len(n) * 2) 270 | b.read_buffer = n * 2 271 | expected_result = b"" 272 | r = b.return_line() 273 | assert r == expected_result 274 | 275 | def test_return_line_with_buffer_space_with_some_contents_between_two_new_lines(self, mocker: MockerFixture): 276 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 277 | fp_mock = mocker.Mock() 278 | _get_file_size_mock.return_value = 1024 279 | 280 | for n in new_lines_bytes: 281 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 282 | b.read_position = 1024 - (len(n) * 2) 283 | b.read_buffer = n + b"Something" + n 284 | expected_result = b"Something" 285 | r = b.return_line() 286 | assert r == expected_result 287 | 288 | def test_return_line_with_buffer_space_with_fully_read_in_contents_at_its_last_line(self, mocker: MockerFixture): 289 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 290 | fp_mock = mocker.Mock() 291 | _get_file_size_mock.return_value = 1024 292 | 293 | for n in new_lines_bytes: 294 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 295 | b.read_position = 0 296 | b.read_buffer = b"LastLineYay" 297 | expected_result = b"LastLineYay" 298 | r = b.return_line() 299 | assert r == expected_result 300 | 301 | def test_return_line_contract_violation(self, mocker: MockerFixture): 302 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 303 | fp_mock = mocker.Mock() 304 | _get_file_size_mock.return_value = 0 305 | 306 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 307 | with pytest.raises(AssertionError): 308 | b.return_line() 309 | 310 | def test_has_returned_every_line_empty_file(self, mocker: MockerFixture): 311 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 312 | fp_mock = mocker.Mock() 313 | _get_file_size_mock.return_value = 0 314 | 315 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 316 | r = b.has_returned_every_line() 317 | assert r 318 | 319 | def test_has_returned_every_line_with_not_fully_read_in_buffer_space(self, mocker: MockerFixture): 320 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 321 | fp_mock = mocker.Mock() 322 | _get_file_size_mock.return_value = 1024 323 | 324 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 325 | b.read_position = 1 326 | r = b.has_returned_every_line() 327 | assert not r 328 | 329 | def test_has_returned_every_line_with_fully_read_in_and_unprocessed_buffer_space(self, mocker: MockerFixture): 330 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 331 | fp_mock = mocker.Mock() 332 | _get_file_size_mock.return_value = 1024 333 | 334 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 335 | b.read_position = 0 336 | b.read_buffer = b"abc" 337 | r = b.has_returned_every_line() 338 | assert not r 339 | 340 | def test_has_returned_every_line_with_fully_read_in_and_processed_buffer_space(self, mocker: MockerFixture): 341 | _get_file_size_mock = mocker.patch("file_read_backwards.buffer_work_space._get_file_size") 342 | fp_mock = mocker.Mock() 343 | _get_file_size_mock.return_value = 1024 344 | 345 | b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) 346 | b.read_position = 0 347 | b.read_buffer = None 348 | r = b.has_returned_every_line() 349 | assert r 350 | -------------------------------------------------------------------------------- /tests/test_file_read_backwards.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """Tests for `file_read_backwards` module.""" 4 | 5 | import itertools 6 | import os 7 | import tempfile 8 | import pytest 9 | 10 | from collections import deque 11 | 12 | from file_read_backwards.file_read_backwards import FileReadBackwards 13 | from file_read_backwards.file_read_backwards import supported_encodings 14 | from file_read_backwards.buffer_work_space import new_lines 15 | 16 | 17 | # doing this xrange/range dance so that we don't need to add additional dependencies of future or six modules 18 | try: 19 | xrange 20 | except NameError: 21 | xrange = range 22 | 23 | 24 | created_files = set() 25 | 26 | 27 | def helper_write(t, s, encoding="utf-8"): 28 | """A helper method to write out string s in specified encoding.""" 29 | t.write(s.encode(encoding)) 30 | 31 | 32 | def helper_create_temp_file(generator=None, encoding='utf-8'): 33 | global created_files 34 | if generator is None: 35 | generator = ("line {}!\n".format(i) for i in xrange(42)) 36 | temp_file = tempfile.NamedTemporaryFile(delete=False) 37 | for line in generator: 38 | helper_write(temp_file, line, encoding) 39 | temp_file.close() 40 | print('Wrote file {}'.format(temp_file.name)) 41 | created_files.add(temp_file) 42 | return temp_file 43 | 44 | 45 | def helper_destroy_temp_file(temp_file): 46 | temp_file.close() 47 | os.unlink(temp_file.name) 48 | 49 | 50 | def helper_destroy_temp_files(): 51 | global created_files 52 | while created_files: 53 | helper_destroy_temp_file(created_files.pop()) 54 | 55 | 56 | @pytest.fixture(scope="module") 57 | def empty_file(): 58 | return helper_create_temp_file(generator=(_ for _ in [])) 59 | 60 | 61 | @pytest.fixture(scope="module") 62 | def long_file(): 63 | return helper_create_temp_file() 64 | 65 | 66 | @pytest.fixture(scope="module", autouse=True) 67 | def cleanup_files(): 68 | yield 69 | helper_destroy_temp_files() 70 | 71 | 72 | class TestFileReadBackwards: 73 | def test_with_completely_empty_file(self, empty_file): 74 | f = FileReadBackwards(empty_file.name) 75 | expected_lines = deque() 76 | lines_read = deque() 77 | for line in f: 78 | lines_read.appendleft(line) 79 | assert expected_lines == lines_read 80 | 81 | def test_file_with_a_single_new_line_char_with_different_encodings(self): 82 | for encoding, new_line in itertools.product(supported_encodings, new_lines): 83 | temp_file = helper_create_temp_file((line for line in [new_line]), encoding=encoding) 84 | f = FileReadBackwards(temp_file.name) 85 | expected_lines = deque([""]) 86 | lines_read = deque() 87 | for line in f: 88 | lines_read.appendleft(line) 89 | assert expected_lines == lines_read 90 | 91 | def test_file_with_one_line_of_text_with_accented_char_followed_by_a_new_line(self): 92 | b = b'Caf\xc3\xa9' # accented e in utf-8 93 | s = b.decode("utf-8") 94 | for new_line in new_lines: 95 | temp_file = helper_create_temp_file((line for line in [s, new_line])) 96 | f = FileReadBackwards(temp_file.name) 97 | expected_lines = deque([s]) 98 | lines_read = deque() 99 | for line in f: 100 | lines_read.appendleft(s) 101 | assert expected_lines == lines_read 102 | 103 | def test_file_with_one_line_of_text_followed_by_a_new_line_with_different_encodings(self): 104 | for encoding, new_line in itertools.product(supported_encodings, new_lines): 105 | temp_file = helper_create_temp_file((line for line in ["something{0}".format(new_line)]), encoding=encoding) 106 | f = FileReadBackwards(temp_file.name) 107 | expected_lines = deque(["something"]) 108 | lines_read = deque() 109 | for line in f: 110 | lines_read.appendleft(line) 111 | assert expected_lines == lines_read 112 | 113 | def test_file_with_varying_number_of_new_lines_and_some_text_in_chunk_size(self): 114 | chunk_size = 3 115 | s = "t" 116 | for number_of_new_lines in xrange(21): 117 | for new_line in new_lines: 118 | temp_file = helper_create_temp_file((line for line in [new_line * number_of_new_lines, s * chunk_size])) 119 | f = FileReadBackwards(temp_file.name, chunk_size=chunk_size) 120 | expected_lines = deque() 121 | for _ in xrange(number_of_new_lines): 122 | expected_lines.append("") 123 | expected_lines.append(s * chunk_size) 124 | lines_read = deque() 125 | for line in f: 126 | lines_read.appendleft(line) 127 | assert expected_lines == lines_read 128 | 129 | def test_file_with_new_lines_and_some_accented_characters_in_chunk_size(self): 130 | chunk_size = 3 131 | b = b'\xc3\xa9' 132 | s = b.decode("utf-8") 133 | for number_of_new_lines in xrange(21): 134 | for new_line in new_lines: 135 | temp_file = helper_create_temp_file((line for line in [new_line * number_of_new_lines, s * chunk_size])) 136 | f = FileReadBackwards(temp_file.name, chunk_size=chunk_size) 137 | expected_lines = deque() 138 | for _ in xrange(number_of_new_lines): 139 | expected_lines.append("") 140 | expected_lines.append(s * chunk_size) 141 | lines_read = deque() 142 | for line in f: 143 | lines_read.appendleft(line) 144 | assert expected_lines == lines_read 145 | 146 | def test_unsupported_encoding(self, empty_file): 147 | with pytest.raises(NotImplementedError): 148 | _ = FileReadBackwards(empty_file.name, encoding="not-supported-encoding") 149 | 150 | def test_file_with_one_line_of_text_readline(self): 151 | s = "Line0" 152 | for new_line in new_lines: 153 | temp_file = helper_create_temp_file((line for line in [s, new_line])) 154 | with FileReadBackwards(temp_file.name) as fp: 155 | line = fp.readline() 156 | expected_line = s + os.linesep 157 | assert line == expected_line 158 | 159 | second_line = fp.readline() 160 | expected_second_line = "" 161 | assert second_line == expected_second_line 162 | 163 | def test_file_with_two_lines_of_text_readline(self): 164 | line0 = "Line0" 165 | line1 = "Line1" 166 | for new_line in new_lines: 167 | line0_with_n = "{}{}".format(line0, new_line) 168 | line1_with_n = "{}{}".format(line1, new_line) 169 | temp_file = helper_create_temp_file((line for line in [line0_with_n, line1_with_n])) 170 | with FileReadBackwards(temp_file.name) as fp: 171 | line = fp.readline() 172 | expected_line = line1 + os.linesep 173 | assert line == expected_line 174 | 175 | second_line = fp.readline() 176 | expected_second_line = line0 + os.linesep 177 | assert second_line == expected_second_line 178 | 179 | third_line = fp.readline() 180 | expected_third_line = "" 181 | assert third_line == expected_third_line 182 | 183 | 184 | class TestFileReadBackwardsAsContextManager: 185 | @pytest.fixture(scope="class", autouse=True) 186 | def temp_file(self): 187 | return helper_create_temp_file() 188 | 189 | def test_behaves_as_classic(self, temp_file): 190 | with FileReadBackwards(temp_file.name) as f: 191 | lines_read = deque() 192 | for line in f: 193 | lines_read.appendleft(line) 194 | f2 = FileReadBackwards(temp_file.name) 195 | lines_read2 = deque() 196 | for l2 in f2: 197 | lines_read2.appendleft(l2) 198 | assert lines_read == lines_read2 199 | 200 | 201 | class TestFileReadBackwardsCloseFunctionality: 202 | @pytest.fixture(scope="class", autouse=True) 203 | def temp_file(self): 204 | return helper_create_temp_file() 205 | 206 | def test_close_on_iterator(self, temp_file): 207 | with FileReadBackwards(temp_file.name) as f: 208 | it = iter(f) 209 | for count, i in enumerate(it): 210 | if count == 2: 211 | break 212 | assert not it.closed 213 | it.close() 214 | assert it.closed 215 | 216 | def test_not_creating_new_iterator(self, temp_file): 217 | with FileReadBackwards(temp_file.name) as f: 218 | it1 = iter(f) 219 | it2 = iter(f) 220 | assert it1 is it2 221 | 222 | def test_close_on_iterator_exhausted(self, temp_file): 223 | with FileReadBackwards(temp_file.name) as f: 224 | it = iter(f) 225 | for _ in it: 226 | pass 227 | assert it.closed 228 | 229 | def test_close_on_reader_exit(self, temp_file): 230 | with FileReadBackwards(temp_file.name) as f: 231 | it = iter(f) 232 | assert it.closed 233 | 234 | def test_close_on_reader_explicitly(self, temp_file): 235 | f = FileReadBackwards(temp_file.name) 236 | it = iter(f) 237 | assert not it.closed 238 | f.close() 239 | assert it.closed 240 | 241 | def test_close_on_reader_with_already_closed_iterator(self, temp_file): 242 | with FileReadBackwards(temp_file.name) as f: 243 | it = iter(f) 244 | it.close() 245 | assert it.closed 246 | 247 | def test_cannot_iterate_when_closed(self, temp_file): 248 | with FileReadBackwards(temp_file.name) as f: 249 | it = iter(f) 250 | it.close() 251 | for _ in it: 252 | pytest.fail("An iterator should be exhausted when closed.") 253 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py39, py310, py311, py312, py313, flake8 3 | 4 | [testenv:flake8] 5 | basepython=python 6 | deps=flake8 7 | commands=flake8 file_read_backwards 8 | 9 | [testenv] 10 | setenv = 11 | PYTHONPATH = {toxinidir}:{toxinidir}/file_read_backwards 12 | 13 | commands = pytest 14 | 15 | ; If you want to make tox run the tests with the same versions, create a 16 | ; requirements.txt with the pinned versions and uncomment the following lines: 17 | ; deps = 18 | ; -r{toxinidir}/requirements.txt 19 | --------------------------------------------------------------------------------