handle no arguments or non-file/non-existen filenames

When using `pecan serve` we should be able to handle nicely
when no options or bad filenames are given to avoid seeing a
not so helpful traceback.
This commit is contained in:
Alfredo Deza
2011-11-17 12:08:13 -05:00
parent 282c0ea2f9
commit 298bed253d

View File

@@ -1,11 +1,12 @@
""" """
PasteScript serve command for Pecan. PasteScript serve command for Pecan.
""" """
from paste import httpserver from paste import httpserver
from paste.script import command as paste_command
from paste.script.serve import ServeCommand as _ServeCommand from paste.script.serve import ServeCommand as _ServeCommand
from base import Command from base import Command
import os
import re import re
@@ -44,9 +45,11 @@ class ServeCommand(_ServeCommand, Command):
setattr(self.options, 'server', None) setattr(self.options, 'server', None)
setattr(self.options, 'server_name', None) setattr(self.options, 'server_name', None)
config_file = self.validate_file(self.args)
# for file-watching to work, we need a filename, not a module # for file-watching to work, we need a filename, not a module
if self.requires_config_file and self.args: if self.requires_config_file and self.args:
self.config = self.load_configuration(self.args[0]) self.config = self.load_configuration(config_file)
self.args[0] = self.config.__file__ self.args[0] = self.config.__file__
if self.options.reload is None: if self.options.reload is None:
self.options.reload = getattr(self.config.app, 'reload', False) self.options.reload = getattr(self.config.app, 'reload', False)
@@ -59,3 +62,8 @@ class ServeCommand(_ServeCommand, Command):
def loadapp(self, app_spec, name, relative_to, **kw): def loadapp(self, app_spec, name, relative_to, **kw):
return self.load_app(self.config) return self.load_app(self.config)
def validate_file(self, argv):
if not argv or not os.path.isfile(argv[0]):
raise paste_command.BadCommand('This command needs a valid config file.')
return argv[0]