changeset 3:4b4bbcf5db31 draft

Uploaded
author saml
date Wed, 21 Nov 2012 12:46:24 -0500
parents 62cfd14e2520
children ee072a7d271b
files sparql_import.py
diffstat 1 files changed, 41 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/sparql_import.py	Wed Nov 21 12:21:20 2012 -0500
+++ b/sparql_import.py	Wed Nov 21 12:46:24 2012 -0500
@@ -10,47 +10,18 @@
 import urllib, sys, re
 
 # -----------------------
-# Option parsing
-# -----------------------
-
-parser = OptionParser()
-parser.add_option("-u", "--url",
-	help = "The URL to the SPARQL endpoint")
-parser.add_option("-q", "--sparql_query",
-	help = "A SPARQL query to send to a SPARQL endpoint")
-parser.add_option("-o", "--output_file",
-	help = "An output file for storing the results")
-(options, args) = parser.parse_args()
-
-if not options.url:
-	sys.exit("You have to specify an URL! Use the -h flag to view command line options!")
-if not options.sparql_query:
-	sys.exit("You have to specify a SPARQL query! Use the -h flag to view command line options!")
-if not options.output_file:
-	sys.exit("You have to specify an output file! Use the -h flag to view command line options!")
-
-if len(options.sparql_query) < 9: 
-	sys.exit("Your SPARQL query is too short (printed below)!\n" + options.sparql_query)
-
-if not re.match("^http", options.url):
-	sys.exit("The URL has to start with 'http://'! Please try again!")
-
-# -----------------------
 # The main code
 # -----------------------
 
 def main():
+	# Parse command line options
+	(options, args) = parse_options()
+
 	# Extract command line options
 	sparql_query = options.sparql_query
-	sparql_query = sparql_query.replace("__oc__","{")
-	sparql_query = sparql_query.replace("__ob__","[")
-	sparql_query = sparql_query.replace("__cc__","}")
-	sparql_query = sparql_query.replace("__cb__","]")
-	sparql_query = sparql_query.replace("__cr__"," ")
-	sparql_query = sparql_query.replace("__cn__"," ")
+	sparql_query = restore_escaped_chars( sparql_query ) 
 	sparql_query = urllib.quote_plus(sparql_query)
 	url = options.url
-
 	output_file = options.output_file
 
 	# Create SPARQL query URL
@@ -66,7 +37,7 @@
                 xmldata = extract_xml( results )
                 tabular = xml_to_tabular( xmldata )
         else:
-                sys.exit("No SPARQL content found in returned data!\nReturned data:\n" + "-"*80 + "\n" + results)
+                sys.exit("No SPARQL content found in returned data!\nReturned data:\n" + "-"*10 + "\n" + results)
 
 	# Print to file
 	of = open(output_file, "w")
@@ -90,10 +61,45 @@
 
 	results = root.getchildren()[1]
 	for result in results:
-		line_bits = [binding.getchildren()[0].text for binding in result.getchildren()]
+		line_bits = ['<' + binding.getchildren()[0].text + '>' for binding in result.getchildren()]
 		line = "\t".join(line_bits)
 		tabular += line + "\n"	
 	return tabular
 
+def restore_escaped_chars( sparql_query ):
+	sparql_query = sparql_query.replace("__oc__","{")
+	sparql_query = sparql_query.replace("__ob__","[")
+	sparql_query = sparql_query.replace("__cc__","}")
+	sparql_query = sparql_query.replace("__cb__","]")
+	sparql_query = sparql_query.replace("__cr__"," ")
+	sparql_query = sparql_query.replace("__cn__"," ")
+	sparql_query = sparql_query.replace("__at__","@")
+	return sparql_query
+
+def parse_options():
+	parser = OptionParser()
+	parser.add_option("-u", "--url",
+		help = "The URL to the SPARQL endpoint")
+	parser.add_option("-q", "--sparql_query",
+		help = "A SPARQL query to send to a SPARQL endpoint")
+	parser.add_option("-o", "--output_file",
+		help = "An output file for storing the results")
+	(options, args) = parser.parse_args()
+
+	if not options.url:
+		sys.exit("You have to specify an URL! Use the -h flag to view command line options!")
+	if not options.sparql_query:
+		sys.exit("You have to specify a SPARQL query! Use the -h flag to view command line options!")
+	if not options.output_file:
+		sys.exit("You have to specify an output file! Use the -h flag to view command line options!")
+
+	if len(options.sparql_query) < 9: 
+		sys.exit("Your SPARQL query is too short (printed below)!\n" + options.sparql_query)
+
+	if not re.match("^http", options.url):
+		sys.exit("The URL has to start with 'http://'! Please try again!")
+
+	return options, args
+
 if __name__ == '__main__':
 	main()