def refresh_view(self, active_doc): """refresh html view of peer""" if active_doc: view = HtmlView(active_doc) self.detail_preview.SetPage(view.get_view(True)) else: self.detail_preview.SetPage("<font color='blue'>%s</font>"\ % _("no neighbors yet"))
def Show(self, peer_desc, do_show=True): """overrides Show""" if do_show: self.peer_id = peer_desc.peer_id pseudo = peer_desc.document.get_pseudo() self.SetTitle("%s's %s" % (pseudo, _("Profile"))) view = HtmlView(peer_desc.document) self.profile_window.SetPage(view.get_view(True)) wx.Dialog.Show(self, do_show)
def refresh_view(self, pseudo, active_doc): """refresh html view of peer""" if active_doc: view = HtmlView(active_doc) self.detail_preview.SetPage(view.get_view(True)) self.frame.enable_peer_states(True, self.facade.get_peer_status(pseudo)) else: self.detail_preview.SetPage("<font color='blue'>%s</font>"\ % _("select neighbor to display")) self.frame.enable_peer_states(False)
def _add_tab(self, peer_desc): """add tab with preview of given peer""" tab_sizer = wx.BoxSizer(wx.VERTICAL) preview_tab = wx.Panel(self.preview_notebook, -1) match_preview = MyHtmlWindow(preview_tab, -1) tab_sizer.Add(match_preview, 1, wx.EXPAND, 0) view = HtmlView(peer_desc) self.tabs[peer_desc.node_id] = preview_tab preview_tab.preview = match_preview preview_tab.pseudo = peer_desc.document.get_pseudo() preview_tab.SetAutoLayout(True) preview_tab.SetSizer(tab_sizer) preview_tab.preview.SetPage(view.get_view()) tab_sizer.Fit(preview_tab) tab_sizer.SetSizeHints(preview_tab) self._update_tab(peer_desc)
def refresh_view(self, peer_id): """refresh html view of peer""" # get descriptor if self.facade.has_peer(peer_id): peer_desc = self.facade.get_peer(peer_id) else: peer_desc = PeerDescriptor(peer_id) # get document full_path = os.sep.join([PROFILE_DIR, peer_id, PROFILE_EXT]) if not peer_desc.document and os.path.exists(full_path): doc = FileDocument() doc.load(full_path) peer_desc.set_document(doc) # display if peer_desc.document: view = HtmlView(peer_desc.document) self.detail_preview.SetPage(view.get_view(True)) self.frame.enable_peer_states(True, peer_desc.state) else: self.detail_preview.SetPage("<font color='blue'>%s</font>" % _("select neighbor to display")) self.frame.enable_peer_states(False)
def _update_tab(self, peer_desc): """update tab with preview of given peer""" view = HtmlView(peer_desc) preview = self.tabs[peer_desc.node_id].preview preview.SetPage(view.get_view()) self.tabs[peer_desc.node_id].Show(True)
def set_page(self, peer_desc): view = HtmlView(peer_desc) self.profile_window.SetPage(view.get_view()) self.SetTitle("%s's %s"% (peer_desc.document.get_pseudo(), _("Profile")))
def export_profile(self, path): """write profile in html format""" html_file = open(path, "w") html_view = HtmlView(self._desc) html_file.write(html_view.get_view(True).encode(ENCODING))
def setUp(self): """override one in unittest.TestCase""" self.document = CacheDocument(PROFILE_TEST, PROFILE_DIRECTORY) self.desc = PeerDescriptor(PROFILE_TEST, document=self.document) self.view = HtmlView(self.desc)
class HtmlTest(unittest.TestCase): """test that all fields are correctly validated""" def setUp(self): """override one in unittest.TestCase""" self.document = CacheDocument(PROFILE_TEST, PROFILE_DIRECTORY) self.desc = PeerDescriptor(PROFILE_TEST, document=self.document) self.view = HtmlView(self.desc) def assert_template(self): """diff result from view with expeceted template""" self.view.import_desc(self.desc) result = difflib.ndiff([line.strip() for line in self.print_template().splitlines()], [line.strip() for line in self.view.get_view(True).splitlines()], charjunk=lambda x: x in [' ', '\t']) result = list(result) for index, line in enumerate(result): if not line.startswith(" "): print self.view.get_view() print "*************" print '\n'.join(result[index-2:index+10]) self.assert_(line.startswith(" ")) def print_template(self): """fill template with values""" return TEMPLATE % (self.document.get_photo(), self.document.pseudo, self.document.get_title(), self.document.get_firstname(), self.document.get_lastname(), self.document.get_email(), self.print_custom(), self.print_files(), self.print_peers()) def print_custom(self): html = ["<div><b>%s</b>: <span>%s</span></div>"% (key, value) for key, value in self.document.get_custom_attributes().iteritems()] return ''.join(html) def print_files(self): html = [] repos = self.document.get_repositories() repos.sort() for repo in repos: html.append(""" <dt>%s</dt> <dd>%d shared files</dd> """% (os.path.basename(repo), len(self.document.get_shared(repo)))) # concatenate all description of directories & return result return ''.join(html) def print_peers(self): html = [] peers = self.document.get_peers() keys = peers.keys() keys.sort() for key in keys: peers_desc = peers[key] doc = peers_desc.document html.append("""<tr> <td>%s</td> <td>%s</td> <td>%s</td> </tr>"""% (peers_desc.pseudo, peers_desc.state, doc and doc.get_email() or "--")) return ''.join(html) # PERSONAL TAB def test_personal(self): """personal info""" self.document.set_title(u"Mr") self.document.set_firstname(u"Bruce") self.document.set_lastname(u"Willis") self.document.set_photo(os.sep.join([images_dir(), u"profile_male.gif"])) self.document.set_email(u"*****@*****.**") self.assert_template() # CUSTOM TAB def test_custom(self): self.document.add_custom_attributes([u"zic", u"jazz"]) self.document.add_custom_attributes([u"cinema", u"Die Hard"]) self.assert_template() # FILE TAB def test_files(self): self.document.add_file(abspath(".")) self.document.expand_dir(abspath("data")) self.document.share_files((abspath("data"), ["routage"], True)) self.document.share_file((abspath("data/emptydir"), True)) self.assert_template() # OTHERS TAB def test_ordered_peer(self): self.document.set_peer((u"emb", PeerDescriptor(PSEUDO))) self.document.set_peer((u"albert", PeerDescriptor(PSEUDO))) self.document.set_peer((u"star", PeerDescriptor(PSEUDO))) self.document.make_friend(u"emb") self.document.make_friend(u"star") self.document.make_friend(u"albert") self.assert_template() def test_adding_peer(self): self.document.set_peer((u"nico", PeerDescriptor(PSEUDO))) self.assert_template() def test_filling_data(self): self.document.fill_data((u"emb", FileDocument(PROFILE_TEST, PROFILE_DIRECTORY))) self.assert_template() def test_peers_status(self): self.document.set_peer((u"nico", PeerDescriptor(PSEUDO))) self.document.blacklist_peer(u"nico") self.assert_template()
def setUp(self): """override one in unittest.TestCase""" self.document = CacheDocument() self.view = HtmlView(self.document)
class HtmlTest(unittest.TestCase): """test that all fields are correctly validated""" def setUp(self): """override one in unittest.TestCase""" self.document = CacheDocument() self.view = HtmlView(self.document) def assert_template(self): """diff result from view with expeceted template""" self.view.import_document(self.document) result = difflib.ndiff( [line.strip() for line in self.print_template().splitlines()], [line.strip() for line in self.view.get_view(True).splitlines()], charjunk=lambda x: x in [" ", "\t"], ) result = list(result) for index, line in enumerate(result): if not line.startswith(" "): print self.view.get_view() print "*************" print "\n".join(result[index - 2 : index + 10]) self.assert_(line.startswith(" ")) def print_template(self): """fill template with values""" return TEMPLATE % ( self.document.get_photo(), self.document.get_pseudo(), self.document.get_title(), self.document.get_firstname(), self.document.get_lastname(), self.document.get_email(), self.document.get_birthday(), self.document.get_language(), self.document.get_address(), self.document.get_postcode(), self.document.get_city(), self.document.get_country(), self.document.get_description(), self.print_hobbies(), self.print_custom(), self.print_files(), self.print_peers(), ) def print_hobbies(self): html = [" <span>%s</span> |" % item for item in self.document.get_hobbies()] or "" return "".join(html) def print_custom(self): html = [ "<div><b>%s</b>: <span>%s</span></div>" % (key, value) for key, value in self.document.get_custom_attributes().iteritems() ] return "".join(html) def print_files(self): html = [] repos = self.document.get_repositories() repos.sort() for repo in repos: html.append( """ <dt>%s</dt> <dd>%d shared files</dd> """ % (repo, len(self.document.get_shared(repo))) ) # concatenate all description of directories & return result return "".join(html) def print_peers(self): html = [] peers = self.document.get_peers() keys = peers.keys() keys.sort() for key in keys: peers_desc = peers[key] doc = peers_desc.document html.append( """<tr> <td>%s</td> <td>%s</td> <td>%s</td> </tr>""" % (peers_desc.state, doc and doc.get_pseudo() or "--", peers_desc.peer_id) ) return "".join(html) # PERSONAL TAB def test_personal(self): """personal info""" self.document.set_title(u"Mr") self.document.set_firstname(u"Bruce") self.document.set_lastname(u"Willis") self.document.set_pseudo(u"john") self.document.set_photo(u"/home/emb/svn/solipsis/trunk/main/solipsis/services/profile/images/profile_male.gif") self.document.set_email(u"*****@*****.**") self.document.set_birthday(u"1/6/1947") self.document.set_language(u"English") self.document.set_address(u"Hill") self.document.set_postcode(u"920") self.document.set_city(u"Los Angeles") self.document.set_country(u"US") self.document.set_description(u"Lots of movies, quite famous, doesn't look much but very effective") self.assert_template() # CUSTOM TAB def test_hobbies(self): self.document.set_hobbies([u"cinema", u"theatre", u"cop", u"action"]) self.assert_template() def test_custom(self): self.document.add_custom_attributes([u"zic", u"jazz"]) self.document.add_custom_attributes([u"cinema", u"Die Hard"]) self.assert_template() # FILE TAB def test_files(self): self.document.add_repository(abspath(u".")) self.document.expand_dir(abspath(u"data")) self.document.share_files((abspath(u"data"), ["routage"], True)) self.document.share_file((abspath(u"data/emptydir"), True)) self.assert_template() # OTHERS TAB def test_ordered_peer(self): self.document.add_peer(u"nico") self.document.make_friend(u"emb") self.document.make_friend(u"star") self.document.make_friend(u"albert") self.assert_template() def test_adding_peer(self): self.document.add_peer(u"nico") self.assert_template() def test_filling_data(self): self.document.fill_data((u"emb", FileDocument())) self.assert_template() def test_peers_status(self): self.document.blacklist_peer(u"nico") self.assert_template()