예제 #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)
예제 #2
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
예제 #3
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
예제 #4
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)
예제 #5
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)
예제 #6
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)
예제 #7
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
예제 #8
0
if __name__ == "__main__":
    doc = docopt(__doc__, version="0.0.1")

    automate = doc["aut"]
    frmt = doc["frmt"]
    rr = doc["rr"]

    ap = doc["<ap>"]
    ap_trg = doc["<ap_trg>"]
    s = doc["<s>"]
    snew = doc["<snew>"]
    """ Create a backup directory or file for automating and renaming. """
    if automate or rr:
        """ Make a backup copy first. """
        nm_di = pth.get_ap_innermst(ap)
        ap_1_di = pth.get_ap_1(ap)
        nm_bk = "{}_{}_{}".format(nm_di,
                                  dttz.crt_prefix_cpy(dttz.get_now_n_ms()),
                                  var.bak)
        ap_bk = pth.jo(ap_1_di, nm_bk)
        difi.cpy(ap, ap_bk)
    """ Creating backup directory or file for formating file
    structures into MKDocs.
    """
    if frmt:
        """ Make a backup copy first. """
        nm_di = pth.get_ap_innermst(ap)
        ap_1_di = pth.get_ap_1(ap)
        nm_bk = "{}_{}_{}".format(nm_di,
                                  dttz.crt_prefix_cpy(dttz.get_now_n_ms()),
                                  "note")