Cleaner code, start/stop domain by user, user directories creations

This commit is contained in:
Cyrille L 2023-09-29 16:51:41 +02:00
parent 2bcda76cd0
commit 26d18ac4b8
14 changed files with 100 additions and 70 deletions

View File

@ -9,6 +9,14 @@ Tyto - Littérateur
# CURRENTLY IN DEV ! # CURRENTLY IN DEV !
## [1.9.10]
- cleaner code
- start/stop domain by user
- User directories check/create when domain activated
## [1.9.9]
- cleaner code with new check/update domain process
## [1.9.8] ## [1.9.8]
- Check/create/update a domain is ready - Check/create/update a domain is ready

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Version: 1.9.9 # Version: 1.9.10
# Updated: 2023-09-29 1695992721 # Updated: 2023-09-29 1695998917
# Tyto - Littérateur # Tyto - Littérateur
# Copyright (C) 2023 Cyrille Louarn <echolib+tyto@a-lec.org> # Copyright (C) 2023 Cyrille Louarn <echolib+tyto@a-lec.org>
@ -41,6 +41,37 @@
# file program : # file program :
#-------------------------- #--------------------------
import os
#===============#
# Error message #
#---------------#
def error_message(path):
print("! Installation error, unused:", path)
sys.exit(1)
#======================================================#
# A little checker to be sure, all files are installed #
#------------------------------------------------------#
def check_install():
if not os.path.exists(libs): error_message(libs)
if not os.path.exists(trfs): error_message(trfs)
for f in prog_files:
f = os.path.join(libs, f + ".py")
if not os.path.exists(f):
error_message(f)
# Only default lang files
for f in lang_files:
f = os.path.join(trfs, f + ".py")
if not os.path.exists(f):
error_message(f)
#======#======================================================================= #======#=======================================================================
# MAIN # # MAIN #
#======# #======#
@ -49,9 +80,35 @@ if not __name__ == "__main__":
print("! Error: '%s' not '%s'"%(__name__, "__main__")) print("! Error: '%s' not '%s'"%(__name__, "__main__"))
sys.exit(1) sys.exit(1)
# files list in /program/
prog_files = {
"args",
"check",
"debug",
"domain",
"forms",
"help",
"langs",
"new",
"post",
"show",
"tools",
"tyto",
"userset"
}
lang_files = {
"logs_en",
"logs_fr",
"website_en",
"website_fr"
}
# Set librairies to import app files # Set librairies to import app files
libs = "/var/lib/tyto/program" libs = "/var/lib/tyto/program"
trfs = "/var/lib/tyto/translations" trfs = "/var/lib/tyto/translations"
check_install()
sys.path.insert(0, libs) sys.path.insert(0, libs)
sys.path.insert(0, trfs) sys.path.insert(0, trfs)

View File

@ -42,10 +42,9 @@ import domain, langs, debug, post, tools
# Create user work domain directories # # Create user work domain directories #
#-------------------------------------------# #-------------------------------------------#
def manage(action, target): def manage(action, target):
# Load domain configuration (update it if needed), check if valid # Load configuration and website lang
#domain.cf_valid() or debug.out(105, domain.name, domain.cf_uri, True, 1, True)
domain.cf_update_values(False) domain.cf_update_values(False)
domain.ready()
target.endswith(".tyto") and post.is_article(target) target.endswith(".tyto") and post.is_article(target)
langs.load_website_lang()

View File

@ -105,6 +105,7 @@ def out(nbr, var, val, show, color, stop):
206 : langs.logs.created_file, 206 : langs.logs.created_file,
207 : langs.logs.updated_file, 207 : langs.logs.updated_file,
208 : langs.logs.website_lang, 208 : langs.logs.website_lang,
209 : langs.logs.domain_on,
255 : langs.logs.later, 255 : langs.logs.later,
} }

View File

