Exemple #1
0
 def starts_block(self, i, lines, new_state, prev_state):
     '''True if the new state starts a block.'''
     trace = False and g.unitTesting
     self.headline = None
     line = lines[i]
     if prev_state.context:
         return False
     if self.c_keywords_pattern.match(line):
         if trace: g.trace('KEYWORD', repr(line))
         return False
     if not self.match_start_patterns(line):
         return False
     if trace: g.trace('MATCH', repr(line))
     # Must not be a complete statement.
     if line.find(';') > -1:
         if trace: g.trace('STATEMENT', repr(line))
         return False
     # Scan ahead until an open { is seen. the skip count.
     self.skip = 0
     while self.skip < 10:
         if new_state.level() > prev_state.level():
             return True
         if trace: g.trace('SKIP', repr(lines[i]))
         self.skip += 1
         i += 1
         if i < len(lines):
             line = lines[i]
             prev_state = new_state
             new_state = self.scan_line(line, prev_state)
         else:
             break
     if trace:
         g.trace('Run-on C function def')
         g.printList(lines[i-self.skip:i])
     return False
Exemple #2
0
 def write(self, root):
     '''Write an @auto tree containing imported rST code.'''
     trace = False and not g.unitTesting
     root_level = root.level()
     if trace: g.trace('='*20, root.h)
     for p in root.subtree():
         if hasattr(self.at, 'force_sentinels'):
             self.put_node_sentinel(p, '.. ')
         ch = self.underline_char(p, root_level)
         # Put the underlined headline
         self.put(p.h)
         # Fix #242: @auto-rst open/save error.
         n = max(4, len(g.toEncodedString(p.h, reportErrors=False)))
         self.put(ch * n)
         # Ensure that every section ends with exactly two newlines.
         s = p.b.rstrip() + '\n\n'
         lines = s.splitlines(False)
         if trace: g.printList(lines)
         if lines and lines[0].strip():
             self.put('')
         # Put the body.
         for s in lines:
             self.put(s)
     root.setVisited()
     return True
Exemple #3
0
 def start_new_block(self, line, new_state, prev_state, stack):
     '''Create a child node and update the stack.'''
     trace = False and g.unitTesting
     assert not prev_state.in_context(), prev_state
     top = stack[-1]
     prev_p = top.p.copy()
     if trace:
         g.trace('line', repr(line))
         g.trace('top_state', top.state)
         g.trace('new_state', new_state)
         g.printList(stack)
     # Adjust the stack.
     if new_state.indent > top.state.indent:
         pass
     elif new_state.indent == top.state.indent:
         stack.pop()
     else:
         self.cut_stack(new_state, stack)
     # Create the child.
     top = stack[-1]
     parent = top.p
     self.gen_refs = top.gen_refs
     h = self.v2_gen_ref(line, parent, top)
     child = self.v2_create_child_node(parent, line, h)
     stack.append(Target(child, new_state))
     # Handle previous decorators.
     new_p = stack[-1].p.copy()
     self.move_decorators(new_p, prev_p)
 def cut_stack(self, new_state, stack):
     '''Cut back the stack until stack[-1] matches new_state.'''
     trace = False and g.unitTesting
     if trace:
         g.trace(new_state)
         g.printList(stack)
     assert len(stack) > 1  # Fail on entry.
     while stack:
         top_state = stack[-1].state
         if new_state.level() < top_state.level():
             if trace: g.trace('new_state < top_state', top_state)
             assert len(stack) > 1, stack  # <
             stack.pop()
         elif top_state.level() == new_state.level():
             if trace: g.trace('new_state == top_state', top_state)
             assert len(stack) > 1, stack  # ==
             stack.pop()
             break
         else:
             # This happens often in valid coffescript programs.
             if trace: g.trace('new_state > top_state', top_state)
             break
     # Restore the guard entry if necessary.
     if len(stack) == 1:
         if trace: g.trace('RECOPY:', stack)
         stack.append(stack[-1])
     assert len(stack) > 1  # Fail on exit.
     if trace: g.trace('new target.p:', stack[-1].p.h)
Exemple #5
0
 def expand_tree(self, w, i, j, tree_s, word):
     '''
     Paste tree_s as children of c.p.
     This happens *before* any substitutions are made.
     '''
     trace = False and not g.unitTesting
     c, u = self.c, self.c.undoer
     if not c.canPasteOutline(tree_s):
         return g.trace('bad copied outline: %s' % tree_s)
     old_p = c.p.copy()
     bunch = u.beforeChangeTree(old_p)
     self.replace_selection(w, i, j, None)
     self.paste_tree(old_p, tree_s)
     # Make all script substitutions first.
     if trace:
         g.trace()
         g.printList([z.h for z in old_p.self_and_subtree()])
     # Original code.  Probably unwise to change it.
     do_placeholder = False
     for p in old_p.self_and_subtree():
         # Search for the next place-holder.
         val, do_placeholder = self.make_script_substitutions(0, 0, p.b)
         if not do_placeholder: p.b = val
     # Now search for all place-holders.
     for p in old_p.subtree():
         if self.find_place_holder(p, do_placeholder):
             break
     u.afterChangeTree(old_p, 'tree-abbreviation', bunch)
