started testing a reversable hash (base64), gid.py now runnable form the command line and supports different GUID formats (sha/base64/text)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2013-03-22 14:32:48 +04:00
parent 97753ea0e0
commit 6d5dea1212
3 changed files with 49 additions and 8 deletions

50
gid.py
View File

@ -1,7 +1,7 @@
#=======================================================================
__version__ = '''0.0.01'''
__sub_version__ = '''20130319145754'''
__sub_version__ = '''20130322142905'''
__copyright__ = '''(c) Alex A. Naanou 2011'''
@ -10,6 +10,7 @@ __copyright__ = '''(c) Alex A. Naanou 2011'''
import os
import sha
import md5
import base64
import time
import pyexiv2 as metadata
@ -30,7 +31,7 @@ def image_gid(path, date=None,
date_format='%Y%m%d-%H%M%S',
default_artist='Unknown',
use_ctime=False,
hash_func=sha.sha):
hash_func=lambda s: sha.sha(s).hexdigest()):
'''
Calculate image GID.
@ -76,7 +77,8 @@ def image_gid(path, date=None,
date = os.path.getctime(path)
data['date'] = time.strftime(date_format, time.gmtime(date))
else:
date = i['Exif.Image.DateTime'].value
## date = i['Exif.Image.DateTime'].value
date = i['Exif.Photo.DateTimeOriginal'].value
data['date'] = date.strftime(date_format)
# check if we need an artist...
if '%(artist)s' in format:
@ -88,15 +90,53 @@ def image_gid(path, date=None,
pass
if hash_func is not None:
return hash_func(format % data).hexdigest()
return hash_func(format % data)
return format % data
#-----------------------------------------------------------------------
if __name__ == '__main__':
pass
from optparse import OptionParser
parser = OptionParser()
##!!! need to define the path so that it shoes up in -h
parser.add_option('-t', '--text',
dest='format',
action='store_const',
const='text',
default='sha',
help='output GUID in base64 format.')
parser.add_option('-b', '--base64',
dest='format',
action='store_const',
const='base64',
default='sha',
help='output GUID in text format.')
parser.add_option('-s', '--sha',
dest='format',
action='store_const',
const='sha',
default='sha',
help='output GUID in sha format.')
options, args = parser.parse_args()
if len(args) != 1:
parser.print_usage()
else:
IN_PATH = args[0]
IN_PATH = IN_PATH.replace('\\', '/')
if options.format == 'text':
print image_gid(IN_PATH, hash_func=None)
elif options.format == 'base64':
# also remove the trailing \n...
print image_gid(IN_PATH, hash_func=lambda s: base64.encodestring(s).strip())
else:
print image_gid(IN_PATH)

View File

@ -1,7 +1,7 @@
#=======================================================================
__version__ = '''0.0.01'''
__sub_version__ = '''20130319002125'''
__sub_version__ = '''20130319151025'''
__copyright__ = '''(c) Alex A. Naanou 2011'''
@ -245,6 +245,7 @@ def gid_index(index, existing=None):
# get file GID...
GID = image_gid('%s.%s' % (os.path.join(*[config['ARCHIVE_ROOT']] + raw[0] + [raw[1]]), raw[2]))
##!!! normalize the image format...
res[GID] = {
'gid': GID,
'name': name,

View File

@ -1,7 +1,7 @@
#=======================================================================
__version__ = '''0.0.01'''
__sub_version__ = '''20120316182131'''
__sub_version__ = '''20130319150549'''
__copyright__ = '''(c) Alex A. Naanou 2011'''
@ -168,7 +168,7 @@ class Index(mapping.Mapping):
pack_ext = self.__pack_ext__
## file_name = name + ext
locations = self.__locations__(name)
# look of the file directly...
# look for the file directly...
for n in locations:
if os.path.exists(os.path.join(self._path, n)):
return json.load(file(os.path.join(self._path, n)))