Add ANSI helper, change Twitter model(s)

This commit is contained in:
Zeva Rose 2022-02-21 02:42:26 -07:00
parent 20f6744457
commit f392757bb7
4 changed files with 83 additions and 1 deletions

View file

@ -11,6 +11,7 @@ extend-ignore =
D401, # First line should be in imperative mood; try rephrasing
D400, # First line should end with a period
D101, # Missing docstring in public class
D106, # Missing docstring in public nested class
# Plugins we don't currently include: flake8-return
R503, # missing explicit return at the end of function ableto return non-None value.

View file

@ -197,6 +197,9 @@ class TwitterAccount(Document):
last_tweet = fields.IntegerField(required=True)
last_sync = fields.DateTimeField(default=datetime.utcnow)
class Meta:
collection_name = "twitteraccount"
@JARVIS_INST.register
class TwitterFollow(Document):
@ -210,6 +213,9 @@ class TwitterFollow(Document):
admin = fields.IntegerField(required=True)
created_at = fields.DateTimeField(default=datetime.utcnow)
class Meta:
collection_name = "twitterfollow"
@JARVIS_INST.register
class Unban(Document):

75
jarvis_core/util/ansi.py Normal file
View file

@ -0,0 +1,75 @@
"""ANSI color helper."""
from enum import Enum, EnumMeta
from typing import Any, List
ESCAPE = "\u001b"
RESET = ESCAPE + "[0m"
class SearchableEnum(EnumMeta):
def __contains__(cls, item: Any):
return item in [v.value for v in cls.__members__.values()]
class Format(Enum, metaclass=SearchableEnum):
NORMAL = 0
BOLD = 1
UNDERLINE = 4
class Fore(Enum, metaclass=SearchableEnum):
GRAY = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
PINK = 35
CYAN = 36
WHITE = 37
class Back(Enum, metaclass=SearchableEnum):
DARK_BLUE = 40
ORANGE = 41
DARK_GRAY = 42
GRAY = 43
LIGHT_GRAY = 44
INDIGO = 45
LIGHTER_GRAY = 46
WHITE = 47
def fmt(*formats: List[Format | Fore | Back] | int) -> str:
"""
Get format string.
Args:
formats: List of format modifiers
Return:
Format string
"""
fore = ""
back = ""
fmt = ""
block_fmt = False
for f in formats:
if isinstance(f, Enum):
f = f.value
if f == 0:
fmt = "0;"
elif f in [1, 4]:
if str(f) not in fmt and not block_fmt:
fmt += f"{f};"
elif 30 <= f <= 37:
fore = f"{f};"
elif 40 <= f <= 47:
back = f"{f};"
ret = fmt + fore + back
if not any(ret, fore, back):
ret = RESET
if ret[-1] == ";":
ret = ret[:-1]
return ESCAPE + "[" + ret + "m"

View file

@ -1,6 +1,6 @@
[tool.poetry]
name = "jarvis-core"
version = "0.4.1"
version = "0.5.0"
description = ""
authors = ["Your Name <you@example.com>"]