Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace call to open by call to platform-specific open #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions examdownloader-cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import subprocess
import sys
import getpass
import examdownloader
from examdownloader import examdownloader, openFile

module = 'CS1010S'
username = 'E0123456'
Expand All @@ -17,7 +16,7 @@ def startDownload(args):
username = args[1]

password = getpass.getpass('Enter password for ' + username + ': ')
ed = examdownloader.examdownloader('CLI')
ed = examdownloader('CLI')


def updateStatus(msg, type='normal'):
Expand All @@ -26,7 +25,7 @@ def updateStatus(msg, type='normal'):
def downloadCallback(status, lastfile='', numFiles=0):
if status:
updateStatus(str(numFiles) + ' papers downloaded successfully!', 'success')
subprocess.call(['open', '-R', lastfile])
openFile(lastfile)
else:
updateStatus('Paper not released by Department', 'error')

Expand Down
7 changes: 3 additions & 4 deletions examdownloader-gui.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from Tkinter import *
import tkFileDialog
import subprocess
import thread
import examdownloader
from examdownloader import examdownloader, openFile

FONT = ('Arial', 14, 'bold')

Expand Down Expand Up @@ -75,12 +74,12 @@ def startDownload(self):
username = self.usernameField.get()
password = self.passwordField.get()
destination = self.destField.get()
ed = examdownloader.examdownloader('GUI')
ed = examdownloader('GUI')

def downloadCallback(status, lastfile='', numFiles=0):
if status:
self.updateStatus(str(numFiles) + ' papers downloaded successfully!', 'success')
subprocess.call(['open', '-R', lastfile])
openFile(lastfile)
else:
self.updateStatus('Paper not released by Department', 'error')

Expand Down
22 changes: 22 additions & 0 deletions examdownloader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import httplib
import urllib
import os
import subprocess
import platform


def openFile(filepath):
"""
Try to open a file in a desktop environment
Fails gracefully if an exception occurs or no file is passed
"""
if not filepath:
return
try:
# snippet taken from here: https://stackoverflow.com/a/435669 by fearless_fool
if platform.system() == 'Darwin': # macOS
subprocess.call(['open', '-R', filepath])
elif platform.system() == 'Windows': # Windows
os.startfile(filepath, 'open')
else: # linux variants
subprocess.call(['xdg-open', filepath])
except OSError:
pass


class examdownloader(object):
def __init__(self, mode):
Expand Down