|
| 1 | +""" |
| 2 | +File Path Manipulation |
| 3 | +""" |
| 4 | + |
| 5 | +import os.path as osp |
| 6 | +from pathlib import Path |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +from mathics.core.atoms import Integer, String |
| 10 | +from mathics.core.builtin import Builtin |
| 11 | +from mathics.core.evaluation import Evaluation |
| 12 | + |
| 13 | +# This tells documentation how to sort this module |
| 14 | +sort_order = "mathics.builtin.file-operations.file_path_operations" |
| 15 | + |
| 16 | + |
| 17 | +class FileNameDrop(Builtin): |
| 18 | + """ |
| 19 | + <url> |
| 20 | + :WMA link: |
| 21 | + https://reference.wolfram.com/language/ref/FileNameDrop.html</url> |
| 22 | +
|
| 23 | + <dl> |
| 24 | + <dt>'FileNameDrop["$path$", $n$]' |
| 25 | + <dd>drops the first $n$ path elements in the file name $path$. |
| 26 | +
|
| 27 | + <dt>'FileNameDrop["$path$", -$n$]' |
| 28 | + <dd>drops the last $n$ path elements in the file name $path$. |
| 29 | +
|
| 30 | + <dt>'FileNameDrop["$path$", {$m$, $n$}]' |
| 31 | + <dd>drops elements $m$ through $n$ path elements in the file name $path$. |
| 32 | +
|
| 33 | + <dt>'FileNameDrop["$path$"]' |
| 34 | + <dd>drops the last path elements in the file name $path$. |
| 35 | + </dl> |
| 36 | +
|
| 37 | + >> path = FileNameJoin{"a","b","c"} |
| 38 | + = ... |
| 39 | +
|
| 40 | + >> FileNameDrop[path, -1] |
| 41 | + = ... |
| 42 | +
|
| 43 | + A shorthand for the above: |
| 44 | +
|
| 45 | + >> FileNameDrop[path] |
| 46 | + = ... |
| 47 | + """ |
| 48 | + |
| 49 | + messages = { |
| 50 | + "notfinished": "m-n handling is not complete.", |
| 51 | + } |
| 52 | + rules = { |
| 53 | + "FileNameDrop[name_]": "FileNameDrop[name, -1]", |
| 54 | + "FileNameDrop[list_List, parms___]": "FileNameDrop[#1,parms]&/@list", |
| 55 | + } |
| 56 | + summary_text = "drop a part of a file path" |
| 57 | + |
| 58 | + def eval_with_n(self, path: String, n: Integer, evaluation: Evaluation) -> String: |
| 59 | + "FileNameDrop[path_String, n_Integer]" |
| 60 | + pos = n.value |
| 61 | + if pos == 0: |
| 62 | + return path |
| 63 | + path_elts = Path(path.value).parts |
| 64 | + path_len = len(path_elts) |
| 65 | + if pos >= path_len or pos <= -path_len: |
| 66 | + return String("") |
| 67 | + |
| 68 | + new_elts = path_elts[pos:] if pos > 0 else path_elts[:pos] |
| 69 | + return String(osp.join(*new_elts) if new_elts else "") |
| 70 | + |
| 71 | + def eval_with_n_to_m( |
| 72 | + self, path: String, n: Integer, m: Integer, evaluation: Evaluation |
| 73 | + ) -> Optional[String]: |
| 74 | + "FileNameDrop[path_String, {n_Integer, m_Integer}]" |
| 75 | + n_pos = n.value |
| 76 | + m_pos = m.value |
| 77 | + path_elts = Path(path.value).parts |
| 78 | + path_len = len(path_elts) |
| 79 | + if n_pos > path_len: |
| 80 | + return path |
| 81 | + |
| 82 | + if n_pos == path_len: |
| 83 | + if n_pos == m_pos or n_pos + m_pos == -1: |
| 84 | + # Not sure why this is so. |
| 85 | + return String(osp.join(*path_elts[:-1])) |
| 86 | + return path |
| 87 | + |
| 88 | + if n_pos > m_pos: |
| 89 | + return path |
| 90 | + |
| 91 | + new_elts = None |
| 92 | + if 0 < n_pos < m_pos: |
| 93 | + new_elts = path_elts[: n_pos - 1] + path_elts[m_pos:] |
| 94 | + elif n_pos <= m_pos <= 0: |
| 95 | + new_elts = path_elts[:n_pos] + path_elts[m_pos + 1 :] |
| 96 | + else: |
| 97 | + evaluation.message("FindNameDrop", "notfinished") |
| 98 | + return None |
| 99 | + |
| 100 | + if new_elts: |
| 101 | + return String(osp.join(*new_elts)) |
| 102 | + else: |
| 103 | + return String("") |
0 commit comments