Exemple #6
0
    def scan_tag(self, s, i, tag_level):
        '''
        Scan an xml tag starting with "<" or "</".

        Adjust the stack as appropriate:
        - "<" adds the tag to the stack.
        - "</" removes the top of the stack if it matches.
        '''
        trace = False
        assert s[i] == '<', repr(s[i])
        end_tag = self.match(s, i, '</')
        # Scan the tag.
        i += (2 if end_tag else 1)
        m = self.ch_pattern.match(s, i)
        if m:
            tag = m.group(0).lower()
            i += len(m.group(0))
        else:
            # All other '<' characters should have had xml/html escapes applied to them.
            self.error('missing tag in position %s of %r' % (i, s))
            g.es_print(repr(s))
            return i, tag_level
        if end_tag:
            self.pop_to_tag(tag, s)
            if tag in self.start_tags:
                tag_level -= 1
        else:
            self.stack.append(tag)
            if tag in self.start_tags:
                tag_level += 1
        if trace:
            g.trace(tag, repr(s))
            g.trace('Returns level: ', tag_level)
            g.printList(self.stack)
        return i, tag_level
    def get_files(self):
        '''Return a list of changed files.'''

        trace = False and not g.unitTesting

        def readable(fn):
            for suffix in (
                    'commit_timestamp.json',
                    '.db',
                    '.leo',
                    '.zip',
            ):
                if fn.strip().endswith(suffix):
                    return False
            return True

        command = 'git diff --name-only %s %s' % (self.rev1 or '', self.rev2
                                                  or '')
        files = [
            z.strip() for z in g.execGitCommand(command, self.repo_dir)
            if readable(z)
        ]
        if trace:
            g.trace(command)
            g.printList(files)
        return files
 def start_new_block(self, i, lines, new_state, prev_state, stack):
     '''Create a child node and update the stack.'''
     trace = False and g.unitTesting
     assert not new_state.in_context(), new_state
     line = lines[i]
     top = stack[-1]
     if trace:
         g.trace('line', repr(line))
         g.trace('top_state', top.state)
         g.trace('new_state', new_state)
         g.printList(stack)
     # Adjust the stack.
     if new_state.indent > top.state.indent:
         pass
     elif new_state.indent == top.state.indent:
         stack.pop()
     else:
         self.cut_stack(new_state, stack)
     # Create the child.
     top = stack[-1]
     parent = top.p
     self.gen_refs = top.gen_refs
     h = self.gen_ref(line, parent, top)
     child = self.create_child_node(parent, line, h)
     stack.append(Target(child, new_state))
Exemple #9
0
 def start_new_block(self, i, lines, new_state, prev_state, stack):
     '''Create a child node and update the stack.'''
     trace = False and g.unitTesting
     trace_stack = False
     line = lines[i]
     target = stack[-1]
     # Insert the reference in *this* node.
     h = self.gen_ref(line, target.p, target)
     # Create a new child and associated target.
     if self.headline: h = self.headline
     if new_state.level() > prev_state.level():
         child = self.create_child_node(target.p, line, h)
     else:
         # We may not have seen the { yet, so adjust.
         # Without this, the new block becomes a child of the preceding.
         new_state = C_ScanState()
         new_state.curlies = prev_state.curlies + 1
         child = self.create_child_node(target.p, line, h)
     stack.append(Target(child, new_state))
     if trace: g.trace('=====', repr(line))
     if trace and trace_stack: g.printList(stack)
     # Add all additional lines of the signature.
     skip = self.skip # Don't change the ivar!
     while skip > 0:
         skip -= 1
         i += 1
         assert i < len(lines), (i, len(lines))
         line = lines[i]
         if trace: g.trace('SCAN', 'name', self.headline, 'line', repr(line))
         if not self.headline:
             self.match_name_patterns(line)
             if self.headline:
                 child.h = '%s %s' % (child.h.strip(), self.headline)
         self.add_line(child, lines[i])
Exemple #10
0
    def scan_tag(self, s, i, tag_level):
        '''
        Scan an xml tag starting with "<" or "</".

        Adjust the stack as appropriate:
        - "<" adds the tag to the stack.
        - "</" removes the top of the stack if it matches.
        '''
        trace = False
        assert s[i] == '<', repr(s[i])
        end_tag = self.match(s, i, '</')
        # Scan the tag.
        i += (2 if end_tag else 1)
        m = self.ch_pattern.match(s, i)
        if m:
            tag = m.group(0).lower()
            i += len(m.group(0))
        else:
            # All other '<' characters should have had xml/html escapes applied to them.
            self.error('missing tag in position %s of %r' % (i, s))
            g.es_print(repr(s))
            return i, tag_level
        if end_tag:
            self.pop_to_tag(tag, s)
            if tag in self.start_tags:
                tag_level -= 1
        else:
            self.stack.append(tag)
            if tag in self.start_tags:
                tag_level += 1
        if trace:
            g.trace(tag, repr(s))
            g.trace('Returns level: ', tag_level)
            g.printList(self.stack)
        return i, tag_level
Exemple #11
0
 def start_new_block(self, i, lines, new_state, prev_state, stack):
     '''Create a child node and update the stack.'''
     trace = False and g.unitTesting
     assert not prev_state.in_context(), prev_state
     line = lines[i]
     top = stack[-1]
     if trace:
         g.trace('line', repr(line))
         g.trace('top_state', top.state)
         g.trace('new_state', new_state)
         g.printList(stack)
     # Adjust the stack.
     if new_state.indent > top.state.indent:
         pass
     elif new_state.indent == top.state.indent:
         stack.pop()
     else:
         self.cut_stack(new_state, stack)
     # Create the child.
     top = stack[-1]
     parent = top.p
     self.gen_ref(line, parent, top)
     h = self.clean_headline(line)
     child = self.create_child_node(parent, line, h)
     self.prepend_lines(child, self.decorator_lines)
     if trace: g.printList(self.get_lines(child))
     self.decorator_lines = []
     target = PythonTarget(child, new_state)
     target.kind = 'class' if h.startswith('class') else 'def'
     stack.append(target)
Exemple #12
0
    def pop_to_tag(self, tag, s):
        '''
        Attempt to pop tag from the top of the stack.

        If the top doesn't match, issue a warning and attempt to recover.
        '''
        trace = False
        if not self.stack:
            self.error('Empty tag stack: %s' % tag)
            g.es_print(repr(s))
            return
        if trace:
            g.trace(tag, repr(s))
            g.printList(self.stack)
        top = self.stack[-1]
        if top == tag:
            self.stack.pop()
            return
        # Only issue one warning per file.
        if trace or self.tag_warning_given:
            self.tag_warning_given = True
            self.error('mismatched closing tag: %s top: %s' % (tag, top))
            g.es_print(repr(s))
            if trace:
                g.trace(self.root.h)
                g.printList(self.stack)
        # Attempt a recovery.
        if tag in self.stack:
            while self.stack:
                top = self.stack.pop()
                # if trace: g.trace('POP: ', top)
                if top == tag:
                    return
