XML datastore: load will now automatically try to load backup
This commit is contained in:
parent
ab2eb405ca
commit
c7706bfc97
1 changed files with 23 additions and 3 deletions
|
@ -15,11 +15,14 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import fcntl
|
||||
import logging
|
||||
import os
|
||||
import xml.parsers.expat
|
||||
|
||||
from nemubot.datastore.abstract import Abstract
|
||||
|
||||
logger = logging.getLogger("nemubot.datastore.xml")
|
||||
|
||||
|
||||
class XML(Abstract):
|
||||
|
||||
|
@ -88,10 +91,27 @@ class XML(Abstract):
|
|||
"""
|
||||
|
||||
data_file = self._get_data_file_path(module)
|
||||
|
||||
# Try to load original file
|
||||
if os.path.isfile(data_file):
|
||||
from nemubot.tools.xmlparser import parse_file
|
||||
try:
|
||||
return parse_file(data_file)
|
||||
else:
|
||||
except xml.parsers.expat.ExpatError:
|
||||
# Try to load from backup
|
||||
for i in range(10):
|
||||
path = data_file + "." + str(i)
|
||||
if os.path.isfile(path):
|
||||
try:
|
||||
cnt = parse_file(path)
|
||||
|
||||
logger.warn("Restoring from backup: %s", path)
|
||||
|
||||
return cnt
|
||||
except xml.parsers.expat.ExpatError:
|
||||
continue
|
||||
|
||||
# Default case: initialize a new empty datastore
|
||||
return Abstract.load(self, module)
|
||||
|
||||
def _rotate(self, path):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue