Ejemplo n.º 1
0
Archivo: client.py Proyecto: umax/ssn
    def login(self):
        """Show login dialog."""

        dialog = self.get_widget('login_dialog')
        response = dialog.run()
        dialog.destroy()
        if response == gtk.RESPONSE_OK:
            self.update_status('Авторизация...')
            login = self.get_widget('login_dialog_entry_login').get_text()
            password = self.get_widget('login_dialog_entry_password').get_text()
            # create Core
            self.core = CoreFactory().product('vkontakte')
            if not self.core.login(login, password):
                self.core = None
                self.update_status( \
                    "Ошибка авторизации: неверный логин или пароль!")
                return
            # create Html parser
            self.parser = ParserFactory().product('vkontakte')
            self.parser.set_core(self.core)
            # create RDF Builder 
            self.rdf_builder = RDFBuilderFactory().product( \
                'vkontakte', self.store_path)
            self.update_status("Загрузка персональной страницы...")
            person = self.add_person_to_store(self.core.my_id)
            self.set_my_info(person)
            self.w_tree.get_object('main_notebook').show()
            self.update_status()
Ejemplo n.º 2
0
Archivo: client.py Proyecto: umax/ssn
class UI:
    def __init__(self):
        self.store_path = os.path.abspath('./store')
        self.core = None
        self.parser = None
        self.rdf_builder = None
        self.rdf_finder = RDFFinder(self.store_path)
        self.rdf_parser = RDFParser(self.store_path)
        self.w_tree = gtk.Builder()
        self.w_tree.add_from_file('./ui/ui.xml')
        self.get_widget = self.w_tree.get_object
        self.window = self.get_widget('main_window')
        self.window.connect('destroy', gtk.main_quit)
        self.get_widget('menu_item_quit').connect('activate', gtk.main_quit)
        self.get_widget('main_notebook').hide()
        self.window.show()
        self.login()

    def login(self):
        """Show login dialog."""

        dialog = self.get_widget('login_dialog')
        response = dialog.run()
        dialog.destroy()
        if response == gtk.RESPONSE_OK:
            self.update_status('Авторизация...')
            login = self.get_widget('login_dialog_entry_login').get_text()
            password = self.get_widget('login_dialog_entry_password').get_text()
            # create Core
            self.core = CoreFactory().product('vkontakte')
            if not self.core.login(login, password):
                self.core = None
                self.update_status( \
                    "Ошибка авторизации: неверный логин или пароль!")
                return
            # create Html parser
            self.parser = ParserFactory().product('vkontakte')
            self.parser.set_core(self.core)
            # create RDF Builder 
            self.rdf_builder = RDFBuilderFactory().product( \
                'vkontakte', self.store_path)
            self.update_status("Загрузка персональной страницы...")
            person = self.add_person_to_store(self.core.my_id)
            self.set_my_info(person)
            self.w_tree.get_object('main_notebook').show()
            self.update_status()

    def set_my_info(self, person):
        """Set my info."""

        self.get_widget('my_info').set_text( \
            self.rdf_parser.parse(self.core.my_id))
        self.get_widget('my_avatar').set_from_file( \
            os.path.join(self.store_path, "%s.jpg" % self.core.my_id))
        self.fill_my_friends_list(person.friends)

    def update_status(self, text=""):
        """Update statusbar text."""

        statusbar = self.get_widget('statusbar')
        statusbar.push(statusbar.get_context_id('test'), text)
        self._update_events()

    def _update_events(self):
        """GTK-specific function. Defreeze GTK UI."""

        while gtk.events_pending():
            gtk.main_iteration(False)

    def add_person_to_store(self, id):
        """Parses person page and save it to RDF file."""

        html = self.core.get_page(id)
        person = self.parser.parse(id, html)
        rdf = self.rdf_builder.build(person)
        image_fname = os.path.join(self.store_path, "%s.jpg" % id)
        if not os.path.exists(image_fname):
            self.core.download_avatar(person.avatar_url, image_fname)
        return person

    def fill_my_friends_list(self, friends_dict):
        my_friends_box = self.get_widget('my_friends_box')
        for child in my_friends_box.get_children():
            my_friends_box.remove(child)
        for friend in friends_dict.values():
            button = gtk.Button(friend)
            button.show()
            my_friends_box.pack_start(button)

    def fill_store_list(self):
        """Show current RDF store."""

        store_box = self.get_widget('store_box')
        for child in store_box.get_children():
            stote_box.remove(child)
        for file in os.listdir(self.store_dir):
            if file.endswith('.rdf') and not os.path.isdir(file):
                text = self.rdf_parser.get_first_name( \
                    os.path.join(self.store_dir, file))
                text += '' + self.rdf_pasrer.get_surname( \
                    os.path.join(self.store_dir, file))
                label = gtk.Label(text)
                label.show()
                store_box.pack_start(label)