Exemple #13
0
 def expand_tree(self, w, i, j, tree_s, word):
     '''
     Paste tree_s as children of c.p.
     This happens *before* any substitutions are made.
     '''
     trace = False and not g.unitTesting
     c, u = self.c, self.c.undoer
     if not c.canPasteOutline(tree_s):
         return g.trace('bad copied outline: %s' % tree_s)
     old_p = c.p.copy()
     bunch = u.beforeChangeTree(old_p)
     self.replace_selection(w, i, j, None)
     self.paste_tree(old_p, tree_s)
     # Make all script substitutions first.
     if trace:
         g.trace()
         g.printList([z.h for z in old_p.self_and_subtree()])
     # Original code.  Probably unwise to change it.
     do_placeholder = False
     for p in old_p.self_and_subtree():
         # Search for the next place-holder.
         val, do_placeholder = self.make_script_substitutions(0, 0, p.b)
         if not do_placeholder: p.b = val
     # Now search for all place-holders.
     for p in old_p.subtree():
         if self.find_place_holder(p, do_placeholder):
             break
     u.afterChangeTree(old_p, 'tree-abbreviation', bunch)
 def post_pass(self, parent):
     '''Massage the created nodes.'''
     trace = False and not g.unitTesting and self.root.h.endswith(
         '1.coffee')
     if trace:
         g.trace('=' * 60)
         for p in parent.self_and_subtree():
             print('***** %s' % p.h)
             g.printList(self.get_lines(p))
     # ===== Generic: use base Importer methods =====
     self.clean_all_headlines(parent)
     self.clean_all_nodes(parent)
     # ===== Specific to coffeescript =====
     #
     self.move_trailing_lines(parent)
     # ===== Generic: use base Importer methods =====
     self.unindent_all_nodes(parent)
     #
     # This sub-pass must follow unindent_all_nodes.
     self.promote_trailing_underindented_lines(parent)
     #
     # This probably should be the last sub-pass.
     self.delete_all_empty_nodes(parent)
     if trace:
         g.trace('-' * 60)
         for p in parent.self_and_subtree():
             print('***** %s' % p.h)
             g.printList(self.get_lines(p))
Exemple #15
0
 def start_new_block(self, i, lines, new_state, prev_state, stack):
     '''Create a child node and update the stack.'''
     trace = False and g.unitTesting
     assert not prev_state.in_context(), prev_state
     line = lines[i]
     top = stack[-1]
     if trace:
         g.trace('line', repr(line))
         g.trace('top_state', top.state)
         g.trace('new_state', new_state)
         g.printList(stack)
     # Adjust the stack.
     if new_state.indent > top.state.indent:
         pass
     elif new_state.indent == top.state.indent:
         stack.pop()
     else:
         self.cut_stack(new_state, stack)
     # Create the child.
     top = stack[-1]
     parent = top.p
     self.gen_ref(line, parent, top)
     h = self.clean_headline(line)
     child = self.create_child_node(parent, line, h)
     self.prepend_lines(child, self.decorator_lines)
     if trace: g.printList(self.get_lines(child))
     self.decorator_lines = []
     target = PythonTarget(child, new_state)
     target.kind = 'class' if h.startswith('class') else 'def'
     stack.append(target)
Exemple #16
0
 def start_new_block(self, i, lines, new_state, prev_state, stack):
     '''Create a child node and update the stack.'''
     trace = False and g.unitTesting
     trace_stack = False
     line = lines[i]
     target = stack[-1]
     # Insert the reference in *this* node.
     h = self.gen_ref(line, target.p, target)
     # Create a new child and associated target.
     if self.headline: h = self.headline
     if new_state.level() > prev_state.level():
         child = self.create_child_node(target.p, line, h)
     else:
         # We may not have seen the { yet, so adjust.
         # Without this, the new block becomes a child of the preceding.
         new_state = Java_ScanState()
         new_state.curlies = prev_state.curlies + 1
         child = self.create_child_node(target.p, line, h)
     stack.append(Target(child, new_state))
     if trace: g.trace('=====', repr(line))
     if trace and trace_stack: g.printList(stack)
     # Add all additional lines of the signature.
     skip = self.skip # Don't change the ivar!
     while skip > 0:
         skip -= 1
         i += 1
         assert i < len(lines), (i, len(lines))
         line = lines[i]
         if trace: g.trace('SCAN', 'name', self.headline, 'line', repr(line))
         if not self.headline:
             self.match_name_patterns(line)
             if self.headline:
                 child.h = '%s %s' % (child.h.strip(), self.headline)
         self.add_line(child, lines[i])
Exemple #17
0
 def write(self, root):
     '''Write an @auto tree containing imported rST code.'''
     trace = False and not g.unitTesting
     root_level = root.level()
     if trace: g.trace('=' * 20, root.h)
     self.write_root(root)
     for p in root.subtree():
         if hasattr(self.at, 'force_sentinels'):
             self.put_node_sentinel(p, '.. ')
         ch = self.underline_char(p, root_level)
         # Put the underlined headline
         self.put(p.h)
         # Fix #242: @auto-rst open/save error.
         n = max(4, len(g.toEncodedString(p.h, reportErrors=False)))
         self.put(ch * n)
         # Ensure that every section ends with exactly two newlines.
         s = p.b.rstrip() + '\n\n'
         lines = s.splitlines(False)
         if trace: g.printList(lines)
         if lines and lines[0].strip():
             self.put('')
         # Put the body.
         for s in lines:
             self.put(s)
     root.setVisited()
     return True
