Skip to content

Commit

Permalink
chore(formatting): ran black
Browse files Browse the repository at this point in the history
  • Loading branch information
Mochi committed Jan 21, 2024
1 parent febd999 commit e873317
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 34 deletions.
9 changes: 7 additions & 2 deletions src/markdown2anki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ def main():
)

# Check for updates
ver.check_for_updates(markdown2anki.__name__, "https://github.com/Mochitto/Markdown2Anki/blob/master/CHANGELOG.md")
ver.check_for_updates(
markdown2anki.__name__,
"https://github.com/Mochitto/Markdown2Anki/blob/master/CHANGELOG.md",
)
expressive_debug(logger, "Processed config from main", config, "json")

logger.info("⏳ Starting cards extraction")

markdown_handle = MarkdownHandler(config["input md file path"])
expressive_debug(logger, "Markdown input file frontmatter", markdown_handle.metadata, "json")
expressive_debug(
logger, "Markdown input file frontmatter", markdown_handle.metadata, "json"
)

try:
cards_with_info = markdown_to_anki(
Expand Down
2 changes: 1 addition & 1 deletion src/markdown2anki/config/config_setup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def setup_typeConfig(base_path: Types.PathString, type_hints=False) -> TypeConfi
help="The name of the obsidian vault where the input file is from.",
important_help="If left empty, Obsidian links will not work in your cards.",
default="",
can_be_empty=True
can_be_empty=True,
)
fileConfig.add_option(
type="ExistingPath",
Expand Down
2 changes: 1 addition & 1 deletion src/markdown2anki/config/first_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def create_anki_package(path_to_config_directory: Path) -> None:
source = Path(__file__).parent / filename
destination = path_to_config_directory / filename
shutil.copy(str(source), str(destination))


def get_welcome_message(path_to_welcome_file: Types.PathString) -> str:
"""
Expand Down
3 changes: 2 additions & 1 deletion src/markdown2anki/config/parse_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# TODO: change me to the real docs
DOCS_URL = "www.google.com"


# Custom formatter used to preserve newlines in help messages
# https://stackoverflow.com/a/22157136/19144535
class SmartFormatter(argparse.HelpFormatter):
Expand Down Expand Up @@ -50,7 +51,7 @@ def _split_lines(self, text, width):
help="R|If present, re-builds the anki apkg file that you can import to your Anki to get the Markdown2Anki note types.\nExample: --apkg",
action="store_true",
default=None,
)
)

CommandLineArgsParser.add_argument(
"-lc",
Expand Down
4 changes: 3 additions & 1 deletion src/markdown2anki/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from markdown2anki.utils import common_types as Types
from markdown2anki.utils.debug_tools import expressive_debug


# Create a logger object
class SingleLevelFilter(logging.Filter):
def __init__(self, passlevel, reject):
Expand All @@ -15,6 +16,7 @@ def filter(self, record):
return record.levelno != self.passlevel
return record.levelno == self.passlevel


def setup_logging(console_debug=False) -> None:
"""
Take a logging level from the logging module,
Expand All @@ -31,7 +33,7 @@ def setup_logging(console_debug=False) -> None:
stderr_handler.addFilter(SingleLevelFilter(logging.DEBUG, True))
stderr_handler.setFormatter(logging.Formatter("%(message)s"))
stderr_handler.setLevel(logging.INFO)

debug_handler = logging.StreamHandler()
debug_handler.addFilter(SingleLevelFilter(logging.DEBUG, False))
debug_handler.setFormatter(
Expand Down
7 changes: 2 additions & 5 deletions src/markdown2anki/markdown_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from markdown2anki.utils import common_types as Types


class MarkdownHandler:
def __init__(self, path: Types.PathString) -> None:
self.filepath = path
Expand All @@ -11,9 +12,5 @@ def __init__(self, path: Types.PathString) -> None:

def get_frontmatter_text(self):
yaml_text = frontmatter.YAMLHandler().export(self.metadata)
frontmatter_text = (
"---\n" +
yaml_text +
"\n---\n\n"
)
frontmatter_text = "---\n" + yaml_text + "\n---\n\n"
return frontmatter_text
8 changes: 2 additions & 6 deletions src/markdown2anki/md_2_anki/process_card/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ def process_card(

formatted_card = {"front": "", "back": ""}
for card_side in swapped_card.keys():
formatted_card[card_side] += format_tab_group(
swapped_card[card_side]["left"]
)
formatted_card[card_side] += format_tab_group(
swapped_card[card_side]["right"]
)
formatted_card[card_side] += format_tab_group(swapped_card[card_side]["left"])
formatted_card[card_side] += format_tab_group(swapped_card[card_side]["right"])

expressive_debug(logger, "Formatted card", formatted_card)
return formatted_card
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ObsidianImagePlugin:
"""

def __init__(self) -> None:

self.OBSIDIAN_IMAGE_REGEX = (
# ![[Something]]
# ![[Something|300]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ def format_tab(tab: CardTypes.HTMLTab) -> CardTypes.FormattedTab:
return tab_copy


def format_tab_group(
tabs_list: List[Types.HTMLString]
) -> Types.HTMLString:
def format_tab_group(tabs_list: List[Types.HTMLString]) -> Types.HTMLString:
# Filter out empty tabs and copy the list
tabs = [tab for tab in tabs_list if tab]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def wrap_tab_group(
tab_content: Types.HTMLString,
tab_content: Types.HTMLString,
) -> Types.HTMLString:
return f'<section class="tab_group">{tab_content}</section>'

Expand Down
15 changes: 12 additions & 3 deletions src/markdown2anki/version_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@

logger = logging.getLogger(__name__)


def check_for_version(current_version: str, latest_version: str) -> bool:
return current_version == latest_version if latest_version else True


def get_current_version(package_name: str) -> str:
return version(package_name)

def get_latest_version(package_name:str) -> str:

def get_latest_version(package_name: str) -> str:
latest_version = ""
try:
response = requests.get(f"https://pypi.org/pypi/{package_name}/json", timeout=2)
response.raise_for_status()
package_data = response.json()
latest_version = package_data['info']['version']
latest_version = package_data["info"]["version"]
except requests.exceptions.RequestException:
pass
return latest_version


def check_for_updates(package_name: str, changelog_url: str):
current_version = get_current_version(package_name)
logger.info(f"Running {package_name} v{current_version} 🌸\n")
Expand All @@ -36,7 +40,12 @@ def check_for_updates(package_name: str, changelog_url: str):
else:
logger.info("✨ Running the latest version!\n")


if __name__ == "__main__":
import type_config

logging.basicConfig(level=logging.INFO)
check_for_updates(type_config.__name__, "https://github.com/Mochitto/type-config/blob/master/CHANGELOG.md")
check_for_updates(
type_config.__name__,
"https://github.com/Mochitto/type-config/blob/master/CHANGELOG.md",
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ def test_tab_without_body_edge_case(self):
process_card(md_input, "my vault")

def test_missing_vault_edge_case(self):
md_input = "## [A tab]\n" "With a text\n" "## [A tab]\n" "Something else [[With a link]]"
md_input = (
"## [A tab]\n"
"With a text\n"
"## [A tab]\n"
"Something else [[With a link]]"
)

with pytest.raises(CardError):
process_card(md_input, "")
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def test_basic_case(self):
assert processed_card["front"] == content

def test_single_letter_case(self):
content = (
"This is a cloze with a single letter {{c1::a}}"
)
content = "This is a cloze with a single letter {{c1::a}}"

assert clozes.are_clozes_in_card(content)
cloze_handler = clozes.HandleClozes(content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ def test_basic_case(self):
)

hashed_clozes = {
"HIIDAJIGJADFBDBJJDF": "{{c1::clozes}}",
"ZEJDDDHDEBDEICGIHBGF": "{{c2::cloze}}",
"HIIDAJIGJADFBDBJJDF": "{{c1::clozes}}",
"ZEJDDDHDEBDEICGIHBGF": "{{c2::cloze}}",
}
clozes_handler = clozes.HandleClozes(content)
hashed_content = clozes_handler._replace_clozes_with_hashes(clozes_handler.card, hashed_clozes)
hashed_content = clozes_handler._replace_clozes_with_hashes(
clozes_handler.card, hashed_clozes
)

assert hashed_content == expected_hashed_content

Expand All @@ -36,14 +38,18 @@ def test_variation_case(self):

hashed_clozes = {"ZIAGEIHFICDIEFCECJF": "{{C394::cloZe}}"}
clozes_handler = clozes.HandleClozes(content)
hashed_content = clozes_handler._replace_clozes_with_hashes(clozes_handler.card, hashed_clozes)
hashed_content = clozes_handler._replace_clozes_with_hashes(
clozes_handler.card, hashed_clozes
)

assert hashed_content == expected_hashed_content

def test_false_case(self):
content = "This is a string with 2 {{c1:clozeS}}.\n" + "Another line."
hashed_clozes = {}
clozes_handler = clozes.HandleClozes(content)
hashed_content = clozes_handler._replace_clozes_with_hashes(clozes_handler.card, hashed_clozes)
hashed_content = clozes_handler._replace_clozes_with_hashes(
clozes_handler.card, hashed_clozes
)

assert hashed_content == content

0 comments on commit e873317

Please sign in to comment.