File: /volume1/@appstore/SynologyPhotos/migration/python/dump_global_config.py
#!/usr/bin/env python3
import json
import logging
import sys
import pg8000
PHOTO_BOOL_CONFIG_KEYS = [
"hide_guest_lightbox_detail",
"allow_root_folder_public",
"enable_face_recognition",
]
PHOTO_STRING_CONFIG_KEYS = [
"excluding_index_format",
]
MOMENTS_TEAM_LIBRAY_KEY = "enable_team_library"
MOMENTS_PERSON_KEY = "enable_person"
MOMENTS_CONFIG_KEYS = [MOMENTS_TEAM_LIBRAY_KEY, MOMENTS_PERSON_KEY]
def init_cursor(database):
user = "postgres"
unix_sock = "/var/run/postgresql/.s.PGSQL.5432"
connected = False
try:
conn = pg8000.connect(user, unix_sock=unix_sock, database=database)
connected = True
except Exception:
return None
if connected:
cursor = conn.cursor()
cursor.execute("SET CLIENT_ENCODING TO 'UTF8'")
return cursor
def get_photo_config(cursor):
if not cursor:
return None
try:
config_keys = PHOTO_BOOL_CONFIG_KEYS + PHOTO_STRING_CONFIG_KEYS
cursor.execute(
"""
SELECT config_key, config_value
FROM photo_config
WHERE config_key IN (%s)
"""
% ",".join(["%s"] * len(config_keys)),
tuple(config_keys),
)
except Exception as err:
logging.error("Failed to get photo config, error: {}".format(err))
return None
result = dict.fromkeys(PHOTO_BOOL_CONFIG_KEYS, False)
for key in PHOTO_STRING_CONFIG_KEYS:
result[key] = ""
for key, value in cursor:
if key in PHOTO_BOOL_CONFIG_KEYS:
result[key] = True if value == "on" else False
if key in PHOTO_STRING_CONFIG_KEYS:
result[key] = value
return result
def get_moments_config(cursor):
if not cursor:
return None
try:
cursor.execute(
"""
SELECT key, value
FROM config
WHERE key IN (%s)
"""
% ",".join(["%s"] * len(MOMENTS_CONFIG_KEYS)),
tuple(MOMENTS_CONFIG_KEYS),
)
except Exception as err:
logging.error("Failed to get moments config, error: {}".format(err))
return None
# default value of config
result = {
MOMENTS_TEAM_LIBRAY_KEY: False,
MOMENTS_PERSON_KEY: True,
}
for key, value in cursor:
result[key] = True if value == "true" else False
return result
def get_moments_team_users(cursor):
if not cursor:
return None
try:
cursor.execute(
"""
SELECT uid, name
FROM user_info
WHERE id IN (
SELECT id_user_info FROM many_team_library_has_many_user_info where permission = 1
)
ORDER BY uid
"""
)
except Exception as err:
logging.error("Failed to get moments team users, error: {}".format(err))
return None
result = []
for uid, name in cursor:
result.append(
{
"uid": uid,
"name": name,
}
)
return result
def main():
cursor_photo = init_cursor("photo")
cursor_moments = init_cursor("synophoto")
config = {
"photo_config": get_photo_config(cursor_photo),
"moments_config": get_moments_config(cursor_moments),
"moments_team_users": get_moments_team_users(cursor_moments),
}
return config
if __name__ == "__main__":
result = main()
if len(sys.argv) >= 2:
path = sys.argv[1]
json.dump(result, open(path, "w"))
else:
print(json.dumps(result))