Browse Source

Redesigned the saving of attributes

Now uses pickle, so that the type of the property is preserved.
Made flexible, so that more properties can be easily added later.
Closes #1575
tags/3.0.2RC1
Safihre 5 years ago
parent
commit
814eeaa900
  1. 50
      sabnzbd/nzbstuff.py

50
sabnzbd/nzbstuff.py

@ -20,6 +20,7 @@ sabnzbd.nzbstuff - misc
""" """
import os import os
import pickle
import time import time
import re import re
import logging import logging
@ -563,6 +564,8 @@ NzbObjectSaver = (
"rating_filtered", "rating_filtered",
) )
NzoAttributeSaver = ("cat", "pp", "script", "priority", "final_name", "password", "url")
# Lock to prevent errors when saving the NZO data # Lock to prevent errors when saving the NZO data
NZO_LOCK = threading.RLock() NZO_LOCK = threading.RLock()
@ -786,11 +789,7 @@ class NzbObject(TryList):
# Pickup backed-up attributes when re-using # Pickup backed-up attributes when re-using
if reuse: if reuse:
cat, pp, script, priority, name, password, self.url = get_attrib_file(self.workpath, 7) cat, pp, script, priority = self.load_attribs()
if name:
self.final_name = name
if password:
self.password = password
# Determine category and find pp/script values # Determine category and find pp/script values
self.cat, pp_tmp, self.script, priority = cat_to_opts(cat, pp, script, priority) self.cat, pp_tmp, self.script, priority = cat_to_opts(cat, pp, script, priority)
@ -1899,9 +1898,35 @@ class NzbObject(TryList):
sabnzbd.save_data(self, self.nzo_id, self.workpath) sabnzbd.save_data(self, self.nzo_id, self.workpath)
def save_attribs(self): def save_attribs(self):
set_attrib_file( """ Save specific attributes for Retry """
self.workpath, (self.cat, self.pp, self.script, self.priority, self.final_name, self.password, self.url) attribs = {}
) for attrib in NzoAttributeSaver:
attribs[attrib] = getattr(self, attrib)
logging.debug("Saving attributes %s for %s", attribs, self.final_name)
sabnzbd.save_data(attribs, ATTRIB_FILE, self.workpath)
def load_attribs(self):
""" Load saved attributes and return them to be parsed """
attribs = sabnzbd.load_data(ATTRIB_FILE, self.workpath, remove=False)
logging.debug("Loaded attributes %s for %s", attribs, self.final_name)
# TODO: Remove fallback to old method in SABnzbd 3.2.0
if not attribs:
cat, pp, script, priority, name, password, self.url = get_attrib_file(self.workpath, 7)
if name:
self.final_name = name
if password:
self.password = password
return cat, pp, script, priority
# Only a subset we want to apply directly to the NZO
for attrib in ("final_name", "password", "url"):
# Only set if it is present and has a value
if attribs.get(attrib):
setattr(self, attrib, attribs[attrib])
# Rest is to be used directly in the NZO-init flow
return attribs["cat"], attribs["pp"], attribs["script"], attribs["priority"]
@synchronized(NZO_LOCK) @synchronized(NZO_LOCK)
def build_pos_nzf_table(self, nzf_ids): def build_pos_nzf_table(self, nzf_ids):
@ -2157,15 +2182,6 @@ def get_attrib_file(path, size):
return [None for _ in range(size)] return [None for _ in range(size)]
def set_attrib_file(path, attribs):
""" Write job's attributes to file """
logging.debug("Writing attributes %s to %s", attribs, path)
path = os.path.join(path, ATTRIB_FILE)
with open(path, "w", encoding="utf-8") as attr_file:
for item in attribs:
attr_file.write("%s\n" % item)
def name_extractor(subject): def name_extractor(subject):
""" Try to extract a file name from a subject line, return `subject` if in doubt """ """ Try to extract a file name from a subject line, return `subject` if in doubt """
result = subject result = subject

Loading…
Cancel
Save