Exemple #18
0
 def scan_tag(self, s, i, tag_level):
     '''
     Scan for the *start* of a beginning *or ending tag at i in s.
     Update tag_level only if the tag matches a tag in self.start_tags.
     '''
     trace = False
     assert s[i] == '<', repr(s[i])
     end_tag = self.match(s, i, '</')
     i += (2 if end_tag else 1)
     tag_i = i
     while i < len(s):
         m = self.ch_pattern.match(s[i])
         if m: i += 1
         else: break
     tag = s[tag_i:i].lower()
     # Here, i has already been incremented.
     if tag and end_tag:
         if self.stack:
             top = self.stack[-1]
             if top[1] == tag:
                 self.stack[-1][0] = '</'
             else:
                 self.error('mismatched closing tag: %s %s' % (
                     tag, top[1]))
         else:
             self.error('tag underflow: %s' % tag)
     elif tag:
         self.stack.append(['<', tag])
         if tag in self.start_tags:
             tag_level += 1
     if trace:
         g.trace('tag: %s end: %s level: %s len(stack): %s %r' % (
             tag, int(end_tag), tag_level, len(self.stack), s))
         g.printList(self.stack)
     return i, tag_level
 def cut_stack(self, new_state, stack):
     '''Cut back the stack until stack[-1] matches new_state.'''
     trace = False and g.unitTesting
     if trace:
         g.trace(new_state)
         g.printList(stack)
     assert len(stack) > 1 # Fail on entry.
     while stack:
         top_state = stack[-1].state
         if new_state.level() < top_state.level():
             if trace: g.trace('new_state < top_state', top_state)
             assert len(stack) > 1, stack # <
             stack.pop()
         elif top_state.level() == new_state.level():
             if trace: g.trace('new_state == top_state', top_state)
             assert len(stack) > 1, stack # ==
             stack.pop()
             break
         else:
             # This happens often in valid coffescript programs.
             if trace: g.trace('new_state > top_state', top_state)
             break
     # Restore the guard entry if necessary.
     if len(stack) == 1:
         if trace: g.trace('RECOPY:', stack)
         stack.append(stack[-1])
     assert len(stack) > 1 # Fail on exit.
     if trace: g.trace('new target.p:', stack[-1].p.h)
Exemple #20
0
 def cut_stack(self, new_state, stack, append=False):
     '''Cut back the stack until stack[-1] matches new_state.'''
     # pylint: disable=arguments-differ
     trace = False # and g.unitTesting
     if trace:
         g.trace(new_state)
         g.printList(stack)
     assert len(stack) > 1 # Fail on entry.
     while stack:
         top_state = stack[-1].state
         if new_state.level() < top_state.level():
             if trace: g.trace('new_state < top_state', top_state)
             assert len(stack) > 1, stack # <
             stack.pop()
         elif top_state.level() == new_state.level():
             if trace: g.trace('new_state == top_state', top_state)
             assert len(stack) > 1, stack # ==
             if append:
                 pass # Append line to the previous node.
             else:
                 stack.pop() # Create a new node.
             break
         else:
             # This happens often in valid Python programs.
             if trace: g.trace('new_state > top_state', top_state)
             break
     # Restore the guard entry if necessary.
     if len(stack) == 1:
         if trace: g.trace('RECOPY:', stack)
         stack.append(stack[-1])
     assert len(stack) > 1 # Fail on exit.
     if trace: g.trace('new target.p:', stack[-1].p.h)
Exemple #21
0
 def hide(self, tag, kwargs, force=False):
     '''Hide all wikiview tags. Now done in the colorizer.'''
     trace = False and not g.unitTesting
     trace_parts = True
     trace_pats = False
     c = self.c
     if not (self.active or force) or kwargs['c'] != c:
         return
     w = c.frame.body.widget
     cursor = w.textCursor()
     s = w.toPlainText()
     if trace:
         g.trace('=====', g.callers())
         g.printList(g.splitLines(s))
     for urlpat in self.urlpats:
         if trace and trace_pats: g.trace(repr(urlpat))
         for m in urlpat.finditer(s):
             if trace: g.trace('FOUND', urlpat.pattern, m.start(0), repr(m.group(0)))
             for group_n, group in enumerate(m.groups()):
                 if group is None:
                     continue
                 if trace and trace_parts: g.trace(
                         m.start(group_n+1),
                         m.end(group_n+1),
                         repr(m.group(group_n+1)))
                 cursor.setPosition(m.start(group_n+1))
                 cursor.setPosition(m.end(group_n+1), cursor.KeepAnchor)
                 cfmt = cursor.charFormat()
                 cfmt.setFontPointSize(self.pts)
                 cfmt.setFontLetterSpacing(self.pct)
                 # cfmt._is_hidden = True  # gets lost
                 cursor.setCharFormat(cfmt)
Exemple #22
0
 def start_new_block(self, i, lines, new_state, prev_state, stack):
     '''Create a child node and update the stack.'''
     trace = False and g.unitTesting
     assert not new_state.in_context(), new_state
     line = lines[i]
     top = stack[-1]
     if trace:
         g.trace('line', repr(line))
         g.trace('top_state', top.state)
         g.trace('new_state', new_state)
         g.printList(stack)
     # Adjust the stack.
     if new_state.indent > top.state.indent:
         pass
     elif new_state.indent == top.state.indent:
         stack.pop()
     else:
         self.cut_stack(new_state, stack)
     # Create the child.
     top = stack[-1]
     parent = top.p
     self.gen_refs = top.gen_refs
     h = self.gen_ref(line, parent, top)
     child = self.create_child_node(parent, line, h)
     stack.append(Target(child, new_state))
Exemple #23
0
 def starts_block(self, i, lines, new_state, prev_state):
     '''True if the new state starts a block.'''
     trace = False and g.unitTesting
     self.headline = None
     line = lines[i]
     if prev_state.context:
         return False
     if self.java_keywords_pattern.match(line):
         if trace: g.trace('KEYWORD', repr(line))
         return False
     if not self.match_start_patterns(line):
         return False
     if trace: g.trace('MATCH', repr(line))
     # Must not be a complete statement.
     if line.find(';') > -1:
         if trace: g.trace('STATEMENT', repr(line))
         return False
     # Scan ahead until an open { is seen. the skip count.
     self.skip = 0
     while self.skip < 10:
         if new_state.level() > prev_state.level():
             return True
         if trace: g.trace('SKIP', repr(lines[i]))
         self.skip += 1
         i += 1
         if i < len(lines):
             line = lines[i]
             prev_state = new_state
             new_state = self.scan_line(line, prev_state)
         else:
             break
     if trace:
         g.trace('Run-on C function def')
         g.printList(lines[i-self.skip:i])
     return False
