/usr/lib/python2.7/dist-packages/easydev/browser.py is in python-easydev 0.9.35+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | # -*- python -*-
# -*- coding: utf-8 -*-
#
# This file is part of the easydev software
#
# Copyright (c) 2011-2017
#
# File author(s): Thomas Cokelaer <cokelaer@gmail.com>
#
# Distributed under the GPLv3 License.
# See accompanying file LICENSE.txt or copy at
# http://www.gnu.org/licenses/gpl-3.0.html
#
# Website: https://github.com/cokelaer/easydev
# Documentation: http://easydev-python.readthedocs.io
#
##############################################################################
"""Universal browser
This module provides a browser in 2 flavours: as a program to use in a Terminal,
or as a Python function that can be used in other software. The underlying code is based on the standard python module :mod:`webbrowser`. With webbrowser module itself, you can already open a URL as follows in a command line interface::
python -m webbrowser -t "http://www.python.org"
However, with **browse**, you can simply type::
browse http://www.python.org
It does not seem to be a big improvments but it is a bit more flexible. First,
there is no need to enter "http://" : it will be added if missing and if this is not a local file.::
browse docs.python.org
browse http://docs.python.org --verbose
Similarly, you can open an image (it uses the default image viewer)::
browse image.png
Or a txt file (or any document provided there is a default executable
to open it). It works like a charm under Linux. Under MAC, it uses the **open**
command so this should also work.
When invoking **browse**, under MacOSX, it actually tries to call **open**
first and then calls webbrowser, if unsuccessful only. Note tested under
Windows but uses webbrowser is used and works for open HTML document and URLs.
You can also look at a directory (starts nautilus under Fedora)::
browse ~/Pictures
See more examples below.
The interest of **browse** is that it can also be used programmatically::
from easydev.browser import browse
# open an image with the default image viewer:
browse("image.png")
# or a web page
browse("http://www.uniprot.org")
There is also an alias **onweb**::
from easydev import onweb
"""
import os
import sys, webbrowser
from optparse import OptionParser
import argparse
def browse(url, verbose=True):
from sys import platform as _platform
if _platform == "linux" or _platform == "linux2":
_browse_linux(url, verbose=True)
elif _platform == "darwin":
# under Mac, it looks like the standard webbrowser may not work as smoothly
# OS X
_browse_mac(url, verbose)
elif _platform == "win32":
# for windows and others, the same code as Linux should work
_browse_linux(url, verbose=True)
else:
_browse_linux(url, verbose=True)
def _browse_mac(url, verbose=True):
if verbose:
print("openning %s" % url)
import os
try:
os.system("open /Applications/Safari.app {}".format(url))
return
except:
pass
try:
os.system("open /Applications/Safari.app {}".format("http://" + url))
return
except:
pass
try:
webbrowser.open_new(url)
except:
if verbose:
print("Could not open %s. Trying to append http://" % url)
try:
webbrowser.open_new("open http://{}".format(url))
except:
print("Could not open http://%s" % url)
raise Exception
def _browse_linux(url, verbose=True):
if verbose:
print("openning %s" % url)
try:
webbrowser.open(url)
return
except:
pass
try:
if verbose:
print("Could not open %s" % url)
webbrowser.open("http://" + url)
return
except:
pass
raise Exception("Could not open http://{}".format(url))
def main(args=None):
if args is None:
args = sys.argv[:]
# check for verbosity
if "--verbose" in args:
verbose = True
args.remove("--verbose")
print(args)
else:
verbose = False
if "--help" in args or len(args) == 1:
print("Browse, a simple command line browser")
print("Author: Thomas Cokelaer, (c) 2012.")
print("USAGE\n\tbrowse http://docs.python.org ")
print("\tbrowse http://docs.python.org --verbose")
print("\tbrowse localfile.html")
print("\tbrowse local_directory (Linux only ?)")
return
url = args[1]
if os.path.exists(url):
if verbose:
print("%s is local file. Trying to open it.\n" % url)
browse(url, verbose)
else:
if verbose:
print("%s seems to be a web address. Trying to open it.\n" %url)
if url.startswith("http"):
browse(url, verbose)
else:
if verbose:
print("%s does not exists and does not starts with http, trying anyway." %url)
browse("http://"+url, verbose)
if __name__ == "__main__":
import sys
main(sys.argv)
|