예제 #1
0
파일: logic.py 프로젝트: j1o1h1n/mu
 def save(self):
     """
     Save the content of the currently active editor tab.
     """
     tab = self._view.current_tab
     if tab is None:
         # There is no active text editor so abort.
         return
     if tab.path is None:
         # Unsaved file.
         tab.path = self._view.get_save_path(get_workspace_dir())
     if tab.path:
         # The user specified a path to a file.
         if not os.path.basename(tab.path).endswith('.py'):
             # No extension given, default to .py
             tab.path += '.py'
         try:
             with open_atomic(tab.path, 'w', newline='') as f:
                 logger.info('Saving script to: {}'.format(tab.path))
                 logger.debug(tab.text())
                 f.write(tab.text())
             tab.setModified(False)
         except OSError as e:
             logger.error(e)
             message = 'Could not save file.'
             information = ("Error saving file to disk. Ensure you have "
                            "permission to write the file and "
                            "sufficient disk space.")
             self._view.show_message(message, information)
     else:
         # The user cancelled the filename selection.
         tab.path = None
예제 #2
0
def check_pycodestyle(code):
    """
    Given some code, uses the PyCodeStyle module (was PEP8) to return a list
    of items describing issues of coding style. See:

    https://pycodestyle.readthedocs.io/en/latest/intro.html
    """
    # PyCodeStyle reads input from files, so make a temporary file containing
    # the code.
    code_fd, code_filename = tempfile.mkstemp()
    os.close(code_fd)
    with open_atomic(code_filename, 'w', newline='') as code_file:
        code_file.write(code)
    # Configure which PEP8 rules to ignore.
    style = StyleGuide(parse_argv=False, config_file=False)
    checker = Checker(code_filename, options=style.options)
    # Re-route stdout to a temporary buffer to be parsed below.
    temp_out = io.StringIO()
    sys.stdout = temp_out
    # Check the code.
    checker.check_all()
    # Put stdout back and read the content of the buffer. Remove the temporary
    # file created at the start.
    sys.stdout = sys.__stdout__
    temp_out.seek(0)
    results = temp_out.read()
    temp_out.close()
    code_file.close()
    os.remove(code_filename)
    # Parse the output from the tool into a dictionary of structured data.
    style_feedback = {}
    for result in results.split('\n'):
        matcher = STYLE_REGEX.match(result)
        if matcher:
            line_no, col, msg = matcher.groups()
            line_no = int(line_no) - 1
            code, description = msg.split(' ', 1)
            if code == 'E303':
                description += ' above this line'
            if line_no not in style_feedback:
                style_feedback[line_no] = []
            style_feedback[line_no].append({
                'line_no': line_no,
                'column': int(col) - 1,
                'message': description.capitalize(),
                'code': code,
            })
    return style_feedback
예제 #3
0
파일: logic.py 프로젝트: matt-land/mu
def check_pycodestyle(code):
    """
    Given some code, uses the PyCodeStyle module (was PEP8) to return a list
    of items describing issues of coding style. See:

    https://pycodestyle.readthedocs.io/en/latest/intro.html
    """
    # PyCodeStyle reads input from files, so make a temporary file containing
    # the code.
    code_fd, code_filename = tempfile.mkstemp()
    os.close(code_fd)
    with open_atomic(code_filename, 'w', newline='') as code_file:
        code_file.write(code)
    # Configure which PEP8 rules to ignore.
    style = StyleGuide(parse_argv=False, config_file=False)
    checker = Checker(code_filename, options=style.options)
    # Re-route stdout to a temporary buffer to be parsed below.
    temp_out = io.StringIO()
    sys.stdout = temp_out
    # Check the code.
    checker.check_all()
    # Put stdout back and read the content of the buffer. Remove the temporary
    # file created at the start.
    sys.stdout = sys.__stdout__
    temp_out.seek(0)
    results = temp_out.read()
    temp_out.close()
    code_file.close()
    os.remove(code_filename)
    # Parse the output from the tool into a dictionary of structured data.
    style_feedback = {}
    for result in results.split('\n'):
        matcher = STYLE_REGEX.match(result)
        if matcher:
            line_no, col, msg = matcher.groups()
            line_no = int(line_no) - 1
            code, description = msg.split(' ', 1)
            if code == 'E303':
                description += ' above this line'
            if line_no not in style_feedback:
                style_feedback[line_no] = []
            style_feedback[line_no].append({
                'line_no': line_no,
                'column': int(col) - 1,
                'message': description.capitalize(),
                'code': code,
            })
    return style_feedback
예제 #4
0
 def save(self):
     """
     Save the content of the currently active editor tab.
     """
     tab = self._view.current_tab
     if tab is None:
         # There is no active text editor so abort.
         return
     if tab.path is None:
         # Unsaved file.
         tab.path = self._view.get_save_path(get_workspace_dir())
     if tab.path:
         # The user specified a path to a file.
         if not os.path.basename(tab.path).endswith('.py'):
             # No extension given, default to .py
             tab.path += '.py'
         with open_atomic(tab.path, 'w', newline='') as f:
             logger.info('Saving script to: {}'.format(tab.path))
             logger.debug(tab.text())
             f.write(tab.text())
         tab.setModified(False)
     else:
         # The user cancelled the filename selection.
         tab.path = None
예제 #5
0
파일: logic.py 프로젝트: matt-land/mu
 def save(self):
     """
     Save the content of the currently active editor tab.
     """
     tab = self._view.current_tab
     if tab is None:
         # There is no active text editor so abort.
         return
     if tab.path is None:
         # Unsaved file.
         tab.path = self._view.get_save_path(get_workspace_dir())
     if tab.path:
         # The user specified a path to a file.
         if not os.path.basename(tab.path).endswith('.py'):
             # No extension given, default to .py
             tab.path += '.py'
         with open_atomic(tab.path, 'w', newline='') as f:
             logger.info('Saving script to: {}'.format(tab.path))
             logger.debug(tab.text())
             f.write(tab.text())
         tab.setModified(False)
     else:
         # The user cancelled the filename selection.
         tab.path = None