Beispiel #1
0
def crt_apnm_img(_ap: str, _nm_note: str, _inx: str) -> object:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile()
    if not bool(pth.get_ext(_ap)): raise exc.ExceptionNotExistsFileExtension()
    if not pth.get_ext(_ap) in var.img_ext:
        raise exc.ExceptionNotExistsImageFile()

    nm_fi = pth.get_ap_innermst(_ap)

    em = crt_apnm_embed(_ap, _nm_note, _inx)
    at = ""
    if get_s_lst(nm_fi).isnumeric():
        at = "{}-{}-{}.{}".format(_nm_note, _inx, var.bak, pth.get_ext(nm_fi))
    else:
        at = "{}-{}-{}-{}.{}".format(
            _nm_note, _inx,
            pth.rm_ext(dttz.rm_prefix(nm_fi), pth.get_ext(nm_fi)), var.bak,
            pth.get_ext(nm_fi))

    return op.struct(apa=pth.jo(pth.get_ap_1(_ap), at),
                     ape=em.ap,
                     nma=at,
                     nme=em.nm,
                     inx=_inx)
Beispiel #2
0
def crt_img_dbg(_ap: str) -> None:
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if difi.chk_exst_fi(_ap): raise exc.ExceptionExistsFile()
    if not pth.get_ext(_ap) in var.img_ext: raise ExceptionNotExistsImageFile()

    com = "convert -size 32x32 xc:black {}".format(_ap)
    subprocess.call(com, shell=True)
Beispiel #3
0
def frmt_mkdocs(_ap: str, _50mb: bool = True) -> bool:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()

    lst = wlk_get_note(_ap)
    """ This program need to do this from the last index. Because, if the
    upper directories changed first, the latter directories that reference
    that upper directory will not be found in the next loop.
    """
    while len(lst) > 0:
        lst_el = lst.pop()  # The list element contains an absolute path
        # to note folder.

        if _50mb:
            if get_size(lst_el) >= 50000000:
                de(lst_el)  # Delete the folder.
                continue  # Continue to next note folder.

        lst_el_ap1 = pth.get_ap_1(lst_el)
        content = get_lst(lst_el)

        for i in content:  # Move back all files into one directory up.
            ap_i = pth.jo(lst_el, i)  # File's original location.
            ap_itrg = pth.jo(lst_el_ap1, i)  # File's target copy location.
            mov(ap_i, ap_itrg)

        de(lst_el)

    return True
Beispiel #4
0
def mov(
    _ap: str,  # Source path or file directory.
    _ap_trg: str,  # Target path or file directory.
    _rep:
    bool = True  # Replace (not merge) existing duplicate at target directory.
) -> bool:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap) or not pth.chk_ap(_ap_trg):
        raise exc.ExceptionNotAbsolutePath()
    if not chk_exst(_ap): raise exc.ExceptionNotExistsDirectoryOrFile()
    if _ap == _ap_trg: raise exc.ExceptionSamePath()
    if _rep and chk_exst(_ap_trg): de(_ap_trg)
    elif not _rep and chk_exst(_ap_trg): return False
    """ Move! """
    shutil.move(_ap, _ap_trg)

    return True
Beispiel #5
0
def get_img_dim(_ap:str) -> tuple:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile()
    if not pth.get_ext(_ap) in var.img_ext: raise exc.ExceptionNotExistsImageFile()

    with Image.open(_ap) as img: return img.size
    return ()
Beispiel #6
0
def chk_md_b(_ap: str):
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile()
    if not pth.get_ext(_ap) == "md": raise exc.ExceptionNotExistsMDFile()
    """ The function of `rd_md()` is used to read all lines in `_ap`. If it
    returns `0` it means the .md file in `_ap` has no line written in it.
    """
    return True if len(rd_md(_ap)) == 0 else False
Beispiel #7
0
def chk_exst_dnd(_ap: str) -> bool:
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()

    l = get_lst(_ap)
    for i in l:
        j = pth.jo(_ap, i)
        if chk_exst_di(j): return True

    return False
Beispiel #8
0
def ren(_ap: str, _nm: str) -> bool:
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not chk_exst(_ap): raise exc.ExceptionNotExistsDirectoryOrFile()
    ap_1 = pth.get_ap_1(_ap)
    ap_trg = pth.jo(ap_1, _nm)
    #if _ap == ap_trg: raise exc.ExceptionSamePath() # PENDING: Perhaps put warning instead as this line.

    if _ap == ap_trg: return False
    shutil.move(_ap, ap_trg)

    return True
