Skip to content
Draft
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
19 changes: 18 additions & 1 deletion pebble_tool/commands/emucontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ class EmuAppConfigCommand(PebbleCommand):

def __call__(self, args):
super(EmuAppConfigCommand, self).__call__(args)

# To use this command in a Github codespace, we need the user browser to reach the
# open port on his github codespace virtual machine instead of 'localhost' when user press
# the submit button, as this script will be listening from the codespace virtual machine.
#
# To achieve this, must be provided the following arguments:
# - '--address {addres}': address to use when sending config data, instead of 'localhost'
# - '--port {port}': port to use to retrieve config data
browser = BrowserController()

try:
if isinstance(self.pebble.transport, ManagedEmulatorTransport):
self.pebble.transport.send_packet(WebSocketPhonesimAppConfig(config=AppConfigSetup()),
Expand All @@ -117,7 +127,12 @@ def __call__(self, args):
else:
config_url = response.config.data

browser = BrowserController()
if args.address:
browser.configure_address(args.address)

if args.port:
browser.configure_port(args.port)

browser.open_config_page(config_url, self.handle_config_close)

def handle_config_close(self, query):
Expand All @@ -132,6 +147,8 @@ def handle_config_close(self, query):
def add_parser(cls, parser):
parser = super(EmuAppConfigCommand, cls).add_parser(parser)
parser.add_argument('--file', help="Name of local file to use for settings page in lieu of URL specified in JS")
parser.add_argument('--address', help="Provide the return address, instead of using localhost")
parser.add_argument('--port', help="Use the specified port instead of creating one")
return parser


Expand Down
18 changes: 15 additions & 3 deletions pebble_tool/util/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@
class BrowserController(object):
def __init__(self):
self.port = None
self.address = None

def configure_port(self, port):
self.port = port

def configure_address(self, address):
self.address = address

def open_config_page(self, url, callback):
self.port = port = self._choose_port()
url = self.url_append_params(url, {'return_to': 'http://localhost:{}/close?'.format(port)})
if self.port == None:
self.port = self._choose_port()
host = 'http://localhost:{}/close?'.format(self.port)
if self.address:
host = '{}/close?'.format(self.address)
url = self.url_append_params(url, {'return_to': host})
webbrowser.open_new(url)
self.serve_page(port, callback)
self.serve_page(int(self.port), callback)
self.__init__()

def serve_page(self, port, callback):
# This is an array so AppConfigHandler doesn't create an instance variable when trying to set the state to False
Expand Down