Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:richmahn/tx-md2html into HEAD
Browse files Browse the repository at this point in the history
Conflicts:
	functions/convert/main.py
  • Loading branch information
Richard Mahn committed Sep 28, 2016
2 parents 64e7146 + 41e9a33 commit c72e661
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
14 changes: 6 additions & 8 deletions functions/convert/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

import os
import tempfile
import boto3

import transform_obs

from glob import glob
from shutil import copyfile

from aws_tools.s3_handler import S3Handler
from general_tools.file_utils import add_file_to_zip


def log_message(log, message):
print('{0}: {1}'.format('tx-md2html_convert', message))
log.append('{0}: {1}'.format('tx-md2html_convert', message))
Expand Down Expand Up @@ -76,8 +74,8 @@ def handle(event, context):
try:
if resource == 'obs':
# call with closing to be sure the temp files get cleaned up
obs = transform_obs.TransformOBS(source, output_dir, options)
try:
obs = transform_obs.TransformOBS(source, output_dir, options)
obs.run()
except Exception as e:
error_message(errors, e.message)
Expand All @@ -94,12 +92,12 @@ def handle(event, context):
for filename in glob(os.path.join(output_dir, '*.html')):
add_file_to_zip(zip_file, filename, os.path.basename(filename))
log_message(log, "Uploading {0} to {1}/{2}".format(os.path.basename(zip_file), cdn_bucket, cdn_file))
s3_client = boto3.client('s3')
s3_client.upload_file(zip_file, cdn_bucket, cdn_file)
cdn_handler = S3Handler(cdn_bucket)
cdn_handler.upload_file(zip_file, cdn_file)
log_message(log, "Upload was successful.")
success = True
except Exception as e:
error_message(errors, '2'+e.message)
error_message(errors, e.message)

return {
'log': log,
Expand Down
48 changes: 28 additions & 20 deletions functions/convert/transform_obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ def __init__(self, source_url, output_dir, options):
self.errors = []

def log_message(self, message):
print('{0}: {1}'.format('tx-md2html_convert', message))
self.log.append('{0}: {1}'.format('tx-md2html_convert', message))
print(message)
self.log.append(message)

def error_message(self, message):
print('{0}: {1}'.format('tx-md2html_convert', message))
self.errors.append('{0}: {1}'.format('tx-md2html_convert', message))
print(message)
self.errors.append(message)

def warning_message(self, message):
print('{0}: {1}'.format('tx-md2html_convert', message))
self.warnings.append('{0}: {1}'.format('tx-md2html_convert', message))
print(message)
self.warnings.append(message)

def close(self):
# delete temp files
Expand Down Expand Up @@ -75,33 +75,41 @@ def run(self):
# read the markdown files and output html files
self.log_message('Processing the OBS markdown files')

files_to_process = sorted(glob(os.path.join(self.files_dir, '*.md')))
files = sorted(glob(os.path.join(self.files_dir, '*')))

current_dir = os.path.dirname(os.path.realpath(__file__))

with open(os.path.join(current_dir, 'obs-template.html')) as template_file:
html_template = string.Template(template_file.read())

complete_html = ''
for filename in files_to_process:
# read the markdown file
with codecs.open(filename, 'r', 'utf-8-sig') as md_file:
md = md_file.read()
html = markdown.markdown(md)
complete_html += html
html = html_template.safe_substitute(content=html)
html_filename = os.path.splitext(os.path.basename(filename))[0]+".html"
write_file(os.path.join(self.output_dir, html_filename), html)
self.log_message('Converted {0} to {1}.'.format(os.path.basename(filename), os.path.basename(html_filename)))
for filename in files:
if filename.endswith('.md'):
# read the markdown file
with codecs.open(filename, 'r', 'utf-8-sig') as md_file:
md = md_file.read()
html = markdown.markdown(md)
complete_html += html
html = html_template.safe_substitute(content=html)
html_filename = os.path.splitext(os.path.basename(filename))[0] + ".html"
output_file = os.path.join(self.output_dir, html_filename)
write_file(output_file, html)
self.log_message(
'Converted {0} to {1}.'.format(os.path.basename(filename), os.path.basename(html_filename)))
else:
output_file = os.path.join(self.output_dir, os.path.basename(filename))
copyfile(filename, output_file)

manifest_file = os.path.join(self.files_dir, 'manifest.json')
if os.path.isfile(manifest_file):
copyfile(manifest_file, os.path.join(self.output_dir, 'manifest.json'))

# Do the OBS inspection
warnings = OBSInspection(self.output_dir).run()
for warning in warnings:
inspector = OBSInspection(self.output_dir)
inspector.run()
for warning in inspector.warnings:
self.warning_message(warning)
for error in inspector.errors:
self.error_message(error)

complete_html = html_template.safe_substitute(content=complete_html)
write_file(os.path.join(self.output_dir, 'all.html'), complete_html)
Expand Down
1 change: 0 additions & 1 deletion functions/register/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import requests
import json
import os


def handle(event, ctx):
Expand Down
1 change: 0 additions & 1 deletion functions/register/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
requests
boto3

0 comments on commit c72e661

Please sign in to comment.