Exemple #24
0
 def cut_stack(self, new_state, stack, append=False):
     '''Cut back the stack until stack[-1] matches new_state.'''
     # pylint: disable=arguments-differ
     trace = False  # and g.unitTesting
     if trace:
         g.trace(new_state)
         g.printList(stack)
     assert len(stack) > 1  # Fail on entry.
     while stack:
         top_state = stack[-1].state
         if new_state.level() < top_state.level():
             if trace: g.trace('new_state < top_state', top_state)
             assert len(stack) > 1, stack  # <
             stack.pop()
         elif top_state.level() == new_state.level():
             if trace: g.trace('new_state == top_state', top_state)
             assert len(stack) > 1, stack  # ==
             if append:
                 pass  # Append line to the previous node.
             else:
                 stack.pop()  # Create a new node.
             break
         else:
             # This happens often in valid Python programs.
             if trace: g.trace('new_state > top_state', top_state)
             break
     # Restore the guard entry if necessary.
     if len(stack) == 1:
         if trace: g.trace('RECOPY:', stack)
         stack.append(stack[-1])
     assert len(stack) > 1  # Fail on exit.
     if trace: g.trace('new target.p:', stack[-1].p.h)
Exemple #25
0
    def pop_to_tag(self, tag, s):
        '''
        Attempt to pop tag from the top of the stack.

        If the top doesn't match, issue a warning and attempt to recover.
        '''
        trace = False
        if not self.stack:
            self.error('Empty tag stack: %s' % tag)
            g.es_print(repr(s))
            return
        if trace:
            g.trace(tag, repr(s))
            g.printList(self.stack)
        top = self.stack[-1]
        if top == tag:
            self.stack.pop()
            return
        # Only issue one warning per file.
        if trace or self.tag_warning_given:
            self.tag_warning_given = True
            self.error('mismatched closing tag: %s top: %s' % (tag, top))
            g.es_print(repr(s))
            if trace:
                g.trace(self.root.h)
                g.printList(self.stack)
        # Attempt a recovery.
        if tag in self.stack:
            while self.stack:
                top = self.stack.pop()
                # if trace: g.trace('POP: ', top)
                if top == tag:
                    return
 def post_pass(self, parent):
     '''Massage the created nodes.'''
     trace = False and not g.unitTesting and self.root.h.endswith('1.coffee')
     if trace:
         g.trace('='*60)
         for p in parent.self_and_subtree():
             print('***** %s' % p.h)
             g.printList(p.v._import_lines)
     # ===== Generic: use base Importer methods =====
     self.clean_all_headlines(parent)
     self.clean_all_nodes(parent)
     # ===== Specific to coffeescript =====
     #
     self.move_trailing_lines(parent)
     # ===== Generic: use base Importer methods =====
     self.unindent_all_nodes(parent)
     #
     # This sub-pass must follow unindent_all_nodes.
     self.promote_trailing_underindented_lines(parent)
     #
     # This probably should be the last sub-pass.
     self.delete_all_empty_nodes(parent)
     if trace:
         g.trace('-'*60)
         for p in parent.self_and_subtree():
             print('***** %s' % p.h)
             g.printList(p.v._import_lines)
Exemple #27
0
 def cut_stack(self, new_state, stack):
     '''Cut back the stack until stack[-1] matches new_state.'''
     trace = False and g.unitTesting
     if trace:
         g.trace(new_state)
         g.printList(stack)
     # This underflow could happen as the result of extra 'end' statement in user code.
     if len(stack) > 1:
         stack.pop()
Exemple #28
0
 def cut_stack(self, new_state, stack):
     '''Cut back the stack until stack[-1] matches new_state.'''
     trace = False and g.unitTesting
     if trace:
         g.trace(new_state)
         g.printList(stack)
     # This underflow could happen as the result of extra 'end' statement in user code.
     if len(stack) > 1:
         stack.pop()
Exemple #29
0
 def add_tags(self):
     '''Add items to self.class/functionTags and from settings.'''
     trace = False
     c, setting = self.c, self.tags_setting
     aList = c.config.getData(setting) or []
     aList = [z.lower() for z in aList]
     if trace:
         g.trace(setting)
         g.printList(aList)
     return aList
Exemple #30
0
 def trace_status(self, line, new_state, prev_state, stack, top):
     '''Print everything important in the i.gen_lines loop.'''
     print('')
     try:
         g.trace('===== %r' % line)
     except Exception:
         g.trace('     top.p: %s' % g.toEncodedString(top.p.h))
     # print('len(stack): %s' % len(stack))
     print(' new_state: %s' % new_state)
     print('prev_state: %s' % prev_state)
     # print(' top.state: %s' % top.state)
     g.printList(stack)
Exemple #31
0
 def trace_status(self, line, new_state, prev_state, stack, top):
     '''Print everything important in the i.gen_lines loop.'''
     print('')
     try:
         g.trace('===== %r' % line)
     except Exception:
         g.trace('     top.p: %s' % g.toEncodedString(top.p.h))
     # print('len(stack): %s' % len(stack))
     print(' new_state: %s' % new_state)
     print('prev_state: %s' % prev_state)
     # print(' top.state: %s' % top.state)
     g.printList(stack)
Exemple #32
0
 def trace_status(self, line, new_state, prev_state, stack, top):
     """Print everything important in the i.gen_lines loop."""
     print('')
     try:
         g.trace(repr(line))
     except Exception:
         g.trace(f"    top.p: {g.toUnicode(top.p.h)}")
     # print('len(stack): %s' % len(stack))
     print(' new_state: %s' % new_state)
     print('prev_state: %s' % prev_state)
     # print(' top.state: %s' % top.state)
     g.printList(stack)
