Skip to content

Commit 744cbe0

Browse files
authored
Add FileNameDrop[] (#1141)
Left to do are changes with mixed negative and positive to integer arg values. I don't understand the behavior though. And this isn't needed for the ChemTools loader anyway. (But ChemTools has other bugs that still need work).
1 parent 8c472bd commit 744cbe0

5 files changed

Lines changed: 109 additions & 0 deletions

File tree

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ New Builtins
66
++++++++++++
77

88
* ``CheckAbort``
9+
* ``FileNameDrop``
910
* ``SetEnvironment``
1011

1112
``mathics`` command line

SYMBOLS_MANIFEST.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ System`FileFormat
399399
System`FileHash
400400
System`FileInformation
401401
System`FileNameDepth
402+
System`FileNameDrop
402403
System`FileNameJoin
403404
System`FileNameSplit
404405
System`FileNameTake

mathics/builtin/file_operations/file_properties.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from mathics.core.systemsymbols import SymbolAbsoluteTime, SymbolFailed, SymbolNone
2121
from mathics.eval.nevaluator import eval_N
2222

23+
sort_order = "mathics.builtin.file-operations.file_properties"
24+
2325

2426
class FileDate(Builtin):
2527
"""

mathics/builtin/file_operations/file_utilities.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from mathics.core.evaluation import Evaluation
1010
from mathics.core.systemsymbols import SymbolFailed
1111

12+
sort_order = "mathics.builtin.file-operations.file_utilities"
13+
1214

1315
class FindList(Builtin):
1416
"""
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)