11import click
22import json
33from pathlib import Path
4+ import itertools
45
56from .validate import validate_file
6- from .io import convert_file
7+ from .io import convert as _convert
78from .parse import parse_elegant as _parse_elegant
9+ from .format import CompactJSONEncoder
10+ from .migrate import migrate as _migrate
11+
12+
13+ FORMATS = "json" , "lte"
14+ dump_latticejson = lambda obj : json .dumps (obj , cls = CompactJSONEncoder , indent = 4 )
815
916
1017@click .group ()
@@ -14,32 +21,73 @@ def main():
1421
1522
1623@main .command ()
17- @click .argument ("output_format" )
1824@click .argument ("file" , type = click .Path (exists = True ))
19- def convert (** kwargs ):
20- """Convert a latticeJSON or elegant file into another format."""
21- output_format = kwargs ["output_format" ].lower ()
22- if output_format in ("latticejson" , "lj" , "json" ):
23- output_format = "latticejson"
24- elif output_format in ("elegant" , "ele" , "lte" ):
25- output_format = "elegant"
26- else :
27- raise Exception (f"Unknown format { output_format } " )
25+ @click .option (
26+ "--from" ,
27+ "from_" ,
28+ type = click .Choice (FORMATS , case_sensitive = False ),
29+ help = "Source format [optional, default: use file extension]" ,
30+ )
31+ @click .option (
32+ "--to" ,
33+ required = True ,
34+ type = click .Choice (FORMATS , case_sensitive = False ),
35+ help = "Destination format" ,
36+ )
37+ def convert (file , from_ , to ):
38+ """Convert a LatticeJSON or elegant file into another format."""
39+ path = Path (file )
40+ if from_ is None :
41+ from_ = path .suffix [1 :]
2842
29- res = convert_file (kwargs ["file" ], output_format )
30- print (res )
43+ click .echo (_convert (path .read_text (), from_ , to ))
3144
3245
3346@main .command ()
3447@click .argument ("file" , type = click .Path (exists = True ))
35- def validate (** kwargs ):
36- """Validate a latticeJSON lattice file."""
37- validate_file (kwargs ["file" ])
48+ def validate (file ):
49+ """Validate a LatticeJSON lattice file."""
50+ validate_file (file )
51+
52+
53+ @main .command ()
54+ @click .argument ("file" , type = click .Path (exists = True ))
55+ def parse_elegant (file ):
56+ """Parse elegant file but do not convert to LatticeJSON."""
57+ text = Path (file ).read_text ()
58+ click .echo (dump_latticejson (_parse_elegant (text )))
59+
60+
61+ @main .command ()
62+ @click .argument ("files" , nargs = - 1 , type = click .Path (exists = True ))
63+ @click .option (
64+ "--dry-run" ,
65+ "-d" ,
66+ is_flag = True ,
67+ help = "Don't write the files back, just output the formatted files." ,
68+ )
69+ def autoformat (files , dry_run ):
70+ """Format a LatticeJSON file."""
71+ for path in itertools .chain .from_iterable (
72+ path .rglob ("*.json" ) if path .is_dir () else (path ,) for path in map (Path , files )
73+ ):
74+ latticejson = json .loads (path .read_text ())
75+ formatted = dump_latticejson (latticejson )
76+ click .secho (f"reformatted { path } " , bold = True )
77+ if dry_run :
78+ click .echo (formatted )
79+ else :
80+ path .write_text (formatted )
3881
3982
4083@main .command ()
4184@click .argument ("file" , type = click .Path (exists = True ))
42- def parse_elegant (** kwargs ):
43- """Parse elegant file but do not convert to latticeJSON."""
44- text = Path (kwargs ["file" ]).read_text ()
45- print (json .dumps (_parse_elegant (text ), indent = 4 ))
85+ @click .option ("--from" , "from_" , required = True , help = "Initial version" )
86+ @click .option ("--to" , required = True , help = "Final version" )
87+ def migrate (file , from_ , to ):
88+ """Migrate old LatticeJSON files to newer versions."""
89+ text = Path (file ).read_text ()
90+ initial_version = from_ .split ("." )
91+ final_version = to .split ("." )
92+ latticejson = _migrate (json .loads (text ), initial_version , final_version )
93+ click .echo (dump_latticejson (latticejson ))
0 commit comments