scripts: csv.py: Simplified -i/--enumerate to alias -bi -Fi=enumerate()
Now that we have the enumerate expr, -i/--enumerate can be implemented entirely during expr eval: - -i/--enumerate => -bi -Fi=enumerate() - -I/--hidden-enumerate => -Bi -Fi=enumerate() Instead of internally reimplementing the same behavior. This is what our help text implies, so might as well put our money where our mouth is. And the less special internals we have, the better. I considered removing -i/-I completely, but it's quite a convenient flag when debugging csv.py expressions.
This commit is contained in:
+26
-33
@@ -1713,7 +1713,6 @@ def compile(fields_, results,
|
|||||||
**{'_notes': notes} if notes is not None else {}))
|
**{'_notes': notes} if notes is not None else {}))
|
||||||
|
|
||||||
def homogenize(Result, results, *,
|
def homogenize(Result, results, *,
|
||||||
enumerates=None,
|
|
||||||
defines=[],
|
defines=[],
|
||||||
depth=1,
|
depth=1,
|
||||||
depth_=0,
|
depth_=0,
|
||||||
@@ -1737,17 +1736,12 @@ def homogenize(Result, results, *,
|
|||||||
# append a result
|
# append a result
|
||||||
results_.append(Result(
|
results_.append(Result(
|
||||||
**(r
|
**(r
|
||||||
# enumerate?
|
|
||||||
| ({e: len(results_) for e in enumerates}
|
|
||||||
if enumerates is not None
|
|
||||||
else {})
|
|
||||||
# keep track of depth?
|
# keep track of depth?
|
||||||
| ({Result._z: depth_} if hasattr(Result, '_z') else {})
|
| ({Result._z: depth_} if hasattr(Result, '_z') else {})
|
||||||
# recurse?
|
# recurse?
|
||||||
| ({Result._children: homogenize(
|
| ({Result._children: homogenize(
|
||||||
Result, r[Result._children],
|
Result, r[Result._children],
|
||||||
# only filter defines at the top level!
|
# only filter defines at the top level!
|
||||||
enumerates=enumerates,
|
|
||||||
depth=depth-1,
|
depth=depth-1,
|
||||||
depth_=depth_+1)}
|
depth_=depth_+1)}
|
||||||
if hasattr(Result, '_children')
|
if hasattr(Result, '_children')
|
||||||
@@ -2421,19 +2415,14 @@ def main(csv_paths, *,
|
|||||||
notes=notes,
|
notes=notes,
|
||||||
**args)
|
**args)
|
||||||
|
|
||||||
# separate out enumerates/mods/exprs
|
# separate out mods/exprs
|
||||||
#
|
#
|
||||||
# enumerate enumerates: -ia
|
|
||||||
# by supports mods: -ba=%(b)s
|
# by supports mods: -ba=%(b)s
|
||||||
# fields/sort/etc supports exprs: -fa=b+c
|
# fields/sort/etc supports exprs: -fa=b+c
|
||||||
#
|
#
|
||||||
enumerates = [k
|
|
||||||
for (k, v), hidden in (by or [])
|
|
||||||
if v == enumerate]
|
|
||||||
mods = [(k, v)
|
mods = [(k, v)
|
||||||
for k, v in it.chain(
|
for k, v in it.chain(
|
||||||
((k, v) for (k, v), hidden in (by or [])
|
((k, v) for (k, v), hidden in (by or [])))
|
||||||
if v != enumerate))
|
|
||||||
if v is not None]
|
if v is not None]
|
||||||
exprs = [(k, v)
|
exprs = [(k, v)
|
||||||
for k, v in it.chain(
|
for k, v in it.chain(
|
||||||
@@ -2515,7 +2504,6 @@ def main(csv_paths, *,
|
|||||||
|
|
||||||
# homogenize
|
# homogenize
|
||||||
results = homogenize(Result, results,
|
results = homogenize(Result, results,
|
||||||
enumerates=enumerates,
|
|
||||||
defines=defines,
|
defines=defines,
|
||||||
depth=depth)
|
depth=depth)
|
||||||
|
|
||||||
@@ -2551,7 +2539,6 @@ def main(csv_paths, *,
|
|||||||
|
|
||||||
# homogenize
|
# homogenize
|
||||||
diff_results = homogenize(Result, diff_results,
|
diff_results = homogenize(Result, diff_results,
|
||||||
enumerates=enumerates,
|
|
||||||
defines=defines,
|
defines=defines,
|
||||||
depth=depth)
|
depth=depth)
|
||||||
|
|
||||||
@@ -2638,29 +2625,35 @@ if __name__ == "__main__":
|
|||||||
'-a', '--all',
|
'-a', '--all',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help="Show all, not just the ones that changed.")
|
help="Show all, not just the ones that changed.")
|
||||||
|
class AppendEnumerate(argparse.Action):
|
||||||
|
def __call__(self, parser, namespace, value, option):
|
||||||
|
if namespace.by is None:
|
||||||
|
namespace.by = []
|
||||||
|
if namespace.fields is None:
|
||||||
|
namespace.fields = []
|
||||||
|
namespace.by.append(((value, None), option in {
|
||||||
|
'-I', '--hidden-enumerate'}))
|
||||||
|
namespace.fields.append(((value, CsvExpr('enumerate()')), True))
|
||||||
|
parser.add_argument(
|
||||||
|
'-i', '--enumerate',
|
||||||
|
action=AppendEnumerate,
|
||||||
|
nargs='?',
|
||||||
|
const='i',
|
||||||
|
help="Enumerate results with this field, equivalent to "
|
||||||
|
" -bi -Fi=enumerate(). This will prevent result folding.")
|
||||||
|
parser.add_argument(
|
||||||
|
'-I', '--hidden-enumerate',
|
||||||
|
action=AppendEnumerate,
|
||||||
|
nargs='?',
|
||||||
|
const='i',
|
||||||
|
help="Like -i/--enumerate, but hidden from the table renderer, "
|
||||||
|
"and doesn't affect -b/--by defaults.")
|
||||||
class AppendBy(argparse.Action):
|
class AppendBy(argparse.Action):
|
||||||
def __call__(self, parser, namespace, value, option):
|
def __call__(self, parser, namespace, value, option):
|
||||||
if namespace.by is None:
|
if namespace.by is None:
|
||||||
namespace.by = []
|
namespace.by = []
|
||||||
namespace.by.append((value, option in {
|
namespace.by.append((value, option in {
|
||||||
'-B', '--hidden-by',
|
'-B', '--hidden-by'}))
|
||||||
'-I', '--hidden-enumerate'}))
|
|
||||||
parser.add_argument(
|
|
||||||
'-i', '--enumerate',
|
|
||||||
action=AppendBy,
|
|
||||||
nargs='?',
|
|
||||||
type=lambda x: (x, enumerate),
|
|
||||||
const=('i', enumerate),
|
|
||||||
help="Enumerate results with this field, equivalent to "
|
|
||||||
" -bi -Fi=enumerate(). This will prevent result folding.")
|
|
||||||
parser.add_argument(
|
|
||||||
'-I', '--hidden-enumerate',
|
|
||||||
action=AppendBy,
|
|
||||||
nargs='?',
|
|
||||||
type=lambda x: (x, enumerate),
|
|
||||||
const=('i', enumerate),
|
|
||||||
help="Like -i/--enumerate, but hidden from the table renderer, "
|
|
||||||
"and doesn't affect -b/--by defaults.")
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-b', '--by',
|
'-b', '--by',
|
||||||
action=AppendBy,
|
action=AppendBy,
|
||||||
|
|||||||
Reference in New Issue
Block a user