Beispiel #9
0
def get_md(_ap: str) -> str:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()
    if not chk_exst_md(_ap): raise exc.ExceptionNotExistsMDFile()
    if chk_exst_md(_ap, True): raise exc.ExceptionExistMultipleMDFiles()

    for i in difi.get_lst(_ap):
        if pth.get_ext(i) == "md": return pth.jo(_ap, i)

    return ""
Beispiel #10
0
def rd_md(_ap: str) -> list:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile()
    if not pth.get_ext(_ap) == "md": raise exc.ExceptionNotExistsMDFile()

    md = open(_ap, "r")
    l = md.readlines()
    md.close()

    return l
Beispiel #11
0
def get_lst_n_md(_ap: str) -> list:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()
    if chk_exst_md(_ap, True): raise exc.ExceptionExistMultipleMDFiles()
    if difi.chk_exst_dnd(_ap): raise exc.ExceptionExistsDirectoryInDirectory()

    l = difi.get_lst(_ap)
    for i in l:
        if pth.get_ext(i) == "md": l.remove(i)

    return l
Beispiel #12
0
def get_size(_ap: str) -> int:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()

    tot = 0
    for i, j, k in os.walk(_ap):
        for l in k:
            apl = pth.jo(i, l)
            tot = tot + os.path.getsize(apl)

    return tot
Beispiel #13
0
def de(_ap: str) -> bool:
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not chk_exst(_ap): return False
    """ Delete! """
    if chk_exst_di(_ap):
        shutil.rmtree(_ap)
        return True
    elif chk_exst_fi(_ap):
        os.remove(_ap)
        return True

    return False
Beispiel #14
0
def cpy(
    _ap: str,  # Source path or file directory.
    _ap_trg: str,  # Target path or file directory.
    _rep:
    bool = True  # Replace (not merge) existing duplicate at target directory.
) -> bool:
    if not pth.chk_ap(_ap) or not pth.chk_ap(_ap_trg):
        raise exc.ExceptionNotAbsolutePath()
    if not chk_exst(_ap): raise exc.ExceptionNotExistsDirectoryOrFile()
    if _ap == _ap_trg: raise exc.ExceptionSamePath()
    if _rep and chk_exst(_ap_trg): de(_ap_trg)
    elif not _rep and chk_exst(_ap_trg): return False
    """ Copy! """
    if chk_exst_di(_ap):
        shutil.copytree(_ap, _ap_trg)
        return True
    elif chk_exst_fi(_ap):
        shutil.copyfile(_ap, _ap_trg)
        return True

    return False
Beispiel #15
0
def wlk_get_note(_ap: str) -> list:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()

    lst = os.walk(_ap)
    lst_ret = []

    for i, j, k in lst:
        if ".git" in j: j.remove(".git")  # Exclude .git directory.
        if not bool(j): lst_ret.append(i)

    return lst_ret
Beispiel #16
0
def crt_apnm_attach(_ap: str, _nm_note: str, _inx: str) -> object:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile()
    if not bool(pth.get_ext(_ap)): raise exc.ExceptionNotExistsFileExtension()

    nm = "{}-{}-{}".format(_nm_note, _inx,
                           dttz.rm_prefix(pth.get_ap_innermst(_ap)))
    inx = _inx + 1
    """ `pth.get_ap_1(_ap)` because the `_ap` refer to the file. The absolute path used
    \in `return` should refer to the note directory.
    """
    return op.struct(ap=pth.jo(pth.get_ap_1(_ap), nm), nm=nm, inx=inx)
Beispiel #17
0
def get_img_dim_h(_ap:str) -> int:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile()
    if not pth.get_ext(_ap) in var.img_ext: raise exc.ExceptionNotExistsImageFile()

    try:
        with Image.open(_ap) as img: return img.size[1]
    except ValueError:
        try:
            comm = subprocess.check_output(["convert \"{}\" -ping -format \"%h\" info:".format(_ap)], shell=True)
            return int(str(comm).split("'")[1])
        except subprocess.CalledProcessError: pass
    return 0
Beispiel #18
0
def chk_exst_md(_ap: str, _m: bool = False) -> bool:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()

    m_cntr = 0  # Counter to check for multiple .md files in `_ap`.

    for i in difi.get_lst(_ap):
        if pth.get_ext(i) == "md":
            if _m: m_cntr = m_cntr + 1
            else: return True
            if _m and m_cntr > 1: return True

    return False
