Skip to content

Commit

Permalink
Add: LiveReload executor (#5)
Browse files Browse the repository at this point in the history
* Add: LiveReload executor

* Clean egg info

* Update doc

* Add: lock file

* Add: wheel

* Add: livereload

* Update travis

* Bump dependencies

* Fix: pylint

* Drop: sphinx-autobuild

* Fix: pass kwargs to serve

* Fix: only run linter on latest python

* fixup! Fix: only run linter on latest python

* Fix: run pylint on python >= 3.5

* Try using ignore-requires-python

* Try using python-version

* Fix: --no-deps

* Verbose log

* Fix: use xargs

* Fix: don't terminate

* Fix: only use pathlib2 if pathlib is not available
  • Loading branch information
eight04 committed Nov 1, 2019
1 parent cd26f45 commit 0f48157
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 70 deletions.
8 changes: 4 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[*.py]
[*]
indent_style = space
indent_size = 4
indent_size = 2

[*.rst]
[*.py]
indent_style = space
indent_size = 2
indent_size = 4
35 changes: 1 addition & 34 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,43 +1,10 @@
# Backup files
*.~
*.bak

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Sphinx documentation
docs/_build/
.venv
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ disable=
design,

missing-docstring,
trailing-whitespace
trailing-whitespace,
import-outside-toplevel

[BASIC]
variable-rgx=^[a-z_]{1,}$
Expand Down
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ python:
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
install:
- python -m pip install -U pip
- pip install -e .
- xargs -a requirements-lock.txt -I % bash -c "pip install %" || true
script:
- python cute.py test
19 changes: 7 additions & 12 deletions cute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

import sys

from xcute import cute, Skip

def readme():
"""Live reload readme"""
from livereload import Server
server = Server()
server.watch("README.rst", "py cute.py readme_build")
server.serve(open_url_delay=1, root="build/readme")
from xcute import cute, Skip, LiveReload

cute(
pkg_name = 'xcute',
lint = 'pylint xcute cute setup',
test = [Skip("lint", sys.version_info[:2] != (3, 6)), 'python test.py', 'readme_build'],
test = [Skip("lint", sys.version_info < (3, 5)), 'python test.py', 'readme_build'],
bump_pre = 'test',
bump_post = ['clean', 'dist', 'release', 'publish', 'install'],
clean = 'x-clean build dist',
clean = 'x-clean build dist *.egg-info',
dist = 'python setup.py sdist bdist_wheel',
release = [
'git add .',
Expand All @@ -36,6 +29,8 @@ def readme():
'build/readme/index.rst build/readme/index.html')
],
readme_pre = "readme_build",
readme = readme,
doc = 'sphinx-autobuild -B -z {pkg_name} docs docs/build'
readme = LiveReload("README.rst", "readme_build", "build/readme"),
doc_build = "sphinx-build docs build/docs",
doc_pre = "doc_build",
doc = LiveReload(["{pkg_name}", "docs"], "doc_build", "build/docs")
)
3 changes: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ Classes
.. autoclass:: Cmd
:members: __call__

.. autoclass:: LiveReload
:members: __call__

.. autoclass:: Log
:members: __call__

Expand Down
53 changes: 53 additions & 0 deletions requirements-lock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
alabaster==0.7.12
argh==0.26.2
astroid==2.3.2
Babel==2.7.0
bleach==3.1.0
certifi==2019.9.11
chardet==3.0.4
colorama==0.4.1
docutils==0.15.2
idna==2.8
imagesize==1.1.0
isort==4.3.21
Jinja2==2.10.3
lazy-object-proxy==1.4.3
livereload==2.6.1
MarkupSafe==1.1.1
mccabe==0.6.1
natsort==6.0.0
ordered-set==3.1.1
packaging==19.2
pathlib2==2.3.5
pathtools==0.1.2
pkginfo==1.5.0.1
port-for==0.3.1
Pygments==2.4.2
pylint==2.4.3
pyparsing==2.4.2
pytz==2019.3
PyYAML==5.1.2
readme-renderer==24.0
requests==2.22.0
requests-toolbelt==0.9.1
semver==2.9.0
Send2Trash==1.5.0
setuptools==41.2.0
six==1.12.0
snowballstemmer==2.0.0
Sphinx==2.2.1
sphinxcontrib-applehelp==1.0.1
sphinxcontrib-devhelp==1.0.1
sphinxcontrib-htmlhelp==1.0.2
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.2
sphinxcontrib-serializinghtml==1.1.3
sphinxcontrib-websupport==1.1.2
tornado==6.0.3
tqdm==4.36.1
twine==2.0.0
urllib3==1.25.6
watchdog==0.9.0
webencodings==0.5.1
wheel==0.33.6
wrapt==1.11.2
14 changes: 6 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
docutils>=0.14,<0.15
twine>=1.6.5,<2
Pygments>=2.1.3,<3
pylint>=1.8.4,<2
sphinx-autobuild>=0.7.1,<0.8
Sphinx>=1.6.5,<2
livereload>=2.5.2,<3
-e .
Pygments==2.4.2
Sphinx==2.2.1
docutils==0.15.2
pylint==2.4.3
twine==2.0.0
wheel==0.33.6
16 changes: 11 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ classifiers =
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Topic :: Software Development :: Libraries :: Python Modules
Topic :: Software Development :: Build Tools

