-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ratings: Add import and saving of ratings
Saving requires a mediagoblin that has the needed hook
- Loading branch information
Showing
5 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
|
||
import pyexiv2 | ||
|
||
|
||
class Metadata(object): | ||
lookups = { | ||
'rating': ('Xmp.xmp.Rating', ), | ||
} | ||
|
||
def __init__(self, path): | ||
self.path = path | ||
self.md = pyexiv2.ImageMetadata(path) | ||
self.dirty = False | ||
|
||
self.read() | ||
|
||
def read(self): | ||
self.md.read() | ||
|
||
def save(self): | ||
if self.dirty: | ||
self.md.write() | ||
self.dirty = False | ||
|
||
def _get_metadata_item(self, lookup): | ||
existing_keys = self.md.keys() | ||
for key in self.lookups[lookup]: | ||
if key in existing_keys: | ||
return self.md.get(key) | ||
|
||
@property | ||
def rating(self): | ||
tag = self._get_metadata_item('rating') | ||
return tag.value if tag else None | ||
|
||
@rating.setter | ||
def rating(self, value): | ||
tag = self._get_metadata_item('rating') | ||
if tag is None: | ||
tag_key = self.lookups['rating'][0] | ||
tag = pyexiv2.XmpTag(tag_key, value) | ||
self.md[tag_key] = tag | ||
else: | ||
tag.value = value | ||
self.dirty = True | ||
|
||
@classmethod | ||
def from_potential_sidecar(self, filepath): | ||
xmp_filepath = u'{}.xmp'.format(filepath) | ||
if os.path.exists(xmp_filepath): | ||
return Metadata(xmp_filepath) | ||
return Metadata(filepath) | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
path = sys.argv[1].decode('utf8') | ||
md = Metadata.from_potential_sidecar(path) | ||
print md.path | ||
print u" rating:", md.rating | ||
if len(sys.argv) > 2: | ||
md.rating = int(sys.argv[2]) | ||
md.save() | ||
print u" set rating to:", md.rating |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#from sqlalchemy import event | ||
# | ||
#from mediagoblin.db.models import MediaEntry | ||
# | ||
#@event.listens_for(MediaEntry.collections, 'append') | ||
#def test(target, value, initiator): | ||
# print "RECV event" | ||
# import ipdb; ipdb.set_trace() | ||
|
||
import re | ||
|
||
from mediagoblin import mg_globals as mgg | ||
|
||
from .metadata import Metadata | ||
|
||
|
||
def media_added_to_collection(collection, media_entry, note): | ||
m = re.match(r'rating:(\d+)', collection.title) | ||
if not m: | ||
return | ||
rating_number = int(m.group(1)) | ||
set_rating_from_media_entry(media_entry, rating_number) | ||
|
||
|
||
def set_rating_from_media_entry(media_entry, rating): | ||
filepath = (mgg.public_store | ||
._cachefile_to_original_filepath( | ||
media_entry.media_files['original'])) | ||
path = mgg.public_store._resolve_filepath(filepath) | ||
set_rating(path) | ||
|
||
|
||
def set_rating(path, rating): | ||
md = Metadata.from_potential_sidecar(path) | ||
md.rating = rating | ||
md.save() | ||
|
||
|
||
def get_rating(path): | ||
md = Metadata.from_potential_sidecar(path) | ||
return md.rating |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters