|
| 1 | +import os |
| 2 | +import json |
| 3 | +import getpass |
| 4 | +import variable |
| 5 | +def get_default_download_folder(): |
| 6 | + |
| 7 | + # try to automatically detect the download folder |
| 8 | + default_download_folder = None |
| 9 | + |
| 10 | + # Platform-specific logic to detect the download folder |
| 11 | + if os.name == "posix": # Linux/Mac |
| 12 | + default_download_folder = os.path.expanduser("~/Downloads") |
| 13 | + elif os.name == "nt": # Windows |
| 14 | + default_download_folder = os.path.join(os.path.expanduser("~"), "Downloads") |
| 15 | + else: |
| 16 | + print("Unsupported operating system.") |
| 17 | + exit(1) |
| 18 | + |
| 19 | + return default_download_folder |
| 20 | + |
| 21 | +def create_default_directories(): |
| 22 | + # If username is "codecraft", use the hardcoded download folder |
| 23 | + username = getpass.getuser() |
| 24 | + if username=="CodeCraft": |
| 25 | + default_dirs = default_dirs = { |
| 26 | + "PDF": os.path.join(os.path.expanduser("~"),"Documents", "PDF"), |
| 27 | + "DOCX": os.path.join(os.path.expanduser("~"),"Documents", "DOCX"), |
| 28 | + "ZIP": variable.ZIP_DIR, |
| 29 | + "Image": os.path.join(os.path.expanduser("~"),"Pictures", "Photos"), |
| 30 | + "Python": variable.PYTHON_DIR, |
| 31 | + "Video": os.path.join(os.path.expanduser("~"),"Videos"), |
| 32 | + "Music": os.path.join(os.path.expanduser("~"), "Music"), |
| 33 | + "Torrent": os.path.join(get_default_download_folder(), "Torrents") |
| 34 | + } |
| 35 | + return default_dirs |
| 36 | + # Define default directories for different file types |
| 37 | + default_dirs = { |
| 38 | + "PDF": os.path.join(os.path.expanduser("~"),"Documents", "PDF"), |
| 39 | + "DOCX": os.path.join(os.path.expanduser("~"),"Documents", "DOCX"), |
| 40 | + "ZIP": os.path.join(get_default_download_folder(), "ZIP"), |
| 41 | + "Image": os.path.join(os.path.expanduser("~"),"Pictures", "Photos"), |
| 42 | + "Python": os.path.join(os.path.expanduser("~"),"Code", "Python"), |
| 43 | + "Video": os.path.join(os.path.expanduser("~"),"Videos"), |
| 44 | + "Music": os.path.join(os.path.expanduser("~"), "Music"), |
| 45 | + "Torrent": os.path.join(get_default_download_folder(), "Torrents") |
| 46 | + } |
| 47 | + |
| 48 | + return default_dirs |
| 49 | + |
| 50 | +def save_variables_to_json(vars_dict): |
| 51 | + with open("config.json", "w") as json_file: |
| 52 | + json.dump(vars_dict, json_file, indent=4) |
| 53 | +def load_variables_from_json(): |
| 54 | + dirname = os.path.dirname(__file__) |
| 55 | + filename = os.path.join(dirname, 'config.json') |
| 56 | + try: |
| 57 | + with open(filename, "r") as json_file: |
| 58 | + loaded_vars = json.load(json_file) |
| 59 | + except FileNotFoundError: |
| 60 | + # If the config file doesn't exist, return an empty dictionary |
| 61 | + return {} |
| 62 | + return loaded_vars |
| 63 | +def main(): |
| 64 | + username = getpass.getuser() |
| 65 | + # Create default directories for different file types |
| 66 | + default_directories = create_default_directories() |
| 67 | + |
| 68 | + # Save the variables to a JSON file |
| 69 | + if (os.path.exists("config.json"))==False: |
| 70 | + save_variables_to_json(default_directories) |
0 commit comments