def patch_webbrowser(): """ Patch webbrowser on macOS to support setting BROWSER=firefox, BROWSER=chrome, etc.. https://bugs.python.org/issue31348 """ # Add support for browsers that aren't defined in the python standard library webbrowser.register('surf', None, webbrowser.BackgroundBrowser('surf')) webbrowser.register('vimb', None, webbrowser.BackgroundBrowser('vimb')) # Fix the opera browser, see https://github.com/michael-lazar/rtv/issues/476. # By default, opera will open a new tab in the current window, which is # what we want to do anyway. webbrowser.register('opera', None, webbrowser.BackgroundBrowser('opera')) if sys.platform != 'darwin' or 'BROWSER' not in os.environ: return # This is a copy of what's at the end of webbrowser.py, except that # it adds MacOSXOSAScript entries instead of GenericBrowser entries. _userchoices = os.environ["BROWSER"].split(os.pathsep) for cmdline in reversed(_userchoices): if cmdline in ('safari', 'firefox', 'chrome', 'default'): browser = webbrowser.MacOSXOSAScript(cmdline) try: webbrowser.register(cmdline, None, browser, update_tryorder=-1) except TypeError: # 3.7 nightly build changed the method signature # pylint: disable=unexpected-keyword-arg webbrowser.register(cmdline, None, browser, preferred=True)
def patch_webbrowser(): """ Some custom patches on top of the python webbrowser module to fix user reported bugs and limitations of the module. """ # https://bugs.python.org/issue31014 # https://github.com/michael-lazar/rtv/issues/588 def register_patch(name, klass, instance=None, update_tryorder=None, preferred=False): """ Wrapper around webbrowser.register() that detects if the function was invoked with the legacy function signature. If so, the signature is fixed before passing it along to the underlying function. Examples: register(name, klass, instance, -1) register(name, klass, instance, update_tryorder=-1) register(name, klass, instance, preferred=True) """ if update_tryorder is not None: preferred = (update_tryorder == -1) return webbrowser._register(name, klass, instance, preferred=preferred) if sys.version_info[:2] >= (3, 7): webbrowser._register = webbrowser.register webbrowser.register = register_patch # Add support for browsers that aren't defined in the python standard library webbrowser.register('surf', None, webbrowser.BackgroundBrowser('surf')) webbrowser.register('vimb', None, webbrowser.BackgroundBrowser('vimb')) webbrowser.register('qutebrowser', None, webbrowser.BackgroundBrowser('qutebrowser')) # Fix the opera browser, see https://github.com/michael-lazar/rtv/issues/476. # By default, opera will open a new tab in the current window, which is # what we want to do anyway. webbrowser.register('opera', None, webbrowser.BackgroundBrowser('opera')) # https://bugs.python.org/issue31348 # Use MacOS actionscript when opening the program defined in by $BROWSER if sys.platform == 'darwin' and 'BROWSER' in os.environ: _userchoices = os.environ["BROWSER"].split(os.pathsep) for cmdline in reversed(_userchoices): if cmdline in ('safari', 'firefox', 'chrome', 'default'): browser = webbrowser.MacOSXOSAScript(cmdline) # pylint: disable=unexpected-keyword-arg webbrowser.register(cmdline, None, browser, update_tryorder=-1)
def __init__(self, solution): self.candidate_solution = solution self.template = env.get_template('table_template.txt') self.filepath = "file://" + os.path.realpath("schedule.html") self.generation = "0" self.times = config.config_times() self.classrooms = config.config_rooms() self.render_temp() if sys.platform[:3] == "win": webbrowser.register('mychrome', None, webbrowser.BackgroundBrowser(browser_path)) elif sys.platform == 'darwin': webbrowser.register('mychrome', None, webbrowser.MacOSXOSAScript('Google Chrome'), -1) # NOTE: might be different on msft webbrowser.get('mychrome').open(self.filepath)
def patch_webbrowser(): """ Patch webbrowser on macOS to support setting BROWSER=firefox, BROWSER=chrome, etc.. https://bugs.python.org/issue31348 """ if sys.platform != 'darwin' or 'BROWSER' not in os.environ: return # This is a copy of what's at the end of webbrowser.py, except that # it adds MacOSXOSAScript entries instead of GenericBrowser entries. _userchoices = os.environ["BROWSER"].split(os.pathsep) for cmdline in reversed(_userchoices): if cmdline in ('safari', 'firefox', 'chrome', 'default'): browser = webbrowser.MacOSXOSAScript(cmdline) try: webbrowser.register(cmdline, None, browser, update_tryorder=-1) except TypeError: # 3.7 nightly build changed the method signature # pylint: disable=unexpected-keyword-arg webbrowser.register(cmdline, None, browser, preferred=True)
ips = ifaddresses(ifaceName).setdefault(AF_INET) if (ips != None): for i in ips: currentaddress = str(i['addr']) # Only add real ips if (currentaddress.startswith('127') == False and currentaddress.startswith('169') == False): DEFAULT_HOST = currentaddress done = True break except: DEFAULT_HOST = '0.0.0.0' return DEFAULT_HOST if sys.platform == 'darwin': webbrowser.register("chrome", None, webbrowser.MacOSXOSAScript('chrome')) def LaunchApplication( fnames ): for fname in (fnames if isinstance(fnames, list) else [fnames]): if os.name == 'nt': # Windows. subprocess.call(('cmd', '/C', 'start', '', fname)) elif sys.platform.startswith('darwin'): subprocess.call(('open', fname)) # Apple else: subprocess.call(('xdg-open', fname)) # Linux def BoldFromFont( font ): # pointSize, family, style, weight, underline=False, face="", encoding return wx.Font( font.GetPixelSize(), font.GetFamily(),
def Run(flow, launch_browser=True, http=None, auth_host_name='localhost', auth_host_port_start=8085): """Run a web flow to get oauth2 credentials. Args: flow: oauth2client.OAuth2WebServerFlow, A flow that is ready to run. launch_browser: bool, If False, give the user a URL to copy into a browser. Requires that they paste the refresh token back into the terminal. If True, opens a web browser in a new window. http: httplib2.Http, The http transport to use for authentication. auth_host_name: str, Host name for the redirect server. auth_host_port_start: int, First port to try for serving the redirect. If this port is taken, it will keep trying incrementing ports until 100 have been tried, then fail. Returns: oauth2client.Credential, A ready-to-go credential that has already been put in the storage. Raises: AuthRequestRejectedException: If the request was rejected. AuthRequestFailedException: If the request fails. """ if launch_browser: # pylint:disable=g-import-not-at-top, Import when needed for performance. import socket import webbrowser success = False port_number = auth_host_port_start while True: try: httpd = tools.ClientRedirectServer( (auth_host_name, port_number), ClientRedirectHandler) except socket.error as e: if port_number > auth_host_port_start + 100: success = False break port_number += 1 else: success = True break if success: flow.redirect_uri = ('http://%s:%s/' % (auth_host_name, port_number)) authorize_url = flow.step1_get_authorize_url() # Without this, Chrome on MacOS will not launch unless Chrome # is already open. This is due to an bug in webbbrowser.py that tries to # open web browsers by app name using i.e. 'Chrome' but the actual app # name is 'Google Chrome' on Mac. if platforms.OperatingSystem.MACOSX == platforms.OperatingSystem.Current( ): try: webbrowser.register( 'Google Chrome', None, webbrowser.MacOSXOSAScript('Google Chrome'), -1) except AttributeError: # If MacOSXOSAScript not defined on module, pass # proceed with default behavior webbrowser.open(authorize_url, new=1, autoraise=True) message = 'Your browser has been opened to visit:' log.err.Print('{message}\n\n {url}\n\n'.format( message=message, url=authorize_url, )) httpd.handle_request() if 'error' in httpd.query_params: raise AuthRequestRejectedException('Unable to authenticate.') if 'code' in httpd.query_params: code = httpd.query_params['code'] else: raise AuthRequestFailedException( 'Failed to find "code" in the query parameters of the redirect.' ) else: message = ( 'Failed to start a local webserver listening on any port ' 'between {start_port} and {end_port}. Please check your ' 'firewall settings or locally running programs that may be ' 'blocking or using those ports.') log.warning( message.format( start_port=auth_host_port_start, end_port=port_number, )) launch_browser = False log.warning('Defaulting to URL copy/paste mode.') if not launch_browser: flow.redirect_uri = client.OOB_CALLBACK_URN authorize_url = flow.step1_get_authorize_url() message = 'Go to the following link in your browser:' log.err.Print('{message}\n\n {url}\n\n'.format( message=message, url=authorize_url, )) try: code = input('Enter verification code: ').strip() except EOFError as e: raise AuthRequestRejectedException(e) try: credential = flow.step2_exchange(code, http=http) except client.FlowExchangeError as e: raise AuthRequestFailedException(e) except ResponseNotReady: raise AuthRequestFailedException( 'Could not reach the login server. A potential cause of this could be ' 'because you are behind a proxy. Please set the environment variables ' 'HTTPS_PROXY and HTTP_PROXY to the address of the proxy in the format ' '"protocol://address:port" (without quotes) and try again.\n' 'Example: HTTPS_PROXY=https://192.168.0.1:8080') return credential
import plotly.offline #import plotly.offline as po import os import uuid import webbrowser from plotly import optional_imports, tools, utils from pkg_resources import resource_string from requests.compat import json as _json #from plotly.offline import get_plotlyjs as pjs #from plotly.offline import get_plotlyjs as pjs #import sys #if sys.platform[:3] == "win": #if sys.platform == 'darwin': webbrowser.register('mychrome', None, webbrowser.MacOSXOSAScript('Google Chrome'), -1) #this registers the Chrome browser class PlotTool(): def __init__(self, **kwargs): pass class qagPlot(): # def __init__(self,**kwargs): # self.data=[] # self.layout=go.Layout() # self.config=None # self.use_iplot=True # self.auto_update=True