11import time
22from dataclasses import asdict , dataclass
33from datetime import datetime
4+ from enum import Enum
45from typing import Any , Dict , List , Optional
56
67from shodo .api import lint_create , lint_result
@@ -64,46 +65,18 @@ class LintFailed(Exception):
6465 pass
6566
6667
67- class Lint :
68- STATUS_PROCESSING = "processing"
69- STATUS_FAILED = "failed"
68+ class LintStatus (Enum ):
69+ PROCESSING = "processing"
70+ FAILED = "failed"
71+ DONE = "done"
7072
71- def __init__ (self , body , lint_id , profile ):
72- self .body = body
73- self .lint_id = lint_id
74- self .body = None
75- self .status = self .STATUS_PROCESSING
76- self .messages = []
77- self .profile = profile
7873
79- def results (self ):
80- while self .status == self .STATUS_PROCESSING :
81- time .sleep (0.5 )
82- res = lint_result (self .lint_id , self .profile )
83- self .status = res .status
84- msgs = [Message .load (m ) for m in res .messages ]
85- self .messages = sorted (msgs , key = lambda m : (m .from_ .line , m .from_ .ch ))
86-
87- if self .status == self .STATUS_FAILED :
88- raise LintFailed
89-
90- return self .messages
91-
92- def __repr__ (self ):
93- return f"Lint({ self .lint_id } )"
94-
95- @classmethod
96- def start (cls , body : str , is_html : bool = False , profile : Optional [str ] = None ):
97- res = lint_create (body , is_html , profile )
98- return cls (body , res .lint_id , profile )
99-
100-
101- def lint (body : str , is_html : bool = False , profile : Optional [str ] = None ) -> LintResult :
74+ def lint (body : str , is_html : bool = False , profile : Optional [str ] = None , _initial_pause : float = 0.25 ) -> LintResult :
10275 create_res = lint_create (body , is_html , profile )
10376
104- status = Lint . STATUS_PROCESSING
105- pause = 0.25
106- while status == Lint . STATUS_PROCESSING :
77+ status = LintStatus . PROCESSING . value
78+ pause = _initial_pause
79+ while status == LintStatus . PROCESSING . value :
10780 time .sleep (pause )
10881 result_res = lint_result (create_res .lint_id , profile )
10982 status = result_res .status
@@ -113,7 +86,7 @@ def lint(body: str, is_html: bool = False, profile: Optional[str] = None) -> Lin
11386 if pause < 16 :
11487 pause *= 2
11588
116- if status == Lint . STATUS_FAILED :
89+ if status == LintStatus . FAILED . value :
11790 raise LintFailed
11891
11992 return LintResult (status = status , messages = messages , updated = result_res .updated )
0 commit comments