Skip to content

Commit

Permalink
Merge 6adde1e into fb20775
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Hopper committed Aug 9, 2016
2 parents fb20775 + 6adde1e commit cf11ae4
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 21 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: python
python:
- '2.7'
- '3.5'
install:
- pip install -r requirements.txt
- pip install coveralls
script: coverage run --source=obs test-setup.py test
after_success: coveralls
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Master: [![Build Status](https://travis-ci.org/unfoldingWord-dev/tx-md2html.svg?branch=master)](https://travis-ci.org/unfoldingWord-dev/tx-md2html) [![Coverage Status](https://coveralls.io/repos/github/unfoldingWord-dev/tx-md2html/badge.svg?branch=master)](https://coveralls.io/github/unfoldingWord-dev/tx-md2html?branch=master)

Develop: [![Build Status](https://travis-ci.org/unfoldingWord-dev/tx-md2html.svg?branch=develop)](https://travis-ci.org/unfoldingWord-dev/tx-md2html) [![Coverage Status](https://coveralls.io/repos/github/unfoldingWord-dev/tx-md2html/badge.svg?branch=develop)](https://coveralls.io/github/unfoldingWord-dev/tx-md2html?branch=develop)


# unfoldingWord Transform markdown to HTML

A library of Python scripts to convert a git repository into publishable text.
Expand Down
45 changes: 26 additions & 19 deletions obs/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import sys
import tempfile
from contextlib import closing

import markdown
from general_tools.file_utils import unzip, load_json_object, make_dir, write_file
from general_tools.print_utils import print_error, print_warning, print_ok
Expand All @@ -19,16 +18,13 @@ class TransformOBS(object):

dir_re = re.compile(r'(<div\s.*?class=".*?obs-content.*?">).*?(</div>)', re.UNICODE + re.DOTALL)

def __init__(self, source_repo_url, output_directory):
def __init__(self, source_repo_url, output_directory, quiet):

if 'git.door43.org' not in source_repo_url:
print_warning('Currently only git.door43.org repositories are supported.')
sys.exit(0)

self.temp_dir = ''
self.temp_dir = tempfile.mkdtemp(prefix='txOBS_')
self.errors = []
self.source_repo_url = source_repo_url
self.output_directory = output_directory
self.quiet = quiet

def close(self):
# delete temp files
Expand All @@ -37,9 +33,11 @@ def close(self):

def run(self):

try:
self.temp_dir = tempfile.mkdtemp(prefix='txOBS_')
if 'git.door43.org' not in self.source_repo_url:
print_warning('Currently only git.door43.org repositories are supported.')
sys.exit(0)

try:
# clean up the git repo url
if self.source_repo_url[-4:] == '.git':
self.source_repo_url = self.source_repo_url[:-4]
Expand All @@ -52,32 +50,39 @@ def run(self):
repo_dir = self.source_repo_url.rpartition('/')[2]
downloaded_file = os.path.join(self.temp_dir, repo_dir + '.zip')
try:
print('Downloading {0}...'.format(file_to_download), end=' ')
if not self.quiet:
print('Downloading {0}...'.format(file_to_download), end=' ')
if not os.path.isfile(downloaded_file):
download_file(file_to_download, downloaded_file)
finally:
print('finished.')
if not self.quiet:
print('finished.')

# unzip the archive
try:
print('Unzipping...'.format(downloaded_file), end=' ')
if not self.quiet:
print('Unzipping...'.format(downloaded_file), end=' ')
unzip(downloaded_file, self.temp_dir)
finally:
print('finished.')
if not self.quiet:
print('finished.')

# get the manifest
try:
print('Reading the manifest...', end=' ')
if not self.quiet:
print('Reading the manifest...', end=' ')
manifest = load_json_object(os.path.join(self.temp_dir, 'manifest.json'))
finally:
print('finished.')
if not self.quiet:
print('finished.')

# create output directory
make_dir(self.output_directory)

# read the markdown files and output html files
try:
print('Processing the OBS markdown files')
if not self.quiet:
print('Processing the OBS markdown files')
files_to_process = []
for i in range(1, 51):
files_to_process.append(str(i).zfill(2) + '.md')
Expand Down Expand Up @@ -106,25 +111,27 @@ def run(self):
self.errors.append(e)

finally:
print('finished.')
if not self.quiet:
print('finished.')

except Exception as e:
print_error(e.message)
self.errors.append(e)


if __name__ == '__main__':
print()

parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-r', '--gitrepo', dest='gitrepo', default=False,
required=True, help='Git repository where the source can be found.')
parser.add_argument('-o', '--outdir', dest='outdir', default=False,
required=True, help='The output directory for markdown files.')
parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', help='Minimize console output.')

args = parser.parse_args(sys.argv[1:])

# call with closing to be sure the temp files get cleaned up
with closing(TransformOBS(args.gitrepo, args.outdir)) as tx:
with closing(TransformOBS(args.gitrepo, args.outdir, args.quiet)) as tx:
tx.run()

print_ok('ALL FINISHED: ', 'Please check the output directory.')
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
markdown
requests
git+git://github.com/unfoldingWord-dev/uw_tools.git#egg=uw_tools
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def run(self):
packages=['register', 'obs'],
long_description=read('README.md'),
classifiers=[],
requires=[
dependency_links=[
'git+git://github.com/unfoldingWord-dev/uw_tools.git#egg=uw_tools',
],
install_requires=[
'markdown',
'requests',
'git+git://github.com/unfoldingWord-dev/uw_tools.git#egg=uw_tools'
'uw_tools'
],
test_suite='tests',
cmdclass={
'install': PostInstallCommand
}
Expand Down
24 changes: 24 additions & 0 deletions test-setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from setuptools import setup

setup(
name="tx-md2html",
version="0.0.1",
author="unfoldingWord",
author_email="unfoldingword.org",
description="Unit test setup file.",
license="MIT",
keywords="",
url="https://github.org/unfoldingWord-dev/tx-md2html",
packages=['obs'],
long_description='Unit test setup file',
classifiers=[],
dependency_links=[
'git+git://github.com/unfoldingWord-dev/uw_tools.git#egg=uw_tools',
],
install_requires=[
'markdown',
'requests',
'uw_tools'
],
test_suite='tests'
)
Empty file added tests/__init__.py
Empty file.
76 changes: 76 additions & 0 deletions tests/test_transformOBS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import print_function, unicode_literals
import os
import shutil
import tempfile
from contextlib import closing
import unittest
from obs.transform import TransformOBS


class TestTransformOBS(unittest.TestCase):

def setUp(self):
"""
Runs before each test
"""
self.out_dir = ''

def tearDown(self):
"""
Runs after each test
"""
# delete temp files
if os.path.isdir(self.out_dir):
shutil.rmtree(self.out_dir, ignore_errors=True)

@classmethod
def setUpClass(cls):
"""
Called before tests in this class are run
"""
pass

@classmethod
def tearDownClass(cls):
"""
Called after tests in this class are run
"""
pass

def test_close(self):
"""
This tests that the temp directory is deleted when the class is closed
"""

with closing(TransformOBS('', '', True)) as tx:
temp_dir = tx.temp_dir

# verify the directory is present
self.assertTrue(os.path.isdir(temp_dir))

# now it should have been deleted
self.assertFalse(os.path.isdir(temp_dir))

def test_run(self):
"""
Runs the converter and verifies the output
"""
# test with the English OBS
repo = 'https://git.door43.org/Door43/en-obs.git'
self.out_dir = tempfile.mkdtemp(prefix='txOBS_Test_')
with closing(TransformOBS(repo, self.out_dir, True)) as tx:
tx.run()

# verify the output
files_to_verify = []
for i in range(1, 51):
files_to_verify.append(str(i).zfill(2) + '.html')

for file_to_verify in files_to_verify:

file_name = os.path.join(self.out_dir, file_to_verify)
self.assertTrue(os.path.isfile(file_name), 'OBS HTML file not found: {0}'.format(file_name))


if __name__ == '__main__':
unittest.main()

0 comments on commit cf11ae4

Please sign in to comment.