Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. import json
  2. import uuid
  3. import base64
  4. import binascii
  5. import os
  6.  
  7.  
  8. try:
  9. import win32api # Only works on Windows.
  10. import pyautogui # Only works on Windows.
  11. except ImportError:
  12. pass
  13.  
  14.  
  15. class Cryptograph():
  16. def __init__(self):
  17. self._mac_addr = str(uuid.getnode())
  18.  
  19. def _encode(self, clear):
  20. """Encode a string based on a key, in this case mac address"""
  21. enc = []
  22. for i in range(len(clear)):
  23. self._mac_addr_c = self._mac_addr[i % len(self._mac_addr)]
  24. enc_c = chr((ord(clear[i]) + ord(self._mac_addr_c)) % 256)
  25. enc.append(enc_c)
  26. return base64.urlsafe_b64encode("".join(enc).encode()).decode()
  27.  
  28. def _decode(self, enc):
  29. """Decode a string based on a key, in this case mac address"""
  30. dec = []
  31. enc = base64.urlsafe_b64decode(enc).decode()
  32. for i in range(len(enc)):
  33. self._mac_addr_c = self._mac_addr[i % len(self._mac_addr)]
  34. dec_c = chr((256 + ord(enc[i]) - ord(self._mac_addr_c)) % 256)
  35. dec.append(dec_c)
  36. return "".join(dec)
  37.  
  38. def _encoding(self, value):
  39. """Returns encoded string"""
  40. return self._encode(value)
  41.  
  42. def _decoding(self, value):
  43. """Returns decoded string"""
  44. return self._decode(value)
  45.  
  46.  
  47. class FileHandler(Cryptograph):
  48. def __init__(self):
  49. Cryptograph.__init__(self)
  50.  
  51. # For some reason try/except FileNotFound doesn't work, so we do it manually.
  52. check_file = os.path.isfile("user_settings.json")
  53. if check_file:
  54. self._filename = "user_settings.json"
  55. else:
  56. self._filename = os.path.join(os.pardir, "user_settings.json")
  57.  
  58. self._data = self._read_conf()
  59.  
  60. def _read_conf(self):
  61. with open(self._filename, "r") as file:
  62. self._data = json.load(file)
  63. return self._data
  64.  
  65. def _write_conf(self, data):
  66. with open(self._filename, "w") as file:
  67. json.dump(data, file, indent=2)
  68.  
  69. def save_ilo_path(self, path):
  70. self._data["reboot"]["ilo_location"] = self._encoding(path)
  71. self._write_conf(self._data)
  72.  
  73. def read_ilo_path(self):
  74. try:
  75. return str(self._decoding(self._data["reboot"]["ilo_location"]))
  76. except binascii.Error:
  77. print("Encoding iLO Installation Path is invalid!\n"
  78. "Please provide it again for re-encryption.")
  79. self.save_ilo_path(input())
  80.  
  81. def save_ilo_password(self, pwd):
  82. self._data["reboot"]["password"] = self._encoding(pwd)
  83. self._write_conf(self._data)
  84.  
  85. def read_ilo_password(self):
  86. try:
  87. return str(self._decoding(self._data["reboot"]["password"]))
  88. except binascii.Error:
  89. print("Invalid Password encoding!\n"
  90. "Please provide it again for re-encryption.")
  91. self.save_ilo_password(input())
  92.  
  93.  
  94. class SleepTimers(FileHandler):
  95. def __init__(self):
  96. FileHandler.__init__(self)
  97. self._data = self._read_conf()
  98.  
  99. def save_sleep_timer(self, key, value):
  100. self._data["reboot"]["sleep_timers"][key] = int(value)
  101. self._write_conf(self._data)
  102.  
  103. def read_sleep_timer(self, key):
  104. return int(self._data["reboot"]["sleep_timers"][key])
  105.  
  106.  
  107. class GetDisplayData:
  108. def __init__(self):
  109. self._display_count = int()
  110. self._primary_display_x, self._primary_display_y = self._get_primary_display_size
  111.  
  112. @property
  113. def get_display_count(self):
  114. self._display_count = len(win32api.EnumDisplayMonitors())
  115. return self._display_count
  116.  
  117. @property
  118. def _get_primary_display_size(self):
  119. return pyautogui.size()
  120.  
  121.  
  122. class ClickCords(FileHandler, GetDisplayData):
  123. def __init__(self):
  124. FileHandler.__init__(self)
  125. GetDisplayData.__init__(self)
  126. self._data = self._read_conf()
  127.  
  128. def which_display(self, cursor_x):
  129. """
  130. This function tries to figure out the setup of the dual display setup of a PC.
  131. Should a PC use more than 3 screens, default everything to display 1.
  132. Challenges:
  133. - We only know screen resolution of screen 1 (Primary).
  134. - We can't figure out layouts of 3 ore more displays.
  135. - No way to get all screen sizes or zoom level.
  136. Logic Implementation:
  137. LOGIC 1: Cursor X-pos smaller than Primary Xsize
  138. LOGIC 2: Cursor X-pos smaller than x=0
  139. What We know:
  140. - Primary Display will always start from tuple(x=0, y=0)
  141. - If Secondary is on LEFT side of Primary, it's x and y will always be negative.
  142. - If Secondary is on RIGHT side of Primary, it's x and y will always be greater than Primary X and Y
  143. DIAGRAM(s):
  144. Display SETUP: Primary | Secondary
  145. 0,0 Primary max(X) Secondary max(x)
  146. |----------------------|----------------------|>
  147. ^ ^
  148. | |
  149. We know this We don't know this.
  150. Display SETUP: Secondary | Primary
  151. Secondary max(-x) (0, 0) Primary max(x)
  152. <|----------------------|----------------------|>
  153. ^ ^
  154. | |
  155. We don't know this. We know this
  156. :param cursor_x: Current Cursor X-position.
  157. :return: String of display1 or display2.
  158. """
  159. if self.get_display_count > 2:
  160. return "display1"
  161. else:
  162. if cursor_x > self._primary_display_x:
  163. return "display2"
  164. elif cursor_x < int(0):
  165. return "display2"
  166. elif 0 < cursor_x <= self._primary_display_x:
  167. return "display1"
  168.  
  169. def save_click_cords(self, display, key, value):
  170. self._data["reboot"]["click_cords"][display][key] = value
  171. self._write_conf(self._data)
  172.  
  173. def read_click_cords(self, display, key):
  174. return self._data["reboot"]["click_cords"][display][key]
  175.  
  176.  
  177. class UserConfigurator(SleepTimers, ClickCords):
  178. def __init__(self):
  179. SleepTimers.__init__(self)
  180. ClickCords.__init__(self)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement