This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ddoc.py
executable file
·172 lines (153 loc) · 4.63 KB
/
ddoc.py
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
#!/usr/bin/env python
import json
import argparse
import os
from couchdb import Database, ResourceNotFound
from fnmatch import fnmatch
import re
parser = argparse.ArgumentParser(
description="""
A tiny util for pushing files into CouchDB to create CouchApps.
Adheres to the same naming conventions as couchapp by default, but
unlike couchapp, they can be overridden. We also don't do any codegen.
https://github.com/couchapp/couchapp/wiki/Complete-Filesystem-to-Design-Doc-Mapping-Example
"""
)
parser.add_argument(
"database",
help="CouchDB url",
type=str
)
parser.add_argument(
"fixture",
help=""".json fixture file that describes the design document.""",
type=argparse.FileType('r'),
default="couchapp.json"
)
SPECIAL_KEYS = frozenset((
"_attachments",
"views",
"shows",
"lists",
"filters",
"validate_doc_update"
))
def omit(d, keys):
"""
Create a new dictionary object that is a shallow copy of `d` but without
the listed `keys`.
Returns a new dictionary.
"""
out = {}
for key, value in d.items():
if key not in keys:
out[key] = value
return out
def walk_files(path):
"""Yield all file paths in a directory, recursively"""
for dirpath, dirs, files in os.walk(path):
for filepath in files:
yield os.path.join(dirpath, filepath)
def fnmatch_any(filename, patterns):
"""
Match a filename to a list of unix glob patterns. If any of the patterns
match, return True
"""
basename = os.path.basename(filename)
for pattern in patterns:
if fnmatch(basename, pattern):
return True
return False
def find_files(path, match=None, ignore=None):
"""
Find files recursively, filtering the result with optional match
and ignore arrays. Matches and ignores are unix-style glob patterns
and applied at every level as we recursively walk for files.
Returns a generator.
"""
filepaths = walk_files(path)
if match:
filepaths = (fp for fp in filepaths if fnmatch_any(fp, match))
if ignore:
filepaths = (fp for fp in filepaths if not fnmatch_any(fp, ignore))
return filepaths
def re_root_path(root, path):
"""Remove root of path (if any) from path"""
return re.sub("^{}/?".format(root), "", path)
def attach_all(db, doc_id, attachments, root=""):
"""
Attach a dict of attachments to a document.
Optional:
root - a path string for the head of the path. This head will
be removed from the attachment's file path.
"""
for filepath in attachments:
with open(filepath, "r") as f:
doc = db[doc_id]
attachment_filepath = re_root_path(root, filepath)
db.put_attachment(doc, content=f, filename=attachment_filepath)
def read_entire_file(filepath):
"""
A mappable file reader.
Returns a string."""
with open(filepath, "r") as f:
return f.read()
def read_file_fixture(fixture):
"""Read all files in fixture to string"""
return {
key: read_entire_file(filepath)
for key, filepath in fixture["views"].keys()
}
def put_fixture(db, fixture):
try:
views = read_file_fixture(fixture["views"])
except KeyError:
views = {}
try:
shows = read_file_fixture(fixture["views"])
except KeyError:
shows = {}
try:
lists = read_file_fixture(fixture["views"])
except KeyError:
lists = {}
try:
filters = read_file_fixture(fixture["views"])
except KeyError:
filters = {}
try:
validate_doc_update = read_entire_file(fixture["validate_doc_update"])
except KeyError:
validate_doc_update = ""
doc = omit(fixture, SPECIAL_KEYS)
if views:
doc["views"] = views
if shows:
doc["shows"] = shows
if lists:
doc["lists"] = lists
if filters:
doc["filters"] = filters
if validate_doc_update:
doc["validate_doc_update"] = validate_doc_update
db.save(doc)
try:
root = fixture["_attachments"]["path"]
attachment_files = find_files(**fixture["_attachments"])
attach_all(db, doc["_id"], attachment_files, root=root)
except KeyError:
pass
def cli_load_fixture():
"""Command line integration"""
args = parser.parse_args()
db = Database(url=args.database)
fixture = json.load(args.fixture)
# First delete any old design document. This avoids having to merge
# attachments or store attachment revision history.
try:
del db[fixture["_id"]]
except ResourceNotFound:
pass
put_fixture(db, fixture)
if __name__ == "__main__":
cli_load_fixture()