Beispiel #19
0
def aw_md_(_ap: str, _ls: list, _m: str) -> bool:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile()
    if not pth.get_ext(_ap) == "md": raise exc.ExceptionNotExistsMDFile()
    if not all(isinstance(i, str) for i in _ls):
        raise exc.ExceptionListNotAllString()
    if not _m in var.opn_mode: raise exc.ExceptionNotExistsOpenMode()

    md = open(_ap, _m)
    for i in _ls:
        print(i, end="", file=md)
    md.close()

    return True
Beispiel #20
0
def crt(
    _ap: str,
    _is_di: bool  # Set `True` to create directory. Set `False` to create file.
) -> bool:
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if (_is_di and chk_exst_di(_ap)) or (not _is_di and chk_exst_fi(_ap)):
        return False
    """ Create directory or create file. """
    if _is_di:
        os.makedirs(_ap)
        return True
    else:
        open(_ap, "w").close()
        return True

    return False
Beispiel #21
0
def crt_apnm_img_cnvrt(_ap:str) -> object:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile()
    if not pth.get_ext(_ap) in var.img_ext: raise exc.ExceptionNotExistsImageFile()

    """ Original image file. """
    org_ap1 = pth.get_ap_1(_ap)
    org = pth.get_ap_innermst(_ap)
    orge = pth.get_ext(org)
    orgne = pth.rm_ext(org, orge)

    """ Backup image file. """
    bnm = "{}_{}.{}".format(orgne, var.bak, orge)
    bnm_ap = pth.jo(org_ap1, bnm)

    """ Converted image file. """
    cnvrt = "{}.{}".format(orgne, "png")
    cnvrt_ap = pth.jo(org_ap1, cnvrt)

    return op.struct(ap_bak=bnm_ap, ap_cn=cnvrt_ap, nm_bak=bnm, nm_cn=cnvrt)
Beispiel #22
0
def ren_recr(_ap: str, _s: str, _s_new: str) -> bool:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()

    lst = wlk_get_note(_ap)
    """ Need to do this from last index. Because if the upper directories changed
    first it won't be found in the next loops.
    """
    while len(lst) > 0:
        lst_el = lst.pop()
        lst_el_innermst = pth.get_ap_innermst(lst_el)
        lst_el_innermst_new = lst_el_innermst.replace(_s, _s_new)

        if lst_el_innermst_new[-1:] == var.note_sp:
            lst_el_innermst_new = lst_el_innermst_new[:-1]
        lst_el_innermst_new = lst_el_innermst_new.replace("--", "-")

        ren(lst_el, lst_el_innermst_new)

    return True
Beispiel #23
0
def crt_apnm_note(_ap: str) -> object:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()
    if difi.chk_exst_dnd(_ap): raise exc.ExceptionExistsDirectoryInDirectory()

    pre = dttz.crt_prefix_n_ms(
        "cet")  # Create prefix name without millisecond.
    """ For the file name, make sure to have everything in lower letter and
    to replace space with the separator.
    """
    nm_fi = pth.get_ap_innermst(_ap).lower().replace(" ", var.note_sp)
    nm = "{}{}{}".format(pre, var.note_sp, nm_fi)
    di = pth.jo(pth.get_ap_1(_ap),
                nm)  # Absolute path into the note directory.
    nm_fi = "{}.{}".format(
        nm,
        "md")  # PENDING: `nm` is actually the folder name. Fix later please.
    fi = pth.jo(
        di, nm_fi)  # Absolute path into the .md file in the note directory.

    return op.struct(ap_di=di, ap_md=fi, nm_di=nm, nm_md=nm_fi)
Beispiel #24
0
def cnvrt_img_ip(_ap:str, _w:int=0, _h:int=0) -> str:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile()
    if not pth.get_ext(_ap) in var.img_ext: raise exc.ExceptionNotExistsImageFile()

    _w = "" if _w <= 0 else _w
    _h = "" if _h <= 0 else _h
    nm_fi = crt_apnm_img_cnvrt(_ap)
    difi.ren(_ap, nm_fi.nm_bak)
    _ap = nm_fi.ap_bak

    """ Convert `_ap` into `nm_fi.ap_cn`. The original `_ap` is retained. """
    com = "convert \"{}\" -resize {}x{} \"{}\"".format(_ap, _w, _h, nm_fi.ap_cn)
    subprocess.run(com, shell=True)

    """ CAUTION: Make sure to check if conversion failed (I created file with image
    extension although it is not image file per se). I only use this for debugging purposes.
    """
    if not difi.chk_exst_fi(nm_fi.ap_cn): difi.ren(_ap, nm_fi.nm_cn)
    else: difi.de(_ap) # Because ImageMagick's `convert` create a new copy of the converted
                       # image, this program needs to delete the backup file.

    return nm_fi.ap_cn
