Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for extensionless files #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions livereload/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@
type=int,
default=0
)

parser.add_argument(
'-e', '--extension',
help='Default extension for extensionless files',
type=str,
default=None,
)

def main(argv=None):
args = parser.parse_args()

# Create a new application
server = Server()
server.watcher.watch(args.directory, delay=args.wait)
server.serve(host=args.host, port=args.port, root=args.directory)
server.serve(host=args.host, port=args.port, root=args.directory, default_extension=args.extension)
21 changes: 21 additions & 0 deletions livereload/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,24 @@ def get(self):
class StaticFileHandler(web.StaticFileHandler):
def should_return_304(self):
return False

def initialize(self, path, default_filename=None, default_extension=None):
super(StaticFileHandler, self).initialize(path, default_filename)
self.default_extension = default_extension

def validate_absolute_path(self, root, absolute_path):
"""
Validate and return the absolute path.

Same behavior as parent StaticFileHandler class, except that
if the file is not found and does not have a file extension,
then ``default_extension`` is appended to the filename is it
is set and such a file exists.
"""
if (self.default_extension is not None
and not os.path.exists(absolute_path)
and os.path.splitext(absolute_path)[1] == ''
and os.path.exists(absolute_path + self.default_extension)):
# Append self.default_extension to extensionless file name.
absolute_path += self.default_extension
return super(StaticFileHandler, self).validate_absolute_path(root, absolute_path)
9 changes: 8 additions & 1 deletion livereload/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class Server(object):
"""
def __init__(self, app=None, watcher=None):
self.root = None
self.default_extension = None

self.app = app
if not watcher:
Expand Down Expand Up @@ -257,11 +258,12 @@ def get_web_handlers(self, script):
(r'/(.*)', StaticFileHandler, {
'path': self.root or '.',
'default_filename': 'index.html',
'default_extension': self.default_extension,
}),
]

def serve(self, port=5500, liveport=None, host=None, root=None, debug=None,
open_url=False, restart_delay=2, open_url_delay=None):
open_url=False, restart_delay=2, open_url_delay=None, default_extension=None):
"""Start serve the server with the given port.

:param port: serve on this port, default is 5500
Expand All @@ -272,11 +274,16 @@ def serve(self, port=5500, liveport=None, host=None, root=None, debug=None,
via Tornado (and causes polling). Defaults to True when
``self.app`` is set, otherwise False.
:param open_url_delay: open webbrowser after the delay seconds
:param default_extension: serve extensionless files with this extension
(set to ``".html"`` and ``foo/bar`` returns the file
``foo/bar.html``). Default is None (disabled).
"""
host = host or '127.0.0.1'
if root is not None:
self.root = root

self.default_extension = default_extension

self._setup_logging()
logger.info('Serving on http://%s:%s' % (host, port))

Expand Down