Exemple #33
0
 def trace_status(self, line, new_state, prev_state, stack, top):
     '''Print everything important in the i.gen_lines loop.'''
     if line.isspace() or line.strip().startswith(';'):
         return # for elisp
     print('')
     try:
         g.trace('===== %r' % line)
     except Exception:
         g.trace('     top.p: %s' % g.toEncodedString(top.p.h))
     # print('len(stack): %s' % len(stack))
     print(' new_state: %s' % new_state)
     print('prev_state: %s' % prev_state)
     # print(' top.state: %s' % top.state)
     g.printList(stack)
Exemple #34
0
 def trace_status(self, line, new_state, prev_state, stack, top):
     '''Print everything important in the i.gen_lines loop.'''
     if line.isspace() or line.strip().startswith(';'):
         return # for elisp
     print('')
     try:
         g.trace('===== %r' % line)
     except Exception:
         g.trace('     top.p: %s' % g.toEncodedString(top.p.h))
     # print('len(stack): %s' % len(stack))
     print(' new_state: %s' % new_state)
     print('prev_state: %s' % prev_state)
     # print(' top.state: %s' % top.state)
     g.printList(stack)
Exemple #35
0
 def trace_status(self, line, new_state, prev_state, stack, top):
     """Print everything important in the i.gen_lines loop."""
     if line.isspace() or line.strip().startswith(';'):
         return  # for elisp
     print('')
     try:
         g.trace(repr(line))
     except Exception:
         g.trace(f"     top.p: {g.toUnicode(top.p.h)}")
     # print('len(stack): %s' % len(stack))
     print(' new_state: %s' % new_state)
     print('prev_state: %s' % prev_state)
     # print(' top.state: %s' % top.state)
     g.printList(stack)
Exemple #36
0
 def cut_stack(self, new_state, stack):
     '''Cut back the stack until stack[-1] matches new_state.'''
     trace = False # and g.unitTesting
     if trace:
         g.trace(new_state)
         g.printList(stack)
     assert len(stack) > 1 # Fail on entry.
     # function/end's are strictly nested, so this suffices.
     stack.pop()
     # Restore the guard entry if necessary.
     if len(stack) == 1:
         if trace: g.trace('RECOPY:', stack)
         stack.append(stack[-1])
     assert len(stack) > 1 # Fail on exit.
     if trace: g.trace('new target.p:', stack[-1].p.h)
Exemple #37
0
 def cut_stack(self, new_state, stack):
     '''Cut back the stack until stack[-1] matches new_state.'''
     trace = False  # and g.unitTesting
     if trace:
         g.trace(new_state)
         g.printList(stack)
     assert len(stack) > 1  # Fail on entry.
     # function/end's are strictly nested, so this suffices.
     stack.pop()
     # Restore the guard entry if necessary.
     if len(stack) == 1:
         if trace: g.trace('RECOPY:', stack)
         stack.append(stack[-1])
     assert len(stack) > 1  # Fail on exit.
     if trace: g.trace('new target.p:', stack[-1].p.h)
Exemple #38
0
 def exec_node(self, script):
     '''Execute the script in node p.'''
     c = self.c
     try:
         c.executeScript(
             namespace=self.namespace,
             script=script,
             raiseFlag=False,
             useSelectedText=False,
         )
     except Exception:
         g.es_exception()
         g.es_print('script...')
         g.printList(g.splitLines(script))
         g.es_print('Ending the tutorial...')
         self.end()
Exemple #39
0
 def exec_node(self, script):
     '''Execute the script in node p.'''
     c = self.c
     try:
         c.executeScript(
             namespace=self.namespace,
             script=script,
             raiseFlag=False,
             useSelectedText=False,
         )
     except Exception:
         g.es_exception()
         g.es_print('script...')
         g.printList(g.splitLines(script))
         g.es_print('Ending the tutorial...')
         self.end()
Exemple #40
0
def report_version():
    try:
        from pylint import lint
    except ImportError:
        g.trace('can not import pylint')
    table = (
        os.path.abspath(os.path.expanduser('~/.leo/pylint-leo-rc.txt')),
        os.path.abspath(os.path.join('leo', 'test', 'pylint-leo-rc.txt')),
    )
    for rc_fn in table:
        try:
            rc_fn = rc_fn.replace('\\', '/')
            lint.Run(["--rcfile=%s" % (rc_fn), '--version',])
        except OSError:
            pass
    g.trace('no rc file found in')
    g.printList(table)
Exemple #41
0
 def gen_ref(self, line, parent, target):
     '''
     Generate the at-others and a flag telling this method whether a previous
     #@+others
     #@-others
     '''
     trace = False # and g.unitTesting
     indent_ws = self.get_str_lws(line)
     h = self.clean_headline(line)
     if not target.at_others_flag:
         target.at_others_flag = True
         ref = '%s@others\n' % indent_ws
         if trace:
             g.trace('indent_ws: %r line: %r parent: %s' % (
                  indent_ws, line, parent.h))
             g.printList(self.get_lines(parent))
         self.add_line(parent,ref)
     return h
Exemple #42
0
 def start_new_block(self, i, lines, new_state, prev_state, stack):
     '''Create a child node and update the stack.'''
     trace = False and g.unitTesting
     if hasattr(new_state, 'in_context'):
         assert not new_state.in_context(), ('start_new_block', new_state)
     line = lines[i]
     target = stack[-1]
     # Insert the reference in *this* node.
     h = self.gen_ref(line, target.p, target)
     # Create a new child and associated target.
     child = self.create_child_node(target.p, line, h)
     if self.tail_lines:
         self.prepend_lines(child, self.tail_lines)
         self.tail_lines = []
     stack.append(Target(child, new_state))
     if trace:
         g.trace('=====', repr(line))
         g.printList(stack)
Exemple #43
0
 def create_latex_html(self, s):
     '''Create an html page embedding the latex code s.'''
     trace = False and not g.unitTesting
     c = self.c
     # pylint: disable=deprecated-method
     try:
         import html
         escape = html.escape
     except AttributeError:
         import cgi
         escape = cgi.escape
     html_s = escape(s)
     template = latex_template % (html_s)
     template = g.adjustTripleString(template, c.tab_width).strip()
     if trace:
         g.trace()
         g.printList(g.splitLines(template))
     return template
