-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_opera.py
86 lines (74 loc) · 2.49 KB
/
fix_opera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "Jan Kubovy"
__email__ = "[email protected]"
'''
Script removes default search engine from Opera so you can set any search shortcut in Opera browser.
You need to run it as Administrator
'''
import os
import stat
import glob
import shutil
FILENAME = 'default_partner_content.json'
FAKE_FILE_NAME = 'prefs_override.json'
OPERA_FOLDER_NAME = 'Opera'
OPERA_APPDATA_FOLDER_NAME = 'Opera Software'
def get_appdata_path() -> str:
path = os.getenv('APPDATA')
if OPERA_APPDATA_FOLDER_NAME in os.listdir(path):
return os.path.join(path, OPERA_APPDATA_FOLDER_NAME)
else:
raise ArgumentError('Can\'t find Opera\'s APPDATA folder')
def get_fake_file_path() -> str:
path = get_appdata_path()
file_recursive_path = os.path.join(path, '**', FAKE_FILE_NAME)
for file_path in glob.glob(file_recursive_path, recursive=True):
return file_path
raise ArgumentError('Can\'t find {0} file'.format(FAKE_FILE_NAME))
def change_base_file_content() -> None:
path = get_appdata_path()
message = 'Nothing to do'
file_recursive_path = os.path.join(path, '**', FILENAME)
fake_file_path = get_fake_file_path()
for file_path in glob.glob(file_recursive_path, recursive=True):
try:
if os.stat(file_path)[stat.ST_MODE] & stat.S_IWRITE > 0: # file is NOT set to readonly
shutil.move(file_path, file_path + '.old')
shutil.copy(fake_file_path, file_path)
os.chmod(file_path, stat.S_IREAD)
message = 'Success'
except IOError as e:
print(e)
print('Change base file:\t{0}'.format(message))
def change_backup_files() -> None:
for path_env in ['ProgramFiles(x86)', 'ProgramW6432']:
base_path = os.getenv(path_env)
path = os.path.join(base_path, OPERA_FOLDER_NAME)
change_backup_files_from_folder(path)
def change_backup_files_from_folder(folder_path: str) -> None:
message = 'Nothing to do'
file_recursive_path = os.path.join(folder_path, '**', FILENAME)
fake_file_path = get_fake_file_path()
for file_path in glob.glob(file_recursive_path, recursive=True):
try:
shutil.copy(fake_file_path, file_path)
os.chmod(file_path, stat.S_IREAD)
message = 'Success'
except OSError:
pass
print('Backup files deleted:\t{0}\t{1}'.format(message, folder_path))
def main() -> None:
change_base_file_content()
change_backup_files()
print('Finished')
if __name__ == '__main__':
try:
main()
except PermissionError as e:
print(e)
print('You need to run it as Administrator')
except ArgumentError as e:
print(e)
except Exception as e:
print(e)