From abdd0b2760db84ca007459bd0e518b31226623d7 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Sun, 21 Aug 2022 17:34:30 +0200 Subject: [PATCH] Make use of pylint to cleanup the syntax. --- dircreate/__init__.py | 106 +++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 47 deletions(-) diff --git a/dircreate/__init__.py b/dircreate/__init__.py index ce0021f..fec8adc 100644 --- a/dircreate/__init__.py +++ b/dircreate/__init__.py @@ -1,5 +1,13 @@ #!/usr/bin/env python3 +"""This code creates a directory named after the user. + +When a user starts using one of the shinyproxy services, a home directory will +be created in the NFS server if it does not exist yet. +The home directory will be then mounted by the shinyproxy service inside the +application container. +""" + import configparser import os from os.path import exists @@ -9,61 +17,65 @@ from flask import request debug = False if debug: - config_path_dir = '/tmp' + config_path_dir = '/tmp' else: - config_path_dir = './instance' -config_filename = f"{config_path_dir}/config.ini" + config_path_dir = './instance' + config_filename = f'{config_path_dir}/config.ini' if exists(config_filename): - config = configparser.ConfigParser() - config.read(config_filename) - if debug: - print("Sections found in the config file: ", config.sections()) - if 'create-homedir' in config.sections(): - for key in config['create-homedir']: - if debug: - print('Key: ', key) - if config['create-homedir']['SECRET_KEY']: - SECRET_KEY = config['create-homedir']['SECRET_KEY'] - else: - raise SystemExit('Error: config file, no SECRET_KEY found.') - if config['create-homedir']['DIR_PREFIX']: - DIR_PREFIX = config['create-homedir']['DIR_PREFIX'] - else: - raise SystemExit('Error: config file, no DIR_PREFIX found.') - else: - raise SystemExit('Error: no create-homedir section found.') + config = configparser.ConfigParser() + config.read(config_filename) + if debug: + print('Sections found in the config file: ', config.sections()) + if 'create-homedir' in config.sections(): + for key in config['create-homedir']: + if debug: + print('Key: ', key) + if config['create-homedir']['SECRET_KEY']: + SECRET_KEY = config['create-homedir']['SECRET_KEY'] + else: + raise SystemExit('Error: config file, no SECRET_KEY found.') + if config['create-homedir']['DIR_PREFIX']: + DIR_PREFIX = config['create-homedir']['DIR_PREFIX'] + else: + raise SystemExit('Error: config file, no DIR_PREFIX found.') + else: + raise SystemExit('Error: no create-homedir section found.') else: - raise SystemExit('Error: no config file found.') + raise SystemExit('Error: no config file found.') + def make_homedir(dir_path): - if not os.path.isdir(dir_path): - permissions = 0o700 - try: - os.mkdir(dir_path, permissions) - except OSError: - return("Creation of the directory %s failed" % dir_path), 500 - else: - return("Directory %s created successfully" % dir_path), 200 + """Create the directory if it does not exist. + """ + if not os.path.isdir(dir_path): + permissions = 0o700 + try: + os.mkdir(dir_path, permissions) + except OSError: + return(f'Creation of the directory {dir_path} failed'), 500 else: - return("Directory %s already exists" % dir_path), 200 + return(f'Directory {dir_path} created successfully'), 200 + return(f'Directory {dir_path} already exists'), 200 + def create_app(test_config=None): - # create and configure the app - create_home = Flask(__name__, instance_relative_config=True) - # ensure the instance folder exists - try: - os.path.isdir(DIR_PREFIX) - except OSError: - pass + """Initialize the service and start it. + """ + # create and configure the app + create_home = Flask(__name__, instance_relative_config=True) + # ensure the instance folder exists + try: + os.path.isdir(DIR_PREFIX) + except OSError: + pass - # a simple page that says hello - @create_home.route('/', methods=['GET', 'POST']) - def makedir(): - if request.method == 'POST': - username = request.form['user'] - return make_homedir(f"{DIR_PREFIX}/{username}") - else: - return "Nosey, aren't you?" + @create_home.route('/', methods=['GET', 'POST']) + def makedir(): + if request.method == 'POST': + username = request.form['user'] + return make_homedir(f'{DIR_PREFIX}/{username}') + else: + return "Nosey, aren't you?" - return create_home + return create_home