Esempio n. 1
0
def generate_latex_command(sumatra_options, graphics_options):
    # determine project and record store to use
    prj = determine_project(sumatra_options)
    record_store = determine_record_store(prj, sumatra_options)
    project_name = determine_project_name(prj, sumatra_options)
    logger.info("Project name: %s", project_name)
    logger.info("Record store: %s", record_store)

    # get record, obtain image uri
    record_label, image_path = get_record_label_and_image_path(sumatra_options['label'])
    record = record_store.get(project_name, record_label)
    image = get_image(record, image_path, sumatra_options)  # automatically checks digest
    logger.info("Record: %s", record_label)
    logger.info("Image: %s", image)
    # download the image to a temporary directory
    if not os.path.exists(LOCAL_IMAGE_CACHE):
        os.makedirs(LOCAL_IMAGE_CACHE)
    local_filename = image.save_copy(LOCAL_IMAGE_CACHE)

    include_graphics_cmd = "\includegraphics"
    if graphics_options:
        include_graphics_cmd += "[%s]" % ",".join("%s=%s" % item for item in graphics_options.items())
    include_graphics_cmd += "{%s}" % local_filename

    # if record_store is web-accessible, wrap the image in a hyperlink
    if hasattr(record_store, 'server_url'):
        target = record_link_url(record_store.server_url, project_name, record_label)
        cmd = "\href{%s}{%s}" % (target, include_graphics_cmd)
    else:
        cmd = include_graphics_cmd

    print(cmd)
Esempio n. 2
0
def generate_latex_command(sumatra_options, graphics_options):
    # determine project and record store to use
    prj = determine_project(sumatra_options)
    record_store = determine_record_store(prj, sumatra_options)
    project_name = determine_project_name(prj, sumatra_options)
    logger.info("Project name: %s", project_name)
    logger.info("Record store: %s", record_store)

    # get record, obtain image uri
    record_label, image_path = get_record_label_and_image_path(
        sumatra_options['label'])
    record = record_store.get(project_name, record_label)
    image = get_image(record, image_path,
                      sumatra_options)  # automatically checks digest
    logger.info("Record: %s", record_label)
    logger.info("Image: %s", image)
    # download the image to a temporary directory
    if not os.path.exists(LOCAL_IMAGE_CACHE):
        os.makedirs(LOCAL_IMAGE_CACHE)
    local_filename = image.save_copy(LOCAL_IMAGE_CACHE)

    include_graphics_cmd = "\includegraphics"
    if graphics_options:
        include_graphics_cmd += "[%s]" % ",".join(
            "%s=%s" % item for item in graphics_options.items())
    include_graphics_cmd += "{%s}" % local_filename

    # if record_store is web-accessible, wrap the image in a hyperlink
    if hasattr(record_store, 'server_url'):
        target = record_link_url(record_store.server_url, project_name,
                                 record_label)
        cmd = "\href{%s}{%s}" % (target, include_graphics_cmd)
    else:
        cmd = include_graphics_cmd

    print(cmd)
Esempio n. 3
0
    def run(self):
        if 'align' in self.options:
            if isinstance(self.state, states.SubstitutionDef):
                # Check for align_v_values.
                if self.options['align'] not in self.align_v_values:
                    raise self.error(
                        'Error in "%s" directive: "%s" is not a valid value '
                        'for the "align" option within a substitution '
                        'definition.  Valid values for "align" are: "%s".'
                        % (self.name, self.options['align'],
                           '", "'.join(self.align_v_values)))
            elif self.options['align'] not in self.align_h_values:
                raise self.error(
                    'Error in "%s" directive: "%s" is not a valid value for '
                    'the "align" option.  Valid values for "align" are: "%s".'
                    % (self.name, self.options['align'],
                       '", "'.join(self.align_h_values)))
        messages = []
        # previous code from Image.run()
        # --------------------------------------------------------------------

        sumatra_options = build_options(self.state.document.settings, self.options)

        # determine which record store to use
        prj = determine_project(sumatra_options)
        record_store = determine_record_store(prj, sumatra_options, self.error)

        # determine the project (short) name
        project_name = determine_project_name(prj, sumatra_options, self.error)

        record_label, image_path = get_record_label_and_image_path(self.arguments[0])
        record = record_store.get(project_name, record_label)
        image = get_image(record, image_path, self.options, self.error)  # automatically checks digest
        if hasattr(image, "url"):
            reference = image.url
        else:
            if not os.path.exists(LOCAL_IMAGE_CACHE):
                os.makedirs(LOCAL_IMAGE_CACHE)
            reference = image.save_copy(LOCAL_IMAGE_CACHE)

        # set values for alt and target, if they have not been specified
        if not 'target' in self.options and hasattr(record_store, 'server_url'):
            self.options['target'] = record_link_url(record_store.server_url, project_name, record_label)
        if not 'alt' in self.options:
            self.options['alt'] = "Data file generated by computation %s" % record_label

        # --------------------------------------------------------------------
        # following code from Image.run()
        self.options['uri'] = reference
        reference_node = None
        if 'target' in self.options:
            block = states.escape2null(
                self.options['target']).splitlines()
            block = [line for line in block]
            target_type, data = self.state.parse_target(
                block, self.block_text, self.lineno)
            if target_type == 'refuri':
                reference_node = nodes.reference(refuri=data)
            elif target_type == 'refname':
                reference_node = nodes.reference(
                    refname=fully_normalize_name(data),
                    name=whitespace_normalize_name(data))
                reference_node.indirect_reference_name = data
                self.state.document.note_refname(reference_node)
            else:                           # malformed target
                messages.append(data)       # data is a system message
            del self.options['target']
        set_classes(self.options)
        image_node = nodes.image(self.block_text, **self.options)
        self.add_name(image_node)
        if reference_node:
            reference_node += image_node
            return messages + [reference_node]
        else:
            return messages + [image_node]
