padma_migration/padma_dump.py
2011-12-05 14:49:34 +01:00

131 lines
3.6 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vi:si:et:sw=2:sts=2:ts=2
# GPL written 2008 by j@pad.ma
import pkg_resources
pkg_resources.require("TurboGears")
from turbogears import config, update_config, start_server
import cherrypy
cherrypy.lowercase_api = True
from os.path import *
import sys
# first look on the command line for a desired config file,
# if it's not on the command line, then
# look for setup.py in this directory. If it's not there, this script is
# probably installed
if len(sys.argv) > 1:
update_config(configfile=sys.argv[1],
modulename="padma.config")
elif exists(join(dirname(__file__), "setup.py")):
update_config(configfile="dev.cfg",modulename="padma.config")
else:
update_config(configfile="prod.cfg",modulename="padma.config")
config.update(dict(package="padma"))
from padma.model import *
import os
import ox
import simplejson as json
prefix = '/tmp/padma'
os.makedirs(prefix)
data = {}
for v in Video.select():
data[v.hid] = v.jsondump()
with open(os.path.join(prefix, 'data.json'), 'w') as f:
json.dump(data, f, indent=2)
users = []
for u in User.select().orderBy('id'):
users.append({
'id': u.id,
'username': u.user_name.strip(),
'email': u.email_address,
'password': 'sha1$$' + u.password,
'created': u.created.strftime('%Y-%m-%dT%H:%M:%SZ'),
'groups': [g.name for g in u.groups],
})
with open(os.path.join(prefix, 'users.json'), 'w') as f:
json.dump(users, f, indent=2)
files = {}
for v in Video.select().orderBy('id'):
f = {
'sha1sum': v.source_hash,
'ogg': v.filename,
'created': int(v.created.strftime('%s'))
}
info = ox.avinfo(v.filename)
f['oshash'] = info.get('metadata', {}).get('SOURCE_OSHASH', '')
f['ogg_oshash'] = info['oshash']
files[v.hid] = f
with open(os.path.join(prefix, 'files.json'), 'w') as f:
json.dump(files, f, indent=2)
lists = []
for l in List.select().orderBy('id'):
data = {
'id': l.hid,
'user': l.creator.user_name.strip(),
'title': l.title.strip(),
'created': int(l.created.strftime('%s')),
'modified': int(l.modified.strftime('%s')),
'public': l.public,
'description': l.description.strip(),
'type': l.type,
}
if data['type'] == 'dynamic':
data['query'] = {'value': l.query, 'key': l.field}
else:
data['items'] = [v.hid for v in l.videos]
lists.append(data)
with open(os.path.join(prefix, 'lists.json'), 'w') as f:
json.dump(lists, f, indent=2)
locations = []
for l in Location.select().orderBy('id'):
data = {}
data['id'] = l.hid
data['name'] = l.name
data['south'] = l.lat_sw
data['west'] = l.lng_sw
data['north'] = l.lat_ne
data['east'] = l.lng_ne
data['lat'] = l.lat_center
data['lng'] = l.lng_center
data['area'] = l.area
data['created'] = int(l.created.strftime('%s'))
data['modified'] = int(l.modified.strftime('%s'))
data['alternativeNames'] = [l.name for l in l.alt_names]
try:
if l.creator:
data['user'] = l.creator.user_name
except SQLObjectNotFound:
pass
data['annotations'] = []
for a in l.layers:
data['annotations'].append(a.hid)
locations.append(data)
with open(os.path.join(prefix, 'locations.json'), 'w') as f:
json.dump(locations, f, indent=2)
notes = []
for n in Notes.select(Notes.q.notes!=''):
notes.append({
'user': n.user.user_name,
'item': n.video.hid,
'note': n.notes
})
with open(os.path.join(prefix, 'notes.json'), 'w') as f:
json.dump(notes, f, indent=2)