dncli 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/python3
  2. # TODO: change the default file list to only apply to listing, not copying/erasing/creating/appending/zapping
  3. VERSION = "0.1"
  4. import os, sys, argparse
  5. def file_copy(f,target,target_is_dir,force):
  6. print(f"call file_copy with args={target},{target_is_dir} and {force}")
  7. dest = target if not target_is_dir else target+'/'+os.path.basename(f)
  8. if os.path.exists(dest) and not force:
  9. go = input("The copy target <<" + dest + ">> exists. Overwrite? (y or n) ")
  10. if go != 'y' and go != 'Y':
  11. return
  12. print(f"copy from {f} to {dest}")
  13. def file_zap(f,all_flag):
  14. print(f"zapping the comment history of {f}")
  15. if all_flag:
  16. print("zapping the entire database")
  17. def file_erase(f, extra):
  18. print(f"erase the comment on file {f}")
  19. def main(args):
  20. parser = argparse.ArgumentParser(description="Display or add comments to files",
  21. epilog="Some options conflict. Use only one of: -l -c -a -H -e -z -Z and one of -d -x")
  22. parser.add_argument('-V',"--version", action="version",version=f"dncli ver:{VERSION}")
  23. parser.add_argument('-v',"--verbose", action='store_true',help="verbose, almost debugging; do not use in scripts")
  24. parser.add_argument('-j',"--json", action="store_true",help="output in JSON format")
  25. parser.add_argument('-l',"--listall", action="store_true",help="list all files, including those without comments")
  26. parser.add_argument('-d',"--db", action="store_true",help="list comments from database")
  27. parser.add_argument('-x',"--xattr", action="store_true",help="list comments from xattr")
  28. parser.add_argument('-n',"--minimal", action="store_true",help="output only comments; useful in scripting")
  29. parser.add_argument('-H',"--history", action="store_true",help="output the history of comments for a file")
  30. parser.add_argument('-c',"--create", metavar="comment", help="add a comment to a file")
  31. parser.add_argument('-a',"--append", metavar="comment", help="append to a comment on a file, separator=';'")
  32. parser.add_argument('-C',"--copy", action="store_true",help="copy a file with its comments")
  33. parser.add_argument('-y',"--cp_force",action="store_true",help="copy over existing files")
  34. parser.add_argument('-e',"--erase", action="store_true",help="erase the comment on a file")
  35. parser.add_argument('-z',"--zap", action="store_true",help="clear the comment history on a file")
  36. parser.add_argument('-Z',"--zapall", action="store_true",help="clear the comment history in the entire database")
  37. parser.add_argument('file_list',nargs='*',help="file(s) for comments; default is all files in ./")
  38. args = parser.parse_args()
  39. # default is to display all files that have comments
  40. # major modes are: display (-l -H), add-comment (-a -c -e), clear-history(-z -Z), copy (-C)
  41. # determine the major mode, then apply an appropriate function over the file_list
  42. #====== 1) build the file list =============
  43. files = args.file_list
  44. if not files:
  45. files = os.listdir(".")
  46. # if the user gave me a single dir...expand it
  47. elif len(files)==1 and os.path.isdir(files[0]):
  48. files = os.listdir(files[0])
  49. print("got the files:", files)
  50. #======= 2) build the function
  51. if args.create or args.append:
  52. print(f"create/append: {args.create} . {args.append}")
  53. elif args.erase:
  54. print(f"erase command")
  55. func = file_erase
  56. loop_args = (0,)
  57. elif args.zap or args.zapall:
  58. print(f"got a zap command {args.zap} . {args.zapall}")
  59. func = file_zap
  60. loop_args = (args.zapall,)
  61. if args.zapall:
  62. files = (1,)
  63. elif args.copy:
  64. print(f"got a copy command to {args.copy}")
  65. # the last item on the file list is the target
  66. n_files = len(files)
  67. if n_files < 2:
  68. print(f"the copy command requires at least two arguments, the last one is the destination")
  69. sys.exit(1)
  70. target = files[-1]
  71. target_is_directory = os.path.isdir(target)
  72. files = files[:-1]
  73. print(f"copy from {files} to {target}")
  74. if n_files > 2 and not target_is_directory:
  75. print(f"multiple copy files must go to a target directory")
  76. sys.exit(3)
  77. func = file_copy
  78. loop_args = (target, target_is_directory, args.cp_force)
  79. else:
  80. print(f"display command with option: {args.listall}")
  81. #====== 3) loop on the list, execute the function =============
  82. for f in files:
  83. func(f,*loop_args)
  84. if __name__ == "__main__":
  85. main(sys.argv)