diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 7738b3aa..00000000 --- a/.flake8 +++ /dev/null @@ -1,4 +0,0 @@ -[flake8] -max_line_length=119 -extend-ignore=F403,F405,E203,E741,F811 -exclude = docs diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6937dc6e..f5474f27 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,16 +1,17 @@ repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: check-added-large-files -- repo: https://github.com/pycqa/flake8 - rev: 4.0.1 - hooks: - - id: flake8 -- repo: https://github.com/psf/black - rev: 19.3b0 - hooks: - - id: black + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.0.1 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - repo: https://github.com/pycqa/flake8 + rev: 4.0.1 + hooks: + - id: flake8 + args: [--config, setup.cfg] + - repo: https://github.com/psf/black + rev: 19.3b0 + hooks: + - id: black diff --git a/_todo_project_name/.editorconfig b/_todo_project_name/.editorconfig new file mode 100644 index 00000000..d4a2c440 --- /dev/null +++ b/_todo_project_name/.editorconfig @@ -0,0 +1,21 @@ +# http://editorconfig.org + +root = true + +[*] +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true +insert_final_newline = true +charset = utf-8 +end_of_line = lf + +[*.bat] +indent_style = tab +end_of_line = crlf + +[LICENSE] +insert_final_newline = false + +[Makefile] +indent_style = tab diff --git a/_todo_project_name/.env.example b/_todo_project_name/.env.example new file mode 100644 index 00000000..8399d229 --- /dev/null +++ b/_todo_project_name/.env.example @@ -0,0 +1,2 @@ +DATA_PATH=data +DATA_RESET=1 diff --git a/_todo_project_name/.github/ISSUE_TEMPLATE.md b/_todo_project_name/.github/ISSUE_TEMPLATE.md new file mode 100644 index 00000000..d70d513a --- /dev/null +++ b/_todo_project_name/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,15 @@ +* Todo Project Name version: +* Python version: +* Operating System: + +### Description + +Describe what you were trying to get done. +Tell us what happened, what went wrong, and what you expected to happen. + +### What I Did + +``` +Paste the command(s) you ran and the output. +If there was a crash, please include the traceback here. +``` diff --git a/_todo_project_name/.github/workflows/ci.yaml b/_todo_project_name/.github/workflows/ci.yaml new file mode 100644 index 00000000..ef99d834 --- /dev/null +++ b/_todo_project_name/.github/workflows/ci.yaml @@ -0,0 +1,46 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Python Github Actions + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.7.5 + uses: actions/setup-python@v2 + with: + python-version: 3.7.5 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e ."[dev]" + - name: Lint + run: | + make lint + - name: Test + run: | + make test + - name: Coverage + run: | + make coverage + - name: Documentation + run: | + make docs + git clone https://github.com/mly32/todo_project_name.git --branch gh-pages --single-branch gh-pages + cp -r docs/_build/html/* gh-pages/ + cd gh-pages + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add . + git commit -m "update documentation" -a || true + - name: Push changes + uses: ad-m/github-push-action@master + with: + branch: gh-pages + directory: gh-pages + github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/_todo_project_name/.gitignore b/_todo_project_name/.gitignore new file mode 100644 index 00000000..4c915d14 --- /dev/null +++ b/_todo_project_name/.gitignore @@ -0,0 +1,106 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# IDE settings +.vscode/ +.idea/ diff --git a/_todo_project_name/.pre-commit-config.yaml b/_todo_project_name/.pre-commit-config.yaml new file mode 100644 index 00000000..f5474f27 --- /dev/null +++ b/_todo_project_name/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.0.1 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - repo: https://github.com/pycqa/flake8 + rev: 4.0.1 + hooks: + - id: flake8 + args: [--config, setup.cfg] + - repo: https://github.com/psf/black + rev: 19.3b0 + hooks: + - id: black diff --git a/_todo_project_name/AUTHORS.rst b/_todo_project_name/AUTHORS.rst new file mode 100644 index 00000000..f9286d00 --- /dev/null +++ b/_todo_project_name/AUTHORS.rst @@ -0,0 +1,13 @@ +======= +Credits +======= + +Development Lead +---------------- + +* Todo Full Name + +Contributors +------------ + +None yet. Why not be the first? diff --git a/_todo_project_name/CONTRIBUTING.rst b/_todo_project_name/CONTRIBUTING.rst new file mode 100644 index 00000000..36e33d23 --- /dev/null +++ b/_todo_project_name/CONTRIBUTING.rst @@ -0,0 +1,126 @@ +.. highlight:: shell + +============ +Contributing +============ + +Contributions are welcome, and they are greatly appreciated! Every little bit +helps, and credit will always be given. + +You can contribute in many ways: + +Types of Contributions +---------------------- + +Report Bugs +~~~~~~~~~~~ + +Report bugs at https://github.com/todo_github_username/todo_project_name/issues. + +If you are reporting a bug, please include: + +* Your operating system name and version. +* Any details about your local setup that might be helpful in troubleshooting. +* Detailed steps to reproduce the bug. + +Fix Bugs +~~~~~~~~ + +Look through the GitHub issues for bugs. Anything tagged with "bug" and "help +wanted" is open to whoever wants to implement it. + +Implement Features +~~~~~~~~~~~~~~~~~~ + +Look through the GitHub issues for features. Anything tagged with "enhancement" +and "help wanted" is open to whoever wants to implement it. + +Write Documentation +~~~~~~~~~~~~~~~~~~~ + +Todo Project Name could always use more documentation, whether as part of the +official Todo Project Name docs, in docstrings, or even on the web in blog posts, +articles, and such. + +Submit Feedback +~~~~~~~~~~~~~~~ + +The best way to send feedback is to file an issue at https://github.com/todo_github_username/todo_project_name/issues. + +If you are proposing a feature: + +* Explain in detail how it would work. +* Keep the scope as narrow as possible, to make it easier to implement. +* Remember that this is a volunteer-driven project, and that contributions + are welcome :) + +Get Started! +------------ + +Ready to contribute? Here's how to set up `todo_project_name` for local development. + +1. Fork the `todo_project_name` repo on GitHub. +2. Clone your fork locally:: + + $ git clone git@github.com:your_name_here/todo_project_name.git + +3. Install your local copy into a virtualenv:: + + $ python -m venv venv + $ source ./venv/bin/activate + $ pip install -e ."[dev]" + $ pre-commit install + +4. Create a branch for local development:: + + $ git checkout -b name-of-your-bugfix-or-feature + + Now you can make your changes locally. + +5. When you're done making changes, check that your changes pass flake8 and the + tests:: + + $ flake8 todo_project_name tests + $ python setup.py test or pytest + +6. Commit your changes and push your branch to GitHub:: + + $ git add . + $ git commit -m "Your detailed description of your changes." + $ git push origin name-of-your-bugfix-or-feature + +7. Submit a pull request through the GitHub website. + +Pull Request Guidelines +----------------------- + +Before you submit a pull request, check that it meets these guidelines: + +1. The pull request should include tests. +2. If the pull request adds functionality, the docs should be updated. Put + your new functionality into a function with a docstring, and add the + feature to the list in README.rst. +3. The pull request should work for Python 3.5, 3.6, 3.7 and 3.8, and for PyPy. Check + https://travis-ci.com/todo_github_username/todo_project_name/pull_requests + and make sure that the tests pass for all supported Python versions. + +Tips +---- + +To run a subset of tests:: + +$ pytest tests.test_todo_project_name + + +Deploying +--------- + +A reminder for the maintainers on how to deploy. +Make sure all your changes are committed (including an entry in HISTORY.rst). +Then run:: + +$ bump2version patch # possible: major / minor / patch +$ git push +$ git push --tags + +Travis will then deploy to PyPI if tests pass. diff --git a/_todo_project_name/HISTORY.rst b/_todo_project_name/HISTORY.rst new file mode 100644 index 00000000..1cd2df59 --- /dev/null +++ b/_todo_project_name/HISTORY.rst @@ -0,0 +1,8 @@ +======= +History +======= + +0.1.0 (2021-10-21) +------------------ + +* First release on PyPI. diff --git a/_todo_project_name/LICENSE b/_todo_project_name/LICENSE new file mode 100644 index 00000000..2ca9717c --- /dev/null +++ b/_todo_project_name/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021, Todo Full Name + +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: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +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. diff --git a/_todo_project_name/MANIFEST.in b/_todo_project_name/MANIFEST.in new file mode 100644 index 00000000..965b2dda --- /dev/null +++ b/_todo_project_name/MANIFEST.in @@ -0,0 +1,11 @@ +include AUTHORS.rst +include CONTRIBUTING.rst +include HISTORY.rst +include LICENSE +include README.rst + +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] + +recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif diff --git a/_todo_project_name/Makefile b/_todo_project_name/Makefile new file mode 100644 index 00000000..2b8dd9a3 --- /dev/null +++ b/_todo_project_name/Makefile @@ -0,0 +1,85 @@ +.PHONY: clean clean-test clean-pyc clean-build docs help +.DEFAULT_GOAL := help + +define BROWSER_PYSCRIPT +import os, webbrowser, sys + +from urllib.request import pathname2url + +webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) +endef +export BROWSER_PYSCRIPT + +define PRINT_HELP_PYSCRIPT +import re, sys + +for line in sys.stdin: + match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) + if match: + target, help = match.groups() + print("%-20s %s" % (target, help)) +endef +export PRINT_HELP_PYSCRIPT + +BROWSER := python -c "$$BROWSER_PYSCRIPT" + +help: + @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) + +clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts + +clean-build: ## remove build artifacts + rm -fr build/ + rm -fr dist/ + rm -fr .eggs/ + find . -name '*.egg-info' -exec rm -fr {} + + find . -name '*.egg' -exec rm -f {} + + +clean-pyc: ## remove Python file artifacts + find . -name '*.pyc' -exec rm -f {} + + find . -name '*.pyo' -exec rm -f {} + + find . -name '*~' -exec rm -f {} + + find . -name '__pycache__' -exec rm -fr {} + + +clean-test: ## remove test and coverage artifacts + rm -f .coverage + rm -fr htmlcov/ + rm -fr .pytest_cache + +lint/flake8: ## check style with flake8 + flake8 todo_project_name tests +lint/black: ## check style with black + black --check todo_project_name tests + +lint: lint/flake8 lint/black ## check style + +test: ## run tests quickly with the default Python + pytest + +coverage: ## check code coverage quickly with the default Python + coverage run --source todo_project_name -m pytest + coverage report -m + coverage html + $(BROWSER) htmlcov/index.html + +docs: ## generate Sphinx HTML documentation, including API docs + rm -f docs/todo_project_name.rst + rm -f docs/modules.rst + sphinx-apidoc -o docs/ todo_project_name + $(MAKE) -C docs clean + $(MAKE) -C docs html + $(BROWSER) docs/_build/html/index.html + +servedocs: docs ## compile the docs watching for changes + watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D . + +release: dist ## package and upload a release + twine upload dist/* + +dist: clean ## builds source and wheel package + python setup.py sdist + python setup.py bdist_wheel + ls -l dist + +install: clean ## install the package to the active Python's site-packages + python setup.py install diff --git a/_todo_project_name/README.rst b/_todo_project_name/README.rst new file mode 100644 index 00000000..e3ec05a2 --- /dev/null +++ b/_todo_project_name/README.rst @@ -0,0 +1,33 @@ +================= +Todo Project Name +================= + +.. image:: https://img.shields.io/github/workflow/status/mly32/todo_project_name/Python%20Github%20Actions?label=CI + :target: https://github.com/mly32/todo_project_name/actions + :alt: GitHub Workflow Status + +.. image:: https://img.shields.io/github/checks-status/mly32/todo_project_name/gh-pages?label=Docs + :target: https://mly32.github.io/todo_project_name/ + :alt: GitHub branch checks state + +.. image:: https://img.shields.io/github/license/mly32/todo_project_name + :target: https://github.com/mly32/todo_project_name/blob/main/LICENSE + :alt: GitHub + +Documentation: https://mly32.github.io/todo_project_name/. + +Todo short description + + +Features +-------- + +* TODO + +Credits +------- + +This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template. + +.. _Cookiecutter: https://github.com/audreyr/cookiecutter +.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage diff --git a/_todo_project_name/data/db.sqlite b/_todo_project_name/data/db.sqlite new file mode 100644 index 00000000..b9c6c890 Binary files /dev/null and b/_todo_project_name/data/db.sqlite differ diff --git a/_todo_project_name/docs/Makefile b/_todo_project_name/docs/Makefile new file mode 100644 index 00000000..532ca830 --- /dev/null +++ b/_todo_project_name/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = python -msphinx +SPHINXPROJ = todo_project_name +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/_todo_project_name/docs/authors.rst b/_todo_project_name/docs/authors.rst new file mode 100644 index 00000000..e122f914 --- /dev/null +++ b/_todo_project_name/docs/authors.rst @@ -0,0 +1 @@ +.. include:: ../AUTHORS.rst diff --git a/_todo_project_name/docs/conf.py b/_todo_project_name/docs/conf.py new file mode 100755 index 00000000..7c7dfca2 --- /dev/null +++ b/_todo_project_name/docs/conf.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python +# +# todo_project_name documentation build configuration file, created by +# sphinx-quickstart on Fri Jun 9 13:47:02 2017. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +# If extensions (or modules to document with autodoc) are in another +# directory, add these directories to sys.path here. If the directory is +# relative to the documentation root, use os.path.abspath to make it +# absolute, like shown here. +# +import os +import sys + +sys.path.insert(0, os.path.abspath("..")) + +import todo_project_name + +# -- General configuration --------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +# +# needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ["_templates"] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +# +# source_suffix = ['.rst', '.md'] +source_suffix = ".rst" + +# The master toctree document. +master_doc = "index" + +# General information about the project. +project = "Todo Project Name" +copyright = "2021, Todo Full Name" +author = "Todo Full Name" + +# The version info for the project you're documenting, acts as replacement +# for |version| and |release|, also used in various other places throughout +# the built documents. +# +# The short X.Y version. +version = todo_project_name.__version__ +# The full version, including alpha/beta/rc tags. +release = todo_project_name.__version__ + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = None + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This patterns also effect to html_static_path and html_extra_path +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = "sphinx" + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = False + + +# -- Options for HTML output ------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = "sphinx_rtd_theme" + +# Theme options are theme-specific and customize the look and feel of a +# theme further. For a list of options available for each theme, see the +# documentation. +# +# html_theme_options = {} + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ["_static"] + + +# -- Options for HTMLHelp output --------------------------------------- + +# Output file base name for HTML help builder. +htmlhelp_basename = "todo_project_namedoc" + + +# -- Options for LaTeX output ------------------------------------------ + +latex_elements = { + # The paper size ('letterpaper' or 'a4paper'). + # + # 'papersize': 'letterpaper', + # The font size ('10pt', '11pt' or '12pt'). + # + # 'pointsize': '10pt', + # Additional stuff for the LaTeX preamble. + # + # 'preamble': '', + # Latex figure (float) alignment + # + # 'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass +# [howto, manual, or own class]). +latex_documents = [ + ( + master_doc, + "todo_project_name.tex", + "Todo Project Name Documentation", + "Todo Full Name", + "manual", + ) +] + + +# -- Options for manual page output ------------------------------------ + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + (master_doc, "todo_project_name", "Todo Project Name Documentation", [author], 1) +] + + +# -- Options for Texinfo output ---------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ( + master_doc, + "todo_project_name", + "Todo Project Name Documentation", + author, + "todo_project_name", + "One line description of project.", + "Miscellaneous", + ) +] diff --git a/_todo_project_name/docs/contributing.rst b/_todo_project_name/docs/contributing.rst new file mode 100644 index 00000000..e582053e --- /dev/null +++ b/_todo_project_name/docs/contributing.rst @@ -0,0 +1 @@ +.. include:: ../CONTRIBUTING.rst diff --git a/_todo_project_name/docs/history.rst b/_todo_project_name/docs/history.rst new file mode 100644 index 00000000..25064996 --- /dev/null +++ b/_todo_project_name/docs/history.rst @@ -0,0 +1 @@ +.. include:: ../HISTORY.rst diff --git a/_todo_project_name/docs/index.rst b/_todo_project_name/docs/index.rst new file mode 100644 index 00000000..777dafcd --- /dev/null +++ b/_todo_project_name/docs/index.rst @@ -0,0 +1,20 @@ +Welcome to Todo Project Name's documentation! +============================================= + +.. toctree:: + :maxdepth: 2 + :caption: Contents: + + readme + installation + usage + modules + contributing + authors + history + +Indices and tables +================== +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/_todo_project_name/docs/installation.rst b/_todo_project_name/docs/installation.rst new file mode 100644 index 00000000..f0a8e609 --- /dev/null +++ b/_todo_project_name/docs/installation.rst @@ -0,0 +1,51 @@ +.. highlight:: shell + +============ +Installation +============ + + +.. Stable release +.. -------------- + +.. To install Todo Project Name, run this command in your terminal: + +.. .. code-block:: console + +.. $ pip install todo_project_name + +.. This is the preferred method to install Todo Project Name, as it will always install the most recent stable release. + +.. If you don't have `pip`_ installed, this `Python installation guide`_ can guide +.. you through the process. + +.. .. _pip: https://pip.pypa.io +.. .. _Python installation guide: http://docs.python-guide.org/en/latest/starting/installation/ + + +From sources +------------ + +The sources for Todo Project Name can be downloaded from the `Github repo`_. + +You can either clone the public repository: + +.. code-block:: console + + $ git clone git://github.com/todo_github_username/todo_project_name + +Or download the `tarball`_: + +.. code-block:: console + + $ curl -OJL https://github.com/todo_github_username/todo_project_name/tarball/master + +Once you have a copy of the source, you can install it with: + +.. code-block:: console + + $ python setup.py install + + +.. _Github repo: https://github.com/todo_github_username/todo_project_name +.. _tarball: https://github.com/todo_github_username/todo_project_name/tarball/master diff --git a/_todo_project_name/docs/make.bat b/_todo_project_name/docs/make.bat new file mode 100644 index 00000000..a2dbdbdb --- /dev/null +++ b/_todo_project_name/docs/make.bat @@ -0,0 +1,36 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=python -msphinx +) +set SOURCEDIR=. +set BUILDDIR=_build +set SPHINXPROJ=todo_project_name + +if "%1" == "" goto help + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The Sphinx module was not found. Make sure you have Sphinx installed, + echo.then set the SPHINXBUILD environment variable to point to the full + echo.path of the 'sphinx-build' executable. Alternatively you may add the + echo.Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% + +:end +popd diff --git a/_todo_project_name/docs/modules.rst b/_todo_project_name/docs/modules.rst new file mode 100644 index 00000000..d3451b2f --- /dev/null +++ b/_todo_project_name/docs/modules.rst @@ -0,0 +1,7 @@ +todo_project_name +================= + +.. toctree:: + :maxdepth: 4 + + todo_project_name diff --git a/_todo_project_name/docs/readme.rst b/_todo_project_name/docs/readme.rst new file mode 100644 index 00000000..72a33558 --- /dev/null +++ b/_todo_project_name/docs/readme.rst @@ -0,0 +1 @@ +.. include:: ../README.rst diff --git a/_todo_project_name/docs/todo_project_name.db.rst b/_todo_project_name/docs/todo_project_name.db.rst new file mode 100644 index 00000000..f4089587 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.db.rst @@ -0,0 +1,38 @@ +todo\_project\_name.db package +============================== + +Submodules +---------- + +todo\_project\_name.db.db module +-------------------------------- + +.. automodule:: todo_project_name.db.db + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.db.db\_normal module +---------------------------------------- + +.. automodule:: todo_project_name.db.db_normal + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.db.models module +------------------------------------ + +.. automodule:: todo_project_name.db.models + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.db + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.flight_sw.rst b/_todo_project_name/docs/todo_project_name.flight_sw.rst new file mode 100644 index 00000000..b15d1e07 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.flight_sw.rst @@ -0,0 +1,22 @@ +todo\_project\_name.flight\_sw package +====================================== + +Submodules +---------- + +todo\_project\_name.flight\_sw.main\_satellite module +----------------------------------------------------- + +.. automodule:: todo_project_name.flight_sw.main_satellite + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.flight_sw + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.op_nav.rst b/_todo_project_name/docs/todo_project_name.op_nav.rst new file mode 100644 index 00000000..cadf01e2 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.op_nav.rst @@ -0,0 +1,22 @@ +todo\_project\_name.op\_nav package +=================================== + +Submodules +---------- + +todo\_project\_name.op\_nav.op\_nav module +------------------------------------------ + +.. automodule:: todo_project_name.op_nav.op_nav + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.op_nav + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.rst b/_todo_project_name/docs/todo_project_name.rst new file mode 100644 index 00000000..93eb6a15 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.rst @@ -0,0 +1,32 @@ +todo\_project\_name package +=========================== + +Subpackages +----------- + +.. toctree:: + + todo_project_name.db + todo_project_name.flight_sw + todo_project_name.op_nav + todo_project_name.utils + +Submodules +---------- + +todo\_project\_name.main module +------------------------------- + +.. automodule:: todo_project_name.main + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.threads.rst b/_todo_project_name/docs/todo_project_name.threads.rst new file mode 100644 index 00000000..e760c1be --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.threads.rst @@ -0,0 +1,38 @@ +todo\_project\_name.threads package +=================================== + +Submodules +---------- + +todo\_project\_name.threads.producer\_consumer\_with\_event module +------------------------------------------------------------------ + +.. automodule:: todo_project_name.threads.producer_consumer_with_event + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.threads.producer\_consumer\_with\_no\_event module +---------------------------------------------------------------------- + +.. automodule:: todo_project_name.threads.producer_consumer_with_no_event + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.threads.run module +-------------------------------------- + +.. automodule:: todo_project_name.threads.run + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.threads + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.todo_basic.rst b/_todo_project_name/docs/todo_project_name.todo_basic.rst new file mode 100644 index 00000000..824c27fa --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.todo_basic.rst @@ -0,0 +1,22 @@ +todo\_project\_name.todo\_basic package +======================================= + +Submodules +---------- + +todo\_project\_name.todo\_basic.todo\_basic module +-------------------------------------------------- + +.. automodule:: todo_project_name.todo_basic.todo_basic + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.todo_basic + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.utils.rst b/_todo_project_name/docs/todo_project_name.utils.rst new file mode 100644 index 00000000..089df686 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.utils.rst @@ -0,0 +1,38 @@ +todo\_project\_name.utils package +================================= + +Submodules +---------- + +todo\_project\_name.utils.constants module +------------------------------------------ + +.. automodule:: todo_project_name.utils.constants + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.utils.globals module +---------------------------------------- + +.. automodule:: todo_project_name.utils.globals + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.utils.log module +------------------------------------ + +.. automodule:: todo_project_name.utils.log + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.utils + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/usage.rst b/_todo_project_name/docs/usage.rst new file mode 100644 index 00000000..18f8532e --- /dev/null +++ b/_todo_project_name/docs/usage.rst @@ -0,0 +1,7 @@ +===== +Usage +===== + +To use Todo Project Name in a project:: + + import todo_project_name diff --git a/_todo_project_name/setup.cfg b/_todo_project_name/setup.cfg new file mode 100644 index 00000000..7a1b86a7 --- /dev/null +++ b/_todo_project_name/setup.cfg @@ -0,0 +1,22 @@ +[bumpversion] +current_version = 0.1.0 +commit = True +tag = True + +[bumpversion:file:setup.py] +search = version='{current_version}' +replace = version='{new_version}' + +[bumpversion:file:todo_project_name/__init__.py] +search = __version__ = '{current_version}' +replace = __version__ = '{new_version}' + +[bdist_wheel] +universal = 1 + +[flake8] +max_line_length=119 +exclude = docs + +[tool:pytest] +addopts = '--ignore="setup.py"' diff --git a/_todo_project_name/setup.py b/_todo_project_name/setup.py new file mode 100644 index 00000000..e4e2b293 --- /dev/null +++ b/_todo_project_name/setup.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +"""The setup script.""" + +from setuptools import find_packages, setup + +with open("README.rst") as readme_file: + readme = readme_file.read() + +with open("HISTORY.rst") as history_file: + history = history_file.read() + +includes = ["todo_project_name", "todo_project_name.*"] + +requirements = ["numpy", "python-dotenv", "sqlalchemy"] + +dev_req = ["black", "flake8", "pip", "pre-commit", "sphinx", "watchdog"] + +docs_req = ["sphinx", "sphinx-rtd-theme"] + +test_req = ["coverage", "pytest"] + +release_req = ["bump2version", "wheel", "twine"] + +extra_req = {"dev": dev_req + docs_req + test_req + release_req} + +setup( + author="Todo Full Name", + author_email="todo@email.com", + python_requires=">=3.6", + classifiers=[ + "Development Status :: 2 - Pre-Alpha", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Natural Language :: English", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + ], + description="Todo short description", + extras_require=extra_req, + install_requires=requirements, + license="MIT license", + long_description=readme + "\n\n" + history, + include_package_data=True, + keywords="todo_project_name", + name="todo_project_name", + packages=find_packages(include=includes), + url="https://github.com/todo_github_username/todo_project_name", + version="0.1.0", + zip_safe=False, +) diff --git a/_todo_project_name/tests/__init__.py b/_todo_project_name/tests/__init__.py new file mode 100644 index 00000000..56623c58 --- /dev/null +++ b/_todo_project_name/tests/__init__.py @@ -0,0 +1 @@ +"""Unit test package for todo_project_name.""" diff --git a/_todo_project_name/tests/test_todo_project_name.py b/_todo_project_name/tests/test_todo_project_name.py new file mode 100644 index 00000000..0844209f --- /dev/null +++ b/_todo_project_name/tests/test_todo_project_name.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +"""Tests for `todo_project_name` package.""" + +import pytest + + +@pytest.fixture +def response(): + """Sample pytest fixture. + + See more at: http://doc.pytest.org/en/latest/fixture.html + """ + # import requests + # return requests.get('https://github.com/audreyr/cookiecutter-pypackage') + + +def test_content(response): + """Sample pytest test function with the pytest fixture as an argument.""" + # from bs4 import BeautifulSoup + # assert 'GitHub' in BeautifulSoup(response.content).title.string + + +def test_todo_add(): + """A basic test.""" + assert 1 + 1 == 2 diff --git a/_todo_project_name/todo_project_name/__init__.py b/_todo_project_name/todo_project_name/__init__.py new file mode 100644 index 00000000..4d2bab71 --- /dev/null +++ b/_todo_project_name/todo_project_name/__init__.py @@ -0,0 +1,5 @@ +"""Top-level package for Todo Project Name.""" + +__author__ = """Todo Full Name""" +__email__ = "todo@email.com" +__version__ = "0.1.0" diff --git a/_todo_project_name/todo_project_name/db/__init__.py b/_todo_project_name/todo_project_name/db/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/_todo_project_name/todo_project_name/db/db.py b/_todo_project_name/todo_project_name/db/db.py new file mode 100644 index 00000000..dd75b8b1 --- /dev/null +++ b/_todo_project_name/todo_project_name/db/db.py @@ -0,0 +1,160 @@ +import logging +import typing +from datetime import datetime + +import db.models as models +import sqlalchemy as sa + +# factory for creating sessions +Session = sa.orm.sessionmaker(models.engine) + +# TODO look into Session.future or scoped session? +# TODO func.min query does it actually return min row?? +# TODO use session.begin? expunge row after creation? or session.refresh? + + +def __add_data(model: models.Base, **kwargs) -> models.Base: + with Session() as session: + row = model(**kwargs) + session.add(row) + session.commit() + logging.info(f"Add {model.__tablename__} data: {row}") + # session.close() + return row + + +def add_flight_data(number: int) -> models.FlightData: + return __add_data(models.FlightData, time=datetime.now(), number=number) + + +def add_att_adjust_data(time: datetime) -> models.AttAdjustRun: + return __add_data(models.AttAdjustRun, time=time) + + +def add_op_nav_data(time: datetime, number: int) -> models.OpNavData: + return __add_data(models.OpNavData, time=time, number=number) + + +def add_op_nav_run(time: datetime) -> models.OpNavRun: + return __add_data(models.OpNavRun, time=time) + + +def __update_data(model: models.Base, id: int, **kwargs) -> None: + with Session.begin() as session: + row = session.query(model).filter(model.id.is_(id)).first() + if row is not None: + logging.info(f"Update from {model.__tablename__} data: {row}") + for (key, val) in kwargs.items(): + setattr(row, key, val) + logging.info(f"Update to {model.__tablename__} data: {row}") + # session.commit() and session.close() + + +def update_flight_data(id: int, number) -> None: + return __update_data(models.FlightData, id, number=number) + + +def update_att_adjust_run(id: int, state: models.RunState) -> None: + return __update_data(models.AttAdjustRun, id, state=state) + + +def update_op_nav_run(id: int, state: models.RunState) -> None: + return __update_data(models.OpNavRun, id, state=state) + + +def __update_failed_runs(model: models.Base) -> int: + """sets all missed runs to FAILED + + :param model: table to update failed runs + :type model: models.Base + :return: the number of missed runs + :rtype: int + """ + with Session.begin() as session: + result = ( + session.query(model) + .filter( + model.state.is_(models.RunState.QUEUED), model.time < datetime.now() + ) + .all() + ) + for row in result: + row.state = models.RunState.FAILED + logging.info(f"Failed {model.__tablename__}: {row}") + num_failed = len(result) + # session.commit() and session.close() + if num_failed > 0: + logging.info(f"Missed {num_failed} runs") + return num_failed + + +def update_failed_att_adjust_runs() -> int: + return __update_failed_runs(models.AttAdjustRun) + + +def update_failed_op_nav_runs() -> int: + return __update_failed_runs(models.OpNavRun) + + +def __query_all_data(model: models.Base) -> typing.List[models.Base]: + with Session() as session: + result = session.query(model).all() + logging.info(f"Get all {model.__tablename__} data: {len(result)} rows") + for r in result: + print(" ", r) + session.expunge_all() + # session.close() + return result + + +def query_all_flight_data() -> typing.List[models.FlightData]: + return __query_all_data(models.FlightData) + + +def query_all_op_nav_data() -> typing.List[models.OpNavData]: + return __query_all_data(models.OpNavData) + + +def query_earliest_flight_data() -> typing.Union[models.FlightData, None]: + with Session() as session: + result = session.query( + models.FlightData, sa.func.min(models.FlightData.number) + ).first() + session.expunge_all() + logging.info(f"Get min flight data: {result[0]}") + return result[0] + # session.close() + + +def query_latest_op_nav_data() -> typing.Union[models.OpNavData, None]: + with Session() as session: + result = session.query( + models.OpNavData, sa.func.max(models.OpNavData.time) + ).first() + session.expunge_all() + # session.close() + logging.info(f"Get latest op-nav data: {result[0]}") + return result[0] + + +def __query_earliest( + model: models.Base, +) -> typing.Union[typing.List[models.Base], None]: + with Session() as session: + result = ( + session.query(model, sa.func.min(model.time)) + .filter(model.state.is_(models.RunState.QUEUED)) + .first() + ) + session.expunge_all() + # session.close() + logging.info(f"Get earliest {model.__tablename__}: {result[0]}") + return result[0] + + +def query_earliest_att_adjust_run() -> typing.Union[models.AttAdjustRun, None]: + return __query_earliest(models.AttAdjustRun) + + +def query_earliest_op_nav_run() -> typing.Union[models.OpNavRun, None]: + return __query_earliest(models.OpNavRun) diff --git a/_todo_project_name/todo_project_name/db/models.py b/_todo_project_name/todo_project_name/db/models.py new file mode 100644 index 00000000..a29ada78 --- /dev/null +++ b/_todo_project_name/todo_project_name/db/models.py @@ -0,0 +1,84 @@ +import enum + +import sqlalchemy as sa +from sqlalchemy.ext.declarative import declarative_base +from utils.constants import CONST + +engine = sa.create_engine(CONST.DB_FILE, echo=False) # set to True for logging +Base = declarative_base() + +# TODO docstrings + + +class RunState(enum.Enum): + QUEUED = enum.auto() + RUNNING = enum.auto() + FAILED = enum.auto() + SUCCEEDED = enum.auto() + + def __str__(self) -> str: + return f"RunState.{self.name}({self.value})" + + +class FlightData(Base): + __tablename__ = "flight_data" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + number = sa.Column(sa.Integer, nullable=False) + + def __init__(self, *, time, number) -> None: + self.time = time + self.number = number + + def __str__(self) -> str: + return f"FlightData({self.time}, {self.number})" + + +class AttAdjustRun(Base): + __tablename__ = "att_adjust_run" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + state = sa.Column(sa.Enum(RunState), nullable=False) # persists string + + def __init__(self, *, time) -> None: + self.time = time + self.state = RunState.QUEUED + + def __str__(self) -> str: + return f"AttAdjustRun({self.time}, {self.state})" + + +class OpNavData(Base): + __tablename__ = "op_nav_data" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + number = sa.Column(sa.Integer, nullable=False) + + def __init__(self, *, time, number) -> None: + self.time = time + self.number = number + + def __str__(self) -> str: + return f"OpNavData({self.time}, {self.number})" + + +class OpNavRun(Base): + __tablename__ = "op_nav_run" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + state = sa.Column(sa.Enum(RunState), nullable=False) + + def __init__(self, *, time) -> None: + self.time = time + self.state = RunState.QUEUED + + def __str__(self) -> str: + return f"OpNavRun({self.time}, {self.state})" + + +# creates all defined table objects into metadata +Base.metadata.create_all(engine) diff --git a/_todo_project_name/todo_project_name/flight_sw/__init__.py b/_todo_project_name/todo_project_name/flight_sw/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/_todo_project_name/todo_project_name/flight_sw/main_satellite.py b/_todo_project_name/todo_project_name/flight_sw/main_satellite.py new file mode 100644 index 00000000..7bf63798 --- /dev/null +++ b/_todo_project_name/todo_project_name/flight_sw/main_satellite.py @@ -0,0 +1,132 @@ +import enum +import logging +import random +import threading +import time +from datetime import datetime + +import utils.globals as globals +from db import db, models +from utils.constants import CONST + +from todo_project_name.op_nav.op_nav import OpNav + +# import sched +# TODO database size +# TODO time comparisons add small epsilon in comparisions +# TODO does camera need to block main thread? makes att-adjust lock really weird + + +class FlightMode(enum.Enum): + Normal = enum.auto() + AttAdjust = enum.auto() + OpNav = enum.auto() + + def __str__(self) -> str: + return f"FlightMode.{self.name}({self.value})" + + +class MainSatellite(threading.Thread): + def __init__(self, **kwargs) -> None: + threading.Thread.__init__(self, **kwargs) + self.name = "main-sat" + + logging.info("Main satellite initialized") + self.mode: FlightMode = FlightMode.Normal + self.continue_run: bool = True + self.run_count: int = 0 + + def run(self) -> None: + # init params & sensors + while self.continue_run: + self.name = f"main-sat-{self.run_count % 10}" + with globals.run_camera_cond: + while globals.want_to_run_camera: + logging.info("Can to run camera set to TRUE") + globals.can_run_camera = True + globals.run_camera_cond.notify_all() + globals.run_camera_cond.wait() + globals.can_run_camera = False + logging.info(f"Flight mode: {self.mode}") # Can to run camera set to FALSE. + + if self.mode == FlightMode.Normal: + db.add_flight_data(number=random.randint(1, 101)) + # db.query_latest_op_nav_data() + time.sleep(1) + elif self.mode == FlightMode.AttAdjust: + db.update_failed_att_adjust_runs() + att_adjust_run = db.query_earliest_att_adjust_run() + if att_adjust_run is not None: + time_diff = (att_adjust_run.time - datetime.now()).total_seconds() + logging.info(f"Att-adjust run scheduled in {time_diff}s") + time.sleep(max(time_diff, 0)) # TODO replace with separate thread? + logging.info("Begin attitude adjustment") + time.sleep(3) + db.update_att_adjust_run( + att_adjust_run.id, models.RunState.SUCCEEDED + ) + logging.info("Complete attitude adjustment") + elif self.mode == FlightMode.OpNav: + db.update_failed_op_nav_runs() + op_nav_run = db.query_earliest_op_nav_run() + if op_nav_run is not None: + op_nav = OpNav() + time_diff = (op_nav_run.time - datetime.now()).total_seconds() + logging.info(f"Op-nav run scheduled in {time_diff}s") + t = threading.Timer(max(time_diff, 0), op_nav.run, (op_nav_run.id,)) + t.name = "op-nav" + t.start() + db.update_op_nav_run(op_nav_run.id, models.RunState.RUNNING) + + self.transition() + self.run_count += 1 + if self.run_count > 15: # For debugging purposes + self.continue_run = False + + def transition(self) -> None: + a_time_diff = -1 + o_time_diff = -1 + + db.update_failed_att_adjust_runs() + att_adjust_run = db.query_earliest_att_adjust_run() + if att_adjust_run is not None: + a_time_diff = (att_adjust_run.time - datetime.now()).total_seconds() + if a_time_diff >= CONST.ATT_ADJUST_RUN_BUFFER: + logging.info( + f"Next att-adjust run too far away; time diff: {a_time_diff}" + ) + else: + self.mode = FlightMode.AttAdjust + return + + db.update_failed_op_nav_runs() + op_nav_run = db.query_earliest_op_nav_run() + if op_nav_run is not None: + o_time_diff = (op_nav_run.time - datetime.now()).total_seconds() + # while query attitude for time < now: sleep() + if ( + (att_adjust_run is not None) + and ( + a_time_diff >= o_time_diff + and a_time_diff <= o_time_diff + CONST.MAX_OP_NAV_CAMERA_TIME + ) + or ( + a_time_diff + CONST.MAX_ATT_ADJUST_TIME >= o_time_diff + and a_time_diff + CONST.MAX_ATT_ADJUST_TIME + <= o_time_diff + CONST.MAX_OP_NAV_CAMERA_TIME + ) + ): + # Att-adjust will happen during op-nav camera + logging.info( + "Next op-nav run interferes with att-adjust. Op-nav run canceld." + ) + db.update_op_nav_run(op_nav_run.id, models.RunState.FAILED) + + # o_time_diff + CONST.MAX_OP_NAV_CAMERA_TIME <= a_time_diff + if o_time_diff >= CONST.OP_NAV_RUN_BUFFER: + logging.info(f"Next op-nav run too far away; time diff: {o_time_diff}") + else: + self.mode = FlightMode.OpNav + return + + self.mode = FlightMode.Normal diff --git a/_todo_project_name/todo_project_name/main.py b/_todo_project_name/todo_project_name/main.py new file mode 100644 index 00000000..5814ebac --- /dev/null +++ b/_todo_project_name/todo_project_name/main.py @@ -0,0 +1,52 @@ +import logging +import random +from datetime import datetime, timedelta + +from db import db +from utils import log + +from todo_project_name.flight_sw.main_satellite import MainSatellite + + +def test_db_functions(): + n = random.randint(1, 101) + + flight_row = db.add_flight_data(number=n) + flight_res = db.query_all_flight_data() + assert len(flight_res) == 1 + assert flight_row.id == flight_res[0].id + + db.update_flight_data(flight_row.id, number=2 * n) + flight_res2 = db.query_all_flight_data() + assert 2 * n == flight_res2[0].number + + +def test_db_att_adjust(): + now = datetime.now() + db.add_att_adjust_data(now + timedelta(seconds=-10)) + db.add_att_adjust_data(now + timedelta(seconds=-15)) + db.add_att_adjust_data(now + timedelta(seconds=15)) + res = db.update_failed_att_adjust_runs() + assert res == 2 + res2 = db.query_earliest_att_adjust_run() + assert res2 is not None and now + timedelta(seconds=15) == res2.time + + +def main_test(): + now = datetime.now() + db.add_att_adjust_data(now + timedelta(seconds=4)) + db.add_op_nav_run(now + timedelta(seconds=2)) + db.add_op_nav_run(now + timedelta(seconds=16)) + + th = MainSatellite(daemon=True) + th.start() + + try: + th.join() + except KeyboardInterrupt: + logging.info("Keyboard Interrupt received; exiting") + + +if __name__ == "__main__": + log.set_up_logging() + main_test() diff --git a/_todo_project_name/todo_project_name/op_nav/__init__.py b/_todo_project_name/todo_project_name/op_nav/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/_todo_project_name/todo_project_name/op_nav/op_nav.py b/_todo_project_name/todo_project_name/op_nav/op_nav.py new file mode 100644 index 00000000..8d3cfe21 --- /dev/null +++ b/_todo_project_name/todo_project_name/op_nav/op_nav.py @@ -0,0 +1,42 @@ +import logging +import time +from datetime import datetime + +import utils.globals as globals +from db import db, models + + +class OpNav: + def __init__(self): + logging.info("Op-nav initialized") + + def __observe(self) -> models.FlightData: + # photo timing should not be stopped + # with globals.global_lock: + with globals.run_camera_cond: + logging.info("Want to run camera set to TRUE") + globals.want_to_run_camera = True + globals.run_camera_cond.notify_all() + while not globals.can_run_camera: + globals.run_camera_cond.wait() + logging.info("Camera recording started") + time.sleep(5) + res = db.query_earliest_flight_data() + logging.info("Camera recording completed") + + logging.info("Want to run camera set to FALSE") + globals.want_to_run_camera = False + globals.run_camera_cond.notify_all() + return res + + def __process_data(self, flight_data: models.FlightData) -> None: + time.sleep(2) + db.add_op_nav_data(datetime.now(), 2 * flight_data.number) + + def run(self, run_id) -> None: + logging.info(f"ID: {run_id}") + logging.info(f"Op-nav run beginning at {datetime.now()}") + res = self.__observe() + self.__process_data(res) + logging.info(f"Op-nav run ending at {datetime.now()}") + db.update_op_nav_run(run_id, models.RunState.SUCCEEDED) diff --git a/_todo_project_name/todo_project_name/utils/__init__.py b/_todo_project_name/todo_project_name/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/_todo_project_name/todo_project_name/utils/constants.py b/_todo_project_name/todo_project_name/utils/constants.py new file mode 100644 index 00000000..af3a7178 --- /dev/null +++ b/_todo_project_name/todo_project_name/utils/constants.py @@ -0,0 +1,92 @@ +import os +import shutil +from pathlib import Path + +from dotenv import dotenv_values + + +class __Const(object): + def __init__(self) -> None: + """Read from dotenv file and setup constants""" + self.__config = dotenv_values() + self.__project_path = (Path(__file__).parent / "../..").resolve() + + @property + def DATA_PATH(self) -> str: + """Dotenv DATA_PATH + :return: sql database parent path + :rtype: str + """ + data_path = ( + self.__config["DATA_PATH"] if self.__config["DATA_PATH"] is not None else "" + ) + if os.path.isabs(data_path): + return data_path + return os.path.join(self.__project_path, data_path) + + @property + def DATA_RESET(self) -> bool: + """Dotenv DATA_RESET + + :return: whether to delete sql database at start + :rtype: bool + """ + return self.__config["DATA_RESET"] == "1" + + @property + def DB_FILE(self) -> str: + """Based off of dotenv DATA_PATH + + :return: formated database path for sqlalchemy + :rtype: str + """ + return "sqlite:///" + os.path.join(self.DATA_PATH, "db.sqlite") + + @property + def ATT_ADJUST_RUN_BUFFER(self) -> float: + """Look ahead time to switch to a different state + + :return: difference in seconds + :rtype: float + """ + return 5.0 + + @property + def OP_NAV_RUN_BUFFER(self) -> float: + """Look ahead time to switch to a different state + + :return: difference in seconds + :rtype: float + """ + return 5.0 + + @property + def MAX_ATT_ADJUST_TIME(self) -> float: + """Worst case time to complete an att-adjust + + :return: time in seconds + :rtype: float + """ + return 6.0 + + @property + def MAX_OP_NAV_CAMERA_TIME(self) -> float: + """Worst case time to complete an op-nav camera run + + :return: time in seconds + :rtype: float + """ + return 20.0 + + +CONST = __Const() + + +def __setup_files() -> None: + """Generate directories and files""" + if CONST.DATA_RESET and os.path.exists(CONST.DATA_PATH): + shutil.rmtree(CONST.DATA_PATH) + os.makedirs(CONST.DATA_PATH, exist_ok=True) + + +__setup_files() diff --git a/_todo_project_name/todo_project_name/utils/globals.py b/_todo_project_name/todo_project_name/utils/globals.py new file mode 100644 index 00000000..34777a05 --- /dev/null +++ b/_todo_project_name/todo_project_name/utils/globals.py @@ -0,0 +1,19 @@ +import threading + +global_lock = threading.Lock() +"""Lock for op-nav camera""" + +want_to_run_camera = False +"""Whether op-nav wants to run the camera""" + +can_run_camera = False +"""Whether the camera can be run exclusively""" + +run_camera_cond = threading.Condition() +"""Condition to run op-nav camera""" + +# can_run_adjust = False +# """Whether att-adjust can be run exclusively""" + +# can_run_adjust_cond = threading.Condition() +# """Condition to run main-sat att-adjust mode""" diff --git a/_todo_project_name/todo_project_name/utils/log.py b/_todo_project_name/todo_project_name/utils/log.py new file mode 100644 index 00000000..42ee358a --- /dev/null +++ b/_todo_project_name/todo_project_name/utils/log.py @@ -0,0 +1,7 @@ +import logging + + +def set_up_logging() -> None: + format = "(%(threadName)-10s) %(asctime)s: %(message)s" + logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S") + # logging.getLogger().setLevel(logging.DEBUG) diff --git a/flight_modes/flight_mode.py b/flight_modes/flight_mode.py index 4b44e1cf..80ab2be0 100644 --- a/flight_modes/flight_mode.py +++ b/flight_modes/flight_mode.py @@ -10,18 +10,19 @@ # It lets your IDE know what type(self._parent) is, without causing any circular imports at runtime. import gc -from time import sleep, time -from multiprocessing import Process -import subprocess +import logging from queue import Empty +from time import sleep, time from traceback import format_tb -import logging import utils.constants as consts import utils.parameters as params - from utils.exceptions import UnknownFlightModeException +# import subprocess +# from multiprocessing import Process + + no_transition_modes = [ consts.FMEnum.SensorMode.value, consts.FMEnum.TestMode.value, @@ -185,8 +186,8 @@ def __exit__(self, exc_type, exc_value, tb): class PauseBackgroundMode(FlightMode): """Model for FlightModes that require precise timing - Pause garbage collection and anything else that could - interrupt critical thread""" + Pause garbage collection and anything else that could + interrupt critical thread""" def run_mode(self): super().run_mode() @@ -300,49 +301,6 @@ def run_mode(self): self.completed_task() -class OpNavMode(FlightMode): - """FMID 5: Optical Navigation Flight Mode - This flight mode is dedicated to starting the Opnav process""" - - # TODO: Flight Software/Opnav interface - flight_mode_id = consts.FMEnum.OpNav.value - - def __init__(self, parent): - super().__init__(parent) - - def run_mode(self): - # TODO: overhaul so opnav calculation only occur while in opnav mode - if not self._parent.opnav_process.is_alive(): - logging.info("[OPNAV]: Able to run next opnav") - self._parent.last_opnav_run = time() - logging.info("[OPNAV]: Starting opnav subprocess") - self._parent.opnav_process = Process( - target=self.opnav_subprocess, args=(self._parent.opnav_proc_queue,) - ) - self._parent.opnav_process.start() - self.completed_task() - - def update_state(self) -> int: - super_fm = super().update_state() - if super_fm != consts.NO_FM_CHANGE: - return super_fm - - # check if opnav db has been updated, then set self.task_completed true - - return consts.NO_FM_CHANGE - - def opnav_subprocess(self, q): - # TODO put in try...except - # TODO change from pytest to actual opnav - # os.system("pytest OpticalNavigation/tests/test_pipeline.py::test_start") - # subprocess.run('pytest OpticalNavigation/tests/test_pipeline.py::test_start', shell=True) - subprocess.run( - "echo [OPNAV]: Subprocess Start; sleep 1m; echo [OPNAV]: Subprocess end", - shell=True, - ) - q.put("Opnav Finished") - - class SensorMode(FlightMode): """FMID 7: Sensor Mode This flight mode is not really well defined and has kinda been forgotten about. One potential idea for it is that @@ -398,7 +356,9 @@ def update_state(self) -> int: time_for_telem: bool = ( time() - self._parent.radio.last_transmit_time ) // 60 < params.TELEM_INTERVAL - need_to_electrolyze: bool = self._parent.telemetry.prs.pressure < params.IDEAL_CRACKING_PRESSURE + need_to_electrolyze: bool = ( + self._parent.telemetry.prs.pressure < params.IDEAL_CRACKING_PRESSURE + ) currently_electrolyzing = self._parent.telemetry.gom.is_electrolyzing # if we don't want to electrolyze (per GS command), set need_to_electrolyze to false diff --git a/flight_modes/flight_mode_factory.py b/flight_modes/flight_mode_factory.py index 5dc13a83..ee96f242 100644 --- a/flight_modes/flight_mode_factory.py +++ b/flight_modes/flight_mode_factory.py @@ -1,20 +1,19 @@ +from utils.constants import FMEnum +from utils.exceptions import UnknownFlightModeException + +from flight_modes.attitude_adjustment import AAMode from flight_modes.flight_mode import ( + CommandMode, + CommsMode, NormalMode, SafeMode, SensorMode, TestMode, - CommsMode, - OpNavMode, - CommandMode, ) - from flight_modes.low_battery import LowBatterySafetyMode - -from flight_modes.restart_reboot import RestartMode, BootUpMode from flight_modes.maneuver_flightmode import ManeuverMode -from flight_modes.attitude_adjustment import AAMode -from utils.constants import FMEnum -from utils.exceptions import UnknownFlightModeException +from flight_modes.opnav_flightmode import OpNavMode +from flight_modes.restart_reboot import BootUpMode, RestartMode FLIGHT_MODE_DICT = { FMEnum.Boot.value: BootUpMode, diff --git a/flight_modes/opnav_flightmode.py b/flight_modes/opnav_flightmode.py index 822bc676..0fa00750 100644 --- a/flight_modes/opnav_flightmode.py +++ b/flight_modes/opnav_flightmode.py @@ -1,70 +1,59 @@ +import logging + +# import sqlite3 from datetime import datetime -from time import sleep -import sqlite3 -from sqlalchemy.exc import SQLAlchemyError from multiprocessing import Process -from utils.constants import FMEnum -from utils.db import OpNavCoordinatesModel -import logging +import utils.constants as consts + from .flight_mode import PauseBackgroundMode +# from sqlalchemy.exc import SQLAlchemyError + + +# from time import sleep + + +# from utils.db import OpNavCoordinatesModel + class OpNavMode(PauseBackgroundMode): + """FMID 5: Optical Navigation Flight Mode + This flight mode is dedicated to starting the Opnav process""" - flight_mode_id = FMEnum.OpNav.value + flight_mode_id = consts.FMEnum.OpNav.value def __init__(self, parent): super().__init__(parent) - self.dummy_opnav_result = ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0), (0.0, 0.0, 0.0)) - - def set_return_val(self, returnval): - self.dummy_opnav_result = returnval - - def acquire_images(self): - return datetime.now() - - def get_position_velocity_attitude(self): - return self.dummy_opnav_result - - def run_opnav(self): - sleep(5.0) - position_acquired_at = self.acquire_images() - self.most_recent_result = self.get_position_velocity_attitude() - opnav_model = OpNavCoordinatesModel.from_tuples( - self.most_recent_result, position_acquired_at - ) - try: - session = self._parent.create_session() - session.add(opnav_model) - session.commit() - finally: - session.close() def read_most_recent_result(self): if self.most_recent_result is not None: return self.most_recent_result else: - try: - session = self._parent.create_session() - last_measurement = ( - session.query(OpNavCoordinatesModel) - .order_by(OpNavCoordinatesModel.measurement_taken) - .first() - ) - if last_measurement is None: - raise Exception("No record of previous OpNav runs") - return self.dummy_opnav_result - except (sqlite3.Error, SQLAlchemyError): - session.close() - raise Exception("Error while reading most recent OpNav result") + pass + # try: + # session = self._parent.create_session() + # last_measurement = ( + # session.query(OpNavCoordinatesModel) + # .order_by(OpNavCoordinatesModel.measurement_taken) + # .first() + # ) + # if last_measurement is None: + # raise Exception("No record of previous OpNav runs") + # return self.dummy_opnav_result + # except (sqlite3.Error, SQLAlchemyError): + # session.close() + # raise Exception("Error while reading most recent OpNav result") def run_mode(self): + # TODO: overhaul so opnav calculation only occur while in opnav mode if not self.opnav_process.is_alive(): logging.info("[OPNAV]: Able to run next opnav") self._parent.last_opnav_run = datetime.now() logging.info("[OPNAV]: Starting opnav subprocess") - self.opnav_process = Process(target=self.opnav_subprocess, args=()) + self.opnav_process = Process( + target=self.opnav_subprocess, args=(self._parent.opnav_proc_queue,) + ) self.opnav_process.start() self.run_opnav() self.task_completed() @@ -72,9 +61,22 @@ def run_mode(self): def update_state(self) -> int: super_fm = super().update_state() - if super_fm != 0: + if super_fm != consts.NO_FM_CHANGE: return super_fm + # check if opnav db has been updated, then set self.task_completed true # return to normal mode if task completed if self.task_completed: - return FMEnum.Normal.value + return consts.NO_FM_CHANGE + + def opnav_subprocess(self, q): + # TODO put in try...except + # TODO change from pytest to actual opnav + # os.system("pytest OpticalNavigation/tests/test_pipeline.py::test_start") + # subprocess.run('pytest OpticalNavigation/tests/test_pipeline.py::test_start', shell=True) + pass + # subprocess.run( + # "echo [OPNAV]: Subprocess Start; sleep 1m; echo [OPNAV]: Subprocess end", + # shell=True, + # ) + # q.put("Opnav Finished") diff --git a/pyrightconfig.json b/pyrightconfig.json index 1c46d38c..ddf0d27d 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -25,11 +25,10 @@ "OpticalNavigation/core/opnav.py", "OpticalNavigation/core/find.py", "OpticalNavigation/core/find_with_blobs.py", - "OpticalNavigation/core/find_with_kmeans.py" - ], - "extraPaths": [ - "OpticalNavigation/" + "OpticalNavigation/core/find_with_kmeans.py", + "_todo_project_name" ], + "extraPaths": ["OpticalNavigation/"], "pythonVersion": "3.7", "pythonPlatform": "Linux", "reportUnboundVariable": "warning", diff --git a/setup.cfg b/setup.cfg index bce83951..d0af8c70 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,3 +12,8 @@ markers= zero_noise_test: marks tests with zero starting noise small_noise_test: marks tests with small starting noise large_noise_test: marks tests with large starting noise + +[flake8] +max_line_length=119 +extend-ignore=F403,F405,E203,E741,F811 +exclude = docs, _todo_project_name/docs/conf.py diff --git a/utils/globals.py b/utils/globals.py new file mode 100644 index 00000000..b45b963b --- /dev/null +++ b/utils/globals.py @@ -0,0 +1,3 @@ +import threading + +global_lock = threading.Lock()