Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2021 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of torrent-download. 

5 

6torrent-download is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11torrent-download is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with torrent-download. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import os 

21import json 

22from puffotter.os import makedirs 

23from puffotter.prompt import prompt 

24from torrent_download.exceptions import MissingConfig 

25 

26 

27class Config: 

28 """ 

29 Configuration for the QBittorrent client 

30 """ 

31 

32 config_file = os.path.join( 

33 os.path.expanduser("~"), 

34 ".config/torrent-download/config.json" 

35 ) 

36 """ 

37 Path to the configuration file 

38 """ 

39 

40 def __init__( 

41 self, 

42 qbittorrent_address: str, 

43 qbittorrent_username: str, 

44 qbittorrent_password: str, 

45 qbittorrent_download_dir: str 

46 ): 

47 """ 

48 Initializes the Config object 

49 :param qbittorrent_address: The qbittorrent address/URL 

50 :param qbittorrent_username: The qbittorent username 

51 :param qbittorrent_password: The qbittorent password 

52 :param qbittorrent_download_dir: The qbittorent download directory path 

53 """ 

54 if qbittorrent_address.endswith("/"): 

55 qbittorrent_address = qbittorrent_address[0:-1] 

56 self.qbittorrent_address = qbittorrent_address 

57 self.qbittorrent_username = qbittorrent_username 

58 self.qbittorrent_password = qbittorrent_password 

59 self.qbittorrent_download_dir = qbittorrent_download_dir 

60 

61 def save(self): 

62 """ 

63 Saves the configuration 

64 :return: None 

65 """ 

66 makedirs(os.path.dirname(self.config_file)) 

67 with open(self.config_file, "w") as f: 

68 json.dump({ 

69 "qbittorrent_address": self.qbittorrent_address, 

70 "qbittorrent_username": self.qbittorrent_username, 

71 "qbittorrent_password": self.qbittorrent_password, 

72 "qbittorrent_download_dir": self.qbittorrent_download_dir 

73 }, f, indent=4) 

74 

75 @classmethod 

76 def load(cls) -> "Config": 

77 """ 

78 Loads an existing configuration from disk 

79 :return: None 

80 """ 

81 if not os.path.isfile(cls.config_file): 

82 raise MissingConfig(cls.config_file) 

83 else: 

84 with open(cls.config_file, "r") as f: 

85 data = json.load(f) 

86 return cls( 

87 data["qbittorrent_address"], 

88 data["qbittorrent_username"], 

89 data["qbittorrent_password"], 

90 data["qbittorrent_download_dir"] 

91 ) 

92 

93 @classmethod 

94 def prompt(cls) -> "Config": 

95 """ 

96 Prompts the user for configuration details 

97 :return: The generated configuration 

98 """ 

99 return cls( 

100 prompt("QBittorrent Address/URL"), 

101 prompt("QBittorrent Username"), 

102 prompt("QBittorrent Password"), 

103 prompt("QBittorrent Download Directory") 

104 )