Skip to content

Commit

Permalink
support opening the browser via pserve.open_url config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Mar 29, 2017
1 parent 50cebd7 commit 24ce680
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
33 changes: 30 additions & 3 deletions pyramid/scripts/pserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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')
Expand Down Expand Up @@ -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" '
Expand Down
26 changes: 26 additions & 0 deletions pyramid/tests/test_scripts/test_pserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 24ce680

Please sign in to comment.