Example #1
0
def get_node(url, r, strict=True):
    try:
        node = r.resolve_url(url)
    except NotExistent as e:
        if strict:
            logger.error("Error while getting url %r\n%s" % (url, r.format_tree()))
            raise
        else:
            logger.warn("Ignoring error: %s" % e)
            return Report()

    node = deepcopy(node)
    node.parent = None
    return node
Example #2
0
File: html.py Project: lowks/reprep
def node_to_html_document(node, filename,
                          resources_dir=None,
                          static_dir=None,
                          extra_css=None,
                          write_pickle=False,
                          pickle_compress=True,
                          extra_html_body_start="",
                          extra_html_body_end=""
                          ):
    '''
    
    :param node:
    :param filename:
    :param resources_dir:
    :param extra_css:
    :param extra_html_body_start: Extra HTML to put at the beginning of <body>
    :param write_pickle:
    :param static_dir: Where to put common materials to all reports. 
    '''
    basename = os.path.basename(filename)
    dirname = os.path.dirname(filename)

    if resources_dir is None:
        resources_dir = os.path.join(dirname, basename + '_resources')

    # print('filename: %s ' % filename)
    # print('Resources_dir: %s' % resources_dir)

    rel_resources_dir = os.path.relpath(os.path.realpath(resources_dir), os.path.realpath(dirname))

    # print('real_resources_dir: %s' % rel_resources_dir)

    if dirname and not os.path.exists(dirname):
        try:
            os.makedirs(dirname)
        except: 
            pass 
    if not os.path.exists(resources_dir):
        try:
            os.makedirs(resources_dir)
        except: 
            pass 

    if static_dir is None:
        static_dir = os.path.join(resources_dir, 'static')
        
    # look for static data
    static = resource_filename("reprep", "static")
    if not os.path.exists(static):
        # XXX:
        logger.warn('Warning: resource dir %s not found' % static)
    else:
        if not os.path.exists(static_dir):
            # XXX: does not work if updated
            try:
                shutil.copytree(static, static_dir)
            except:
                if os.path.exists(static_dir):
                    # race condition
                    pass
                else:
                    raise
        
    # print('static_dir: %s' % static_dir)
    rel_static_dir = os.path.relpath(os.path.realpath(static_dir), os.path.realpath(dirname))
    # print('rel_static_dir: %s' % rel_static_dir)

    with open(filename, 'w') as f:
        mapping = {'resources': rel_resources_dir,
                   'title': str(node.nid),
                   'static': rel_static_dir,
                   'extra_css': extra_css if extra_css else "",
                   'date': isodate_with_secs(),
                   'mathjax_header': mathjax_header,
                   'extra_html_body_start': extra_html_body_start,
                   'extra_html_body_end': extra_html_body_end}

        f.write(Template(header).substitute(mapping))

        context = html_context(f,
                    resources_dir=resources_dir,
                    rel_resources_dir=rel_resources_dir,
                    write_pickle=write_pickle,
                    pickle_compress=pickle_compress)
        node_to_html(node, context)

        f.write(Template(footer).substitute(mapping))