mirror of
https://github.com/codex-storage/deluge.git
synced 2025-01-11 12:04:10 +00:00
This commit is contained in:
parent
df694996a9
commit
b28a636c62
@ -100,7 +100,9 @@ class persistent_state:
|
|||||||
# The manager for the torrent system
|
# The manager for the torrent system
|
||||||
|
|
||||||
class manager:
|
class manager:
|
||||||
def __init__(self, client_ID, version, user_agent, base_dir):
|
# blank_slate mode ignores the two pickle files and DHT state file, i.e. you start
|
||||||
|
# completely fresh. When quitting, the old files will be overwritten
|
||||||
|
def __init__(self, client_ID, version, user_agent, base_dir, blank_slate=False):
|
||||||
self.base_dir = base_dir
|
self.base_dir = base_dir
|
||||||
|
|
||||||
# Ensure directories exist
|
# Ensure directories exist
|
||||||
@ -129,21 +131,27 @@ class manager:
|
|||||||
self.supp_torrent_states = {} # unique_ID->dict of data
|
self.supp_torrent_states = {} # unique_ID->dict of data
|
||||||
|
|
||||||
# Unpickle the preferences, or create a new one
|
# Unpickle the preferences, or create a new one
|
||||||
|
self.prefs = DEFAULT_PREFS
|
||||||
|
if not blank_slate:
|
||||||
try:
|
try:
|
||||||
pkl_file = open(self.base_dir + "/" + PREFS_FILENAME, 'rb')
|
pkl_file = open(self.base_dir + "/" + PREFS_FILENAME, 'rb')
|
||||||
self.prefs = pickle.load(pkl_file)
|
self.prefs = pickle.load(pkl_file)
|
||||||
pkl_file.close()
|
pkl_file.close()
|
||||||
except IOError:
|
except IOError:
|
||||||
self.prefs = DEFAULT_PREFS
|
pass
|
||||||
|
|
||||||
# Apply preferences. Note that this is before any torrents are added
|
# Apply preferences. Note that this is before any torrents are added
|
||||||
self.apply_prefs()
|
self.apply_prefs()
|
||||||
|
|
||||||
# Apply DHT, if needed. Note that this is before any torrents are added
|
# Apply DHT, if needed. Note that this is before any torrents are added
|
||||||
if self.get_pref('use_DHT'):
|
if self.get_pref('use_DHT'):
|
||||||
|
if not blank_slate:
|
||||||
pytorrent_core.start_DHT(self.base_dir + "/" + DHT_FILENAME)
|
pytorrent_core.start_DHT(self.base_dir + "/" + DHT_FILENAME)
|
||||||
|
else:
|
||||||
|
pytorrent_core.start_DHT("")
|
||||||
|
|
||||||
# Unpickle the state, or create a new one
|
# Unpickle the state, or create a new one
|
||||||
|
if not blank_slate:
|
||||||
try:
|
try:
|
||||||
pkl_file = open(self.base_dir + "/" + STATE_FILENAME, 'rb')
|
pkl_file = open(self.base_dir + "/" + STATE_FILENAME, 'rb')
|
||||||
self.state = pickle.load(pkl_file)
|
self.state = pickle.load(pkl_file)
|
||||||
@ -153,37 +161,35 @@ class manager:
|
|||||||
self.sync()
|
self.sync()
|
||||||
except IOError:
|
except IOError:
|
||||||
self.state = persistent_state()
|
self.state = persistent_state()
|
||||||
|
else:
|
||||||
|
self.state = persistent_state()
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
# Pickle the prefs
|
# Pickle the prefs
|
||||||
|
print "Pickling prefs..."
|
||||||
output = open(self.base_dir + "/" + PREFS_FILENAME, 'wb')
|
output = open(self.base_dir + "/" + PREFS_FILENAME, 'wb')
|
||||||
pickle.dump(self.prefs, output)
|
pickle.dump(self.prefs, output)
|
||||||
output.close()
|
output.close()
|
||||||
|
|
||||||
# Pickle the state
|
# Pickle the state
|
||||||
|
print "Pickling state..."
|
||||||
output = open(self.base_dir + "/" + STATE_FILENAME, 'wb')
|
output = open(self.base_dir + "/" + STATE_FILENAME, 'wb')
|
||||||
pickle.dump(self.state, output)
|
pickle.dump(self.state, output)
|
||||||
output.close()
|
output.close()
|
||||||
|
|
||||||
# Save fastresume data
|
# Save fastresume data
|
||||||
|
print "Saving fastresume data..."
|
||||||
self.save_fastresume_data()
|
self.save_fastresume_data()
|
||||||
|
|
||||||
# Stop DHT, if needed
|
# Stop DHT, if needed
|
||||||
if self.get_pref('use_DHT'):
|
if self.get_pref('use_DHT'):
|
||||||
|
print "Stopping DHT..."
|
||||||
pytorrent_core.stop_DHT(self.base_dir + "/" + DHT_FILENAME)
|
pytorrent_core.stop_DHT(self.base_dir + "/" + DHT_FILENAME)
|
||||||
|
|
||||||
# Shutdown torrent core
|
# Shutdown torrent core
|
||||||
|
print "Quitting the core..."
|
||||||
pytorrent_core.quit()
|
pytorrent_core.quit()
|
||||||
|
|
||||||
# This is the EXTERNAL function, for the GUI. It returns the core_state + supp_state
|
|
||||||
def get_torrent_state(self, unique_ID):
|
|
||||||
ret = self.get_torrent_core_state(unique_ID, True).copy()
|
|
||||||
|
|
||||||
if self.get_supp_torrent_state(unique_ID) is not None:
|
|
||||||
ret.update(self.get_supp_torrent_state(unique_ID))
|
|
||||||
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def get_pref(self, key):
|
def get_pref(self, key):
|
||||||
# If we have a value, return, else fallback on default_prefs, else raise an error
|
# If we have a value, return, else fallback on default_prefs, else raise an error
|
||||||
# the fallback is useful if the source has newer prefs than the existing pref state,
|
# the fallback is useful if the source has newer prefs than the existing pref state,
|
||||||
@ -205,19 +211,6 @@ class manager:
|
|||||||
|
|
||||||
self.apply_prefs()
|
self.apply_prefs()
|
||||||
|
|
||||||
def apply_prefs(self):
|
|
||||||
print "Applying preferences"
|
|
||||||
pytorrent_core.set_download_rate_limit(self.get_pref('max_download_rate'))
|
|
||||||
|
|
||||||
pytorrent_core.set_upload_rate_limit(self.get_pref('max_upload_rate'))
|
|
||||||
|
|
||||||
pytorrent_core.set_listen_on(self.get_pref('listen_on')[0],
|
|
||||||
self.get_pref('listen_on')[1])
|
|
||||||
|
|
||||||
pytorrent_core.set_max_connections(self.get_pref('max_connections'))
|
|
||||||
|
|
||||||
pytorrent_core.set_max_uploads(self.get_pref('max_uploads'))
|
|
||||||
|
|
||||||
def add_torrent(self, filename, save_dir, compact):
|
def add_torrent(self, filename, save_dir, compact):
|
||||||
self.add_torrent_ns(filename, save_dir, compact)
|
self.add_torrent_ns(filename, save_dir, compact)
|
||||||
return self.sync() # Syncing will create a new torrent in the core, and return it's ID
|
return self.sync() # Syncing will create a new torrent in the core, and return it's ID
|
||||||
@ -262,6 +255,15 @@ class manager:
|
|||||||
ret['DHT_nodes'] = pytorrent_core.get_DHT_info()
|
ret['DHT_nodes'] = pytorrent_core.get_DHT_info()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
# This is the EXTERNAL function, for the GUI. It returns the core_state + supp_state
|
||||||
|
def get_torrent_state(self, unique_ID):
|
||||||
|
ret = self.get_torrent_core_state(unique_ID, True).copy()
|
||||||
|
|
||||||
|
if self.get_supp_torrent_state(unique_ID) is not None:
|
||||||
|
ret.update(self.get_supp_torrent_state(unique_ID))
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
def queue_up(self, unique_ID):
|
def queue_up(self, unique_ID):
|
||||||
curr_index = self.get_queue_index(unique_ID)
|
curr_index = self.get_queue_index(unique_ID)
|
||||||
if curr_index > 0:
|
if curr_index > 0:
|
||||||
@ -297,6 +299,9 @@ class manager:
|
|||||||
def is_user_paused(self, unique_ID):
|
def is_user_paused(self, unique_ID):
|
||||||
return self.unique_IDs[unique_ID].user_paused
|
return self.unique_IDs[unique_ID].user_paused
|
||||||
|
|
||||||
|
def get_num_torrents(self):
|
||||||
|
return pytorrent_core.get_num_torrents()
|
||||||
|
|
||||||
def get_unique_IDs(self):
|
def get_unique_IDs(self):
|
||||||
return self.unique_IDs.keys()
|
return self.unique_IDs.keys()
|
||||||
|
|
||||||
@ -326,20 +331,6 @@ class manager:
|
|||||||
(index >= self.state.max_active_torrents or self.is_user_paused(unique_ID)):
|
(index >= self.state.max_active_torrents or self.is_user_paused(unique_ID)):
|
||||||
pytorrent_core.pause(unique_ID)
|
pytorrent_core.pause(unique_ID)
|
||||||
|
|
||||||
def calc_ratio(self, unique_ID, torrent_state):
|
|
||||||
up = float(torrent_state['total_upload'] + self.unique_IDs[unique_ID].uploaded_memory)
|
|
||||||
down = float(torrent_state["total_done"])
|
|
||||||
|
|
||||||
try:
|
|
||||||
ret = float(up/down)
|
|
||||||
except:
|
|
||||||
ret = -1
|
|
||||||
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def get_num_torrents(self):
|
|
||||||
return pytorrent_core.get_num_torrents()
|
|
||||||
|
|
||||||
# Handle them for the backend's purposes, but still send them up in case the client
|
# Handle them for the backend's purposes, but still send them up in case the client
|
||||||
# wants to do something - show messages, for example
|
# wants to do something - show messages, for example
|
||||||
def handle_events(self):
|
def handle_events(self):
|
||||||
@ -407,14 +398,12 @@ class manager:
|
|||||||
|
|
||||||
def add_torrent_ns(self, filename, save_dir, compact):
|
def add_torrent_ns(self, filename, save_dir, compact):
|
||||||
# Cache torrent file
|
# Cache torrent file
|
||||||
(temp, torrent_file_short) = os.path.split(filename)
|
(temp, filename_short) = os.path.split(filename)
|
||||||
|
|
||||||
time.sleep(0.01) # Ensure we use a unique time for the new filename
|
if filename_short in os.listdir(self.base_dir + "/" + TORRENTS_SUBDIR):
|
||||||
new_name = str(time.time()) + ".torrent"
|
raise PyTorrentError("Duplicate Torrent, it appears: " + filename_short)
|
||||||
full_new_name = self.base_dir + "/" + TORRENTS_SUBDIR + "/" + new_name
|
|
||||||
|
|
||||||
if new_name in os.listdir(self.base_dir + "/" + TORRENTS_SUBDIR):
|
full_new_name = self.base_dir + "/" + TORRENTS_SUBDIR + "/" + filename_short
|
||||||
raise PyTorrentError("Could not cache torrent file locally, failed: " + new_name)
|
|
||||||
|
|
||||||
shutil.copy(filename, full_new_name)
|
shutil.copy(filename, full_new_name)
|
||||||
|
|
||||||
@ -470,3 +459,28 @@ class manager:
|
|||||||
|
|
||||||
def get_queue_index(self, unique_ID):
|
def get_queue_index(self, unique_ID):
|
||||||
return self.state.queue.index(unique_ID)
|
return self.state.queue.index(unique_ID)
|
||||||
|
|
||||||
|
def apply_prefs(self):
|
||||||
|
print "Applying preferences"
|
||||||
|
pytorrent_core.set_download_rate_limit(self.get_pref('max_download_rate'))
|
||||||
|
|
||||||
|
pytorrent_core.set_upload_rate_limit(self.get_pref('max_upload_rate'))
|
||||||
|
|
||||||
|
pytorrent_core.set_listen_on(self.get_pref('listen_on')[0],
|
||||||
|
self.get_pref('listen_on')[1])
|
||||||
|
|
||||||
|
pytorrent_core.set_max_connections(self.get_pref('max_connections'))
|
||||||
|
|
||||||
|
pytorrent_core.set_max_uploads(self.get_pref('max_uploads'))
|
||||||
|
|
||||||
|
def calc_ratio(self, unique_ID, torrent_state):
|
||||||
|
up = float(torrent_state['total_upload'] + self.unique_IDs[unique_ID].uploaded_memory)
|
||||||
|
down = float(torrent_state["total_done"])
|
||||||
|
|
||||||
|
try:
|
||||||
|
ret = float(up/down)
|
||||||
|
except:
|
||||||
|
ret = -1
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ from time import sleep
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
manager = pytorrent.manager("PT", "0500", "pytorrent - testing only",
|
manager = pytorrent.manager("PT", "0500", "pytorrent - testing only",
|
||||||
os.path.expanduser("~") + "/Temp")
|
os.path.expanduser("~") + "/Temp")#, blank_slate=True)
|
||||||
|
|
||||||
#manager.set_pref('max_upload_rate', 6*1024)
|
#manager.set_pref('max_upload_rate', 6*1024)
|
||||||
|
|
||||||
@ -34,5 +34,5 @@ try:
|
|||||||
print ""
|
print ""
|
||||||
sleep(2)
|
sleep(2)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print "Shutting down:"
|
print "Shutting down..."
|
||||||
manager.quit()
|
manager.quit()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user