Beispiel #25
0
def get_lst(_ap: str) -> list:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()

    return os.listdir(_ap)
Beispiel #26
0
def chk_exst_fi(_ap: str) -> bool:
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    return os.path.isfile(_ap)
Beispiel #27
0
def repair(_ap: str) -> str:
    _ap = pth.ncnp(_ap)

    print("repair {}".format(_ap))

    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()
    if chk_exst_md(_ap, True): raise exc.ExceptionExistMultipleMDFiles()
    if not chk_exst_md(_ap): raise exc.ExceptionNotExistsMDFile()
    if chk_exst_md(_ap):
        if chk_md_b(get_md(_ap)): raise exc.ExceptionMDFileNoContent()

    nm_note_folder = pth.get_ap_innermst(_ap)
    """ Check if there is prefix in the note's folder. """
    if not dttz.chk_prefix(nm_note_folder):
        st_note_folder = crt_apnm_note(_ap)  # Struct name.
        """ Change the name of the note's folder. """
        difi.ren(_ap, st_note_folder.nm_di)
        """ Set back all variables that use `_ap`. """
        _ap = st_note_folder.ap_di
        nm_note_folder = pth.get_ap_innermst(_ap)

    prefix_note_folder = dttz.get_prefix(nm_note_folder)
    ap_md = get_md(_ap)
    nm_md = "{}.{}".format(nm_note_folder, "md")  # .md file name. Taken
    # from the note's folder.
    """ Rename the .md file directly. If the name is already
    proper then this proces goes to `except`.
    """
    try:
        difi.ren(ap_md, nm_md)
        ap_md = pth.jo(_ap, nm_md)  # Get the .md file back.
    except exc.ExceptionSamePath:
        pass

    line_md = rd_md(ap_md)  # Read the content of the .md file.

    i = 0  # Index for looping.
    inx = 1  # Index for attaching and embedding file in .md file.

    while i < len(line_md):
        if chk_s_md(line_md[i]) == s_type.attach or\
        chk_s_md(line_md[i]) == s_type.embed:
            nm_fi = get_fi_rp(line_md[i])  # File name.
            ap_fi = pth.jo(_ap, nm_fi)  # Absolute path to the file.

            if not difi.chk_exst(ap_fi):
                print("note folder: {}".format(_ap))
                print("file: {}".format(ap_fi))
                raise exc.ExceptionNotExistsDirectoryOrFile()
            """ Flags. """
            fl_attach = False
            fl_embed = False
            fl_img = False
            fl_img_600 = False  # If image file is 600 pixels width.
            fl_img_png = False  # If image file is .png.
            fl_nm_number = False  # If file named as a number (for example, 1.jpg, 1.pdf, ...).
            """ Set up flags. """
            if chk_s_md(line_md[i]) == s_type.attach: fl_attach = True
            if chk_s_md(line_md[i]) == s_type.embed:
                fl_embed = True
                if pth.get_ext(nm_fi) in var.img_ext:
                    fl_img = True
                    if img.get_img_dim_w(ap_fi) == 600: fl_img_600 = True
                    if pth.get_ext(nm_fi) == "png": fl_img_png = True
            if get_s_lst(nm_fi).isnumeric(): fl_nm_number = True

            if fl_attach:
                """ If the file name is a number then display the index number. """
                nm_fi = "{}-{}.{}".format(nm_note_folder, inx,
                                          pth.get_ext(nm_fi))
                """
                if fl_nm_number: nm_fi = "{}-{}.{}".format(nm_note_folder, inx, pth.get_ext(nm_fi))
                else:
                    Cleaning.

                    PENDING: Please move this into separate function later on.
                    PENDING: There is also a problem if the file initiated as a image attachment and at the second `repair()`
                             the index will be grow again. There is no error shown yet though.

                    print("="*50)
                    print(nm_note_folder)
                    print(nm_fi)
                    separate_note_folder = nm_note_folder.split("-")
                    print(separate_note_folder)
                    test = nm_fi
                    for asd in separate_note_folder:
                        test = test.replace(asd, "")
                    test = test.split("-")
                    index = len(test) - 1
                    while index > 0:
                        if not test[index].isnumeric() and not test[index] == "":
                            print(test[index])
                        index = index - 1
                    print(test)
                    print("="*50)
                    get_name = nm_fi.replace(nm_note_folder, "")
                    get_name = get_name.replace("--", "")
                    get_name = get_name[1:]
                    get_name = get_name.split("-")
                    index_temp = 0
                    new_string = ""
                    while index_temp < len(get_name):
                        if index_temp == 0 and get_name[index_temp].isnumeric():
                            index_temp = index_temp + 1
                            continue
                        if new_string == "": new_string = get_name[index_temp]
                        else: new_string = "{}-{}".format(new_string, get_name[index_temp])
                        index_temp = index_temp + 1
                    nm_fi = "{}-{}-{}".format(nm_note_folder, inx, new_string)

                print("_ap = {}".format(_ap))
                print("="*50)
                print("nm_note_folder = {}".format(nm_note_folder))
                print("inx = {}".format(inx))
                print("pth.get_ext(nm_fi) = {}".format(pth.get_ext(nm_fi)))
                print("dttz.rm_prefix(nm_fi) = {}".format(dttz.rm_prefix(nm_fi)))
                print("nm_fi = {}".format(nm_fi))
                print("="*50)
                print("*"*50)
                """
                """ PENDING: Stopped increasing index on back up file, because I am afraid of file
                conflict with the next file in this iteration in case the name is the same.
                """
                """ Rename the file with proper proper index number and prefix. """
                difi.ren(ap_fi, nm_fi)
                ap_fi = pth.jo(_ap, nm_fi)
                """ Make this lines back into the main list to be inputted
                to the opened .md file.
                """
                line_md[i] = "{}{}".format(crt_s_md(nm_fi, False), "\n")

            elif fl_embed:  # PENDING: Please check this `if`.
                """ Only embed image file. """
                if not fl_img:
                    print(ap_fi)
                    raise exc.ExceptionNotExistsImageFile()
                """ PENDING: Backup image identifier is now missing. But that is okay I guess. """

                if not fl_img_600 or not fl_img_png:
                    inx = inx + 1
                    nm_fi_attach = "{}-{}.{}".format(nm_note_folder, inx,
                                                     pth.get_ext(nm_fi))
                    inx = inx - 1
                    #if fl_nm_number: nm_fi_attach = "{}-{}-{}.{}".format(nm_note_folder, inx, var.bak, pth.get_ext(nm_fi))
                    #else: nm_fi_attach = "{}-{}-{}.{}".format(nm_note_folder, inx, pth.rm_ext(dttz.rm_prefix(nm_fi), var.bak, pth.get_ext(nm_fi)))
                """ Constructing attach string for file name. """
                nm_fi = "{}-{}.{}".format(nm_note_folder, inx,
                                          pth.get_ext(nm_fi))
                if not fl_img_600 or not fl_img_png: inx = inx + 2
                """ Rename the file with proper proper index number and prefix. """
                difi.ren(ap_fi, nm_fi)
                ap_fi = pth.jo(_ap, nm_fi)

                if not fl_img_600 or not fl_img_png:
                    ap_fi_attach = pth.jo(_ap, nm_fi_attach)
                    difi.cpy(ap_fi,
                             ap_fi_attach)  # Copy into the attachment file.
                    """ Convert into .png with 600 pixels width. """
                    ap_fi_convert = img.cnvrt_img_ip_600(ap_fi)
                    nm_fi_convert = pth.get_ap_innermst(ap_fi_convert)
                    """ Put back to the lines. """
                    line_md[i] = "{}{}".format(crt_s_md(nm_fi_attach, False),
                                               "\n")
                    line_md.insert(
                        i, "{}{}".format(crt_s_md(nm_fi_convert, True),
                                         "\n\n"))
                    i = i + 1
                else:
                    line_md[i] = "{}{}".format(crt_s_md(nm_fi, True), "\n")

            inx = inx + 1

        i = i + 1

    line_md[len(line_md) -
            1] = line_md[len(line_md) -
                         1].rstrip()  # Remove the last line line breaks.
    wrt_md(ap_md, line_md)
    """ This line below is to debug and to show the .md file in Linux and Windows. """
    #if nm_note_folder == "20010101-0000-cet-d": import os; os.system("{} {}".format("xdg-open", ap_md))
    #if nm_note_folder == "20010101-0000-cet-d": import os; os.system(ap_md)
    #import os; os.system("{} {}".format("xdg-open", ap_md))
    #import os; os.system(ap_md);

    return _ap
