Skip to content

Commit

Permalink
Revert "#38 separated json into separate file"
Browse files Browse the repository at this point in the history
This reverts commit 32ddd53.
  • Loading branch information
pombredanne committed Dec 7, 2015
1 parent 514504c commit 84b1b30
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/scancode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,11 @@ def save_results(results, format, input, output_file): # @ReservedAssignment
assert format in formats
if format == 'html':
output_file.write(as_html(results))

elif format == 'html-app':
output_file.write(as_html_app(input, output_file))
output_file.write(as_html_app(results, input, output_file))
try:
create_html_app_assets(results, output_file)
create_html_app_assets(output_file)
except HtmlAppAssetCopyWarning:
click.secho('\nHTML app creation skipped when printing to terminal.',
err=True, fg='yellow')
Expand Down
40 changes: 16 additions & 24 deletions src/scancode/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
from os.path import dirname
from os.path import exists
from os.path import join
from os.path import abspath
from os import makedirs

from commoncode import fileutils

Expand Down Expand Up @@ -59,14 +57,20 @@ def get_template_dir(format): # @ReservedAssignment
return join(dirname(__file__), 'templates', format)


def as_html_app(scanned_path, output_file):
def as_html_app(detected_data, scanned_path, output_file):
"""
Return an HTML string built from a list of results and the html-app template.
"""
template = get_html_template('html-app')
_, assets_dir = get_html_app_files_dirs(output_file)

return template.render(assets_dir=assets_dir, scanned_path=scanned_path)
import json
html_dirs = get_html_app_files_dirs(output_file)
if html_dirs:
_, assets_dir = html_dirs
else:
assets_dir = ''
return template.render(results=json.dumps(detected_data),
assets_dir=assets_dir,
scanned_path=scanned_path)


class HtmlAppAssetCopyWarning(Exception):
Expand All @@ -77,26 +81,21 @@ class HtmlAppAssetCopyError(Exception):
pass


def is_stdout(output_file):
return output_file.name == '<stdout>'


def get_html_app_files_dirs(output_file):
"""
Return a tuple of (parent_dir, dir_name) directory named after the
`output_file` file object file_base_name (stripped from extension) and a
`_files` suffix Return empty strings if output is to stdout.
`_files` suffix Return None if output is to stdout.
"""
if is_stdout(output_file):
return '', ''

file_name = output_file.name
if file_name == '<stdout>':
return
parent_dir = dirname(file_name)
dir_name = fileutils.file_base_name(file_name) + '_files'
return parent_dir, dir_name


def create_html_app_assets(results, output_file):
def create_html_app_assets(output_file):
"""
Given an html-app output_file, create the corresponding `_files` directory
and copy the assets to this directory. The target directory is deleted if it
Expand All @@ -106,21 +105,14 @@ def create_html_app_assets(results, output_file):
HtmlAppAssetCopyError if the copy was not possible.
"""
try:
if is_stdout(output_file):
raise HtmlAppAssetCopyWarning()
assets_dir = join(get_template_dir('html-app'), 'assets')

tgt_dirs = get_html_app_files_dirs(output_file)
if not tgt_dirs:
raise HtmlAppAssetCopyWarning()
target_dir = join(*tgt_dirs)
if exists(target_dir):
fileutils.delete(target_dir)
fileutils.copytree(assets_dir, target_dir)

# write json data
import json
root_path, assets_dir = get_html_app_files_dirs(output_file)
with open(join(root_path, assets_dir, 'data.json'), 'w') as f:
f.write('data=' + json.dumps(results))
except HtmlAppAssetCopyWarning, w:
raise w
except Exception, e:
Expand Down
3 changes: 1 addition & 2 deletions src/scancode/templates/html-app/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
<script src="{{ assets_dir }}/jquery.min.js"></script>
<script src="{{ assets_dir }}/jstree.min.js"></script>
<script src="{{ assets_dir }}/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="{{ assets_dir }}/data.json"></script>
<script type="text/javascript">
var icon_file = "glyphicon glyphicon-file";
var icon_dir_close = "glyphicon glyphicon-folder-close";
Expand All @@ -131,7 +130,7 @@

// Prepare data for tree and table
$( document ).ready(function() {
var dataArray = data;
var dataArray = {{results|safe}};
var jsTreeData = toJSTreeFormat(dataArray);
var dtData = toDataTableFormat(dataArray);
renderTree(jsTreeData.reverse());
Expand Down

0 comments on commit 84b1b30

Please sign in to comment.