Make use of pylint to cleanup the syntax.

This commit is contained in:
Andrea Dell'Amico 2022-08-21 17:34:30 +02:00
parent 65bc6f79f1
commit abdd0b2760
Signed by: andrea.dellamico
GPG Key ID: 147ABE6CEB9E20FF
1 changed files with 59 additions and 47 deletions

View File

@ -1,5 +1,13 @@
#!/usr/bin/env python3 #!/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 configparser
import os import os
from os.path import exists from os.path import exists
@ -9,61 +17,65 @@ from flask import request
debug = False debug = False
if debug: if debug:
config_path_dir = '/tmp' config_path_dir = '/tmp'
else: else:
config_path_dir = './instance' config_path_dir = './instance'
config_filename = f"{config_path_dir}/config.ini" config_filename = f'{config_path_dir}/config.ini'
if exists(config_filename): if exists(config_filename):
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(config_filename) config.read(config_filename)
if debug: if debug:
print("Sections found in the config file: ", config.sections()) print('Sections found in the config file: ', config.sections())
if 'create-homedir' in config.sections(): if 'create-homedir' in config.sections():
for key in config['create-homedir']: for key in config['create-homedir']:
if debug: if debug:
print('Key: ', key) print('Key: ', key)
if config['create-homedir']['SECRET_KEY']: if config['create-homedir']['SECRET_KEY']:
SECRET_KEY = config['create-homedir']['SECRET_KEY'] SECRET_KEY = config['create-homedir']['SECRET_KEY']
else: else:
raise SystemExit('Error: config file, no SECRET_KEY found.') raise SystemExit('Error: config file, no SECRET_KEY found.')
if config['create-homedir']['DIR_PREFIX']: if config['create-homedir']['DIR_PREFIX']:
DIR_PREFIX = config['create-homedir']['DIR_PREFIX'] DIR_PREFIX = config['create-homedir']['DIR_PREFIX']
else: else:
raise SystemExit('Error: config file, no DIR_PREFIX found.') raise SystemExit('Error: config file, no DIR_PREFIX found.')
else: else:
raise SystemExit('Error: no create-homedir section found.') raise SystemExit('Error: no create-homedir section found.')
else: else:
raise SystemExit('Error: no config file found.') raise SystemExit('Error: no config file found.')
def make_homedir(dir_path): def make_homedir(dir_path):
if not os.path.isdir(dir_path): """Create the directory if it does not exist.
permissions = 0o700 """
try: if not os.path.isdir(dir_path):
os.mkdir(dir_path, permissions) permissions = 0o700
except OSError: try:
return("Creation of the directory %s failed" % dir_path), 500 os.mkdir(dir_path, permissions)
else: except OSError:
return("Directory %s created successfully" % dir_path), 200 return(f'Creation of the directory {dir_path} failed'), 500
else: 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): def create_app(test_config=None):
# create and configure the app """Initialize the service and start it.
create_home = Flask(__name__, instance_relative_config=True) """
# ensure the instance folder exists # create and configure the app
try: create_home = Flask(__name__, instance_relative_config=True)
os.path.isdir(DIR_PREFIX) # ensure the instance folder exists
except OSError: try:
pass os.path.isdir(DIR_PREFIX)
except OSError:
pass
# a simple page that says hello @create_home.route('/', methods=['GET', 'POST'])
@create_home.route('/', methods=['GET', 'POST']) def makedir():
def makedir(): if request.method == 'POST':
if request.method == 'POST': username = request.form['user']
username = request.form['user'] return make_homedir(f'{DIR_PREFIX}/{username}')
return make_homedir(f"{DIR_PREFIX}/{username}") else:
else: return "Nosey, aren't you?"
return "Nosey, aren't you?"
return create_home return create_home