You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parse_arguments.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/env python
  2. """Parses given arguments"""
  3. import random
  4. import os
  5. from .decrypter import decrypt_files
  6. from .encrypter import encrypt_files
  7. from .uploader import upload_files
  8. from .hostlist import generate_host_string
  9. def check_max_filesize(chosen_file, max_size):
  10. """
  11. Checks file sizes for host
  12. """
  13. if os.path.getsize(chosen_file) > max_size:
  14. return False
  15. else:
  16. return True
  17. def parse_arguments(args, clone_list):
  18. """
  19. Makes parsing arguments a function.
  20. """
  21. returned_string=""
  22. host_number = args.host
  23. if args.show_list:
  24. print(generate_host_string(clone_list, "Available hosts: "))
  25. exit()
  26. if args.decrypt:
  27. for i in args.files:
  28. print(decrypt_files(i))
  29. exit()
  30. if args.files:
  31. for i in args.files:
  32. if args.limit_size:
  33. if args.host == host_number and host_number is not None:
  34. if not check_max_filesize(i, clone_list[host_number][3]):
  35. host_number = None
  36. for n, host in enumerate(clone_list):
  37. if not check_max_filesize(i, host[3]):
  38. clone_list[n] = None
  39. if not clone_list:
  40. print('None of the clones is able to support so big file.')
  41. if args.no_cloudflare:
  42. if args.host == host_number and host_number is not None and not clone_list[host_number][4]:
  43. print("This host uses Cloudflare, please choose different host.")
  44. exit(1)
  45. else:
  46. for n, host in enumerate(clone_list):
  47. if not host[4]:
  48. clone_list[n] = None
  49. clone_list = list(filter(None, clone_list))
  50. if host_number is None or args.host != host_number:
  51. host_number = random.randrange(0, len(clone_list))
  52. while True:
  53. try:
  54. if args.encrypt:
  55. returned_string = encrypt_files(clone_list[host_number], args.only_link, i)
  56. else:
  57. returned_string = upload_files(open(i, 'rb'), \
  58. clone_list[host_number], args.only_link, i)
  59. if args.only_link:
  60. print(returned_string[0])
  61. else:
  62. print(returned_string)
  63. except IndexError:
  64. #print('Selected server (' + clone_list[host_number][0] + ') is offline.')
  65. #print('Trying other host.')
  66. host_number = random.randrange(0, len(clone_list))
  67. continue
  68. except IsADirectoryError:
  69. print('limf does not support directory upload, if you want to upload ' \
  70. 'every file in directory use limf {}/*.'.format(i.replace('/', '')))
  71. if args.log:
  72. with open(os.path.expanduser(args.logfile), "a+") as logfile:
  73. if args.only_link:
  74. logfile.write(returned_string[1])
  75. else:
  76. logfile.write(returned_string)
  77. logfile.write("\n")
  78. break
  79. else:
  80. print("limf: try 'limf -h' for more information")