From 03fef8209188185c5a863573627eb7f1c936cc58 Mon Sep 17 00:00:00 2001 From: Elias Torres Date: Tue, 5 Sep 2006 14:54:26 -0400 Subject: [PATCH] added support for foaf:OnlineAccount information, checkout examples/foaf-based.ini --- examples/foaf-based.ini | 43 ++++++++++++++++++++++++++ planet/config.py | 5 +++ planet/foaf.py | 67 +++++++++++++++++++++++++++++++++-------- tests/test_foaf.py | 11 ++++--- 4 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 examples/foaf-based.ini diff --git a/examples/foaf-based.ini b/examples/foaf-based.ini new file mode 100644 index 0000000..dd4e48a --- /dev/null +++ b/examples/foaf-based.ini @@ -0,0 +1,43 @@ +# Planet configuration file + +# Every planet needs a [Planet] section +[Planet] +# name: Your planet's name +# link: Link to the main page +# owner_name: Your name +# owner_email: Your e-mail address +name = Elias' Planet +link = http://torrez.us/planet/ +owner_name = Elias Torres +owner_email = elias@torrez.us + +# cache_directory: Where cached feeds are stored +# log_level: One of DEBUG, INFO, WARNING, ERROR or CRITICAL +cache_directory = /tmp/venus/ +log_level = DEBUG + +# The following provide defaults for each template: +# output_theme: "theme" of the output +# output_dir: Directory to place output files +# items_per_page: How many items to put on each page +output_theme = mobile +output_dir = /var/www/planet +items_per_page = 60 + +# If non-zero, all feeds which have not been updated in the indicated +# number of days will be marked as inactive +activity_threshold = 90 + +# filters to be run +filters = excerpt.py + +# filter parameters +[excerpt.py] +omit = img p br +width = 500 + +# subscription list +[http://torrez.us/who#elias] +content_type = foaf +online_accounts = http://del.icio.us/|http://del.icio.us/rss/{foaf:accountName} + http://flickr.com/|http://api.flickr.com/services/feeds/photos_public.gne?id={foaf:accountName} diff --git a/planet/config.py b/planet/config.py index 7fe9336..a65cd28 100644 --- a/planet/config.py +++ b/planet/config.py @@ -177,6 +177,11 @@ def load(config_file): # retrieve list options (e.g., etag, last-modified) from cache options = {} + + # add original options + for key, value in parser.items(list): + options[key] = value + try: cached_config = ConfigParser() cached_config.read(cache_filename) diff --git a/planet/foaf.py b/planet/foaf.py index d2d2439..9a0e5b5 100644 --- a/planet/foaf.py +++ b/planet/foaf.py @@ -1,11 +1,13 @@ from ConfigParser import ConfigParser # input = foaf, output = ConfigParser -def foaf2config(rdf, baseuri, config=None): +def foaf2config(rdf, config=None): - if not config: + if not config or not config.sections(): config = ConfigParser() + section = config.sections().pop() + try: from RDF import Model, NS, Parser, Statement except: @@ -14,20 +16,37 @@ def foaf2config(rdf, baseuri, config=None): if hasattr(rdf, 'read'): rdf = rdf.read() + # account mappings, none by default + # form: accounts = {url to service homepage (as found in FOAF)}|{URI template}\n* + # example: http://del.icio.us/|http://del.icio.us/rss/{foaf:accountName} + accounts = {} + if(config.has_option(section, 'online_accounts')): + values = config.get(section, 'online_accounts') + for account_map in values.split('\n'): + try: + homepage, map = account_map.split('|') + accounts[homepage] = map + except: + pass + model = Model() def handler(code, level, facility, message, line, column, byte, file, uri): pass - Parser().parse_string_into_model(model,rdf,baseuri,handler) + Parser().parse_string_into_model(model,rdf,section,handler) dc = NS('http://purl.org/dc/elements/1.1/') foaf = NS('http://xmlns.com/foaf/0.1/') rdfs = NS('http://www.w3.org/2000/01/rdf-schema#') for statement in model.find_statements(Statement(None,foaf.weblog,None)): + + # feed owner + person = statement.subject + feed = model.get_target(statement.object,rdfs.seeAlso) if not feed: continue - title = model.get_target(statement.subject,foaf.name) + title = model.get_target(person,foaf.name) if not title: title = model.get_target(statement.object,dc.title) if not title: continue @@ -36,6 +55,35 @@ def foaf2config(rdf, baseuri, config=None): config.add_section(feed) config.set(feed, 'name', str(title)) + # if we don't have mappings, we're done + if not accounts.keys(): + continue + + # now look for OnlineAccounts for the same person + for statement in model.find_statements(Statement(person,foaf.holdsAccount,None)): + rdfaccthome = model.get_target(statement.object,foaf.accountServiceHomepage) + rdfacctname = model.get_target(statement.object,foaf.accountName) + + if not rdfaccthome or not rdfacctname: continue + + if not rdfaccthome.is_resource() or not accounts.has_key(str(rdfaccthome.uri)): continue + + if not rdfacctname.is_literal(): continue + + rdfacctname = rdfacctname.literal_value['string'] + rdfaccthome = str(rdfaccthome.uri) + + # shorten feed title a bit + try: + servicetitle = rdfaccthome.replace('http://','').split('/')[0] + except: + servicetitle = rdfaccthome + + feed = accounts[rdfaccthome].replace("{foaf:accountName}", rdfacctname) + if not config.has_section(feed): + config.add_section(feed) + config.set(feed, 'name', "%s (%s)" % (title, servicetitle)) + return config if __name__ == "__main__": @@ -43,14 +91,7 @@ if __name__ == "__main__": config = ConfigParser() for uri in sys.argv[1:]: - foaf2config(urllib.urlopen(uri), uri, config) + config.add_section(uri) + foaf2config(urllib.urlopen(uri), config) config.write(sys.stdout) - -if __name__ == "__main__": - # small main program which converts FOAF into config.ini format - import sys, urllib - config = ConfigParser() - for foaf in sys.argv[1:]: - foaf2config(urllib.urlopen(foaf), config) - config.write(sys.stdout) diff --git a/tests/test_foaf.py b/tests/test_foaf.py index 8efe824..22eaedb 100644 --- a/tests/test_foaf.py +++ b/tests/test_foaf.py @@ -43,34 +43,35 @@ class FoafTest(unittest.TestCase): def setUp(self): self.config = ConfigParser() + self.config.add_section(blogroll) # # Tests # def test_foaf_document(self): - foaf2config(test_foaf_document, blogroll, self.config) + foaf2config(test_foaf_document, self.config) self.assertEqual('Danny Ayers', self.config.get(testfeed, 'name')) def test_no_foaf_name(self): test = test_foaf_document.replace('foaf:name','foaf:title') - foaf2config(test, blogroll, self.config) + foaf2config(test, self.config) self.assertEqual('Raw Blog by Danny Ayers', self.config.get(testfeed, 'name')) def test_no_weblog(self): test = test_foaf_document.replace('rdfs:seeAlso','rdfs:seealso') - foaf2config(test, blogroll, self.config) + foaf2config(test, self.config) self.assertFalse(self.config.has_section(testfeed)) def test_invalid_xml_before(self): test = '\n' + test_foaf_document - foaf2config(test, blogroll, self.config) + foaf2config(test, self.config) self.assertFalse(self.config.has_section(testfeed)) def test_invalid_xml_after(self): test = test_foaf_document.strip()[:-1] - foaf2config(test, blogroll, self.config) + foaf2config(test, self.config) self.assertEqual('Danny Ayers', self.config.get(testfeed, 'name')) # these tests only make sense if libRDF is installed