How to use the moltemplate.ttree_lex.TextBlock function in moltemplate

To help you get started, we’ve selected a few moltemplate examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jewettaij / moltemplate / moltemplate / lttree.py View on Github external
for i in range(0, len(table)):
        j = 0
        if isinstance(table[i][0], TextBlock):
            j += 1
        assert(hasattr(table[i], '__len__'))
        syntax_err = False
        if len(table[i]) == j+0:
            pass  # skip blank lines
        elif ((len(table[i]) > j+0) and
              isinstance(table[i][0], TextBlock) and
              (len(table[i][0].text) > 0) and
              (table[i][0].text == '#')):
            pass  # skip comment lines
        if ((len(table[i]) > j+1) and
            isinstance(table[i][j+0], VarRef) and
            isinstance(table[i][j+1], TextBlock)):
            var_ref = table[i][j+0]
            if print_full_atom_type_names:
                var_name = var_ref.prefix[0] + \
                            CanonicalDescrStr(var_ref.nptr.cat_name,
                                              var_ref.nptr.cat_node,
                                              var_ref.nptr.leaf_node,
                                              var_ref.srcloc)
            else:
                var_name = var_ref.nptr.leaf_node.name
            # remove the "@atom:" prefix before the variable name:
            if var_name.find('@atom:') == 0:
                var_name = var_name[6:]
            elif var_name.find('@/atom:') == 0:
                var_name = var_name[7:]
            new_comment = '  # ' + var_name
            if (len(table[i]) == j+2):
github jewettaij / moltemplate / moltemplate / lttree.py View on Github external
command.context_node))
                elif isinstance(command, PushLeftCommand):
                    postprocessing_commands.append(PushLeftCommand(transform_block,
                                                                   command.srcloc,
                                                                   command.context_node))

        elif isinstance(command, WriteFileCommand):

            # --- Throw away lines containin references to deleted variables:---

            # First: To edit the content of a template,
            #        you need to make a deep local copy of it
            tmpl_list = []
            for entry in command.tmpl_list:
                if isinstance(entry, TextBlock):
                    tmpl_list.append(TextBlock(entry.text,
                                               entry.srcloc))  # , entry.srcloc_end))
                else:
                    tmpl_list.append(entry)

            #     Now throw away lines with deleted variables

            DeleteLinesWithBadVars(tmpl_list)

            # --- Now render the text ---
            text = Render(tmpl_list,
                          substitute_vars)

            # ---- Coordinates of the atoms, must be rotated
            # and translated after rendering.
            # In addition, other vectors (dipoles, ellipsoid orientations)
            # must be processed.
github jewettaij / moltemplate / moltemplate / ttree_lex.py View on Github external
token_ltmpl = []
                        j += 1
                        continue

                new_src_loc = OSrcLoc(prev_src_loc.infile, lineno)
                new_src_loc.order = prev_src_loc.order

                for c in token_str:
                    # Reminder to self:  c != delim  (so c!='\n' if delim='\n')
                    # (We keep track of '\n' characters in delimiters above.)
                    if c == '\n':
                        lineno += 1

                new_src_loc.lineno = lineno

                text_block = TextBlock(token_str,
                                       new_src_loc)

                prev_src_loc = new_src_loc

                if len(token_ltmpl) == 0:
                    if delim_found:
                        tokens_lltmpl.append(text_block)
                        del token_ltmpl
                        token_ltmpl = []
                    else:
                        token_ltmpl.append(text_block)
                else:
                    if delim_found:
                        if len(token_str) > 0:
                            token_ltmpl.append(text_block)
                            tokens_lltmpl.append(token_ltmpl)
github jewettaij / moltemplate / moltemplate / ettree.py View on Github external
elif isinstance(command, PushLeftCommand):
                    postprocessing_commands.append(PushLeftCommand(transform_block,
                                                                   command.srcloc,
                                                                   command.context_node))


        elif isinstance(command, WriteFileCommand):

            # --- Throw away lines containin references to deleted variables:---

            # First: To edit the content of a template, 
            #        you need to make a deep local copy of it
            tmpl_list = []
            for entry in command.tmpl_list:
                if isinstance(entry, TextBlock):
                    tmpl_list.append(TextBlock(entry.text, 
                                               entry.srcloc)) #, entry.srcloc_end))
                else:
                    tmpl_list.append(entry)


            # --- Now throw away lines with deleted variables ---

            DeleteLinesWithBadVars(tmpl_list)

            # --- Now render the text ---
            text = Render(tmpl_list, 
                          substitute_vars)

            # ---- Coordinates of the atoms, must be rotated 
            # and translated after rendering.
            # In addition, other vectors (dipoles, ellipsoid orientations)
github jewettaij / moltemplate / moltemplate / ttree_lex.py View on Github external
#sys.stdout.write('   ReadTemplate: text_block=\''+''.join(text_block_plist)+'\'\n')
                    assert(text_block_plist[-2] in self.escape)
                    del text_block_plist[-2]

            if terminate_text:
                #sys.stdout.write('ReadTemplate() appending: ')
                # sys.stdout.write(text_block)

                # tmpl_list.append( [text_block,
                #                   ((prev_filename, prev_lineno),
                #                    (self.infile, self.lineno))] )

                if simplify_output:
                    tmpl_list.append(''.join(text_block_plist))
                else:
                    tmpl_list.append(TextBlock(''.join(text_block_plist),
                                               OSrcLoc(prev_filename, prev_lineno)))
                    #, OSrcLoc(self.infile, self.lineno)))
                if not done_reading:
                    # The character that ended the text block
                    # was a variable delimiter (like '$'), in which case
                    # we should put it (nextchar) in the variable's prefix.
                    var_prefix = nextchar
                else:
                    var_prefix = ''
                var_descr_plist = []
                var_suffix = ''
                prev_filename = self.infile
                prev_lineno = self.lineno
                del text_block_plist
                text_block_plist = []
                # gc.collect()
