MCPcopy
hub / github.com/Textualize/rich / parse

Method parse

rich/style.py:498–561  ·  view source on GitHub ↗

Parse a style definition. Args: style_definition (str): A string containing a style. Raises: errors.StyleSyntaxError: If the style definition syntax is invalid. Returns: `Style`: A Style instance.

(cls, style_definition: str)

Source from the content-addressed store, hash-verified

496 @classmethod
497 @lru_cache(maxsize=4096)
498 def parse(cls, style_definition: str) -> "Style":
499 """Parse a style definition.
500
501 Args:
502 style_definition (str): A string containing a style.
503
504 Raises:
505 errors.StyleSyntaxError: If the style definition syntax is invalid.
506
507 Returns:
508 `Style`: A Style instance.
509 """
510 if style_definition.strip() == "none" or not style_definition:
511 return cls.null()
512
513 STYLE_ATTRIBUTES = cls.STYLE_ATTRIBUTES
514 color: Optional[str] = None
515 bgcolor: Optional[str] = None
516 attributes: Dict[str, Optional[Any]] = {}
517 link: Optional[str] = None
518
519 words = iter(style_definition.split())
520 for original_word in words:
521 word = original_word.lower()
522 if word == "on":
523 word = next(words, "")
524 if not word:
525 raise errors.StyleSyntaxError("color expected after 'on'")
526 try:
527 Color.parse(word)
528 except ColorParseError as error:
529 raise errors.StyleSyntaxError(
530 f"unable to parse {word!r} as background color; {error}"
531 ) from None
532 bgcolor = word
533
534 elif word == "not":
535 word = next(words, "")
536 attribute = STYLE_ATTRIBUTES.get(word)
537 if attribute is None:
538 raise errors.StyleSyntaxError(
539 f"expected style attribute after 'not', found {word!r}"
540 )
541 attributes[attribute] = False
542
543 elif word == "link":
544 word = next(words, "")
545 if not word:
546 raise errors.StyleSyntaxError("URL expected after 'link'")
547 link = word
548
549 elif word in STYLE_ATTRIBUTES:
550 attributes[STYLE_ATTRIBUTES[word]] = True
551
552 else:
553 try:
554 Color.parse(word)
555 except ColorParseError as error:

Callers 15

test_boolFunction · 0.45
test_parseFunction · 0.45
test_link_idFunction · 0.45
test_get_row_styleFunction · 0.45
test_theme_stackFunction · 0.45
test_text_with_styleFunction · 0.45
test_get_pulse_segmentsFunction · 0.45
test_decodeFunction · 0.45

Calls 4

StyleClass · 0.85
nullMethod · 0.80
splitMethod · 0.45
getMethod · 0.45

Tested by 15

test_boolFunction · 0.36
test_parseFunction · 0.36
test_link_idFunction · 0.36
test_get_row_styleFunction · 0.36
test_theme_stackFunction · 0.36
test_text_with_styleFunction · 0.36
test_get_pulse_segmentsFunction · 0.36
test_decodeFunction · 0.36