diff --git a/pyramid/scripts/pserve.py b/pyramid/scripts/pserve.py index caa73704ad..18a1e2c2fc 100644 --- a/pyramid/scripts/pserve.py +++ b/pyramid/scripts/pserve.py @@ -146,11 +146,11 @@ def pserve_file_config(self, filename, global_conf=None): config = self.ConfigParser(defaults=defaults) config.optionxform = str - config.read(filename) try: + config.read(filename) items = dict(config.items('pserve')) - except configparser.NoSectionError: - items = {} + except (IOError, configparser.NoSectionError): + return watch_files = aslist(items.get('watch_files', ''), flatten=False) @@ -163,10 +163,31 @@ def pserve_file_config(self, filename, global_conf=None): file = os.path.join(here, file) self.watch_files.add(os.path.abspath(file)) + # attempt to determine the url of the server open_url = items.get('open_url') if open_url: self.open_url = open_url + def _guess_server_url(self, filename, server_name, + global_conf=None): # pragma: no cover + server_name = server_name or 'main' + here = os.path.abspath(os.path.dirname(filename)) + defaults = {} + if global_conf: + defaults.update(global_conf) + defaults['here'] = here + + config = self.ConfigParser(defaults=defaults) + config.optionxform = str + try: + config.read(filename) + items = dict(config.items('server:' + server_name)) + except (IOError, configparser.NoSectionError): + return + + if 'port' in items: + return 'http://127.0.0.1:{port}'.format(**items) + def run(self): # pragma: no cover if not self.args.config_uri: self.out('You must give a config file') @@ -194,6 +215,12 @@ def run(self): # pragma: no cover if self.args.browser and not hupper.is_active(): self.pserve_file_config(config_path, global_conf=vars) url = self.open_url + + # do not guess the url if the server is sourced from a different + # location than the config_path + if not url and server_spec == app_spec: + url = self._guess_server_url(config_path, server_name, vars) + if not url: self.out('WARNING: could not determine the server\'s url to ' 'open the browser. To fix this set the "open_url" ' diff --git a/pyramid/tests/test_scripts/test_pserve.py b/pyramid/tests/test_scripts/test_pserve.py index bb3303a104..d5578b3eae 100644 --- a/pyramid/tests/test_scripts/test_pserve.py +++ b/pyramid/tests/test_scripts/test_pserve.py @@ -80,6 +80,32 @@ def test_config_file_finds_watch_files(self): os.path.abspath(os.path.join(here, '*.py')), ])) + def test_config_file_finds_open_url(self): + inst = self._makeOne('development.ini') + self.config_factory.items = [( + 'open_url', 'http://127.0.0.1:8080/', + )] + inst.pserve_file_config('/base/path.ini', global_conf={'a': '1'}) + self.assertEqual(self.config_factory.defaults, { + 'a': '1', + 'here': os.path.abspath('/base'), + }) + self.assertEqual(inst.open_url, 'http://127.0.0.1:8080/') + + def test__guess_server_url(self): + inst = self._makeOne('development.ini') + self.config_factory.items = [( + 'port', '8080', + )] + url = inst._guess_server_url( + '/base/path.ini', 'main', global_conf={'a': '1'}) + self.assertEqual(self.config_factory.defaults, { + 'a': '1', + 'here': os.path.abspath('/base'), + }) + self.assertEqual(self.config_factory.parser.section, 'server:main') + self.assertEqual(url, 'http://127.0.0.1:8080') + def test_reload_call_hupper_with_correct_args(self): from pyramid.scripts import pserve