Skip to content

Commit 9c1751e

Browse files
committed
Get rid of tuple in prepare_docset
1 parent cbadfc2 commit 9c1751e

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

doc2dash/__main__.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import click
1212

13+
from characteristic import attributes
14+
1315
from . import __version__, parsers
1416
from .parsers.utils import patch_anchors
1517

@@ -110,24 +112,24 @@ def main(source, force, name, quiet, verbose, destination, add_to_dash,
110112
.format(click.format_filename(source))
111113
)
112114
raise SystemExit(errno.EINVAL)
113-
docs, db_conn = prepare_docset(source, dest, name, index_page)
114-
doc_parser = dt(doc_path=docs)
115+
docset = prepare_docset(source, dest, name, index_page)
116+
doc_parser = dt(doc_path=docset.docs)
115117
log.info(('Converting ' + click.style('{parser_name}', bold=True) +
116118
' docs from "{src}" to "{dst}".')
117119
.format(parser_name=dt.name,
118120
src=click.format_filename(source),
119121
dst=click.format_filename(dest)))
120122

121-
with db_conn:
123+
with docset.db_conn:
122124
log.info('Parsing documentation...')
123125
toc = patch_anchors(doc_parser, show_progressbar=not quiet)
124126
for entry in doc_parser.parse():
125-
db_conn.execute(
127+
docset.db_conn.execute(
126128
'INSERT INTO searchIndex VALUES (NULL, ?, ?, ?)',
127129
entry.as_tuple()
128130
)
129131
toc.send(entry)
130-
count = (db_conn.execute('SELECT COUNT(1) FROM searchIndex')
132+
count = (docset.db_conn.execute('SELECT COUNT(1) FROM searchIndex')
131133
.fetchone()[0])
132134
log.info(('Added ' +
133135
click.style('{count:,}',
@@ -204,13 +206,18 @@ def setup_paths(source, destination, name, add_to_global, force):
204206
return source, dest, name
205207

206208

209+
@attributes(["path", "docs", "plist", "db_conn"])
210+
class DocSet(object):
211+
pass
212+
213+
207214
def prepare_docset(source, dest, name, index_page):
208215
"""
209216
Create boilerplate files & directories and copy vanilla docs inside.
210217
211218
Return a tuple of path to resources and connection to sqlite db.
212219
"""
213-
resources = os.path.join(dest, 'Contents/Resources/')
220+
resources = os.path.join(dest, "Contents", "Resources")
214221
docs = os.path.join(resources, 'Documents')
215222
os.makedirs(resources)
216223

@@ -222,6 +229,7 @@ def prepare_docset(source, dest, name, index_page):
222229
)
223230
db_conn.commit()
224231

232+
plist_path = os.path.join(dest, "Contents", "Info.plist")
225233
plist_cfg = {
226234
'CFBundleIdentifier': name,
227235
'CFBundleName': name,
@@ -234,11 +242,11 @@ def prepare_docset(source, dest, name, index_page):
234242

235243
plistlib.writePlist(
236244
plist_cfg,
237-
os.path.join(dest, 'Contents/Info.plist')
245+
plist_path,
238246
)
239247

240248
shutil.copytree(source, docs)
241-
return docs, db_conn
249+
return DocSet(path=dest, docs=docs, plist=plist_path, db_conn=db_conn)
242250

243251

244252
def add_icon(icon_data, dest):

tests/test_main.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def fake_prepare(source, dest, name, index_page):
7272
'CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, '
7373
'type TEXT, path TEXT)'
7474
)
75-
return 'data', db_conn
75+
return main.DocSet(path=str(tmpdir), docs='data', plist=None,
76+
db_conn=db_conn)
7677

7778
monkeypatch.chdir(tmpdir)
7879
png_file = tmpdir.join("icon.png")
@@ -207,15 +208,15 @@ def test_plist_creation(self, monkeypatch, tmpdir):
207208
m_ct = MagicMock()
208209
monkeypatch.setattr(shutil, 'copytree', m_ct)
209210
os.mkdir('bar')
210-
main.prepare_docset(
211+
docset = main.prepare_docset(
211212
"some/path/foo", 'bar', name="foo", index_page=None
212213
)
213214
m_ct.assert_called_once_with(
214215
'some/path/foo',
215216
'bar/Contents/Resources/Documents',
216217
)
217218
assert os.path.isfile('bar/Contents/Resources/docSet.dsidx')
218-
p = plistlib.readPlist('bar/Contents/Info.plist')
219+
p = plistlib.readPlist(docset.plist)
219220
assert p == {
220221
'CFBundleIdentifier': 'foo',
221222
'CFBundleName': 'foo',
@@ -237,9 +238,9 @@ def test_with_index_page(self, monkeypatch, tmpdir):
237238
m_ct = MagicMock()
238239
monkeypatch.setattr(shutil, 'copytree', m_ct)
239240
os.mkdir('bar')
240-
main.prepare_docset('some/path/foo', 'bar', name='foo',
241-
index_page='foo.html')
242-
p = plistlib.readPlist('bar/Contents/Info.plist')
241+
docset = main.prepare_docset('some/path/foo', 'bar', name='foo',
242+
index_page='foo.html')
243+
p = plistlib.readPlist(docset.plist)
243244
assert p == {
244245
'CFBundleIdentifier': 'foo',
245246
'CFBundleName': 'foo',

0 commit comments

Comments
 (0)