├── .gitignore ├── .travis.yml ├── AUTHORS.rst ├── CONTRIBUTING.rst ├── HISTORY.rst ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.rst ├── docs ├── Makefile ├── authors.rst ├── conf.py ├── contributing.rst ├── history.rst ├── index.rst ├── make.bat ├── models.rst ├── modules.rst ├── readme.rst └── swapi.rst ├── requirements.txt ├── setup.cfg ├── setup.py ├── swapi ├── __init__.py ├── exceptions.py ├── models.py ├── settings.py ├── swapi.py └── utils.py ├── tests └── test_swapi.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | 21 | # Installer logs 22 | pip-log.txt 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | htmlcov 29 | 30 | # Translations 31 | *.mo 32 | 33 | # Mr Developer 34 | .mr.developer.cfg 35 | .project 36 | .pydevproject 37 | 38 | # Complexity 39 | output/*.html 40 | output/*/index.html 41 | 42 | # Sphinx 43 | docs/_build 44 | 45 | *venv* 46 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Config file for automatic testing at travis-ci.org 2 | 3 | language: python 4 | 5 | python: 6 | - "2.7" 7 | - "3.3" 8 | - "3.4" 9 | 10 | # command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors 11 | install: pip install -r requirements.txt 12 | 13 | # command to run tests, e.g. python setup.py test 14 | script: nosetests tests/test_swapi.py 15 | -------------------------------------------------------------------------------- /AUTHORS.rst: -------------------------------------------------------------------------------- 1 | ======= 2 | Credits 3 | ======= 4 | 5 | Development Lead 6 | ---------------- 7 | 8 | * Paul Hallett 9 | 10 | Contributors 11 | ------------ 12 | 13 | Colm Harrington 14 | -------------------------------------------------------------------------------- /CONTRIBUTING.rst: -------------------------------------------------------------------------------- 1 | ============ 2 | Contributing 3 | ============ 4 | 5 | Contributions are welcome, and they are greatly appreciated! Every 6 | little bit helps, and credit will always be given. 7 | 8 | You can contribute in many ways: 9 | 10 | Types of Contributions 11 | ---------------------- 12 | 13 | Report Bugs 14 | ~~~~~~~~~~~ 15 | 16 | Report bugs at https://github.com/phalt/swapi-python/issues. 17 | 18 | If you are reporting a bug, please include: 19 | 20 | * Your operating system name and version. 21 | * Any details about your local setup that might be helpful in troubleshooting. 22 | * Detailed steps to reproduce the bug. 23 | 24 | Fix Bugs 25 | ~~~~~~~~ 26 | 27 | Look through the GitHub issues for bugs. Anything tagged with "bug" 28 | is open to whoever wants to implement it. 29 | 30 | Implement Features 31 | ~~~~~~~~~~~~~~~~~~ 32 | 33 | Look through the GitHub issues for features. Anything tagged with "feature" 34 | is open to whoever wants to implement it. 35 | 36 | Write Documentation 37 | ~~~~~~~~~~~~~~~~~~~ 38 | 39 | swapi-python could always use more documentation, whether as part of the 40 | official swapi-python docs, in docstrings, or even on the web in blog posts, 41 | articles, and such. 42 | 43 | Submit Feedback 44 | ~~~~~~~~~~~~~~~ 45 | 46 | The best way to send feedback is to file an issue at https://github.com/phalt/swapi-python/issues. 47 | 48 | If you are proposing a feature: 49 | 50 | * Explain in detail how it would work. 51 | * Keep the scope as narrow as possible, to make it easier to implement. 52 | * Remember that this is a volunteer-driven project, and that contributions 53 | are welcome :) 54 | 55 | Get Started! 56 | ------------ 57 | 58 | Ready to contribute? Here's how to set up `swapi-python` for local development. 59 | 60 | 1. Fork the `swapi-python` repo on GitHub. 61 | 2. Clone your fork locally:: 62 | 63 | $ git clone git@github.com:your_name_here/swapi-python.git 64 | 65 | 3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:: 66 | 67 | $ mkvirtualenv swapi-python 68 | $ cd swapi-python/ 69 | $ python setup.py develop 70 | 71 | 4. Create a branch for local development:: 72 | 73 | $ git checkout -b name-of-your-bugfix-or-feature 74 | 75 | Now you can make your changes locally. 76 | 77 | 5. When you're done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:: 78 | 79 | $ flake8 swapi-python tests 80 | $ python setup.py test 81 | $ tox 82 | 83 | To get flake8 and tox, just pip install them into your virtualenv. 84 | 85 | 6. Commit your changes and push your branch to GitHub:: 86 | 87 | $ git add . 88 | $ git commit -m "Your detailed description of your changes." 89 | $ git push origin name-of-your-bugfix-or-feature 90 | 91 | 7. Submit a pull request through the GitHub website. 92 | 93 | Pull Request Guidelines 94 | ----------------------- 95 | 96 | Before you submit a pull request, check that it meets these guidelines: 97 | 98 | 1. The pull request should include tests. 99 | 2. If the pull request adds functionality, the docs should be updated. Put 100 | your new functionality into a function with a docstring, and add the 101 | feature to the list in README.rst. 102 | 3. The pull request should work for Python 2.6, 2.7, 3.3, and 3.4, and for PyPy. Check 103 | https://travis-ci.org/phalt/swapi-python/pull_requests 104 | and make sure that the tests pass for all supported Python versions. 105 | 106 | Tips 107 | ---- 108 | 109 | To run a subset of tests:: 110 | 111 | $ python -m unittest tests.test_swapi-python 112 | -------------------------------------------------------------------------------- /HISTORY.rst: -------------------------------------------------------------------------------- 1 | .. :changelog: 2 | 3 | History 4 | ------- 5 | 6 | 0.1.0 (2014-12-21) 7 | --------------------- 8 | 9 | * First release on PyPI. 10 | 11 | 12 | 0.1.2 (2014-12-22) 13 | ------------------ 14 | 15 | * Python 3.3 and Python 3.4 compatability 16 | 17 | 18 | 0.1.3 (2014-12-22) 19 | ------------------- 20 | 21 | * Add "swapi-python" to User-Agent to help with analytics 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Paul Hallett 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | * Neither the name of swapi-python nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include AUTHORS.rst 2 | include CONTRIBUTING.rst 3 | include HISTORY.rst 4 | include LICENSE 5 | include README.rst 6 | 7 | recursive-include tests * 8 | recursive-exclude * __pycache__ 9 | recursive-exclude * *.py[co] 10 | 11 | recursive-include docs *.rst conf.py Makefile make.bat 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean-pyc clean-build docs clean 2 | 3 | help: 4 | @echo "clean - remove all build, test, coverage and Python artifacts" 5 | @echo "clean-build - remove build artifacts" 6 | @echo "clean-pyc - remove Python file artifacts" 7 | @echo "clean-test - remove test and coverage artifacts" 8 | @echo "lint - check style with flake8" 9 | @echo "test - run tests quickly with the default Python" 10 | @echo "test-all - run tests on every Python version with tox" 11 | @echo "coverage - check code coverage quickly with the default Python" 12 | @echo "docs - generate Sphinx HTML documentation, including API docs" 13 | @echo "release - package and upload a release" 14 | @echo "dist - package" 15 | 16 | clean: clean-build clean-pyc clean-test 17 | 18 | clean-build: 19 | rm -rf build/ 20 | rm -rf dist/ 21 | rm -rf *.egg-info 22 | rm -rf htmlcov/ 23 | 24 | clean-pyc: 25 | find . -name '*.pyc' -exec rm -f {} + 26 | find . -name '*.pyo' -exec rm -f {} + 27 | find . -name '*~' -exec rm -f {} + 28 | find . -name '__pycache__' -exec rm -fr {} + 29 | 30 | clean-test: 31 | rm -fr .tox/ 32 | rm -f .coverage 33 | rm -fr htmlcov/ 34 | 35 | lint: 36 | flake8 swapi tests 37 | 38 | test: 39 | nosetests tests/test_swapi.py 40 | 41 | coverage: 42 | nosetests tests/test_swapi.py --with-coverage --cover-package=swapi 43 | coverage html 44 | open htmlcov/index.html 45 | 46 | docs: 47 | rm -f docs/swapi-python.rst 48 | rm -f docs/modules.rst 49 | sphinx-apidoc -o docs/ swapi 50 | $(MAKE) -C docs clean 51 | $(MAKE) -C docs html 52 | open docs/_build/html/index.html 53 | 54 | release: clean 55 | python setup.py sdist upload 56 | python setup.py bdist_wheel upload 57 | 58 | dist: clean 59 | python setup.py sdist 60 | python setup.py bdist_wheel 61 | ls -l dist 62 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | =============================== 2 | swapi-python 3 | =============================== 4 | 5 | .. image:: https://badge.fury.io/py/swapi.png 6 | :target: http://badge.fury.io/py/swapi 7 | 8 | .. image:: https://travis-ci.org/phalt/swapi-python.png?branch=master 9 | :target: https://travis-ci.org/phalt/swapi-python 10 | 11 | .. image:: https://pypip.in/d/swapi/badge.png 12 | :target: https://pypi.python.org/pypi/swapi 13 | 14 | 15 | A Python helper library for swapi.co - the Star Wars API 16 | 17 | NOTE: Tests will run against hosted API as opposed to data from github repo 18 | 19 | * Free software: BSD license 20 | * Documentation: https://swapi-python.readthedocs.org. 21 | 22 | ============ 23 | Installation 24 | ============ 25 | 26 | At the command line:: 27 | 28 | $ pip install swapi 29 | 30 | Basic Usage 31 | ======== 32 | 33 | To use swapi-python in a project:: 34 | 35 | import swapi 36 | 37 | All resources are accessible through the top-level ``get_resource()`` methods:: 38 | 39 | luke = swapi.get_person(1) 40 | tatooine = swapi.get_planet(1) 41 | 42 | Methods 43 | ======= 44 | 45 | These are the top-level methods you can use to get resources from swapi.co. To learn more about the models and objects that are returned, see the ``models`` page. 46 | 47 | get_person(id) 48 | ------------ 49 | 50 | Return a single ``Person`` resource. 51 | 52 | Example:: 53 | 54 | swapi.get_person(1) 55 | >>> 56 | 57 | 58 | get_planet(id) 59 | ------------ 60 | 61 | Return a single ``Planet`` resource. 62 | 63 | Example:: 64 | 65 | swapi.get_planet(1) 66 | >>> 67 | 68 | 69 | get_starship(id) 70 | ------------ 71 | 72 | Return a single ``Starship`` resource. 73 | 74 | Example:: 75 | 76 | swapi.get_starship(6) 77 | >>> 78 | 79 | 80 | get_vehicle(id) 81 | ------------ 82 | 83 | Return a single ``Vehicle`` resource. 84 | 85 | Example:: 86 | 87 | swapi.get_vehicle(4) 88 | >>> 89 | 90 | 91 | get_film(id) 92 | ------------ 93 | 94 | Return a single ``Film`` resource. 95 | 96 | Example:: 97 | 98 | swapi.get_film(1) 99 | >>> 100 | 101 | 102 | get_all("resource") 103 | ------------ 104 | 105 | Return a ``QuerySet`` containing all the items in a single resource. See the ```models``` page for more information on the models used in swapi-python. 106 | 107 | Example:: 108 | 109 | swapi.get_all("films") 110 | >>> 111 | -------------------------------------------------------------------------------- /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/swapi-python.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/swapi-python.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/swapi-python" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/swapi-python" 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 | # swapi-python 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 | RTD_NEW_THEME = True 19 | 20 | # If extensions (or modules to document with autodoc) are in another 21 | # directory, add these directories to sys.path here. If the directory is 22 | # relative to the documentation root, use os.path.abspath to make it 23 | # absolute, like shown here. 24 | #sys.path.insert(0, os.path.abspath('.')) 25 | 26 | # Get the project root dir, which is the parent dir of this 27 | cwd = os.getcwd() 28 | project_root = os.path.dirname(cwd) 29 | 30 | # Insert the project root dir as the first element in the PYTHONPATH. 31 | # This lets us ensure that the source package is imported, and that its 32 | # version is used. 33 | sys.path.insert(0, project_root) 34 | 35 | import swapi 36 | 37 | # -- General configuration --------------------------------------------- 38 | 39 | # If your documentation needs a minimal Sphinx version, state it here. 40 | #needs_sphinx = '1.0' 41 | 42 | # Add any Sphinx extension module names here, as strings. They can be 43 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 44 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] 45 | 46 | # Add any paths that contain templates here, relative to this directory. 47 | templates_path = ['_templates'] 48 | 49 | # The suffix of source filenames. 50 | source_suffix = '.rst' 51 | 52 | # The encoding of source files. 53 | #source_encoding = 'utf-8-sig' 54 | 55 | # The master toctree document. 56 | master_doc = 'index' 57 | 58 | # General information about the project. 59 | project = u'swapi-python' 60 | copyright = u'2015, Paul Hallett' 61 | 62 | # The version info for the project you're documenting, acts as replacement 63 | # for |version| and |release|, also used in various other places throughout 64 | # the built documents. 65 | # 66 | # The short X.Y version. 67 | version = swapi.__version__ 68 | # The full version, including alpha/beta/rc tags. 69 | release = swapi.__version__ 70 | 71 | # The language for content autogenerated by Sphinx. Refer to documentation 72 | # for a list of supported languages. 73 | #language = None 74 | 75 | # There are two options for replacing |today|: either, you set today to 76 | # some non-false value, then it is used: 77 | #today = '' 78 | # Else, today_fmt is used as the format for a strftime call. 79 | #today_fmt = '%B %d, %Y' 80 | 81 | # List of patterns, relative to source directory, that match files and 82 | # directories to ignore when looking for source files. 83 | exclude_patterns = ['_build'] 84 | 85 | # The reST default role (used for this markup: `text`) to use for all 86 | # documents. 87 | #default_role = None 88 | 89 | # If true, '()' will be appended to :func: etc. cross-reference text. 90 | #add_function_parentheses = True 91 | 92 | # If true, the current module name will be prepended to all description 93 | # unit titles (such as .. function::). 94 | #add_module_names = True 95 | 96 | # If true, sectionauthor and moduleauthor directives will be shown in the 97 | # output. They are ignored by default. 98 | #show_authors = False 99 | 100 | # The name of the Pygments (syntax highlighting) style to use. 101 | pygments_style = 'sphinx' 102 | 103 | # A list of ignored prefixes for module index sorting. 104 | #modindex_common_prefix = [] 105 | 106 | # If true, keep warnings as "system message" paragraphs in the built 107 | # documents. 108 | #keep_warnings = False 109 | 110 | 111 | # -- Options for HTML output ------------------------------------------- 112 | 113 | # The theme to use for HTML and HTML Help pages. See the documentation for 114 | # a list of builtin themes. 115 | html_theme = 'default' 116 | 117 | # Theme options are theme-specific and customize the look and feel of a 118 | # theme further. For a list of options available for each theme, see the 119 | # documentation. 120 | #html_theme_options = {} 121 | 122 | # Add any paths that contain custom themes here, relative to this directory. 123 | #html_theme_path = [] 124 | 125 | # The name for this set of Sphinx documents. If None, it defaults to 126 | # " v documentation". 127 | #html_title = None 128 | 129 | # A shorter title for the navigation bar. Default is the same as 130 | # html_title. 131 | #html_short_title = None 132 | 133 | # The name of an image file (relative to this directory) to place at the 134 | # top of the sidebar. 135 | #html_logo = None 136 | 137 | # The name of an image file (within the static path) to use as favicon 138 | # of the docs. This file should be a Windows icon file (.ico) being 139 | # 16x16 or 32x32 pixels large. 140 | #html_favicon = None 141 | 142 | # Add any paths that contain custom static files (such as style sheets) 143 | # here, relative to this directory. They are copied after the builtin 144 | # static files, so a file named "default.css" will overwrite the builtin 145 | # "default.css". 146 | html_static_path = ['_static'] 147 | 148 | # If not '', a 'Last updated on:' timestamp is inserted at every page 149 | # bottom, using the given strftime format. 150 | #html_last_updated_fmt = '%b %d, %Y' 151 | 152 | # If true, SmartyPants will be used to convert quotes and dashes to 153 | # typographically correct entities. 154 | #html_use_smartypants = True 155 | 156 | # Custom sidebar templates, maps document names to template names. 157 | #html_sidebars = {} 158 | 159 | # Additional templates that should be rendered to pages, maps page names 160 | # to template names. 161 | #html_additional_pages = {} 162 | 163 | # If false, no module index is generated. 164 | #html_domain_indices = True 165 | 166 | # If false, no index is generated. 167 | #html_use_index = True 168 | 169 | # If true, the index is split into individual pages for each letter. 170 | #html_split_index = False 171 | 172 | # If true, links to the reST sources are added to the pages. 173 | #html_show_sourcelink = True 174 | 175 | # If true, "Created using Sphinx" is shown in the HTML footer. 176 | # Default is True. 177 | #html_show_sphinx = True 178 | 179 | # If true, "(C) Copyright ..." is shown in the HTML footer. 180 | # Default is True. 181 | #html_show_copyright = True 182 | 183 | # If true, an OpenSearch description file will be output, and all pages 184 | # will contain a tag referring to it. The value of this option 185 | # must be the base URL from which the finished HTML is served. 186 | #html_use_opensearch = '' 187 | 188 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 189 | #html_file_suffix = None 190 | 191 | # Output file base name for HTML help builder. 192 | htmlhelp_basename = 'swapi-pythondoc' 193 | 194 | 195 | # -- Options for LaTeX output ------------------------------------------ 196 | 197 | latex_elements = { 198 | # The paper size ('letterpaper' or 'a4paper'). 199 | #'papersize': 'letterpaper', 200 | 201 | # The font size ('10pt', '11pt' or '12pt'). 202 | #'pointsize': '10pt', 203 | 204 | # Additional stuff for the LaTeX preamble. 205 | #'preamble': '', 206 | } 207 | 208 | # Grouping the document tree into LaTeX files. List of tuples 209 | # (source start file, target name, title, author, documentclass 210 | # [howto/manual]). 211 | latex_documents = [ 212 | ('index', 'swapi-python.tex', 213 | u'swapi-python Documentation', 214 | u'Paul Hallett', 'manual'), 215 | ] 216 | 217 | # The name of an image file (relative to this directory) to place at 218 | # the top of the title page. 219 | #latex_logo = None 220 | 221 | # For "manual" documents, if this is true, then toplevel headings 222 | # are parts, not chapters. 223 | #latex_use_parts = False 224 | 225 | # If true, show page references after internal links. 226 | #latex_show_pagerefs = False 227 | 228 | # If true, show URL addresses after external links. 229 | #latex_show_urls = False 230 | 231 | # Documents to append as an appendix to all manuals. 232 | #latex_appendices = [] 233 | 234 | # If false, no module index is generated. 235 | #latex_domain_indices = True 236 | 237 | 238 | # -- Options for manual page output ------------------------------------ 239 | 240 | # One entry per manual page. List of tuples 241 | # (source start file, name, description, authors, manual section). 242 | man_pages = [ 243 | ('index', 'swapi-python', 244 | u'swapi-python Documentation', 245 | [u'Paul Hallett'], 1) 246 | ] 247 | 248 | # If true, show URL addresses after external links. 249 | #man_show_urls = False 250 | 251 | 252 | # -- Options for Texinfo output ---------------------------------------- 253 | 254 | # Grouping the document tree into Texinfo files. List of tuples 255 | # (source start file, target name, title, author, 256 | # dir menu entry, description, category) 257 | texinfo_documents = [ 258 | ('index', 'swapi-python', 259 | u'swapi-python Documentation', 260 | u'Paul Hallett', 261 | 'swapi-python', 262 | 'One line description of project.', 263 | 'Miscellaneous'), 264 | ] 265 | 266 | # Documents to append as an appendix to all manuals. 267 | #texinfo_appendices = [] 268 | 269 | # If false, no module index is generated. 270 | #texinfo_domain_indices = True 271 | 272 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 273 | #texinfo_show_urls = 'footnote' 274 | 275 | # If true, do not generate a @detailmenu in the "Top" node's menu. 276 | #texinfo_no_detailmenu = False 277 | -------------------------------------------------------------------------------- /docs/contributing.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../CONTRIBUTING.rst 2 | -------------------------------------------------------------------------------- /docs/history.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../HISTORY.rst 2 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. swapi-python documentation master file, created by 2 | sphinx-quickstart on Tue Jul 9 22:26:36 2013. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to swapi-python's documentation! 7 | ====================================== 8 | 9 | Contents: 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | readme 15 | installation 16 | usage 17 | models 18 | contributing 19 | authors 20 | history 21 | 22 | Indices and tables 23 | ================== 24 | 25 | * :ref:`genindex` 26 | * :ref:`modindex` 27 | * :ref:`search` 28 | 29 | -------------------------------------------------------------------------------- /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\swapi-python.qhcp 119 | echo.To view the help file: 120 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\swapi-python.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/models.rst: -------------------------------------------------------------------------------- 1 | ==== 2 | Models 3 | ==== 4 | 5 | Swapi-python uses custom models to handle single resources and collections of resources in a native and pythonic way. 6 | 7 | This page documents the extra methods and functions of those classes. 8 | 9 | 10 | Single Resource Model 11 | ===================== 12 | 13 | A single resource model maps all the standard attributes for a resource as properties on the object. For example, a ``People`` resource has a ``name`` and ``height`` attribute. These can be accessed with swapi-python as properties, like so:: 14 | 15 | luke = swapi.get_person(1) 16 | luke.name 17 | >>> u'Luke Skywalker' 18 | luke.height 19 | >>> u'172' 20 | 21 | 22 | Getting Related Resources 23 | ------------------------- 24 | 25 | Each resource model has specific methods for getting back the collection of resources related to it. For example, the People resource can have Starship Resources linked to it. With swapi-python you can get those resources like so:: 26 | 27 | luke = swapi.get_person(1) 28 | ships = luke.get_starships() 29 | 30 | Each resource model has slightly different methods. Multiple resources return a Multiple Collection Model (see below). 31 | 32 | People 33 | ------ 34 | * ``get_starships()`` 35 | Return the starships that this person has piloted. 36 | 37 | * ``get_vehicles()`` 38 | Return the vehicles that this person has piloted. 39 | 40 | * ``get_homeworld()`` 41 | Get the homeworld of this person. 42 | 43 | * ``get_species()`` 44 | Return the species of this person. 45 | 46 | * ``get_films()`` 47 | Return the films that this person has been in. 48 | 49 | Species 50 | ------- 51 | * ``get_films()`` 52 | Return the films that this species has been in. 53 | 54 | * ``get_people()`` 55 | Return the people who are a member of this species. 56 | 57 | * ``get_homeworld()`` 58 | Return the homeworld of this species. 59 | 60 | Planet 61 | ------- 62 | * ``get_residents()`` 63 | Return the people who live on this planet. 64 | 65 | * ``get_films()`` 66 | Return the films that this planet has appeared in. 67 | 68 | Starship 69 | -------- 70 | * ``get_pilots()`` 71 | Return the pilots of this starship. 72 | 73 | * ``get_films()`` 74 | Return the films that this starship has been in. 75 | 76 | Vehicle 77 | ------- 78 | * ``get_pilots()`` 79 | Return the pilots of this vehicle. 80 | 81 | * ``get_films()`` 82 | Return the films that this vehicle has been in. 83 | 84 | Film 85 | ---- 86 | * ``get_starships()`` 87 | Get the starships in this film. 88 | 89 | * ``get_characters()`` 90 | Get the characters in this film. 91 | 92 | * ``get_vehicles()`` 93 | Get the vehicles in this film. 94 | 95 | * ``get_planets()`` 96 | Get the planets in this film. 97 | 98 | * ``get_species()`` 99 | Get the species in this film. 100 | 101 | * ``gen_opening_crawl()`` 102 | A generator yielding each line of the opening crawl for this film. 103 | 104 | * ``print_crawl()`` 105 | A novelty method that prints out each line of the opening crawl with a 0.5 second delay between each line. 106 | 107 | 108 | Multiple Collection Model 109 | ========================= 110 | 111 | When you query swapi.co for multiple resources of the same type, they will be returned as a ``ResourceQuerySet``, which is a collection of those resources that you requested. For example, to get the ``Starship`` resources linked to a person, you can do the following:: 112 | 113 | luke = swapi.get_person(1) 114 | starships = luke.get_starships() 115 | >>> 116 | 117 | ``ResourceQuerySet`` models have additional methods for dealing with multiple resources. 118 | 119 | The items are accessible as the ``item`` property on the ResourceQuerySet if you want to directly access them:: 120 | 121 | starships.items 122 | >>> [, ] 123 | 124 | .order_by("attribute") 125 | ---------------------- 126 | 127 | Return the list of Single Resource Models in this collection, ordered by a particular attribute. For example:: 128 | 129 | planets = swapi.get_all("planets") 130 | for p in planets.order_by("diameter"): 131 | print(p.name) 132 | 133 | This will return the planets ordered by their diameter. 134 | 135 | This method will try to turn the attribute into an interger before ordering them, as most resources are unicode strings. If you try to order by string, it will order it alphabetically. Please be aware that for string ordering it might not always come out as you would expect. 136 | 137 | .count() 138 | ------- 139 | 140 | Return a count of the number of items in this collection.. 141 | 142 | .iter() 143 | ------- 144 | 145 | An iterable method that can be used to iterate over each item in this collection:: 146 | 147 | for v in vehicles.iter(): 148 | print(v.name) 149 | -------------------------------------------------------------------------------- /docs/modules.rst: -------------------------------------------------------------------------------- 1 | swapi 2 | ===== 3 | 4 | .. toctree:: 5 | :maxdepth: 4 6 | 7 | swapi 8 | -------------------------------------------------------------------------------- /docs/readme.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../README.rst 2 | -------------------------------------------------------------------------------- /docs/swapi.rst: -------------------------------------------------------------------------------- 1 | swapi package 2 | ============= 3 | 4 | Submodules 5 | ---------- 6 | 7 | swapi.exceptions module 8 | ----------------------- 9 | 10 | .. automodule:: swapi.exceptions 11 | :members: 12 | :undoc-members: 13 | :show-inheritance: 14 | 15 | swapi.models module 16 | ------------------- 17 | 18 | .. automodule:: swapi.models 19 | :members: 20 | :undoc-members: 21 | :show-inheritance: 22 | 23 | swapi.settings module 24 | --------------------- 25 | 26 | .. automodule:: swapi.settings 27 | :members: 28 | :undoc-members: 29 | :show-inheritance: 30 | 31 | swapi.swapi module 32 | ------------------ 33 | 34 | .. automodule:: swapi.swapi 35 | :members: 36 | :undoc-members: 37 | :show-inheritance: 38 | 39 | swapi.utils module 40 | ------------------ 41 | 42 | .. automodule:: swapi.utils 43 | :members: 44 | :undoc-members: 45 | :show-inheritance: 46 | 47 | 48 | Module contents 49 | --------------- 50 | 51 | .. automodule:: swapi 52 | :members: 53 | :undoc-members: 54 | :show-inheritance: 55 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Jinja2==2.7.3 2 | MarkupSafe==0.23 3 | PyYAML==3.11 4 | Pygments==2.0.1 5 | Sphinx==1.2.3 6 | binaryornot==0.3.0 7 | coverage==3.7.1 8 | docutils==0.12 9 | flake8==2.2.5 10 | mccabe==0.3 11 | mock==1.0.1 12 | nose==1.3.4 13 | pep8==1.5.7 14 | pyflakes==0.8.1 15 | requests==2.5.0 16 | six==1.8.0 17 | ujson==1.33 18 | wheel==0.24.0 19 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [wheel] 2 | universal = 1 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | try: 6 | from setuptools import setup 7 | except ImportError: 8 | from distutils.core import setup 9 | 10 | 11 | readme = open('README.rst').read() 12 | history = open('HISTORY.rst').read().replace('.. :changelog:', '') 13 | 14 | requirements = [ 15 | 'requests==2.5.0', 'six==1.8.0', 'ujson==1.33' 16 | ] 17 | 18 | setup( 19 | name='swapi', 20 | version='0.1.3', 21 | description='A Python helper library for swapi.co - the Star Wars API', 22 | long_description=readme + '\n\n' + history, 23 | author='Paul Hallett', 24 | author_email='paulandrewhallett@gmail.com', 25 | url='https://github.com/phalt/swapi-python', 26 | packages=[ 27 | 'swapi', 28 | ], 29 | package_dir={'swapi': 30 | 'swapi'}, 31 | include_package_data=True, 32 | install_requires=requirements, 33 | license="BSD", 34 | zip_safe=False, 35 | keywords='swapi', 36 | classifiers=[ 37 | 'Development Status :: 2 - Pre-Alpha', 38 | 'Intended Audience :: Developers', 39 | 'License :: OSI Approved :: BSD License', 40 | 'Natural Language :: English', 41 | "Programming Language :: Python :: 2", 42 | 'Programming Language :: Python :: 2.6', 43 | 'Programming Language :: Python :: 2.7', 44 | 'Programming Language :: Python :: 3', 45 | 'Programming Language :: Python :: 3.3', 46 | 'Programming Language :: Python :: 3.4', 47 | ], 48 | test_suite='tests', 49 | ) 50 | -------------------------------------------------------------------------------- /swapi/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = 'Paul Hallett' 4 | __email__ = 'paulandrewhallett@gmail.com' 5 | __version__ = '0.1.0' 6 | 7 | from .swapi import ( 8 | get_all, 9 | get_person, 10 | get_planet, 11 | get_starship, 12 | get_vehicle, 13 | get_film, 14 | get_species 15 | ) 16 | 17 | from .settings import ( 18 | PEOPLE, 19 | PLANETS, 20 | STARSHIPS, 21 | VEHICLES, 22 | FILMS, 23 | SPECIES 24 | ) 25 | -------------------------------------------------------------------------------- /swapi/exceptions.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | class ResourceDoesNotExist(Exception): 5 | pass 6 | -------------------------------------------------------------------------------- /swapi/models.py: -------------------------------------------------------------------------------- 1 | import ujson as json 2 | import time 3 | 4 | import six 5 | try: 6 | from swapi.utils import query 7 | except: 8 | from utils import query 9 | 10 | 11 | class BaseModel(object): 12 | 13 | def __init__(self, raw_data): 14 | json_data = json.loads(raw_data) 15 | for key, value in six.iteritems(json_data): 16 | setattr(self, key, value) 17 | 18 | 19 | class BaseQuerySet(object): 20 | 21 | def __init__(self): 22 | self.items = [] 23 | 24 | def order_by(self, order_attribute): 25 | ''' Return the list of items in a certain order ''' 26 | to_return = [] 27 | for f in sorted(self.items, key=lambda i: getattr(i, order_attribute)): 28 | to_return.append(f) 29 | return to_return 30 | 31 | def count(self): 32 | ''' Get the number of items in this queryset''' 33 | return len(self.items) 34 | 35 | def iter(self): 36 | ''' A generator that returns each resource in self.items ''' 37 | for i in self.items: 38 | yield i 39 | 40 | 41 | class StarshipQuerySet(BaseQuerySet): 42 | 43 | def __init__(self, list_of_urls): 44 | super(StarshipQuerySet, self).__init__() 45 | for url in list_of_urls: 46 | response = query(url) 47 | self.items.append(Starship(response.content)) 48 | 49 | def __repr__(self): 50 | return ''.format(str(len(self.items))) 51 | 52 | 53 | class Starship(BaseModel): 54 | 55 | def __init__(self, raw_data): 56 | super(Starship, self).__init__(raw_data) 57 | 58 | def __repr__(self): 59 | return ''.format(self.name) 60 | 61 | def get_films(self): 62 | return FilmQuerySet(self.films) 63 | 64 | def get_pilots(self): 65 | return PeopleQuerySet(self.pilots) 66 | 67 | 68 | class VehicleQuerySet(BaseQuerySet): 69 | 70 | def __init__(self, list_of_urls): 71 | super(VehicleQuerySet, self).__init__() 72 | for url in list_of_urls: 73 | response = query(url) 74 | self.items.append(Vehicle(response.content)) 75 | 76 | def __repr__(self): 77 | return ''.format(str(len(self.items))) 78 | 79 | 80 | class Vehicle(BaseModel): 81 | 82 | def __init__(self, raw_data): 83 | super(Vehicle, self).__init__(raw_data) 84 | 85 | def __repr__(self): 86 | return ''.format(self.name) 87 | 88 | def get_films(self): 89 | return FilmQuerySet(self.films) 90 | 91 | def get_pilots(self): 92 | return PeopleQuerySet(self.pilots) 93 | 94 | 95 | class FilmQuerySet(BaseQuerySet): 96 | 97 | def __init__(self, list_of_urls): 98 | super(FilmQuerySet, self).__init__() 99 | for url in list_of_urls: 100 | response = query(url) 101 | self.items.append(Film(response.content)) 102 | 103 | def __repr__(self): 104 | return ''.format(str(len(self.items))) 105 | 106 | 107 | class Film(BaseModel): 108 | 109 | def __init__(self, raw_data): 110 | super(Film, self).__init__(raw_data) 111 | 112 | def __repr__(self): 113 | return ''.format(self.title) 114 | 115 | def get_starships(self): 116 | return StarshipQuerySet(self.starships) 117 | 118 | def get_characters(self): 119 | return PeopleQuerySet(self.characters) 120 | 121 | def get_vehicles(self): 122 | return VehicleQuerySet(self.vehicles) 123 | 124 | def get_planets(self): 125 | return PlanetQuerySet(self.planets) 126 | 127 | def get_species(self): 128 | return SpeciesQuerySet(self.species) 129 | 130 | def gen_opening_crawl(self): 131 | ''' Return a generator yielding each line of the opening crawl''' 132 | for line in self.opening_crawl.split('\n'): 133 | yield line 134 | 135 | def print_crawl(self): 136 | ''' Print the opening crawl one line at a time ''' 137 | print("Star Wars") 138 | time.sleep(.5) 139 | print("Episode {0}".format(self.episode_id)) 140 | time.sleep(.5) 141 | print("") 142 | time.sleep(.5) 143 | print("{0}".format(self.title)) 144 | for line in self.gen_opening_crawl(): 145 | time.sleep(.5) 146 | print(line) 147 | 148 | 149 | class PlanetQuerySet(BaseQuerySet): 150 | 151 | def __init__(self, list_of_urls): 152 | super(PlanetQuerySet, self).__init__() 153 | for url in list_of_urls: 154 | response = query(url) 155 | self.items.append(Planet(response.content)) 156 | 157 | def __repr__(self): 158 | return ''.format(str(len(self.items))) 159 | 160 | 161 | class Planet(BaseModel): 162 | 163 | def __init__(self, raw_data): 164 | super(Planet, self).__init__(raw_data) 165 | 166 | def __repr__(self): 167 | return ''.format(self.name) 168 | 169 | def get_films(self): 170 | return FilmQuerySet(self.films) 171 | 172 | def get_residents(self): 173 | return PeopleQuerySet(self.residents) 174 | 175 | 176 | class SpeciesQuerySet(BaseQuerySet): 177 | 178 | def __init__(self, list_of_urls): 179 | super(SpeciesQuerySet, self).__init__() 180 | for url in list_of_urls: 181 | response = query(url) 182 | self.items.append(Species(response.content)) 183 | 184 | def __repr__(self): 185 | return ''.format(str(len(self.items))) 186 | 187 | 188 | class Species(BaseModel): 189 | 190 | def __init__(self, raw_data): 191 | super(Species, self).__init__(raw_data) 192 | 193 | def __repr__(self): 194 | return ''.format(self.name) 195 | 196 | def get_films(self): 197 | return FilmQuerySet(self.films) 198 | 199 | def get_people(self): 200 | return PeopleQuerySet(self.people) 201 | 202 | def get_homeworld(self): 203 | response = query(self.homeworld) 204 | return Planet(response.content) 205 | 206 | 207 | class PeopleQuerySet(BaseQuerySet): 208 | 209 | def __init__(self, list_of_urls): 210 | super(PeopleQuerySet, self).__init__() 211 | for url in list_of_urls: 212 | response = query(url) 213 | self.items.append(People(response.content)) 214 | 215 | def __repr__(self): 216 | return ''.format(str(len(self.items))) 217 | 218 | 219 | class People(BaseModel): 220 | ''' Representing a single person ''' 221 | 222 | def __init__(self, raw_data): 223 | super(People, self).__init__(raw_data) 224 | 225 | def __repr__(self): 226 | return ''.format(self.name) 227 | 228 | def get_starships(self): 229 | return StarshipQuerySet(self.starships) 230 | 231 | def get_films(self): 232 | return FilmQuerySet(self.films) 233 | 234 | def get_vehicles(self): 235 | return VehicleQuerySet(self.vehicles) 236 | 237 | def get_homeworld(self): 238 | response = query(self.homeworld) 239 | return Planet(response.content) 240 | 241 | def get_species(self): 242 | return SpeciesQuerySet(self.species) 243 | -------------------------------------------------------------------------------- /swapi/settings.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ''' 4 | Settings and stuff. 5 | ''' 6 | 7 | import os 8 | DEBUG = bool(os.environ.get(('DEBUG'), False)) 9 | if DEBUG: 10 | BASE_URL = 'http://localhost:8000/api' 11 | else: 12 | BASE_URL = 'http://swapi.co/api' 13 | 14 | PEOPLE = 'people' 15 | PLANETS = 'planets' 16 | STARSHIPS = 'starships' 17 | VEHICLES = 'vehicles' 18 | FILMS = 'films' 19 | SPECIES = 'species' 20 | -------------------------------------------------------------------------------- /swapi/swapi.py: -------------------------------------------------------------------------------- 1 | try: 2 | from swapi import settings 3 | from swapi.utils import query, all_resource_urls 4 | from swapi.models import ( 5 | People, 6 | PeopleQuerySet, 7 | Planet, 8 | PlanetQuerySet, 9 | Starship, 10 | StarshipQuerySet, 11 | Vehicle, 12 | VehicleQuerySet, 13 | Species, 14 | SpeciesQuerySet, 15 | Film, 16 | FilmQuerySet, 17 | ) 18 | except: 19 | import settings 20 | from utils import query, all_resource_urls 21 | from models import ( 22 | People, 23 | PeopleQuerySet, 24 | Planet, 25 | PlanetQuerySet, 26 | Starship, 27 | StarshipQuerySet, 28 | Vehicle, 29 | VehicleQuerySet, 30 | Species, 31 | SpeciesQuerySet, 32 | Film, 33 | FilmQuerySet, 34 | ) 35 | 36 | def _get(id, type): 37 | ''' Return a single person ''' 38 | result = query("{0}/{1}/{2}/".format( 39 | settings.BASE_URL, 40 | type, 41 | str(id)) 42 | ) 43 | return result 44 | 45 | 46 | def get_all(resource): 47 | ''' Return all of a single resource ''' 48 | QUERYSETS = { 49 | settings.PEOPLE: PeopleQuerySet, 50 | settings.PLANETS: PlanetQuerySet, 51 | settings.STARSHIPS: StarshipQuerySet, 52 | settings.VEHICLES: VehicleQuerySet, 53 | settings.SPECIES: SpeciesQuerySet, 54 | settings.FILMS: FilmQuerySet 55 | } 56 | 57 | urls = all_resource_urls( 58 | "{0}/{1}/".format(settings.BASE_URL, resource) 59 | ) 60 | 61 | return QUERYSETS[resource](urls) 62 | 63 | 64 | def get_planet(planet_id): 65 | ''' Return a single planet ''' 66 | result = _get(planet_id, settings.PLANETS) 67 | return Planet(result.content) 68 | 69 | 70 | def get_person(people_id): 71 | ''' Return a single person ''' 72 | result = _get(people_id, settings.PEOPLE) 73 | return People(result.content) 74 | 75 | 76 | def get_starship(starship_id): 77 | ''' Return a single starship ''' 78 | result = _get(starship_id, settings.STARSHIPS) 79 | return Starship(result.content) 80 | 81 | 82 | def get_vehicle(vehicle_id): 83 | ''' Return a single vehicle ''' 84 | result = _get(vehicle_id, settings.VEHICLES) 85 | return Vehicle(result.content) 86 | 87 | 88 | def get_species(species_id): 89 | ''' Return a single species ''' 90 | result = _get(species_id, settings.SPECIES) 91 | return Species(result.content) 92 | 93 | 94 | def get_film(film_id): 95 | ''' Return a single film ''' 96 | result = _get(film_id, settings.FILMS) 97 | return Film(result.content) 98 | -------------------------------------------------------------------------------- /swapi/utils.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import ujson as json 3 | try: 4 | from swapi import exceptions 5 | except: 6 | import exceptions 7 | 8 | 9 | def query(query): 10 | headers = {'User-Agent': 'swapi-python'} 11 | response = requests.get(query, headers=headers) 12 | if response.status_code != 200: 13 | raise exceptions.ResourceDoesNotExist('Resource does not exist') 14 | return response 15 | 16 | 17 | def all_resource_urls(query): 18 | ''' Get all the URLs for every resource ''' 19 | urls = [] 20 | next = True 21 | while next: 22 | response = requests.get(query) 23 | json_data = json.loads(response.content) 24 | for resource in json_data['results']: 25 | urls.append(resource['url']) 26 | if bool(json_data['next']): 27 | query = json_data['next'] 28 | else: 29 | next = False 30 | return urls 31 | -------------------------------------------------------------------------------- /tests/test_swapi.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | test_swapi.py 6 | ---------------------------------- 7 | 8 | Tests for `swapi` module. 9 | """ 10 | 11 | import unittest 12 | import swapi 13 | 14 | from swapi.models import ( 15 | Starship, 16 | StarshipQuerySet, 17 | Film, 18 | FilmQuerySet, 19 | Vehicle, 20 | VehicleQuerySet, 21 | Planet, 22 | PlanetQuerySet, 23 | Species, 24 | SpeciesQuerySet, 25 | PeopleQuerySet 26 | ) 27 | 28 | from swapi.utils import query 29 | from swapi.exceptions import ResourceDoesNotExist 30 | from swapi.settings import BASE_URL 31 | 32 | 33 | class SwapiTest(unittest.TestCase): 34 | 35 | def test_get_all(self): 36 | people = swapi.get_all('people') 37 | self.assertEquals(people.count(), 87) 38 | self.assertEquals('', people.__repr__()) 39 | 40 | def test_repr_(self): 41 | starship = swapi.get_starship(3) 42 | self.assertEquals('', starship.__repr__()) 43 | vehicle = swapi.get_vehicle(4) 44 | self.assertEquals('', vehicle.__repr__()) 45 | film = swapi.get_film(1) 46 | self.assertEquals('', film.__repr__()) 47 | planet = swapi.get_planet(1) 48 | self.assertEquals('', planet.__repr__()) 49 | species = swapi.get_species(1) 50 | self.assertEquals('', species.__repr__()) 51 | 52 | def test_queryset_order_by(self): 53 | planets = swapi.get_all('planets') 54 | ordered = planets.order_by('diameter') 55 | self.assertEquals(ordered[0].diameter, '0') 56 | 57 | people = swapi.get_all('people') 58 | ordered = people.order_by('name') 59 | self.assertEquals(ordered[0].name, 'Ackbar') 60 | 61 | def test_get_person(self): 62 | luke = swapi.get_person(1) 63 | self.assertEquals(luke.name, "Luke Skywalker") 64 | self.assertEquals( 65 | '', luke.__repr__()) 66 | 67 | def test_get_person_starships(self): 68 | luke = swapi.get_person(1) 69 | starships = luke.get_starships() 70 | self.assertEquals(type(starships.items[0]), Starship) 71 | self.assertEquals(starships.__repr__(), "") 72 | 73 | def test_get_person_films(self): 74 | luke = swapi.get_person(1) 75 | films = luke.get_films() 76 | self.assertEquals(type(films.items[0]), Film) 77 | self.assertEquals(films.__repr__(), "") 78 | 79 | def test_get_person_vehicles(self): 80 | luke = swapi.get_person(1) 81 | vehicles = luke.get_vehicles() 82 | self.assertEquals(type(vehicles.items[0]), Vehicle) 83 | self.assertEquals(vehicles.__repr__(), "") 84 | 85 | def test_get_person_homeworld(self): 86 | luke = swapi.get_person(1) 87 | home = luke.get_homeworld() 88 | tatooine = swapi.get_planet(1) 89 | self.assertEquals(type(home), Planet) 90 | self.assertEquals(home.name, tatooine.name) 91 | 92 | def test_get_person_species(self): 93 | luke = swapi.get_person(1) 94 | species = swapi.get_species(1) 95 | human = luke.get_species() 96 | self.assertEquals(type(species), Species) 97 | 98 | def test_species_get_people(self): 99 | species = swapi.get_species(1) 100 | self.assertEquals(type(species.get_people()), PeopleQuerySet) 101 | 102 | def test_species_get_films(self): 103 | species = swapi.get_species(1) 104 | self.assertEquals(type(species.get_films()), FilmQuerySet) 105 | 106 | def test_species_get_homeworld(self): 107 | species = swapi.get_species(1) 108 | self.assertEquals(type(species.get_homeworld()), Planet) 109 | 110 | def test_planet_get_films(self): 111 | planet = swapi.get_planet(1) 112 | self.assertEquals(type(planet.get_films()), FilmQuerySet) 113 | 114 | def test_planet_get_residents(self): 115 | planet = swapi.get_planet(1) 116 | self.assertEquals(type(planet.get_residents()), PeopleQuerySet) 117 | 118 | def test_film_get_characters(self): 119 | film = swapi.get_film(1) 120 | self.assertEquals(type(film.get_characters()), PeopleQuerySet) 121 | 122 | def test_film_get_starships(self): 123 | film = swapi.get_film(1) 124 | self.assertEquals(type(film.get_starships()), StarshipQuerySet) 125 | self.assertIn('