Exemple #44
0
 def start_new_block(self, i, lines, new_state, prev_state, stack):
     '''Create a child node and update the stack.'''
     trace = False and g.unitTesting
     if hasattr(new_state, 'in_context'):
         assert not new_state.in_context(), ('start_new_block', new_state)
     line = lines[i]
     target=stack[-1]
     # Insert the reference in *this* node.
     h = self.gen_ref(line, target.p, target)
     # Create a new child and associated target.
     child = self.create_child_node(target.p, line, h)
     if self.tail_lines:
         self.prepend_lines(child, self.tail_lines)
         self.tail_lines = []
     stack.append(Target(child, new_state))
     if trace:
         g.trace('=====', repr(line))
         g.printList(stack)
Exemple #45
0
 def gen_ref(self, line, parent, target):
     '''
     Generate the at-others and a flag telling this method whether a previous
     #@+others
     #@-others
     '''
     trace = False  # and g.unitTesting
     indent_ws = self.get_str_lws(line)
     h = self.clean_headline(line)
     if not target.at_others_flag:
         target.at_others_flag = True
         ref = '%s@others\n' % indent_ws
         if trace:
             g.trace('indent_ws: %r line: %r parent: %s' %
                     (indent_ws, line, parent.h))
             g.printList(self.get_lines(parent))
         self.add_line(parent, ref)
     return h
Exemple #46
0
def report_version():
    try:
        from pylint import lint
    except ImportError:
        g.trace('can not import pylint')
    table = (
        os.path.abspath(os.path.expanduser('~/.leo/pylint-leo-rc.txt')),
        os.path.abspath(os.path.join('leo', 'test', 'pylint-leo-rc.txt')),
    )
    for rc_fn in table:
        try:
            rc_fn = rc_fn.replace('\\', '/')
            lint.Run([
                "--rcfile=%s" % (rc_fn),
                '--version',
            ])
        except OSError:
            pass
    g.trace('no rc file found in')
    g.printList(table)
Exemple #47
0
 def make_contained_widgets(self):
     ### The *only* make_contained_widgets (plural) in npyscreen.
     trace = False
     trace_widgets = True
     self._my_widgets = []
     height = self.height // self.__class__._contained_widget_height
     if trace: g.trace(self.__class__.__name__, height)  #, g.callers(2))
     # Called from BoxTitle.make_contained_widget.
     for h in range(height):
         ### EKR: it's LeoMLTree._contained_widgets that we have to emulate.
         self._my_widgets.append(
             self._contained_widgets(
                 self.parent,
                 rely=(h * self._contained_widget_height) + self.rely,
                 relx=self.relx,
                 max_width=self.width,
                 max_height=self.__class__._contained_widget_height))
     if trace and trace_widgets:
         g.printList(self._my_widgets)
         g.printList(['value: %r' % (z.value) for z in self._my_widgets])
    def get_files(self):
        '''Return a list of changed files.'''
        
        trace = False and not g.unitTesting
            
        def readable(fn):
            for suffix in ('commit_timestamp.json', '.db', '.leo', '.zip', ):
                if fn.strip().endswith(suffix):
                    return False
            return True

        command = 'git diff --name-only %s %s' % (self.rev1 or '', self.rev2 or '')
        files = [
            z.strip() for z in g.execGitCommand(command, self.repo_dir)
                if readable(z)
        ]
        if trace:
            g.trace(command)
            g.printList(files)
        return files
Exemple #49
0
 def readFile(self, fileName, root):
     '''
     Read the file from the cache if possible.
     Return (s,ok,key)
     '''
     trace = (False or g.app.debug) and not g.unitTesting
     showHits = False
     showLines = False
     showList = False
     sfn = g.shortFileName(fileName)
     if not g.enableDB:
         if trace: g.trace('g.enableDB is False', fileName)
         return '', False, None
     if trace:
         g.trace('=====', root.v.gnx, 'children', root.numberOfChildren(),
                 fileName)
     s = g.readFileIntoEncodedString(fileName, silent=True)
     if s is None:
         if trace: g.trace('empty file contents', fileName)
         return s, False, None
     assert not g.isUnicode(s)
     if trace and showLines:
         for i, line in enumerate(g.splitLines(s)):
             print('%3d %s' % (i, repr(line)))
     # There will be a bug if s is not already an encoded string.
     key = self.fileKey(fileName, s, requireEncodedString=True)
     # Fix bug #385: use the full fileName, not root.h.
     ok = self.db and key in self.db
     if ok:
         if trace and showHits: g.trace('cache hit', key[-6:], sfn)
         # Delete the previous tree, regardless of the @<file> type.
         while root.hasChildren():
             root.firstChild().doDelete()
         # Recreate the file from the cache.
         aList = self.db.get(key)
         if trace and showList:
             g.printList(list(g.flatten_list(aList)))
         self.createOutlineFromCacheList(root.v, aList, fileName=fileName)
     elif trace:
         g.trace('cache miss', key[-6:], sfn)
     return s, ok, key
Exemple #50
0
 def make_contained_widgets(self):
     ### The *only* make_contained_widgets (plural) in npyscreen.
     trace = False
     trace_widgets = True
     self._my_widgets = []
     height = self.height // self.__class__._contained_widget_height
     if trace: g.trace(self.__class__.__name__, height) #, g.callers(2))
         # Called from BoxTitle.make_contained_widget.
     for h in range(height):
         ### EKR: it's LeoMLTree._contained_widgets that we have to emulate.
         self._my_widgets.append(
             self._contained_widgets(
                 self.parent,
                 rely=(h*self._contained_widget_height)+self.rely,
                 relx = self.relx,
                 max_width=self.width,
                 max_height=self.__class__._contained_widget_height
         ))
     if trace and trace_widgets:
         g.printList(self._my_widgets)
         g.printList(['value: %r' % (z.value) for z in self._my_widgets])
