Skip to content

Commit

Permalink
Merge pull request #34 from travelton/excluded_extensions
Browse files Browse the repository at this point in the history
Feature: Ignore File Extensions
  • Loading branch information
tjwalch authored Dec 19, 2018
2 parents 7ce366f + c401ce1 commit ea3edaa
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

Version 0.3.3
------------
Addition of ignored file extensions


Version 0.1
------------
First release
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ Or staticfiles directories using: ::

$ ./manage.py livereload --ignore-static-dirs

You can ignore file extensions: ::

$ ./manage.py livereload --ignore-file-extensions=.less,.scss


Extra files and/or paths to watch for changes can be added as positional arguments. By default livereload server watches the files that are found by your staticfiles finders and your template loaders. ::

$ python manage.py livereload path/to/my-extra-directory/
Expand Down
2 changes: 1 addition & 1 deletion livereload/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""django-livereload"""
__version__ = '0.3.2'
__version__ = '0.3.3'
__license__ = 'BSD License'

__author__ = 'Tomas Walch'
Expand Down
9 changes: 9 additions & 0 deletions livereload/management/commands/livereload.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def add_arguments(self, parser):
action='store',
help='Extra files or directories to watch',
)
parser.add_argument(
'--ignore-file-extensions',
action='store',
help='File extensions to ignore',
)
parser.add_argument(
'--host', dest='host', default=livereload_host(), help='Host address for livereload sever.'
)
Expand Down Expand Up @@ -54,6 +59,10 @@ def handle(self, *args, **options):
watch_dirs.extend([os.path.join(app_config.path, 'static')
for app_config in app_configs])

ignore_file_extensions = options.get('ignore_file_extensions', '').split(',')
for extension in ignore_file_extensions:
server.ignore_file_extension(extension.strip())

for dir in filter(None, watch_dirs):
server.watch(dir)

Expand Down
12 changes: 11 additions & 1 deletion livereload/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,22 @@ class Server(object):
CPU usage.
"""
def __init__(self, watcher=None):
self._setup_logging()
if not watcher:
watcher_cls = get_watcher_class()
watcher = watcher_cls()
self.watcher = watcher

def ignore_file_extension(self, extension):
"""
Configure a file extension to be ignored.
:param extension: file extension to be ignored
(ex. .less, .scss, etc)
"""
logger.info('Ignoring file extension: {}'.format(extension))
self.watcher.ignore_file_extension(extension)

def watch(self, filepath, func=None, delay=None):
"""Add the given filepath for watcher list.
Expand Down Expand Up @@ -137,7 +148,6 @@ def serve(self, liveport=None, host=None, restart_delay=2):
:param open_url_delay: open webbrowser after the delay seconds
"""
host = host or '127.0.0.1'
self._setup_logging()
logger.info('Serving on http://%s:%s' % (host, liveport))

self.application(host, liveport=liveport)
Expand Down
14 changes: 10 additions & 4 deletions livereload/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ def __init__(self):
self.filepath = None
self._start = time.time()

def ignore(self, filename):
"""Ignore a given filename or not."""
# ignored file extensions
self.ignored_file_extensions = ['.pyc', '.pyo', '.o', '.swp']

def should_ignore(self, filename):
"""Should ignore a given filename?"""
_, ext = os.path.splitext(filename)
return ext in ['.pyc', '.pyo', '.o', '.swp']
return ext in self.ignored_file_extensions

def ignore_file_extension(self, extension):
self.ignored_file_extensions.append(extension)

def watch(self, path, func=None, delay=0, ignore=None):
"""Add a task to watcher.
Expand Down Expand Up @@ -94,7 +100,7 @@ def is_file_changed(self, path, ignore=None):
if not os.path.isfile(path):
return False

if self.ignore(path):
if self.should_ignore(path):
return False

if ignore and ignore(path):
Expand Down

0 comments on commit ea3edaa

Please sign in to comment.