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
"""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
@ -12,13 +20,13 @@ if debug:
config_path_dir = '/tmp'
else:
config_path_dir = './instance'
config_filename = f"{config_path_dir}/config.ini"
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())
print('Sections found in the config file: ', config.sections())
if 'create-homedir' in config.sections():
for key in config['create-homedir']:
if debug:
@ -36,19 +44,24 @@ if exists(config_filename):
else:
raise SystemExit('Error: no config file found.')
def make_homedir(dir_path):
"""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("Creation of the directory %s failed" % dir_path), 500
return(f'Creation of the directory {dir_path} failed'), 500
else:
return("Directory %s created successfully" % dir_path), 200
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):
"""Initialize the service and start it.
"""
# create and configure the app
create_home = Flask(__name__, instance_relative_config=True)
# ensure the instance folder exists
@ -57,12 +70,11 @@ def create_app(test_config=None):
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}")
return make_homedir(f'{DIR_PREFIX}/{username}')
else:
return "Nosey, aren't you?"