# Path: plugins/sessionsaver/sessionsaver/store/sessionstore.py
# class SessionStore(GObject.Object):
#     __gsignals__ = {
#         "session-added":    (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
#                              (GObject.TYPE_PYOBJECT,)),
#         "session-changed":  (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
#                              (GObject.TYPE_PYOBJECT,)),
#         "session-removed":  (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
#                             (GObject.TYPE_PYOBJECT,))
#     }
# 
#     def __init__(self):
#         super(SessionStore, self).__init__()
#         self._sessions = []
# 
#     def __iter__(self):
#         return iter(self._sessions)
# 
#     def __getitem__(self, index):
#         return self._sessions[index]
# 
#     def __getslice__(self, i, j):
#         return self._sessions[i:j]
# 
#     def __len__(self):
#         return len(self._sessions)
# 
#     def do_session_added(self, session):
#         self._sessions.append(session)
#         self._sessions.sort()
# 
#     def do_session_changed(self, session):
#         index = self._sessions.index(session)
#         self._sessions[index] = session
# 
#     def add(self, session):
#         assert isinstance(session, Session)
# 
#         if session in self:
#             self.emit('session-changed', session)
#         else:
#             self.emit('session-added', session)
# 
#     def do_session_removed(self, session):
#         self._sessions.remove(session)
# 
#     def remove(self, session):
#         assert isinstance(session, Session)
#         if session in self:
#             self.emit('session-removed', session)
# 
#     def index(self, session):
#         return self._sessions.index(session)
# 
# Path: plugins/sessionsaver/sessionsaver/store/session.py
# class Session(object):
#     def __init__(self, name, files = None):
#         super(Session, self).__init__()
#         self.name = name
#         if files is None:
#             files = []
#         self.files = files
# 
#     def __lt__(self, session):
#         return (self.name.lower() < session.name.lower())
# 
#     def __eq__(self, session):
#         return (self.name.lower() == session.name.lower())
# 
#     def add_file(self, filename):
#         self.files.append(Gio.file_new_for_uri(filename))


import os.path
from xml.parsers import expat
from gi.repository import GLib
from .sessionstore import SessionStore
from .session import Session
        if not os.path.isdir(dirname):
            os.makedirs(dirname)

        fp = open(self.filename, "wb")
        fp.write(bytes('<?xml version="1.0" encoding="UTF-8"?>\n','UTF-8'))
        fp.write(bytes(self.dump(),'UTF-8'))
        fp.close()

    def load(self):
        if not os.path.isfile(self.filename):
            return

        parser = expat.ParserCreate('UTF-8')
        parser.buffer_text = True
        parser.StartElementHandler = self._expat_start_handler
        parser.EndElementHandler = self._expat_end_handler

        self._current_session = None
        try:
            parser.ParseFile(open(self.filename, 'rb'))
        except:
            return
        del self._current_session

    def _expat_start_handler(self, tag, attr):
        if tag == 'file':
            assert self._current_session is not None
            self._current_session.add_file(str(attr['path']))
        elif tag == 'session':
            assert self._current_session is None

# Path: plugins/sessionsaver/sessionsaver/store/sessionstore.py
# class SessionStore(GObject.Object):
#     __gsignals__ = {
#         "session-added":    (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
#                              (GObject.TYPE_PYOBJECT,)),
#         "session-changed":  (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
#                              (GObject.TYPE_PYOBJECT,)),
#         "session-removed":  (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
#                             (GObject.TYPE_PYOBJECT,))
#     }
# 
#     def __init__(self):
#         super(SessionStore, self).__init__()
#         self._sessions = []
# 
#     def __iter__(self):
#         return iter(self._sessions)
# 
#     def __getitem__(self, index):
#         return self._sessions[index]
# 
#     def __getslice__(self, i, j):
#         return self._sessions[i:j]
# 
#     def __len__(self):
#         return len(self._sessions)
# 
#     def do_session_added(self, session):
#         self._sessions.append(session)
#         self._sessions.sort()
# 
#     def do_session_changed(self, session):
#         index = self._sessions.index(session)
#         self._sessions[index] = session
# 
#     def add(self, session):
#         assert isinstance(session, Session)
# 
#         if session in self:
#             self.emit('session-changed', session)
#         else:
#             self.emit('session-added', session)
# 
#     def do_session_removed(self, session):
#         self._sessions.remove(session)
# 
#     def remove(self, session):
#         assert isinstance(session, Session)
#         if session in self:
#             self.emit('session-removed', session)
# 
#     def index(self, session):
#         return self._sessions.index(session)
# 
# Path: plugins/sessionsaver/sessionsaver/store/session.py
# class Session(object):
#     def __init__(self, name, files = None):
#         super(Session, self).__init__()
#         self.name = name
#         if files is None:
#             files = []
#         self.files = files
# 
#     def __lt__(self, session):
#         return (self.name.lower() < session.name.lower())
# 
#     def __eq__(self, session):
#         return (self.name.lower() == session.name.lower())
# 
#     def add_file(self, filename):
#         self.files.append(Gio.file_new_for_uri(filename))


# Path: plugins/sessionsaver/sessionsaver/store/xmlsessionstore.py
import os.path
from xml.parsers import expat
from gi.repository import GLib
from .sessionstore import SessionStore
from .session import Session

        if not os.path.isdir(dirname):
            os.makedirs(dirname)

        fp = open(self.filename, "wb")
        fp.write(bytes('<?xml version="1.0" encoding="UTF-8"?>\n','UTF-8'))
        fp.write(bytes(self.dump(),'UTF-8'))
        fp.close()

    def load(self):
        if not os.path.isfile(self.filename):
            return

        parser = expat.ParserCreate('UTF-8')
        parser.buffer_text = True
        parser.StartElementHandler = self._expat_start_handler
        parser.EndElementHandler = self._expat_end_handler

        self._current_session = None
        try:
            parser.ParseFile(open(self.filename, 'rb'))
        except:
            return
        del self._current_session

    def _expat_start_handler(self, tag, attr):
        if tag == 'file':
            assert self._current_session is not None
            self._current_session.add_file(str(attr['path']))
        elif tag == 'session':
            assert self._current_session is None

            self._current_session = Session(attr['name'])