Exemple #51
0
 def remove_singleton_at_others(self, parent):
     '''Replace @others by the body of a singleton child node.'''
     trace = False
     found = False
     if trace:
         print('')
         g.trace(parent.h)
         print('')
     for p in parent.subtree():
         if p.numberOfChildren() == 1:
             child = p.firstChild()
             lines = self.get_lines(p)
             matches = [i for i,s in enumerate(lines) if self.at_others.match(s)]
             if len(matches) == 1:
                 found = True
                 i = matches[0]
                 if trace:
                     g.trace('===== @others, line', i)
                     g.printList(lines)
                     g.trace('.....')
                     g.printList(self.get_lines(child))
                 lines = lines[:i] + self.get_lines(child) + lines[i+1:]
                 if trace:
                     g.trace('----- result')
                     g.printList(lines)
                 self.set_lines(p, lines)
                 self.clear_lines(child) # Delete child later. Is this enough???
             elif len(matches) > 1:
                 if trace: g.trace('Ambiguous @others', p.h)
             else:
                 if trace: g.trace('No @others directive', p.h)
     return found
Exemple #52
0
 def remove_singleton_at_others(self, parent):
     '''Replace @others by the body of a singleton child node.'''
     trace = False
     found = False
     if trace:
         print('')
         g.trace(parent.h)
         print('')
     for p in parent.subtree():
         if p.numberOfChildren() == 1:
             child = p.firstChild()
             lines = self.get_lines(p)
             matches = [
                 i for i, s in enumerate(lines) if self.at_others.match(s)
             ]
             if len(matches) == 1:
                 found = True
                 i = matches[0]
                 if trace:
                     g.trace('===== @others, line', i)
                     g.printList(lines)
                     g.trace('.....')
                     g.printList(self.get_lines(child))
                 lines = lines[:i] + self.get_lines(child) + lines[i + 1:]
                 if trace:
                     g.trace('----- result')
                     g.printList(lines)
                 self.set_lines(p, lines)
                 self.clear_lines(
                     child)  # Delete child later. Is this enough???
             elif len(matches) > 1:
                 if trace: g.trace('Ambiguous @others', p.h)
             else:
                 if trace: g.trace('No @others directive', p.h)
     return found
Exemple #53
0
 def readFile(self, fileName, root):
     '''
     Read the file from the cache if possible.
     Return (s,ok,key)
     '''
     trace = (False or g.app.debug) and not g.unitTesting
     showHits = False
     showLines = False
     showList = False
     sfn = g.shortFileName(fileName)
     if not g.enableDB:
         if trace: g.trace('g.enableDB is False', fileName)
         return '', False, None
     if trace: g.trace('=====', root.v.gnx, 'children', root.numberOfChildren(), fileName)
     s = g.readFileIntoEncodedString(fileName, silent=True)
     if s is None:
         if trace: g.trace('empty file contents', fileName)
         return s, False, None
     assert not g.isUnicode(s)
     if trace and showLines:
         for i, line in enumerate(g.splitLines(s)):
             print('%3d %s' % (i, repr(line)))
     # There will be a bug if s is not already an encoded string.
     key = self.fileKey(fileName, s, requireEncodedString=True)
         # Fix bug #385: use the full fileName, not root.h.
     ok = self.db and key in self.db
     if ok:
         if trace and showHits: g.trace('cache hit', key[-6:], sfn)
         # Delete the previous tree, regardless of the @<file> type.
         while root.hasChildren():
             root.firstChild().doDelete()
         # Recreate the file from the cache.
         aList = self.db.get(key)
         if trace and showList:
             g.printList(list(g.flatten_list(aList)))
         self.createOutlineFromCacheList(root.v, aList, fileName=fileName)
     elif trace:
         g.trace('cache miss', key[-6:], sfn)
     return s, ok, key
Exemple #54
0
    def end_tag(self, s, tag, tag_level):
        '''
        Handle the ">" or "/>" that ends an element.

        Ignore ">" except for void tags.
        '''
        trace = False
        if trace:
            g.trace(tag, repr(s))
            g.printList(self.stack)
        if self.stack:
            if tag == '/>':
                top = self.stack.pop()
                if top in self.start_tags:
                    tag_level -= 1
            else:
                top = self.stack[-1]
                if top in self.void_tags:
                    self.stack.pop()
        elif tag == '/>':
            g.es_print("Warning: ignoring dubious /> in...")
            g.es_print(repr(s))
        return tag_level
Exemple #55
0
 def end_tag(self, s, tag, tag_level):
     '''Handle the end of a tag.'''
     trace = False
     stack = self.stack
     if not stack:
         g.trace('stack underflow: tag: %s in %r' % (tag, s))
         return tag_level
     data = stack[-1]
     tag1, tag2 = data
     if tag1 == '</' or tag == '/>':
         stack.pop()
         if tag2 in self.start_tags:
             if tag_level > 0:
                 tag_level -= 1
             elif trace:
                 g.trace('unexpected end tag: %s in %r' % (tag, s))
     else:
         # '>' just ends the opening element. No change to the stack.
         pass
     if trace:
         g.trace(tag)
         g.printList(stack)
     return tag_level
Exemple #56
0
 def promote_trailing_underindented_lines(self, parent):
     '''
     Promote all trailing underindent lines to the node's parent node,
     deleting one tab's worth of indentation. Typically, this will remove
     the underindent escape.
     '''
     trace = True
     pattern = self.escape_pattern # A compiled regex pattern
     for p in parent.subtree():
         lines = self.get_lines(p)
         tail = []
         while lines:
             line = lines[-1]
             m = pattern.match(line)
             if m:
                 lines.pop()
                 n_str = m.group(1)
                 try:
                     n = int(n_str)
                 except ValueError:
                     break
                 if n == abs(self.tab_width):
                     new_line = line[len(m.group(0)):]
                     tail.append(new_line)
                 else:
                     g.trace('unexpected unindent value', n)
                     break
             else:
                 break
         if tail:
             if trace:
                 g.trace(parent.h)
                 g.printList(reversed(tail))
             parent = p.parent()
             self.set_lines(p, lines)
             self.extend_lines(parent, reversed(tail))