Beispiel #28
0
def init(_ap: str) -> str:
    _ap = pth.ncnp(_ap)

    print("init {}".format(_ap))

    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not difi.chk_exst_di(_ap): raise exc.ExceptionNotExistsDirectory()
    if chk_exst_md(_ap, True): raise exc.ExceptionExistMultipleMDFiles()
    if chk_exst_md(_ap):
        if not chk_md_b(get_md(_ap)): raise exc.ExceptionMDFileContent()

    nm = crt_apnm_note(_ap)
    org_di_nm = pth.get_ap_innermst(_ap)
    org_md_nm = "{}.{}".format(org_di_nm, "md")
    org_md_ap = pth.jo(_ap, org_md_nm)
    """ Check if prefix is already in directory. """
    if not dttz.chk_prefix(org_di_nm):
        difi.ren(_ap, nm.nm_di)
        """ Set back some variables that used `_ap`. """
        _ap = pth.jo(pth.get_ap_1(_ap), nm.nm_di)
        org_di_nm = pth.get_ap_innermst(_ap)
        org_md_nm = "{}.{}".format(org_di_nm, "md")
        org_md_ap = pth.jo(_ap, org_md_nm)
    """ Check if an .md file is already exists in the note directory. """
    if not chk_exst_md(_ap): crt_md(org_md_ap)
    """ Get the .md file. """
    md = get_md(_ap)  # Absolute path to the .md file.
    md_nm = pth.get_ap_innermst(
        md)  # The name of the .md file with the extension.
    md_nmnext = pth.rm_ext(
        md_nm,
        pth.get_ext(md_nm))  # The name of the .md file without the extension.
    """ The name of .md file should be the same with the note directory.
    If the name is not the same then rename the .md file.
    """
    if not org_di_nm == md_nmnext:
        difi.ren(md, "{}.md".format(org_di_nm))
        """ Set back every parameters that use `md`. """
        md = pth.jo(_ap, "{}.md".format(org_di_nm))
        md_nm = pth.get_ap_innermst(
            md)  # The name of the .md file with the extension.
        md_nmnext = pth.rm_ext(
            md_nm, "md")  # The name of the .md file without the extension.

    inx = 1  # Index number for files.
    lst = []  # List of strings that will be put into `md`.
    prefix = dttz.get_prefix(org_di_nm)
    for i in get_lst_n_md(_ap):
        iap = pth.jo(_ap, i)
        iext = pth.get_ext(i)

        if pth.get_ext(i) in var.img_ext:
            apnmi = crt_apnm_img(iap, org_di_nm, inx)
            inx = apnmi.inx + 1  # PENDING: Put this into `crt_...()` function.
            """ Constructing sized image file for embedding and original file for attachment. """
            difi.ren(iap, apnmi.nme)  # Renaming file before converting.
            difi.cpy(apnmi.ape, apnmi.apa
                     )  # This is the original file. Copied before conversion.

            cnvrt = img.cnvrt_img_ip_600(apnmi.ape)  # Convert!
            """ The first line break is for each line itself. The next three
            line breaks are meant for empty space to write the notes.
            """
            """ Put this file into the .md file in `md`. """
            lst.append("{}{}".format(
                crt_s_md(pth.get_ap_innermst(cnvrt), True), "\n\n"))
            lst.append("{}{}".format(crt_s_md(apnmi.nma, False), "\n\n\n\n"))
        else:
            apnma = crt_apnm_attach(iap, org_di_nm, inx)
            inx = apnma.inx

            difi.ren(iap, apnma.nm)  # Renaming file before converting.
            lst.append("{}{}".format(crt_s_md(
                apnma.nm, False), "\n\n\n\n"))  # Put this file into .md file.

    lst[len(lst) - 1] = lst[len(lst) -
                            1].rstrip()  # Remove the last line line breaks.
    wrt_md(md, lst)  # Write the `lst` into `md`.
    """ This line below is to debug and to show the .md file in Linux and Windows. """
    #import os; os.system("{} {}".format("xdg-open", md))
    #import os; os.system(md);

    return _ap
Beispiel #29
0
def chk_exst(_ap: str) -> bool:
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    return os.path.exists(_ap)
Beispiel #30
0
def crt_md(_ap: str) -> bool:
    _ap = pth.ncnp(_ap)
    if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath()
    if not pth.get_ext(_ap) == "md": raise exc.ExceptionNotExistsMDFile()

    return difi.crt(_ap, False)