Daniel@0: # Part of DML (Digital Music Laboratory) Daniel@0: # Copyright 2014-2015 AUTHOR_AFFILIATION Daniel@0: Daniel@0: # This program is free software; you can redistribute it and/or Daniel@0: # modify it under the terms of the GNU General Public License Daniel@0: # as published by the Free Software Foundation; either version 2 Daniel@0: # of the License, or (at your option) any later version. Daniel@0: # Daniel@0: # This program is distributed in the hope that it will be useful, Daniel@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: # GNU General Public License for more details. Daniel@0: # Daniel@0: # You should have received a copy of the GNU General Public Daniel@0: # License along with this library; if not, write to the Free Software Daniel@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: Daniel@0: from rdflib import Graph Daniel@0: from rdflib.plugins.parsers.notation3 import BadSyntax Daniel@0: import warnings Daniel@0: import codecs Daniel@0: import platform Daniel@0: Daniel@0: # Load and parse an n3 file Daniel@0: def get_rdf_graph_from_n3(n3_file_uri): Daniel@0: Daniel@0: graph = Graph() Daniel@0: Daniel@0: try: Daniel@0: graph.parse(n3_file_uri, format="n3") Daniel@0: except UnicodeDecodeError: Daniel@0: Daniel@0: n3_file_str = uri2path(n3_file_uri) Daniel@0: n3_file_iso = codecs.open(n3_file_str, 'r', "iso-8859-1") Daniel@0: Daniel@0: # check if n3 is valid and parse Daniel@0: # repair if necessary Daniel@0: graph = parse_potentially_corrupt_n3(n3_file_iso.read()) Daniel@0: Daniel@0: except (AssertionError, BadSyntax): Daniel@0: Daniel@0: n3_file_str = uri2path(n3_file_uri) Daniel@0: n3_file = open(n3_file_str, 'r') Daniel@0: graph = parse_potentially_corrupt_n3(n3_file.read()) Daniel@0: Daniel@0: return graph Daniel@0: Daniel@0: # can parse truncated n3 Daniel@0: def parse_potentially_corrupt_n3(content): Daniel@0: feature_graph = Graph() Daniel@0: # test if file is complete. Daniel@0: # if not, delete the last corrupted entry Daniel@0: if not '.' in content[-4:]: Daniel@0: warnings.warn("Incomplete rdf file, ignoring last entry") Daniel@0: # we find the last correct event Daniel@0: lastentry = content.rfind(':event') Daniel@0: feature_graph.parse(data=content[:lastentry], format="n3") Daniel@0: else: Daniel@0: feature_graph.parse(data=content, format="n3") Daniel@0: Daniel@0: return feature_graph Daniel@0: Daniel@0: # returns filepath from url Daniel@0: def uri2path(n3_file_uri): Daniel@0: Daniel@0: n3_file_uri_str = n3_file_uri.__str__() Daniel@0: Daniel@0: # Assume that n3_file_uri_str starts with 'file://' - we need to remove that Daniel@0: if 'Win' in platform.system(): Daniel@0: FILE_URI_START_INDEX = 8 Daniel@0: else: Daniel@0: FILE_URI_START_INDEX = 7 Daniel@0: Daniel@0: n3_file_str = n3_file_uri_str[FILE_URI_START_INDEX:len(n3_file_uri_str)] Daniel@0: return n3_file_str