diff options
Diffstat (limited to 'tests/functional-tests/common/utils/extractor.py')
-rw-r--r-- | tests/functional-tests/common/utils/extractor.py | 109 |
1 files changed, 57 insertions, 52 deletions
diff --git a/tests/functional-tests/common/utils/extractor.py b/tests/functional-tests/common/utils/extractor.py index 8dd05604e..36ed0b744 100644 --- a/tests/functional-tests/common/utils/extractor.py +++ b/tests/functional-tests/common/utils/extractor.py @@ -26,6 +26,7 @@ import subprocess class ExtractorParser(object): + def parse_tracker_extract_output(self, text): """ Parse stdout of `tracker-extract --file` to get SPARQL statements. @@ -47,9 +48,9 @@ class ExtractorParser(object): value = extras[value] if metadata.has_key(att): - metadata [att].append(value) + metadata[att].append(value) else: - metadata [att] = [value] + metadata[att] = [value] return metadata @@ -104,7 +105,7 @@ class ExtractorParser(object): grouped_lines = [] current_line = "" anon_node_open = False - for l in embedded.split ("\n\t"): + for l in embedded.split("\n\t"): if "[" in l: current_line = current_line + l anon_node_open = True @@ -113,7 +114,7 @@ class ExtractorParser(object): if "]" in l: anon_node_open = False current_line += l - final_lines = self.__handle_anon_nodes (current_line.strip ()) + final_lines = self.__handle_anon_nodes(current_line.strip()) grouped_lines = grouped_lines + final_lines current_line = "" continue @@ -121,23 +122,24 @@ class ExtractorParser(object): if anon_node_open: current_line += l else: - if (len (l.strip ()) == 0): + if (len(l.strip()) == 0): continue - - final_lines = self.__handle_multivalues (l.strip ()) + + final_lines = self.__handle_multivalues(l.strip()) grouped_lines = grouped_lines + final_lines - return map (self.__clean_value, grouped_lines) + return map(self.__clean_value, grouped_lines) def __process_where_part(self, where): - gettags = re.compile ("(\?\w+)\ a\ nao:Tag\ ;\ nao:prefLabel\ \"([\w\ -]+)\"") + gettags = re.compile( + "(\?\w+)\ a\ nao:Tag\ ;\ nao:prefLabel\ \"([\w\ -]+)\"") tags = {} - for l in where.split ("\n"): - if len (l) == 0: + for l in where.split("\n"): + if len(l) == 0: continue - match = gettags.search (l) + match = gettags.search(l) if (match): - tags [match.group(1)] = match.group (2) + tags[match.group(1)] = match.group(2) else: print "This line is not a tag:", l @@ -150,17 +152,17 @@ class ExtractorParser(object): -> a nfo:Image ; -> a nmm:Photo ; """ - hasEscapedComma = re.compile ("\".+,.+\"") + hasEscapedComma = re.compile("\".+,.+\"") - if "," in line and not hasEscapedComma.search (line): - prop, multival = line.split (" ", 1) + if "," in line and not hasEscapedComma.search(line): + prop, multival = line.split(" ", 1) results = [] - for value in multival.split (","): - results.append ("%s %s" % (prop, value.strip ())) + for value in multival.split(","): + results.append("%s %s" % (prop, value.strip())) return results else: return [line] - + def __handle_anon_nodes(self, line): """ Traslates anonymous nodes in 'flat' properties: @@ -175,11 +177,11 @@ class ExtractorParser(object): -> nfo:hasMediaFileListEntry:entryUrl "file://x.mp3" """ - + # hasTag case - if line.startswith ("nao:hasTag"): - getlabel = re.compile ("nao:prefLabel\ \"([\w\ -]+)\"") - match = getlabel.search (line) + if line.startswith("nao:hasTag"): + getlabel = re.compile("nao:prefLabel\ \"([\w\ -]+)\"") + match = getlabel.search(line) if (match): line = 'nao:hasTag:prefLabel "%s" ;' % (match.group(1)) return [line] @@ -188,32 +190,34 @@ class ExtractorParser(object): return [line] # location case - elif line.startswith ("slo:location"): + elif line.startswith("slo:location"): results = [] # Can have country AND/OR city - getpa = re.compile ("slo:postalAddress\ \<([\w:-]+)\>") - pa_match = getpa.search (line) - + getpa = re.compile("slo:postalAddress\ \<([\w:-]+)\>") + pa_match = getpa.search(line) + if (pa_match): - results.append ('slo:location:postalAddress "%s" ;' % (pa_match.group(1))) + results.append( + 'slo:location:postalAddress "%s" ;' % (pa_match.group(1))) else: print "FIXME another location subproperty in ", line return results - elif line.startswith ("nco:creator"): - getcreator = re.compile ("nco:fullname\ \"([\w\ ]+)\"") - creator_match = getcreator.search (line) + elif line.startswith("nco:creator"): + getcreator = re.compile("nco:fullname\ \"([\w\ ]+)\"") + creator_match = getcreator.search(line) if (creator_match): - new_line = 'nco:creator:fullname "%s" ;' % (creator_match.group (1)) + new_line = 'nco:creator:fullname "%s" ;' % ( + creator_match.group(1)) return [new_line] else: print "Something special in this line '%s'" % (line) - elif line.startswith ("nfo:hasMediaFileListEntry"): - return self.__handle_playlist_entries (line) - + elif line.startswith("nfo:hasMediaFileListEntry"): + return self.__handle_playlist_entries(line) + else: return [line] @@ -225,14 +229,15 @@ class ExtractorParser(object): -> nfo:hMFLE:entryUrl '...' ... """ - geturl = re.compile ("nfo:entryUrl \"([\w\.\:\/]+)\"") - entries = line.strip () [len ("nfo:hasMediaFileListEntry"):] + geturl = re.compile("nfo:entryUrl \"([\w\.\:\/]+)\"") + entries = line.strip()[len("nfo:hasMediaFileListEntry"):] results = [] - for entry in entries.split (","): - url_match = geturl.search (entry) + for entry in entries.split(","): + url_match = geturl.search(entry) if (url_match): - new_line = 'nfo:hasMediaFileListEntry:entryUrl "%s" ;' % (url_match.group (1)) - results.append (new_line) + new_line = 'nfo:hasMediaFileListEntry:entryUrl "%s" ;' % ( + url_match.group(1)) + results.append(new_line) else: print " *** Something special in this line '%s'" % (entry) return results @@ -241,16 +246,16 @@ class ExtractorParser(object): """ the value comes with a ';' or a '.' at the end """ - if (len (value) < 2): - return value.strip () - - clean = value.strip () + if (len(value) < 2): + return value.strip() + + clean = value.strip() if value[-1] in [';', '.']: - clean = value [:-1] + clean = value[:-1] + + clean = clean.replace("\"", "") - clean = clean.replace ("\"", "") - - return clean.strip () + return clean.strip() def get_tracker_extract_output(filename, mime_type=None): @@ -258,14 +263,14 @@ def get_tracker_extract_output(filename, mime_type=None): Runs `tracker-extract --file` to extract metadata from a file. """ - tracker_extract = os.path.join (cfg.EXEC_PREFIX, 'tracker-extract') + tracker_extract = os.path.join(cfg.EXEC_PREFIX, 'tracker-extract') command = [tracker_extract, '--file', filename] if mime_type is not None: command.extend(['--mime', mime_type]) try: - log ('Running: %s' % ' '.join(command)) - output = subprocess.check_output (command) + log('Running: %s' % ' '.join(command)) + output = subprocess.check_output(command) except subprocess.CalledProcessError as e: raise Exception("Error %i from tracker-extract, output: %s" % (e.returncode, e.output)) |