github.com/LanceLRQ/deer-common@v0.0.9-0.20210319081233-e8222ac018a8/provider/python.go (about)

     1  /* Python Compiler Provider
     2   * (C) 2019 LanceLRQ
     3   *
     4   * This code is licenced under the GPLv3.
     5   */
     6  package provider
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  type Py2CompileProvider struct {
    14  	CodeCompileProvider
    15  }
    16  
    17  type Py3CompileProvider struct {
    18  	CodeCompileProvider
    19  }
    20  
    21  func NewPy2CompileProvider() *Py2CompileProvider {
    22  	return &Py2CompileProvider{
    23  		CodeCompileProvider{
    24  			isReady:  false,
    25  			realTime: true,
    26  			Name:     "python2",
    27  		},
    28  	}
    29  }
    30  
    31  func (prov *Py2CompileProvider) Init(code string, workDir string) error {
    32  	prov.realTime = true
    33  	prov.codeContent = code
    34  	prov.workDir = workDir
    35  	prov.Name = "python2"
    36  
    37  	err := prov.checkWorkDir()
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	err = prov.initFiles(".py", "")
    43  	if err != nil {
    44  		return nil
    45  	}
    46  	prov.isReady = true
    47  	return nil
    48  }
    49  
    50  func (prov *Py2CompileProvider) Compile() (result bool, errmsg string) {
    51  	return true, ""
    52  }
    53  
    54  func (prov *Py2CompileProvider) GetRunArgs() (args []string) {
    55  	argsRaw := fmt.Sprintf(CompileCommands.Python2, prov.codeFilePath)
    56  	args = strings.Split(argsRaw, " ")
    57  	return
    58  }
    59  
    60  func (prov *Py2CompileProvider) IsCompileError(remsg string) bool {
    61  	return strings.Contains(remsg, "SyntaxError") ||
    62  		strings.Contains(remsg, "IndentationError") ||
    63  		strings.Contains(remsg, "ImportError")
    64  }
    65  
    66  func NewPy3CompileProvider() *Py3CompileProvider {
    67  	return &Py3CompileProvider{
    68  		CodeCompileProvider{
    69  			isReady:  false,
    70  			realTime: true,
    71  			Name:     "python3",
    72  		},
    73  	}
    74  }
    75  
    76  func (prov *Py3CompileProvider) Init(code string, workDir string) error {
    77  	prov.codeContent = code
    78  	prov.workDir = workDir
    79  
    80  	err := prov.checkWorkDir()
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	err = prov.initFiles(".py", "")
    86  	if err != nil {
    87  		return nil
    88  	}
    89  	prov.isReady = true
    90  	return nil
    91  }
    92  
    93  func (prov *Py3CompileProvider) Compile() (result bool, errmsg string) {
    94  	return true, ""
    95  }
    96  
    97  func (prov *Py3CompileProvider) GetRunArgs() (args []string) {
    98  	argsRaw := fmt.Sprintf(CompileCommands.Python3, prov.codeFilePath)
    99  	args = strings.Split(argsRaw, " ")
   100  	return
   101  }
   102  
   103  func (prov *Py3CompileProvider) IsCompileError(remsg string) bool {
   104  	return strings.Contains(remsg, "SyntaxError") ||
   105  		strings.Contains(remsg, "IndentationError") ||
   106  		strings.Contains(remsg, "ImportError")
   107  }