Expand All @@ -29,15 +31,19 @@ zip_safe = True
packages = find:

install_requires =
semver >= 2.4.1, < 3
ordered-set >= 2.0.1, < 3
send2trash >= 1.3.0, < 2
natsort >= 5.0.2, < 6
pathlib2 >= 2.3.2, < 3; python_version < "3.5"
livereload~=2.6
natsort~=6.0
ordered-set~=3.1
pathlib2~=2.3;python_version < "3.5"
semver~=2.9
send2trash~=1.5

[options.entry_points]
console_scripts =
x-clean = xcute.utils:clean
x-cat = xcute.utils:concat
x-copy = xcute.utils:copy
x-pipe = xcute.utils:pipe

[vpip]
command_fallback = python cute.py
47 changes: 41 additions & 6 deletions xcute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from inspect import isclass
import re
try:
import pathlib2 as pathlib
except ImportError:
import pathlib
except ImportError:
import pathlib2 as pathlib
import sys
import shlex
import subprocess
Expand Down Expand Up @@ -379,6 +379,41 @@ def __call__(self, *args):
run_task(task, *args)
except Exception as err: # pylint: disable=broad-except
log(err)

class LiveReload:
"""An executor which spawns a live-reload server."""
def __init__(self, pattern, task, html_base, **kwargs):
"""
:arg pattern: Glob pattern, filename, or folder to be watched.
:type pattern: str or list[str]
:arg task: A task that should be run when the file changes.
:arg str html_base: Path to the folder containing HTML files.
:arg kwargs: Other arguments will be passed to `Server.serve
<https://github.com/lepture/python-livereload/blob/d5f6a2e3fab5e4308dc744e26792ce83581703d9/livereload/server.py#L275>`__
"""
self.pattern = [pattern] if isinstance(pattern, str) else pattern
self.task = task
self.html_base = html_base
self.kwargs = kwargs

def __call__(self):
"""When called, spawn a `livereload <https://github.com/lepture/python-livereload>`__
server.
`A live example <https://github.com/eight04/pyXcute/blob/master/cute.py>`__
"""
from livereload import Server
# pylint: disable=no-member
# https://github.com/lepture/python-livereload/issues/209
import asyncio
try:
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
except AttributeError:
pass
server = Server()
for p in self.pattern:
server.watch(f(p), lambda: run_task(self.task))
server.serve(open_url_delay=1, root=self.html_base, **self.kwargs)

def cute(**tasks):
"""Main entry point.
Expand Down Expand Up @@ -431,10 +466,10 @@ def cute(**tasks):
except subprocess.CalledProcessError as err:
# printing stack trace of process error doesn't help
log(err)
exit(1)
sys.exit(1)
except Exception: # pylint: disable=broad-except
traceback.print_exc()
exit(1)
sys.exit(1)

def parse_args(args=None):
"""Parse sys.argv. Export "init", "args" to config."""
Expand Down Expand Up @@ -545,8 +580,7 @@ def my_task():
}
"""A dictionary that is used across all tasks. It has 2 purpoeses:
1. A convenience way to share variables between your ``cute.py`` file and
``xcute`` module.
1. A convenience way to share variables between your ``cute.py`` file and ``xcute`` module.
2. The context for string formatting. See :func:`f`.
By default, it has following keys:
Expand All @@ -572,6 +606,7 @@ def my_task():
* ``tasks``: ``dict``. This is what you send to :func:`cute`.
* ``tty``: ``bool``. ``True`` if the output is a terminal.
* ``version``: ``str``. A version number. Also see :func:`cute`.
"""

task_converter = TaskConverter()
Expand Down

0 comments on commit 0f48157

Please sign in to comment.