You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@community.apache.org by rg...@apache.org on 2009/12/29 23:55:25 UTC
svn commit: r894504 - /comdev/nearby_people/nearby/foaf.py
Author: rgardler
Date: Tue Dec 29 22:55:25 2009
New Revision: 894504
URL: http://svn.apache.org/viewvc?rev=894504&view=rev
Log:
Add support for external FOAF files (need some error checking here - not sure how to do that in Python and it's time for bed)
Modified:
comdev/nearby_people/nearby/foaf.py
Modified: comdev/nearby_people/nearby/foaf.py
URL: http://svn.apache.org/viewvc/comdev/nearby_people/nearby/foaf.py?rev=894504&r1=894503&r2=894504&view=diff
==============================================================================
--- comdev/nearby_people/nearby/foaf.py (original)
+++ comdev/nearby_people/nearby/foaf.py Tue Dec 29 22:55:25 2009
@@ -22,6 +22,7 @@
import datetime
import geo_helper
import sys,os
+import urllib
NS_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns"
NS_GEO = "http://www.w3.org/2003/01/geo/wgs84_pos#"
@@ -61,83 +62,85 @@
continue
print uri
- surname = None
- name = None
- uid = None
- lat = None
- long = None
- projects = []
- depiction = None
- weblogs = []
-
if uri.startswith(PEOPLE_FOAF_NAMESPACE):
file = os.path.join(PEOPLE_FOAF_PATH, uri.replace(PEOPLE_FOAF_NAMESPACE,""))
- foaf = parse(file)
-
- surnameN = foaf.getElementsByTagNameNS(NS_FOAF,"family_name")
- nameN = foaf.getElementsByTagNameNS(NS_FOAF,"name")
- longN = foaf.getElementsByTagNameNS(NS_GEO,"long")
- latN = foaf.getElementsByTagNameNS(NS_GEO,"lat")
- uidN = foaf.getElementsByTagNameNS(NS_FOAF,"Person")
- currentProjectsN = foaf.getElementsByTagNameNS(NS_FOAF,"currentProject")
- depictionN = foaf.getElementsByTagNameNS(NS_FOAF,"depiction")
- weblogsN = foaf.getElementsByTagNameNS(NS_FOAF,"weblog")
-
- if surnameN:
- surname = surnameN[0].firstChild.data
- if nameN:
- name = nameN[0].firstChild.data
- if longN:
- long = longN[0].firstChild.data
- if latN:
- lat = latN[0].firstChild.data
- if uidN:
- uid = uidN[0].getAttribute("rdf:ID")
- if depictionN:
- depiction = depictionN[0].getAttribute("rdf:resource")
+ self._parse_foaf(file)
else:
- # TODO - external FOAF support
- # ....
- pass
-
- # People are funny about their lat and long
- if lat:
- if lat.endswith("N"):
- lat = lat[:-1]
- if lat.endswith("S"):
- lat = lat[:-1]
- if not lat.startswith("-"):
- lat = "-%s" % lat
- if long:
- if long.endswith("E"):
- long = long[:-1]
- if long.endswith("W"):
- long = long[:-1]
- if not long.startswith("-"):
- long = "-%s" % long
-
- if currentProjectsN:
- for projectN in currentProjectsN:
- projectName = projectN.getElementsByTagNameNS(NS_DOAP,"name")
- if not projectName:
- projectName = projectN.getElementsByTagNameNS(NS_PM,"name")
- if projectName:
- projects.append({"name": projectName[0].firstChild.data})
-
- if weblogsN:
- for weblogN in weblogsN:
- documentN = weblogN.getElementsByTagNameNS(NS_FOAF, "Document")
- if documentN:
- title = documentN[0].getElementsByTagNameNS(NS_DC,"title")[0].firstChild.data
- url = documentN[0].getAttribute("rdf:about")
- weblogs.append({"title": title, "url": url})
-
- # Finish building up
- if name and surname and uid and lat and long:
- self.people[name] = {
- "name": name, "surname": surname, "uid":uid,
- "latitude": lat, "longitude": long, "projects": projects,
- "avatar": depiction, "weblogs": weblogs
- }
-
+ usock = urllib.urlopen(uri)
+ self._parse_foaf(usock)
+ usock.close()
+
self.updated_at = datetime.datetime.utcnow()
+
+ def _parse_foaf(self, file):
+ surname = None
+ name = None
+ uid = None
+ lat = None
+ long = None
+ projects = []
+ depiction = None
+ weblogs = []
+
+ foaf = parse(file)
+ surnameN = foaf.getElementsByTagNameNS(NS_FOAF,"family_name")
+ nameN = foaf.getElementsByTagNameNS(NS_FOAF,"name")
+ longN = foaf.getElementsByTagNameNS(NS_GEO,"long")
+ latN = foaf.getElementsByTagNameNS(NS_GEO,"lat")
+ uidN = foaf.getElementsByTagNameNS(NS_FOAF,"Person")
+ currentProjectsN = foaf.getElementsByTagNameNS(NS_FOAF,"currentProject")
+ depictionN = foaf.getElementsByTagNameNS(NS_FOAF,"depiction")
+ weblogsN = foaf.getElementsByTagNameNS(NS_FOAF,"weblog")
+
+ if surnameN:
+ surname = surnameN[0].firstChild.data
+ if nameN:
+ name = nameN[0].firstChild.data
+ if longN:
+ long = longN[0].firstChild.data
+ if latN:
+ lat = latN[0].firstChild.data
+ if uidN:
+ uid = uidN[0].getAttribute("rdf:ID")
+ if depictionN:
+ depiction = depictionN[0].getAttribute("rdf:resource")
+
+ # People are funny about their lat and long
+ if lat:
+ if lat.endswith("N"):
+ lat = lat[:-1]
+ if lat.endswith("S"):
+ lat = lat[:-1]
+ if not lat.startswith("-"):
+ lat = "-%s" % lat
+ if long:
+ if long.endswith("E"):
+ long = long[:-1]
+ if long.endswith("W"):
+ long = long[:-1]
+ if not long.startswith("-"):
+ long = "-%s" % long
+
+ if currentProjectsN:
+ for projectN in currentProjectsN:
+ projectName = projectN.getElementsByTagNameNS(NS_DOAP,"name")
+ if not projectName:
+ projectName = projectN.getElementsByTagNameNS(NS_PM,"name")
+ if projectName:
+ projects.append({"name": projectName[0].firstChild.data})
+
+ if weblogsN:
+ for weblogN in weblogsN:
+ documentN = weblogN.getElementsByTagNameNS(NS_FOAF, "Document")
+ if documentN:
+ title = documentN[0].getElementsByTagNameNS(NS_DC,"title")[0].firstChild.data
+ url = documentN[0].getAttribute("rdf:about")
+ weblogs.append({"title": title, "url": url})
+
+ # Finish building up
+ if name and surname and uid and lat and long:
+ self.people[name] = {
+ "name": name, "surname": surname, "uid":uid,
+ "latitude": lat, "longitude": long, "projects": projects,
+ "avatar": depiction, "weblogs": weblogs
+ }
\ No newline at end of file