/usr/share/pyshared/dmedia/views.py is in python-dmedia 0.6.0~repack-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | # Authors:
# Jason Gerard DeRose <jderose@novacut.com>
#
# dmedia: distributed media library
# Copyright (C) 2010, 2011 Jason Gerard DeRose <jderose@novacut.com>
#
# This file is part of `dmedia`.
#
# `dmedia` is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# `dmedia` is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with `dmedia`. If not, see <http://www.gnu.org/licenses/>.
"""
Defines the dmedia CouchDB views.
"""
import logging
from couchdb import ResourceNotFound
log = logging.getLogger()
_sum = '_sum'
_count = '_count'
type_type = """
function(doc) {
if (doc.type) {
emit(doc.type, null);
}
}
"""
batch_time = """
function(doc) {
if (doc.type == 'dmedia/batch') {
emit(doc.time, null);
}
}
"""
import_time = """
function(doc) {
if (doc.type == 'dmedia/import') {
emit(doc.time, null);
}
}
"""
# views in the 'file' design only index docs for which doc.type == 'dmedia/file'
file_stored = """
// Get list of all files on a given store, total bytes on that store
function(doc) {
if (doc.type == 'dmedia/file') {
var key;
for (key in doc.stored) {
emit(key, doc.bytes);
}
}
}
"""
file_bytes = """
function(doc) {
if (doc.type == 'dmedia/file' && typeof(doc.bytes) == 'number') {
emit(doc.bytes, doc.bytes);
}
}
"""
file_ext = """
function(doc) {
if (doc.type == 'dmedia/file') {
emit(doc.ext, null);
}
}
"""
file_mime = """
function(doc) {
if (doc.type == 'dmedia/file') {
emit(doc.mime, null);
}
}
"""
file_mtime = """
function(doc) {
if (doc.type == 'dmedia/file') {
emit(doc.mtime, null);
}
}
"""
file_import_id = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.import_id) {
emit(doc.import_id, null);
}
}
"""
# views in the 'user' design only index docs for which doc.type == 'dmedia/file'
# and doc.origin == 'user'
user_copies = """
// Durability of user's personal files
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user') {
var copies = 0;
var key;
for (key in doc.stored) {
copies += doc.stored[key].copies;
}
emit(copies, null);
}
}
"""
user_media = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user') {
emit(doc.media, null);
}
}
"""
user_tags = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user' && doc.tags) {
var key;
for (key in doc.tags) {
emit(key, doc.tags[key]);
}
}
}
"""
user_all = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user') {
emit(doc.mtime, null);
}
}
"""
user_video = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user') {
if (doc.media == 'video') {
emit(doc.mtime, null);
}
}
}
"""
user_image = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user') {
if (doc.media == 'image') {
emit(doc.mtime, null);
}
}
}
"""
user_audio = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user') {
if (doc.media == 'audio') {
emit(doc.mtime, null);
}
}
}
"""
user_inbox = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user') {
if (doc.status == null) {
emit(doc.mtime, null);
}
}
}
"""
user_reject = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user') {
if (doc.status == 'reject') {
emit(doc.mtime, null);
}
}
}
"""
user_keep = """
function(doc) {
if (doc.type == 'dmedia/file' && doc.origin == 'user') {
if (doc.status == 'keep') {
emit(doc.mtime, null);
}
}
}
"""
designs = (
('type', (
('type', type_type, _count),
)),
('batch', (
('time', batch_time, None),
)),
('import', (
('time', import_time, None),
)),
('file', (
('stored', file_stored, _sum),
('import_id', file_import_id, None),
('bytes', file_bytes, _sum),
('ext', file_ext, _count),
('mime', file_mime, _count),
('mtime', file_mtime, None),
)),
('user', (
('copies', user_copies, None),
('media', user_media, _count),
('tags', user_tags, _count),
('all', user_all, None),
('video', user_video, None),
('image', user_image, None),
('audio', user_audio, None),
# Inbox workflow
('inbox', user_inbox, _count),
('reject', user_reject, _count),
('keep', user_keep, _count),
)),
)
def iter_views(views):
for (name, map_, reduce_) in views:
if reduce_ is None:
yield (name, {'map': map_.strip()})
else:
yield (name, {'map': map_.strip(), 'reduce': reduce_.strip()})
def build_design_doc(design, views):
doc = {
'_id': '_design/' + design,
'language': 'javascript',
'views': dict(iter_views(views)),
}
return doc
def update_design_doc(db, doc):
assert '_rev' not in doc
try:
old = db[doc['_id']]
doc['_rev'] = old['_rev']
if doc != old:
db.save(doc)
return 'changed'
else:
return 'same'
except ResourceNotFound:
db.save(doc)
return 'new'
def init_views(db):
log.info('Initializing views in %r', db)
for (name, views) in designs:
doc = build_design_doc(name, views)
update_design_doc(db, doc)
|