99
1010import click
1111import numpy as np
12- import prettytable
1312
1413from . import dataset
1514from . import local
@@ -342,6 +341,7 @@ def shelephant_cp(args: list[str], paths: list[str] = None, filter_paths: bool =
342341 if suffix_source != pathlib .Path ("" ):
343342 files = [os .path .relpath (p , suffix_source ) for p in files ]
344343
344+ # interaction not directly with command-line
345345 if len (paths ) > 0 :
346346 if (common_prefix / deepest ) != pathlib .Path ("" ):
347347 strip = common_prefix / deepest
@@ -660,7 +660,17 @@ def _shelephant_diff_parser():
660660 shelephant_diff <sourceinfo.yaml> <destinfo.yaml> --filter "?=, !="
661661 shelephant_diff <sourceinfo.yaml> <destinfo.yaml> -o <diff.yaml>
662662
663- Note that if filter contains only one operation the output YAML-file will be a list.
663+ .. note::
664+
665+ ``--filter`` allows to output only a limited number of directions.
666+ For convenience, and to bypass syntax limitations, the following aliases are available:
667+
668+ - ``<``: ``<-``
669+ - ``>``: ``->``
670+ - ``!``: ``!=``
671+ - ``~``: ``!=``
672+ - ``?``: ``?=``
673+ - ``=``: ``==``
664674 """
665675 )
666676
@@ -679,14 +689,13 @@ class MyFmt(
679689 parser .add_argument (
680690 "--mode" , type = str , help = "Use 'sha256', 'rsync', or 'basic'." , default = "sha256"
681691 )
682- parser .add_argument ("--sort" , type = str , help = "Sort printed table by column." )
683- parser .add_argument ("--table" , type = str , default = "SINGLE_BORDER" , help = "Select print style." )
684- parser .add_argument ("--filter" , type = str , help = "Filter to direction (separated by ',')." )
685- parser .add_argument ("-o" , "--output" , type = pathlib .Path , help = "Dump as YAML file." )
686- parser .add_argument ("-f" , "--force" , action = "store_true" , help = "Force overwrite output." )
692+ parser .add_argument ("--list" , action = "store_true" , help = "Output list instead of dictionary" )
693+ parser .add_argument ("--filter" , type = str , help = "Filter to directions separated by ','" )
694+ parser .add_argument ("-o" , "--output" , type = pathlib .Path , help = "Dump as YAML file" )
695+ parser .add_argument ("-f" , "--force" , action = "store_true" , help = "Force overwrite output file" )
687696 parser .add_argument ("--version" , action = "version" , version = version )
688- parser .add_argument ("source" , type = pathlib .Path , help = "Source information. " )
689- parser .add_argument ("dest" , type = pathlib .Path , help = "Destination directory/information. " )
697+ parser .add_argument ("source" , type = pathlib .Path , help = "Source information" )
698+ parser .add_argument ("dest" , type = pathlib .Path , help = "Destination directory/information" )
690699 return parser
691700
692701
@@ -704,17 +713,17 @@ def shelephant_diff(args: list[str]):
704713 assert len (args .mode ) == 1 , "Only one mode allowed."
705714 assert shutil .which ("rsync" ) is not None or "rsync" not in args .mode , "rsync not available."
706715
707- source = dataset .Location .from_yaml (args .source )
708- files = source .files (info = False )
709-
710716 if args .dest .is_file ():
711717 dest = dataset .Location .from_yaml (args .dest )
712718 else :
713719 dest = dataset .Location (root = args .dest , ssh = args .ssh )
714720
721+ source = dataset .Location .from_yaml (args .source )
722+
715723 if "sha256" in args .mode :
716724 status = source .diff (dest )
717725 elif "rsync" in args .mode :
726+ files = source .files (info = False )
718727 left = source .diff (dest )["<-" ]
719728 [files .remove (file ) for file in left ]
720729 status = rsync .diff (source .hostpath , dest .hostpath , files )
@@ -726,51 +735,26 @@ def shelephant_diff(args: list[str]):
726735 raise ValueError (f"Unknown mode '{ args .mode } '." )
727736
728737 if args .filter :
729- keys = [key .strip () for key in args .filter .split ("," )]
738+ alias = {">" : "->" , "<" : "<-" , "!" : "!=" , "~" : "!=" , "?" : "?=" , "=" : "==" }
739+ alias = {** alias , ** {v : v for v in alias .values ()}}
740+ filters = [alias [i .strip ()] for i in args .filter .split ("," )]
741+ keys = [key .strip () for key in filters ]
730742 keys = [key for key in keys if key in status ]
731743 status = {key : status [key ] for key in keys }
732744
733745 for key in list (status .keys ()):
734746 if len (status [key ]) == 0 :
735747 del status [key ]
736748
749+ if args .list :
750+ assert len (status ) == 1 , "--list output only works if only one direction is selected."
751+ status = status [list (status .keys ())[0 ]]
752+
737753 if args .output :
738- if len (status ) == 1 :
739- status = status [list (status .keys ())[0 ]]
740754 yaml .dump (args .output , status , force = args .force )
741755 return
742756
743- out = prettytable .PrettyTable ()
744- if args .table == "PLAIN_COLUMNS" :
745- out .set_style (prettytable .PLAIN_COLUMNS )
746- elif args .table == "SINGLE_BORDER" :
747- out .set_style (prettytable .SINGLE_BORDER )
748- out .field_names = ["source" , "sync" , "dest" ]
749- out .align ["source" ] = "l"
750- out .align ["sync" ] = "c"
751- out .align ["dest" ] = "l"
752-
753- left = status .pop ("->" , [])
754- right = status .pop ("<-" , [])
755- equal = status .pop ("==" , [])
756-
757- for key in status :
758- for item in status [key ]:
759- out .add_row ([item , key , item ])
760-
761- for item in left :
762- out .add_row ([item , "->" , "" ])
763-
764- for item in right :
765- out .add_row (["" , "<-" , item ])
766-
767- for item in equal :
768- out .add_row ([item , "==" , item ])
769-
770- if args .sort is None :
771- print (out .get_string ())
772- else :
773- print (out .get_string (sortby = args .sort ))
757+ output .diff (status , colors = args .colors )
774758
775759
776760def _shelephant_main_parser ():
0 commit comments