~pyshareteam/pyshare/0.6

« back to all changes in this revision

Viewing changes to pluginWrapper.py

  • Committer: Sebastian Kacprzak
  • Date: 2011-02-06 15:32:30 UTC
  • Revision ID: naicik@gmail.com-20110206153230-lh0tnr4dtmwmdjud
make uploading with password from Imageshack_us work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
# -*- coding: utf-8 -*-
3
3
 
4
 
#    Copyright (C) <2009>  <Sebastian Kacprzak> <naicik |at| gmail |dot| com>
 
4
#    Copyright (C) <2009,2011>  <Sebastian Kacprzak> <naicik |at| gmail |dot| com>
5
5
 
6
6
#    This program is free software: you can redistribute it and/or modify
7
7
#    it under the terms of the GNU General Public License as published by
18
18
 
19
19
"""This module is supposed to make sure that plugins:
20
20
-don't crash the program
21
 
-errorCallback would be called if anything bad happen
 
21
-errorCallback is called if anything bad happen
22
22
-plugins are running in their own threads
 
23
-log errors
 
24
-number of concurrent uploads is not exceeded
23
25
 
24
26
Do note that this module is not a security sandbox
25
27
(fe: malicious plugin stil can delete home folder)"""
26
28
 
27
29
from threading import Thread
28
30
from sys import exc_info
 
31
from plugins.libs.cliHelper import printLink, printProgressToCLI
 
32
from Settings import Settings
 
33
 
29
34
import logging
30
35
from helpers.gnomeHelper import getPyShareHomeDirectory
31
36
LOG_PATH = getPyShareHomeDirectory() + 'errorLog'
33
38
logger1 = logging.getLogger('pluginWrapper')
34
39
 
35
40
 
 
41
 
36
42
def __runMethod(method, arguments,errorCallback,errorCallbackArgument):
37
43
    """runs method and catches all exceptions
38
44
    errorCallback function will be called if Exception occurs"""
50
56
    t.start()
51
57
    return t
52
58
 
53
 
def uploadFiles(uploader, errorCallback,
 
59
def uploadFiles(Uploader, errorCallback,
54
60
    files, resultCallbackFunction, progressCallbackFunction, fileNumber, semaphore, username="", password=""):
55
61
    """uploads files with given uploader, each file in new thread
56
62
    each thread would be given incremented fileNumber
58
64
    errorCallback function will be called if Exception occurs"""
59
65
    threadList = []
60
66
    for file in files:
61
 
        t = runMethodInThread(uploader.uploadFile,
62
 
            (file, resultCallbackFunction, progressCallbackFunction, fileNumber, semaphore, username, password),
 
67
        t = runMethodInThread(__createUploaderInstanceAndUploadFile,
 
68
            (Uploader, file, resultCallbackFunction, progressCallbackFunction, fileNumber, semaphore, username, password),
63
69
            errorCallback,
64
70
            fileNumber)
65
71
        threadList.append(t)
66
72
        fileNumber += 1
67
73
    return threadList
68
74
 
69
 
def uploadFile(uploader, errorCallback,
 
75
def uploadFile(Uploader, errorCallback,
70
76
    file, resultCallbackFunction, progressCallbackFunction, fileNumber, semaphore, username="", password=""):
71
77
    """uploads file with given uploader
72
78
    errorCallback function will be called if Exception occurs"""
73
 
    t = runMethodInThread(uploader.uploadFile,
74
 
        (file, resultCallbackFunction, progressCallbackFunction, fileNumber, semaphore, username, password),
 
79
    t = runMethodInThread(__createUploaderInstanceAndUploadFile,
 
80
        (Uploader, file, semaphore,
 
81
            username, password, 
 
82
            resultCallbackFunction, progressCallbackFunction,
 
83
            fileNumber),
75
84
        errorCallback,
76
85
        fileNumber)
77
86
    return t
78
87
 
 
88
def __createUploaderInstanceAndUploadFile(Uploader, file, semaphore,
 
89
    username, password,
 
90
    resultCallbackFunction=printLink, progressCallbackFunction=printProgressToCLI,
 
91
    fileNumber = 0):
 
92
    uploader = Uploader(fileNumber, progressCallbackFunction)
 
93
    credentials = Settings().getUploaderCredentials(uploader.NAME)
 
94
    if credentials:
 
95
        username, password = credentials
 
96
    semaphore.acquire()
 
97
    try:
 
98
        uploader.upload(file, resultCallbackFunction, username, password)
 
99
    finally:
 
100
        semaphore.release()
 
101
 
 
102