actions.py :  » Network » Torrent-Swapper » swapper » ABC » Torrent » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Torrent Swapper 
Torrent Swapper » swapper » ABC » Torrent » actions.py
import sys
import wx

from time import time

from Utility.getscrapedata import ScrapeThread
from Utility.constants import *#IGNORE:W0611

################################################################
#
# Class: TorrentActions
#
# Handles processing of most all the user-initiated actions
# on a torrent.
#
################################################################        
class TorrentActions:
    def __init__(self, torrent):
        self.torrent = torrent
        self.utility = torrent.utility
        
        self.lastgetscrape = 0
        
        self.oldstatus = None

    def scrape(self, faildialog = False, manualscrape = False):        
        # Manual Scraping should be done no more than once per minute
        if (manualscrape):
            if (time() - self.lastgetscrape < 60):
                if faildialog:
                    dlg = wx.MessageDialog(None, self.utility.lang.get('warningscrapelessthanmin'), self.utility.lang.get('error'), wx.ICON_ERROR)
                    dlg.ShowModal()
                    dlg.Destroy()
                return False
        # Automatic Scraping can be done as often as once an hour
        elif (self.lastgetscrape != 0) and ((time() - self.lastgetscrape) < 1200):
            # Scraping again too soon
            return False

        ScrapeThread(self.utility, self.torrent, manualscrape).start()
        
        self.lastgetscrape = time()
        
        return True
    
    # pause a torrent or release it,
    # returns True if we actually did something
    def pause(self, release = False):
        torrent = self.torrent
        
        # We need to release the torrent
        if release:
            return self.pauseResume()
        
        # Pause the torrent if it isn't already paused or queued
        if torrent.status.value == STATUS_PAUSE or not torrent.status.isActive():
            return False

        self.oldstatus = torrent.status.value

        torrent.status.updateStatus(STATUS_PAUSE)

#        if torrent.connection.engine is not None and torrent.connection.engine.dow is not None:
        if torrent.status.isActive():
            torrent.connection.engine.dow.Pause()

        torrent.updateSingleItemStatus()
        return True
        
    def pauseResume(self):
        torrent = self.torrent
        
        if torrent.status.value != STATUS_PAUSE:
            return False

        torrent.status.updateStatus(self.oldstatus)

        # pause an active process
        ###########################################
        if torrent.status.isActive():
            torrent.connection.engine.dow.Unpause()
            return True

        return False

    # stop a single torrent, returns True if we actually did something
    def stop(self):
        torrent = self.torrent

        if torrent.status.isDoneUploading():
            return True
        
        if torrent.status.value == STATUS_STOP:
            return False
        
        torrent.connection.stopEngine(update = False)
        
        torrent.status.updateStatus(STATUS_STOP)
        
        torrent.updateSingleItemStatus()
                
        return True
        
    # Return True if we put something into queue
    def queue(self):
        torrent = self.torrent
        
        if torrent.status.isDoneUploading():
            # Might need to return True to show something happened
            return True

        # Do nothing if already queued, stopped, or done uploading
        if torrent.status.value == STATUS_QUEUE:
            return False

        torrent.connection.stopEngine(update = False)
        
        torrent.status.updateStatus(STATUS_QUEUE)
        
        torrent.updateSingleItemStatus()
        
        return True

    def resume(self, skipcheck = False):
        torrent = self.torrent
        
        ################### Resume for On-Hold State ###########################
        if torrent.status.value == STATUS_PAUSE:
            return self.pauseResume()

        ################## Resume for Other inactive States ##############################
        
        # Don't resume if done uploading or currently active
        if torrent.status.isDoneUploading():
            return True
        
        if torrent.status.isActive():
            return False
            
        # If the file is complete and it's finished uploading,
        # don't need to resume
        if self.torrent.status.isDoneUploading():
            self.torrent.updateSingleItemStatus()
            # This may indicate that something has changed, so return True
            return True

        torrent.status.updateStatus(STATUS_QUEUE)
#        torrent.files.skipcheck = skipcheck
        
        torrent.connection.startEngine()
        
        return True

    def hashCheck(self):
        torrent = self.torrent
        
        # Don't need to do hashcheck if already checking
        if torrent.status.value == STATUS_HASHCHECK:
            return False

        self.oldstatus = torrent.status.value
        
#        # (if it's currently active, wait for it to stop)
#        torrent.connection.stopEngine(waitForThread = True)
        torrent.connection.stopEngine()
        
        torrent.connection.startEngine(STATUS_HASHCHECK)

        return True

        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.