Skip to content

Commit

Permalink
Merge branch 'webbrowser'
Browse files Browse the repository at this point in the history
see pull request #77

Conflicts:
	isrcsubmit.py
  • Loading branch information
JonnyJD committed Oct 8, 2013
2 parents de24839 + 3c41e00 commit 9535ba5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
4 changes: 2 additions & 2 deletions doc/isrcsubmit.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Options
backends are: mediatools, media_info, cdrdao, libdiscid, discisrc. They are
tried in this order otherwise.
--browser=<browser>
Program to open URLs. If not given, xdg-open, firefox, chromium, opera and
explorer are tried.
Program to open URLs. This will be automatically deteced for most setups,
if not chosen manually.
--force-submit
Always open TOC/disc ID submission page in browser.
--server=<server>
Expand Down
76 changes: 54 additions & 22 deletions isrcsubmit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
# starting with highest priority
BACKENDS = ["mediatools", "media_info", "cdrdao", "libdiscid", "discisrc"]
BROWSERS = ["xdg-open", "x-www-browser",
"firefox", "chromium", "opera", "safari", "explorer"]
"firefox", "chromium", "chrome", "opera"]
# The webbrowser module is used when nothing is found in this list.
# This especially happens on Windows and Mac OS X (browser mostly not in PATH)

import os
import re
import sys
import codecs
import getpass
import tempfile
import webbrowser
from datetime import datetime
from optparse import OptionParser
from subprocess import Popen, PIPE, call
Expand Down Expand Up @@ -180,8 +183,8 @@ def gather_options(argv):
+ " disc. Possible backends are: %s." % ", ".join(BACKENDS)
+ " They are tried in this order otherwise." )
parser.add_option("--browser", metavar="BROWSER",
help="Program to open urls. If not chosen, we try these:\n"
+ ", ".join(BROWSERS))
help="Program to open URLs. This will be automatically detected"
" for most setups, if not chosen manually.")
parser.add_option("--force-submit", action="store_true", default=False,
help="Always open TOC/disc ID in browser.")
parser.add_option("--server", metavar="SERVER",
Expand Down Expand Up @@ -313,8 +316,52 @@ def find_browser():
if has_program(browser):
return browser

# default to the first "real" browser in the list, as of now: firefox
return BROWSERS[1]
# This will use the webbrowser module to find a default
return None

def open_browser(url, exit=False, submit=False):
"""open url in the selected browser, default if none
"""
if options.browser:
if exit:
try:
if os.name == "nt":
# silly but necessary for spaces in the path
os.execlp(options.browser, '"' + options.browser + '"', url)
else:
# linux/unix works fine with spaces
os.execlp(options.browser, options.browser, url)
except OSError as err:
print_error("Couldn't open the url in %s: %s"
% (options.browser, str(err)))
if submit:
print_error2("Please submit via:", url)
sys.exit(1)
else:
try:
if options.debug:
Popen([options.browser, url])
else:
devnull = open(os.devnull, "w")
Popen([options.browser, url], stdout=devnull)
except FileNotFoundError as err:
print_error("Couldn't open the url in %s: %s"
% (options.browser, str(err)))
if submit:
print_error2("Please submit via:", url)
else:
try:
if options.debug:
webbrowser.open(url)
else:
# this supresses stdout
webbrowser.get().open(url)
except webbrowser.Error as err:
print_error("Couldn't open the url:", str(err))
if submit:
print_error2("Please submit via:", url)
if exit:
sys.exit(1)

def get_real_mac_device(option_device):
"""drutil takes numbers as drives.
Expand Down Expand Up @@ -443,18 +490,7 @@ def ask_for_submission(url, print_url=False):
submit_requested = user_input(" [y/N] ") == "y"

if submit_requested:
try:
if os.name == "nt":
# silly but necessary for spaces in the path
os.execlp(options.browser, '"' + options.browser + '"', url)
else:
# linux/unix works fine with spaces
os.execlp(options.browser, options.browser, url)
except OSError as err:
print_error("Couldn't open the url in %s: %s"
% (options.browser, str(err)))
print_error2("Please submit it via:", url)
sys.exit(1)
open_browser(url, exit=True, submit=True)
elif print_url:
print("Please submit the Disc ID with this url:")
print(url)
Expand Down Expand Up @@ -925,11 +961,7 @@ def cleanup_isrcs(release, isrcs):

url = "http://%s/isrc/%s" % (options.server, isrc)
if user_input("Open ISRC in the browser? [Y/n] ") != "n":
if options.debug:
Popen([options.browser, url])
else:
devnull = open(os.devnull, "w")
Popen([options.browser, url], stdout=devnull)
open_browser(url)
user_input("(press <return> when done with this ISRC) ")


Expand Down

0 comments on commit 9535ba5

Please sign in to comment.