From 079dff8d9f9700ad7c4d843be9dea1834130bcd0 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Sun, 21 Jun 2020 20:21:33 +0100 Subject: [PATCH] cleanup --- jellyfin_accounts/__init__.py | 20 +++++++++---------- jellyfin_accounts/data_store.py | 4 ++++ jellyfin_accounts/jf_api.py | 27 ++++++++++++++------------ jellyfin_accounts/setup.py | 14 ++----------- jellyfin_accounts/validate_password.py | 5 ----- 5 files changed, 31 insertions(+), 39 deletions(-) diff --git a/jellyfin_accounts/__init__.py b/jellyfin_accounts/__init__.py index 6e6af33..e96e901 100755 --- a/jellyfin_accounts/__init__.py +++ b/jellyfin_accounts/__init__.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -__version__ = "0.2" +__version__ = "0.2.1" import secrets import configparser @@ -102,13 +102,13 @@ for key in ['user_configuration', 'user_displayprefs']: with open(config['files']['invites'], 'r') as f: temp_invites = json.load(f) -if 'invites' in temp_invites: +if 'invites' in temp_invites: new_invites = {} log.info('Converting invites.json to new format, temporary.') - for el in temp_invites['invites']: - i = {'valid_till': el['valid_till']} - if 'email' in el: - i['email'] = el['email'] + for el in temp_invites['invites']: + i = {'valid_till': el['valid_till']} + if 'email' in el: + i['email'] = el['email'] new_invites[el['code']] = i with open(config['files']['invites'], 'w') as f: f.write(json.dumps(new_invites, indent=4, default=str)) @@ -178,17 +178,17 @@ def main(): config['jellyfin']['device'], config['jellyfin']['device_id']) print("NOTE: This can now be done through the web ui.") - print(""" + print(""" This tool lets you grab various settings from a user, so that they can be applied every time a new account is created. """) print("Step 1: User Policy.") - print(""" - A user policy stores a users permissions (e.g access rights and + print(""" + A user policy stores a users permissions (e.g access rights and most of the other settings in the 'Profile' and 'Access' tabs of a user). """) success = False - msg = "Get public users only or all users? (requires auth) [public/all]: " + msg = "Get public users only or all users? (requires auth) [public/all]: " public = False while not success: choice = input(msg) diff --git a/jellyfin_accounts/data_store.py b/jellyfin_accounts/data_store.py index 54d5517..56a2527 100644 --- a/jellyfin_accounts/data_store.py +++ b/jellyfin_accounts/data_store.py @@ -2,6 +2,10 @@ import json import datetime class JSONFile(dict): + """ + Behaves like a dictionary, but automatically + reads and writes to a JSON file (most of the time). + """ @staticmethod def readJSON(path): try: diff --git a/jellyfin_accounts/jf_api.py b/jellyfin_accounts/jf_api.py index 08229bd..6ccf70f 100644 --- a/jellyfin_accounts/jf_api.py +++ b/jellyfin_accounts/jf_api.py @@ -66,13 +66,15 @@ class Jellyfin: "User-Agent": self.useragent, "X-Emby-Authorization": self.auth } - def getUsers(self, username="all", id="all", public=True): + def getUsers(self, username: str = "all", + userId: str = "all", + public: bool = True): """ Returns details on user(s), such as ID, Name, Policy. :param username: (optional) Username to get info about. Leave blank to get all users. - :param id: (optional) User ID to get info about. + :param userId: (optional) User ID to get info about. Leave blank to get all users. :param public: True = Get publicly visible users only (no auth required), False = Get all users (auth required). @@ -124,7 +126,8 @@ class Jellyfin: return user if not match: raise self.UserNotFoundError - def authenticate(self, username, password): + + def authenticate(self, username: str, password: str): """ Authenticates by name with Jellyfin. @@ -151,7 +154,7 @@ class Jellyfin: return True else: raise self.AuthenticationError - def setPolicy(self, userId, policy): + def setPolicy(self, userId: str, policy: dict): """ Sets a user's policy (Admin rights, Library Access, etc.) by user ID. @@ -161,7 +164,7 @@ class Jellyfin: return requests.post(self.server+"/Users/"+userId+"/Policy", headers=self.header, params=policy) - def newUser(self, username, password): + def newUser(self, username: str, password: str): for user in self.getUsers(): if user['Name'] == username: raise self.UserExistsError @@ -176,7 +179,7 @@ class Jellyfin: else: raise self.AuthenticationRequiredError return response - def getViewOrder(self, userId, public=True): + def getViewOrder(self, userId: str, public: bool = True): if not public: param = '?IncludeHidden=true' else: @@ -187,7 +190,7 @@ class Jellyfin: for library in views: orderedViews.append(library['Id']) return orderedViews - def setConfiguration(self, userId, configuration): + def setConfiguration(self, userId: str, configuration: dict): """ Sets a user's configuration (Settings the user can change themselves). :param userId: ID of the user to modify. @@ -207,17 +210,17 @@ class Jellyfin: raise self.AuthenticationRequiredError else: raise self.UnknownError - def getConfiguration(self, username="all", id="all"): + def getConfiguration(self, username: str = "all", userId: str = "all"): """ Gets a user's Configuration. This can also be found in getUsers if public is set to False. :param username: The user's username. - :param id: The user's ID. + :param userId: The user's ID. """ return self.getUsers(username=username, - id=id, + userId=userId, public=False)['Configuration'] - def getDisplayPreferences(self, userId): + def getDisplayPreferences(self, userId: str): """ Gets a user's Display Preferences (Home layout). :param userId: The user's ID. @@ -235,7 +238,7 @@ class Jellyfin: raise self.AuthenticationRequiredError else: raise self.UnknownError - def setDisplayPreferences(self, userId, preferences): + def setDisplayPreferences(self, userId: str, preferences: dict): """ Sets a user's Display Preferences (Home layout). :param userId: The user's ID. diff --git a/jellyfin_accounts/setup.py b/jellyfin_accounts/setup.py index 6ddcfd1..79dae3d 100644 --- a/jellyfin_accounts/setup.py +++ b/jellyfin_accounts/setup.py @@ -3,18 +3,10 @@ from configparser import RawConfigParser from jellyfin_accounts.jf_api import Jellyfin from jellyfin_accounts import config, config_path, app, first_run from jellyfin_accounts import web_log as log +from jellyfin_accounts.web_api import resp import os if first_run: - def resp(success=True, code=500): - if success: - r = jsonify({'success': True}) - r.status_code = 200 - else: - r = jsonify({'success': False}) - r.status_code = code - return r - def tempJF(server): return Jellyfin(server, config['jellyfin']['client'], @@ -30,7 +22,6 @@ if first_run: def setup(): return render_template('setup.html') - @app.route('/') def static_proxy(path): if 'html' not in path: @@ -38,7 +29,6 @@ if first_run: else: return render_template('404.html'), 404 - @app.route('/modifyConfig', methods=['POST']) def modifyConfig(): log.info('Config modification requested') @@ -59,10 +49,10 @@ if first_run: with open(config_path, 'w') as config_file: temp_config.write(config_file) log.debug('Config written') + # ugly exit, sorry os._exit(1) return resp() - @app.route('/testJF', methods=['GET', 'POST']) def testJF(): data = request.get_json() diff --git a/jellyfin_accounts/validate_password.py b/jellyfin_accounts/validate_password.py index eb166cf..5ab7147 100644 --- a/jellyfin_accounts/validate_password.py +++ b/jellyfin_accounts/validate_password.py @@ -42,8 +42,3 @@ class PasswordValidator: text += criterion lines[criterion] = text return lines - - - - -