Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_primitive_token(value, multiline_strings_allowed=True):
"""
Creates and returns a single token for the given primitive atomic value.
Raises NotPrimitiveError when the given value is not a primitive atomic value
"""
if value is None:
return create_primitive_token("")
elif isinstance(value, bool):
return tokens.Token(tokens.TYPE_BOOLEAN, u"true" if value else u"false")
elif isinstance(value, int):
return tokens.Token(tokens.TYPE_INTEGER, u"{}".format(value))
elif isinstance(value, float):
return tokens.Token(tokens.TYPE_FLOAT, u"{}".format(value))
elif isinstance(value, (datetime.datetime, datetime.date, datetime.time)):
return tokens.Token(tokens.TYPE_DATE, value.isoformat())
elif isinstance(value, basestring):
return create_string_token(
value, multiline_strings_allowed=multiline_strings_allowed
)
raise NotPrimitiveError("{} of type {}".format(value, type(value)))
def create_whitespace(source_substring):
return Token(tokens.TYPE_WHITESPACE, source_substring)
"""
Creates a PunctuationElement instance containing an operator token of the specified type. The operator
should be a TOML source str.
"""
operator_type_map = {
",": tokens.TYPE_OP_COMMA,
"=": tokens.TYPE_OP_ASSIGNMENT,
"[": tokens.TYPE_OP_SQUARE_LEFT_BRACKET,
"]": tokens.TYPE_OP_SQUARE_RIGHT_BRACKET,
"[[": tokens.TYPE_OP_DOUBLE_SQUARE_LEFT_BRACKET,
"]]": tokens.TYPE_OP_DOUBLE_SQUARE_RIGHT_BRACKET,
"{": tokens.TYPE_OP_CURLY_LEFT_BRACKET,
"}": tokens.TYPE_OP_CURLY_RIGHT_BRACKET,
}
ts = (tokens.Token(operator_type_map[operator], operator),)
return PunctuationElement(ts)
def create_whitespace_element(length=1, char=" "):
"""
Creates and returns a WhitespaceElement containing spaces.
"""
ts = (tokens.Token(tokens.TYPE_WHITESPACE, char),) * length
return WhitespaceElement(ts)
text, bare_string_allowed=False, multiline_strings_allowed=True
):
"""
Creates and returns a single string token.
Raises ValueError on non-string input.
"""
if not isinstance(text, basestring):
raise ValueError("Given value must be a string")
if text == "":
return tokens.Token(
tokens.TYPE_STRING, '""'.format(_escape_single_line_quoted_string(text))
)
elif bare_string_allowed and _bare_string_regex.match(text):
return tokens.Token(tokens.TYPE_BARE_STRING, text)
elif multiline_strings_allowed and (
len(tuple(c for c in text if c == "\n")) >= 2 or len(text) > 80
):
# If containing two or more newlines or is longer than 80 characters we'll use the multiline string format
return _create_multiline_string_token(text)
else:
return tokens.Token(
tokens.TYPE_STRING, u'"{}"'.format(_escape_single_line_quoted_string(text))
)
if not isinstance(text, basestring):
raise ValueError("Given value must be a string")
if text == "":
return tokens.Token(
tokens.TYPE_STRING, '""'.format(_escape_single_line_quoted_string(text))
)
elif bare_string_allowed and _bare_string_regex.match(text):
return tokens.Token(tokens.TYPE_BARE_STRING, text)
elif multiline_strings_allowed and (
len(tuple(c for c in text if c == "\n")) >= 2 or len(text) > 80
):
# If containing two or more newlines or is longer than 80 characters we'll use the multiline string format
return _create_multiline_string_token(text)
else:
return tokens.Token(
tokens.TYPE_STRING, u'"{}"'.format(_escape_single_line_quoted_string(text))
)
def create_newline_element():
"""
Creates and returns a single NewlineElement.
"""
ts = (tokens.Token(tokens.TYPE_NEWLINE, "\n"),)
return NewlineElement(ts)
def _create_multiline_string_token(text):
escaped = text.replace(u'"""', u'"""')
if len(escaped) > 50:
return tokens.Token(
tokens.TYPE_MULTILINE_STRING,
u'"""\n{}\\\n"""'.format(_break_long_text(escaped)),
)
else:
return tokens.Token(tokens.TYPE_MULTILINE_STRING, u'"""{}"""'.format(escaped))