Exemple #1
0
def unzip_file(zipfile_path: Path) -> Path:
    if "zip" not in magic.from_file(zipfile_path.as_posix(), mime=True):
        ep("apkg does not appear to be a ZIP file...")
        return -1
    with ZipFile(zipfile_path.as_posix(), 'r') as apkg:
        apkg.extractall(zipfile_path.stem)
    return Path(zipfile_path.stem)
def getImageShape(image_path):

    metaStr = magic.from_file(image_path)
    res = re.search(string=metaStr, pattern='height=(\d+).*width=(\d+)')
    if res:
        height, width = res.groups()
    return [int(height), int(width)]
Exemple #3
0
def unzip_file(zipfile_path: Path) -> Path:
	"""Attempts at unzipping the file, if the apkg is corrupt or is not appear to be zip, raises an Exception"""
	if "zip" not in magic.from_file(zipfile_path.as_posix(), mime=True):
		raise Exception("Error: apkg does not appear to be a ZIP file...")
	with ZipFile(zipfile_path.as_posix(), 'r') as apkg:
		apkg.extractall(zipfile_path.stem)
	return Path(zipfile_path.stem)
Exemple #4
0
def SuperMemoElement(card: Card) -> None:
    global doc, tag, text, get_id, IMAGES_TEMP, DEFAULT_SIDE, SIDES
    IMAGES_TEMP = ()

    QContent_Sounds = ()
    QContent_Videos = ()

    AContent_Sounds = ()
    AContent_Videos = ()

    if "[sound:" in str(card.q):
        g = re.search(r"(?:\[sound:)([^]]+)(?:\])", str(card.q))
        if g is not None:
            for p in g.groups():
                m = Path("{}/{}".format("out/out_files/elements", p))
                if m.exists():
                    if any([ext in m.suffix for ext in ["mp3", "ogg", "wav"]]) \
                      or "audio" in magic.from_file(m.as_posix(), mime=True):
                        QContent_Sounds = QContent_Sounds + (p, )
                    if any([ext in m.suffix for ext in ["mp4", "wmv", "mkv"]]) \
                      or "video" in magic.from_file(m.as_posix(), mime=True):
                        QContent_Videos = QContent_Videos + (p, )

    if "[sound:" in str(card.a):
        g = re.search(r"(?:\[sound:)([^]]+)(?:\])", str(card.a))
        if g is not None:
            for p in g.groups():
                m = Path("{}/{}".format("out/out_files/elements", p))
                if m.exists():
                    if any([ext in m.suffix for ext in ["mp3", "ogg", "wav"]]) \
                      or "audio" in magic.from_file(m.as_posix(), mime=True):
                        AContent_Sounds = AContent_Sounds + (p, )
                    if any([ext in m.suffix for ext in ["mp4", "wmv", "mkv"]]) \
                      or "video" in magic.from_file(m.as_posix(), mime=True):
                        AContent_Videos = AContent_Videos + (p, )

    card.q = Formatters.reSound.sub("", card.q)
    card.a = Formatters.reSound.sub("", card.a)

    enforceSectionJS = """<script>document.createElement("section");</script>"""
    liftIERestriction = """<meta http-equiv="X-UA-Compatible" content="IE=10">"""
    forcedCss = """<style>img{max-width:50%;}</style>"""
    with tag('SuperMemoElement'):
        with tag('ID'):
            text(get_id())
        with tag('Type'):
            text('Item')
        with tag('Content'):
            with tag('Question'):
                a = wrapHtmlIn(card.q, 'head', 'body')
                res = cleanHtml(a, imgcmp=IMAGES_AS_COMPONENT)
                if IMAGES_AS_COMPONENT:
                    IMAGES_TEMP = IMAGES_TEMP + res["imgs"]
                if MAINTAIN_STYLING:
                    a = insertHtmlAt(res["soup"], enforceSectionJS, 'head', 0)
                    a = insertHtmlAt(a, liftIERestriction, 'head', 0)
                else:
                    a = res["soup"]
                if not IMAGES_AS_COMPONENT and len(IMAGES_TEMP) != 0:
                    a = insertHtmlAt(a, forcedCss, 'head', 0)
                a = strip_control_characters(a)
                a = a.encode("ascii", "xmlcharrefreplace").decode("utf-8")
                text(a)

            for s in QContent_Videos:
                with tag('Video'):
                    with tag('URL'):
                        text(
                            os.path.expandvars(r'%LocalAppData%') +
                            "\\temp\\smmedia\\{}".format(s))
                    with tag('Name'):
                        text(s)
                    if DEFAULT_SIDE != SIDES[2] and \
                      DEFAULT_SIDE != SIDES[0]:
                        with tag("Question"):
                            text("F")
                        with tag("Answer"):
                            text("T")
                    else:
                        with tag("Question"):
                            text("T")
                        with tag("Answer"):
                            text("F")

            for s in QContent_Sounds:
                with tag('Sound'):
                    with tag('URL'):
                        text(
                            os.path.expandvars(r'%LocalAppData%') +
                            "\\temp\\smmedia\\{}".format(s))
                    with tag('Name'):
                        text(s)
                    with tag('Text'):
                        text("")
                    if DEFAULT_SIDE != SIDES[2] and \
                      DEFAULT_SIDE != SIDES[0]:
                        with tag("Question"):
                            text("F")
                        with tag("Answer"):
                            text("T")
                    else:
                        with tag("Question"):
                            text("T")
                        with tag("Answer"):
                            text("F")

            # html = Soup(a,'html.parser')
            # m=[p['href'] for p in html.find_all('a') ]
            # urls.append(m[0]) if len(m) else ""

            with tag('Answer'):
                res = cleanHtml(card.a, imgcmp=IMAGES_AS_COMPONENT)
                if IMAGES_AS_COMPONENT:
                    IMAGES_TEMP = IMAGES_TEMP + res["imgs"]
                if MAINTAIN_STYLING:
                    a = insertHtmlAt(res["soup"], enforceSectionJS, 'head', 0)
                    a = insertHtmlAt(a, liftIERestriction, 'head', 0)
                else:
                    a = res["soup"]
                if not IMAGES_AS_COMPONENT and len(IMAGES_TEMP) != 0:
                    a = insertHtmlAt(a, forcedCss, 'head', 0)
                a = strip_control_characters(a)
                a = a.encode("ascii", "xmlcharrefreplace").decode("utf-8")
                text(a)

            for s in AContent_Videos:
                with tag('Video'):
                    with tag('URL'):
                        text(
                            os.path.expandvars(r'%LocalAppData%') +
                            "\\temp\\smmedia\\{}".format(s))
                    with tag('Name'):
                        text(s)
                    if DEFAULT_SIDE != SIDES[2] and \
                      DEFAULT_SIDE != SIDES[1]:
                        with tag("Question"):
                            text("T")
                        with tag("Answer"):
                            text("F")
                    else:
                        with tag("Question"):
                            text("F")
                        with tag("Answer"):
                            text("T")

            for s in AContent_Sounds:
                with tag('Sound'):
                    with tag('URL'):
                        text(
                            os.path.expandvars(r'%LocalAppData%') +
                            "\\temp\\smmedia\\{}".format(s))
                    with tag('Name'):
                        text(s)
                    with tag('Text'):
                        text("")
                    if DEFAULT_SIDE != SIDES[2] and \
                      DEFAULT_SIDE != SIDES[1]:
                        with tag("Question"):
                            text("T")
                        with tag("Answer"):
                            text("F")
                    else:
                        with tag("Question"):
                            text("F")
                        with tag("Answer"):
                            text("T")

            for img in IMAGES_TEMP:
                with tag('Image'):
                    with tag('URL'):
                        text(
                            os.path.expandvars(r'%LocalAppData%') +
                            "\\temp\\smmedia\\{}".format(img))
                    with tag('Name'):
                        text(img)
                    if DEFAULT_SIDE == SIDES[1]:
                        with tag("Question"):
                            text("F")
                        with tag("Answer"):
                            text("T")
                    elif DEFAULT_SIDE == SIDES[0]:
                        with tag("Question"):
                            text("T")
                        with tag("Answer"):
                            text("F")

            if False and cardHasData(card):
                with tag("LearningData"):
                    with tag("Interval"):
                        text("1")
                    with tag("Repetitions"):
                        text("1")
                    with tag("Lapses"):
                        text("0")
                    with tag("LastRepetition"):
                        text(datetime.date("").strftime("%d.%m.%Y"))
                    with tag("AFactor"):
                        text("3.92")
                    with tag("UFactor"):
                        text("3")