1+
2+
13def parse_mysql_enum (enum_definition ):
24 """
35 Accepts a MySQL ENUM definition string (case–insensitive),
@@ -20,7 +22,7 @@ def parse_mysql_enum(enum_definition):
2022 raise ValueError ("String does not start with 'enum'" )
2123
2224 # Find the first opening parenthesis.
23- pos = s .find ("(" )
25+ pos = s .find ('(' )
2426 if pos == - 1 :
2527 raise ValueError ("Missing '(' in the enum definition" )
2628
@@ -46,22 +48,22 @@ def _extract_parenthesized_content(s, start_index):
4648 ', " or `) and also to skip over escape sequences in single/double quotes.
4749 (Backticks do not process backslash escapes.)
4850 """
49- if s [start_index ] != "(" :
51+ if s [start_index ] != '(' :
5052 raise ValueError ("Expected '(' at position {}" .format (start_index ))
5153 depth = 1
5254 i = start_index + 1
5355 content_start = i
5456 in_quote = None # will be set to a quoting character when inside a quoted literal
5557
5658 # Allow these quote characters.
57- allowed_quotes = ("'" , '"' , "`" )
59+ allowed_quotes = ("'" , '"' , '`' )
5860
5961 while i < len (s ):
6062 c = s [i ]
6163 if in_quote :
6264 # Inside a quoted literal.
6365 if in_quote in ("'" , '"' ):
64- if c == " \\ " :
66+ if c == ' \\ ' :
6567 # Skip the escape character and the next character.
6668 i += 2
6769 continue
@@ -85,16 +87,16 @@ def _extract_parenthesized_content(s, start_index):
8587 in_quote = c
8688 i += 1
8789 continue
88- elif c == "(" :
90+ elif c == '(' :
8991 depth += 1
9092 i += 1
9193 continue
92- elif c == ")" :
94+ elif c == ')' :
9395 depth -= 1
9496 i += 1
9597 if depth == 0 :
9698 # Return the substring inside (excluding the outer parentheses)
97- return s [content_start : i - 1 ], i
99+ return s [content_start : i - 1 ], i
98100 continue
99101 else :
100102 i += 1
@@ -114,7 +116,7 @@ def _parse_enum_values(content):
114116 """
115117 values = []
116118 i = 0
117- allowed_quotes = ("'" , '"' , "`" )
119+ allowed_quotes = ("'" , '"' , '`' )
118120 while i < len (content ):
119121 # Skip any whitespace.
120122 while i < len (content ) and content [i ].isspace ():
@@ -123,38 +125,34 @@ def _parse_enum_values(content):
123125 break
124126 # The next non–whitespace character must be one of the allowed quotes.
125127 if content [i ] not in allowed_quotes :
126- raise ValueError (
127- "Expected starting quote for enum value at position {} in {!r}" .format (
128- i , content
129- )
130- )
128+ raise ValueError ("Expected starting quote for enum value at position {} in {!r}" .format (i , content ))
131129 quote = content [i ]
132130 i += 1 # skip the opening quote
133131
134132 literal_chars = []
135133 while i < len (content ):
136134 c = content [i ]
137135 # For single- and double–quotes, process backslash escapes.
138- if quote in ("'" , '"' ) and c == " \\ " :
136+ if quote in ("'" , '"' ) and c == ' \\ ' :
139137 if i + 1 < len (content ):
140138 next_char = content [i + 1 ]
141139 # Mapping for common escapes. (For the quote character, map it to itself.)
142140 escapes = {
143- "0" : " \0 " ,
144- "b" : " \b " ,
145- "n" : " \n " ,
146- "r" : " \r " ,
147- "t" : " \t " ,
148- "Z" : " \x1a " ,
149- " \\ " : " \\ " ,
150- quote : quote ,
141+ '0' : ' \0 ' ,
142+ 'b' : ' \b ' ,
143+ 'n' : ' \n ' ,
144+ 'r' : ' \r ' ,
145+ 't' : ' \t ' ,
146+ 'Z' : ' \x1a ' ,
147+ ' \\ ' : ' \\ ' ,
148+ quote : quote
151149 }
152150 literal_chars .append (escapes .get (next_char , next_char ))
153151 i += 2
154152 continue
155153 else :
156154 # Trailing backslash – treat it as literal.
157- literal_chars .append (" \\ " )
155+ literal_chars .append (' \\ ' )
158156 i += 1
159157 continue
160158 elif c == quote :
@@ -171,27 +169,24 @@ def _parse_enum_values(content):
171169 literal_chars .append (c )
172170 i += 1
173171 # Finished reading one literal; join the characters.
174- value = "" .join (literal_chars )
172+ value = '' .join (literal_chars )
175173 values .append (value )
176174
177175 # Skip whitespace after the literal.
178176 while i < len (content ) and content [i ].isspace ():
179177 i += 1
180178 # If there’s a comma, skip it; otherwise, we must be at the end.
181179 if i < len (content ):
182- if content [i ] == "," :
180+ if content [i ] == ',' :
183181 i += 1
184182 else :
185- raise ValueError (
186- "Expected comma between enum values at position {} in {!r}" .format (
187- i , content
188- )
189- )
183+ raise ValueError ("Expected comma between enum values at position {} in {!r}"
184+ .format (i , content ))
190185 return values
191186
192187
193188# --- For testing purposes ---
194- if __name__ == " __main__" :
189+ if __name__ == ' __main__' :
195190 tests = [
196191 "enum('point','qwe','def')" ,
197192 "ENUM('asd', 'qwe', 'def')" ,
@@ -208,4 +203,4 @@ def _parse_enum_values(content):
208203 result = parse_mysql_enum (t )
209204 print ("Input: {}\n Parsed: {}\n " .format (t , result ))
210205 except Exception as e :
211- print ("Error parsing {}: {}\n " .format (t , e ))
206+ print ("Error parsing {}: {}\n " .format (t , e ))
0 commit comments