示例#1
0
文件: testfile.py 项目: atkoehler/gts
    def determine_indent_size(self, default, min, max):
        from system.utils import expand_all_tabs

        # TODO: try block shouldn't return False on exception
        try:
            source_file = open(self.file_loc)
            contents = source_file.read()
            source_file.close()
        except:
            return False

        def indent_amount(lines, i, prev_indent_amt):
            "Determine the amount a line is indented"

            if i < len(lines) and len(lines[i].strip()) > 0:
                return lines[i].find(lines[i].strip()[0]) - prev_indent_amt
            else:
                return -prev_indent_amt

        def next_nonblank(lines, i):
            "Find index of first non blank line starting at i"

            while i < len(lines) and len(lines[i].strip()) == 0:
                i = i + 1
            return i

        self.indent_size = 0
        lines = contents.split("\n")
        expand_all_tabs(lines, default)
        for (i, line) in enumerate(lines):
            if line.find("{") != -1:
                if line.lstrip().find("{") == 0:
                    first_index = line.find("{")
                else:
                    first_index = line.find(line.lstrip()[0])

                if line.find("}") == -1:
                    non_blank = next_nonblank(lines, i + 1)
                    spacer = indent_amount(lines, non_blank, first_index)
                    if spacer >= min and spacer <= max:
                        self.indent_size = spacer
                        break

        if self.indent_size < min or self.indent_size > max:
            self.indent_size = default
示例#2
0
def long_check(test, source, max_len, deduction):
    from system.utils import expand_all_tabs
    
    line_nums = []
    
    # TODO implement try block
    file = open(source.file_loc)
    contents = file.read()
    file.close()

    lines = contents.split("\n")
    expand_all_tabs(lines, source.indent_size)
    for (i, line) in enumerate(lines):
        if len(line.rstrip()) > max_len:
            line_nums.append(i+1)

    test.score = -1 * deduction * len(line_nums)
    return line_nums
示例#3
0
def create_blocks(source):
    """
    Create a list of levels, each level contains all lines in file for that
    level indentation block. 
    
    """
    from system.utils import expand_all_tabs
     
    # Indentation is dependent on other the absence of other style problems
    if source.style_halt:
        return (False, [])
    
    indeces = []
    blocks = []
    
    # TODO implement try block
    f = open(source.file_loc)
    contents = f.read()
    f.close()
   
    # global level 
    level = 0
    begin = 1
    blocks.append(Block(level, begin))
    indeces.append(0)
    
     
    # loop over all the lines in the file splitting up levels
    file_lines = contents.split("\n")
    expand_all_tabs(file_lines, source.indent_size)
    for (i, line) in enumerate(file_lines):
        # determine if end of level exists on line
        if line.find("}") != -1:
            # make sure not to pop global off in case of mismatch braces
            if len(indeces) > 1:
                # only pop if belong within indent level one up
                if line.lstrip().find("}") == 0:
                    indeces.pop()
                    popped = True

        # add line to current level
        cur = indeces[-1]
        blocks[cur].lines.append(SourceLine(i+1, line))

        # determine if new level needs to be created
        if line.find("{") != -1:
            # create a new object for discovered level
            level = blocks[cur].level + 1
            begin = i+1 
            
            # update lists of tuples and indeces
            indeces.append(len(blocks))
            blocks.append(Block(level, begin))
        
        # determine if end of level exists on line and haven't popped
        if line.find("}") != -1 and not popped:
            # make sure not to pop global in case of mismatch braces
            if len(indeces) > 1:
                indeces.pop()                  
        
        popped = False
    
    return (len(blocks) != 0, blocks)