Esempio n. 4
0
 def test_determine_project_with_local_project(self):
     os.mkdir(".smt")
     open(os.path.join(".smt", "project"), "w").close()  # create empty file
     prj = utils.determine_project({})
     self.assertEqual(prj.path, None)
     shutil.rmtree(".smt")
Esempio n. 5
0
 def test_determine_project_with_no_project(self):
     prj = utils.determine_project({})
     self.assertEqual(prj, None)
Esempio n. 6
0
 def test_determine_project_with_project_dir(self):
     prj = utils.determine_project({'project_dir': '/path/to/project'})
     self.assertEqual(prj.path, '/path/to/project')
Esempio n. 7
0
    def run(self):
        if 'align' in self.options:
            if isinstance(self.state, states.SubstitutionDef):
                # Check for align_v_values.
                if self.options['align'] not in self.align_v_values:
                    raise self.error(
                        'Error in "%s" directive: "%s" is not a valid value '
                        'for the "align" option within a substitution '
                        'definition.  Valid values for "align" are: "%s".' %
                        (self.name, self.options['align'], '", "'.join(
                            self.align_v_values)))
            elif self.options['align'] not in self.align_h_values:
                raise self.error(
                    'Error in "%s" directive: "%s" is not a valid value for '
                    'the "align" option.  Valid values for "align" are: "%s".'
                    % (self.name, self.options['align'], '", "'.join(
                        self.align_h_values)))
        messages = []
        # previous code from Image.run()
        # --------------------------------------------------------------------

        sumatra_options = build_options(self.state.document.settings,
                                        self.options)

        # determine which record store to use
        prj = determine_project(sumatra_options)
        record_store = determine_record_store(prj, sumatra_options, self.error)

        # determine the project (short) name
        project_name = determine_project_name(prj, sumatra_options, self.error)

        record_label, image_path = get_record_label_and_image_path(
            self.arguments[0])
        record = record_store.get(project_name, record_label)
        image = get_image(record, image_path, self.options,
                          self.error)  # automatically checks digest
        if hasattr(image, "url"):
            reference = image.url
        else:
            if not os.path.exists(LOCAL_IMAGE_CACHE):
                os.makedirs(LOCAL_IMAGE_CACHE)
            reference = image.save_copy(LOCAL_IMAGE_CACHE)

        # set values for alt and target, if they have not been specified
        if not 'target' in self.options and hasattr(record_store,
                                                    'server_url'):
            self.options['target'] = record_link_url(record_store.server_url,
                                                     project_name,
                                                     record_label)
        if not 'alt' in self.options:
            self.options[
                'alt'] = "Data file generated by computation %s" % record_label

        # --------------------------------------------------------------------
        # following code from Image.run()
        self.options['uri'] = reference
        reference_node = None
        if 'target' in self.options:
            block = states.escape2null(self.options['target']).splitlines()
            block = [line for line in block]
            target_type, data = self.state.parse_target(
                block, self.block_text, self.lineno)
            if target_type == 'refuri':
                reference_node = nodes.reference(refuri=data)
            elif target_type == 'refname':
                reference_node = nodes.reference(
                    refname=fully_normalize_name(data),
                    name=whitespace_normalize_name(data))
                reference_node.indirect_reference_name = data
                self.state.document.note_refname(reference_node)
            else:  # malformed target
                messages.append(data)  # data is a system message
            del self.options['target']
        set_classes(self.options)
        image_node = nodes.image(self.block_text, **self.options)
        self.add_name(image_node)
        if reference_node:
            reference_node += image_node
            return messages + [reference_node]
        else:
            return messages + [image_node]
Esempio n. 8
0
 def test_determine_project_with_no_project(self):
     prj = utils.determine_project({})
     self.assertEqual(prj, None)
Esempio n. 9
0
 def test_determine_project_with_local_project(self):
     os.mkdir(".smt")
     open(os.path.join(".smt", "project"), "w").close()  # create empty file
     prj = utils.determine_project({})
     self.assertEqual(prj.path, None)
     shutil.rmtree(".smt")
Esempio n. 10
0
 def test_determine_project_with_project_dir(self):
     prj = utils.determine_project({'project_dir': '/path/to/project'})
     self.assertEqual(prj.path, '/path/to/project')