@ -545,61 +545,41 @@ def cf_update_values(write):
ult_dlf.write(f) ult_dlf.write(f)
#=============================================#
# Ensure domain is valid and ready to be used #
#---------------------------------------------#
def cf_valid():
global active
if not tools.compare_ini_ft(cf_uri, tyto.ini_domain):
cf_update_values(False)
# Check if cf need to be updated
ult_cf_load()
ult_cf_hash = ult_cf.get("DOMAIN", "hash")
now_cf_hash = tools.get_filesum(cf_uri, True)
if now_cf_hash != ult_cf_hash:
cf_update_values(False)
active = is_active()
active and create_wrk_dirs()
return active
#========================================# #========================================#
# When user wants to (de)activate domain # # When user wants to (de)activate domain #
# Update key value and ensure conf is ok # #
# if activated, check/create wrk dirs #
#----------------------------------------# #----------------------------------------#
def userset_status(action): def userset_status(action):
do = { do = {
"start" : "yes", "start" : "yes",
"stop" : "no" "stop" : "no"
} }
tools.update_ini_file(cf_uri, "DOMAIN", "activated", do[action]) tools.update_ini_file(cf_uri, "DOMAIN", "activated", do[action])
cf_valid() cf_update_values(False)
ready()
if action == "start":
#==============================# status = cf.get("DOMAIN", "activated")
# Check if domain is activated # debug.out(209, "[DOMAIN] activated = %s"%status, cf_uri, True, 0, False)
# Return True or False
#------------------------------#
def is_active():
cf_load()
# Domain activated ?
try: return cf.getboolean("DOMAIN", "activated")
except: return False
#===================================# #========================================#
# Create user domain work directory # # (After configuration file was checked) #
#-----------------------------------# # Exit if domain not activated #
def create_wrk_dirs(): # #
cf_load() # or check/create wrk directories #
#----------------------------------------#
def ready():
if not activated:
status = cf.get("DOMAIN", "activated")
debug.out(105, "[DOMAIN] activated = %s"%status, cf_uri, True, 1, True)
for key, directory in cf.items("USER_DIRS"): for key, directory in cf.items("USER_DIRS"):
tools.create_dirs(directory) tools.create_dirs(directory)
#======#======================================================================= #======#=======================================================================
# MAIN # # MAIN #

View File

@ -141,33 +141,14 @@ def update_ini_file(file_path, section, key, val):
config.read(file_path) config.read(file_path)
# New value is same as registred # New value is same as registred
if config.get(section, key) == val: try:
return if config.get(section, key) == val:
return
except:
config.add_section(section)
# Update file with new value # Update file with new value
config.set(section, key, val) config.set(section, key, val)
with open(file_path, "w") as f: with open(file_path, "w") as f:
config.write(f) config.write(f)
#==================================================#
# Compare database file with origin file template #
# Used for domain and article configuration file #
# Return True, if ini keys are same #
#--------------------------------------------------#
def compare_ini_ft(cf_uri, template):
with open(cf_uri, "r") as f:
ln = 0
lines = f.read()
lines = lines.rsplit("\n")
for key_t in template.rsplit("\n"):
ln += 1
key_t = key_t.rsplit("=")[0]
key_f = lines[ln -1].rsplit("=")[0]
if not key_t == key_f:
debug.out(9, "l=%s: %s != %s"%(ln, key_f, key_t), cf_uri, False, 2, False)
return False
return True

View File

@ -40,8 +40,10 @@ import langs, forms, domain
# Specific to action "set # # Specific to action "set #
#------------------------------------# #------------------------------------#
def manage(action, target): def manage(action, target):
# Load or Exit if no configuration
domain.cf_load()
if action == "set": if action == "set":
domain.cf_load()
do = { do = {
"title" : forms.ask_domain_title, "title" : forms.ask_domain_title,
"date" : forms.ask_domain_date, "date" : forms.ask_domain_date,
@ -52,6 +54,7 @@ def manage(action, target):
} }
do[target](True) do[target](True)
elif action in ("start", "stop") and target == "domain": elif action in ("start", "stop") and target == "domain":
domain.userset_status(action) domain.userset_status(action)

View File

@ -73,6 +73,7 @@ lang_logs_sys = "Langue des logs"
website_lang = "Langue du site web" website_lang = "Langue du site web"
domains_no = "Aucun domaine trouvé" domains_no = "Aucun domaine trouvé"
domain_found = "Domaine présent" domain_found = "Domaine présent"
domain_on = "Domaine activé"
domain_created = "Domaine déjà créé" domain_created = "Domaine déjà créé"
domain_updated = "Domaine mis à jour" domain_updated = "Domaine mis à jour"
domain_new = "Domaine créé" domain_new = "Domaine créé"