github jewettaij / moltemplate / moltemplate / lttree.py View on Github external
assert(isinstance(table[i][j+2], TextBlock))
                # If this line doesn't already contain a comment, then add one
                if table[i][j+2].text.find('#') == -1:
                    table[i][j+2].text += new_comment
                else:
                    # Insert a space between 2nd column and the comment
                    table[i][j+2].text = '  '+table[i][j+2].text

            # Also add spaces between any words within the comments.  This is
            # necessary because TableFromTemplate() removed all whitespace
            for k in range(j+3, len(table[i])):
                table[i][k].text = ' '+table[i][k].text
                
            # We must insert a space between the first and second columns
            # because TableFromTemplate() removes this whitespace separator.
            table[i].insert(j+1, TextBlock(' ', table[i][j+1].srcloc))
                    
        else:
            raise InputError('----------------------------------------------------\n' +
                             '     Syntax error near ' +
                             ErrorLeader(table[i][j+0].srcloc.infile,
                                         table[i][j+0].srcloc.lineno) + '\n'
                             '     The format is incorrect.\n')
        # Add a newline:
        table[i].append(TextBlock('\n',table[i][j+1].srcloc))

    # Now flatten the "table" (which is a list-of-lists) 
    # into a simple 1-dimensional list
    # (of alternating VarRefs and TextBlocks, in this case)

    templ_list = [entry for sublist in table for entry in sublist]
github jewettaij / moltemplate / moltemplate / lttree.py View on Github external
if print_full_atom_type_names:
                var_name = var_ref.prefix[0] + \
                            CanonicalDescrStr(var_ref.nptr.cat_name,
                                              var_ref.nptr.cat_node,
                                              var_ref.nptr.leaf_node,
                                              var_ref.srcloc)
            else:
                var_name = var_ref.nptr.leaf_node.name
            # remove the "@atom:" prefix before the variable name:
            if var_name.find('@atom:') == 0:
                var_name = var_name[6:]
            elif var_name.find('@/atom:') == 0:
                var_name = var_name[7:]
            new_comment = '  # ' + var_name
            if (len(table[i]) == j+2):
                table[i].append(TextBlock(new_comment,
                                          table[i][j+1].srcloc))
            else:
                assert(len(table[i]) > j+2)
                assert(isinstance(table[i][j+2], TextBlock))
                # If this line doesn't already contain a comment, then add one
                if table[i][j+2].text.find('#') == -1:
                    table[i][j+2].text += new_comment
                else:
                    # Insert a space between 2nd column and the comment
                    table[i][j+2].text = '  '+table[i][j+2].text

            # Also add spaces between any words within the comments.  This is
            # necessary because TableFromTemplate() removed all whitespace
            for k in range(j+3, len(table[i])):
                table[i][k].text = ' '+table[i][k].text
github jewettaij / moltemplate / moltemplate / ttree_lex.py View on Github external
reading_var = False

            # If we reached the end of the template (and the user requests it),
            # then the terminal character can be included in the list
            # of text_blocks to be returned to the caller.
            if done_reading and keep_terminal_char:
                #sys.stdout.write('ReadTemplate() appending: \''+nextchar+'\'\n')
                # Here we create a new text block which contains only the
                # terminal character (nextchar).
                # tmpl_list.append( [nextchar,
                #                   ((self.infile, self.lineno),
                #                    (self.infile, self.lineno))] )
                if simplify_output:
                    tmpl_list.append(nextchar)
                else:
                    tmpl_list.append(TextBlock(nextchar,
                                               OSrcLoc(self.infile, self.lineno)))
                    #, OSrcLoc(self.infile, self.lineno)))

            if escaped_state:
                escaped_state = False
            else:
                if nextchar in self.escape:
                    escaped_state = True

        #sys.stderr.write("*** TMPL_LIST0  = ***", tmpl_list)
        return tmpl_list  # <- return value stored here
github jewettaij / moltemplate / moltemplate / lttree.py View on Github external
the variable names are discarded.)
    Therefore we have to work with a messy "tmpl_list" object
    which contains the text in a pre-rendered form.  The "tmpl_list" object
    is a list of alternating TextBlocks and VarRef objects.
    This function rebuilds this tmpl_list object, splitting it into separate
    lines (which it currently is not) and then adding comments to the end
    of each line (if there isn't one there already).  Finally it renders
    the resulting template and returns that text to the caller.
    """

    table = TableFromTemplate(tmpl_list,
                              [[' ', '\t', '\r'], '\n'],
                              [True, True])
    for i in range(0, len(table)):
        j = 0
        if isinstance(table[i][0], TextBlock):
            j += 1
        assert(hasattr(table[i], '__len__'))
        syntax_err = False
        if len(table[i]) == j+0:
            pass  # skip blank lines
        elif ((len(table[i]) > j+0) and
              isinstance(table[i][0], TextBlock) and
              (len(table[i][0].text) > 0) and
              (table[i][0].text == '#')):
            pass  # skip comment lines
        if ((len(table[i]) > j+1) and
            isinstance(table[i][j+0], VarRef) and
            isinstance(table[i][j+1], TextBlock)):
            var_ref = table[i][j+0]
            if print_full_atom_type_names:
                var_name = var_ref.prefix[0] + \