From 2aaa644d0a7510c7dab4f71c80effed9ec32dcd7 Mon Sep 17 00:00:00 2001 From: Cyrille L Date: Tue, 28 Feb 2023 11:21:24 +0100 Subject: [PATCH] Updated code for actions, targets. New help --- src/usr/bin/tyto | 20 ++-- src/var/lib/tyto/program/args.py | 68 +++++++----- src/var/lib/tyto/program/db.py | 170 ++++++++++++++++-------------- src/var/lib/tyto/program/infos.py | 47 +++++---- src/var/lib/tyto/program/logs.py | 1 + src/var/lib/tyto/program/new.py | 2 + src/var/lib/tyto/program/show.py | 11 +- 7 files changed, 179 insertions(+), 140 deletions(-) diff --git a/src/usr/bin/tyto b/src/usr/bin/tyto index 66f7f76..a2af7cb 100755 --- a/src/usr/bin/tyto +++ b/src/usr/bin/tyto @@ -30,17 +30,19 @@ sys.path.insert(0, '/var/lib/tyto/program') # MAIN # # Treat Arguments # #--------------------#------------------------------------------------- +import logs if not __name__ == "__main__": - print(':< Mismatch program start') - sys.exit(1) + logs.out("14", '', True) + +# Check arguments import args action = args.set_action() target = args.set_target() -import check, domain, html, new, publish, show, wip, infos # Command start argument +import check, domain, html, new, publish, show, wip, infos actions = { 'check' : check.manage_check, 'help' : infos.tyto, @@ -58,10 +60,8 @@ actions = { } -# Argument 1 -#----------- -try: - actions[action](target) -except KeyError: - print(':< No action for "%s", with "%s"'%(action, target)) - sys.exit(1) +# Start action +# Argument's Check done in args.py +#--------------------------------- +actions[action](target) + diff --git a/src/var/lib/tyto/program/args.py b/src/var/lib/tyto/program/args.py index a038ce8..ec26e56 100644 --- a/src/var/lib/tyto/program/args.py +++ b/src/var/lib/tyto/program/args.py @@ -18,39 +18,56 @@ #********************************************************************** import sys -import infos +import infos, logs # Arguments from command line # tyto [action] [target] #---------------------------- -actions = ( - 'check', - 'edit', - 'edit-db', - 'edit-wip', - 'edit-www', - 'help', - 'new', - 'show', - 'show-db', - 'show-wip', - 'show-www', - 'wip', - 'publish' - ) +actions = \ +( + 'check', + 'edit', + 'edit-db', + 'edit-wip', + 'edit-www', + 'help', + 'new', + 'show', + 'show-db', + 'show-wip', + 'show-www', + 'wip', + 'publish' +) pass_actions = ( 'new' ) -pass_targets = ( - 'domain', - 'footer', - 'metas', - 'navbar', - 'sidebar' - ) +# Actions that needs to check for article's database +pass_db = \ +( + 'check', + 'edit-db', + 'edit-wip', + 'edit-www', + 'publish', + 'show-db', + 'show-wip', + 'show-www', + 'wip', + +) + +pass_targets = \ +( + 'domain', + 'footer', + 'metas', + 'navbar', + 'sidebar' +) action = '' target = '' @@ -61,13 +78,14 @@ noaction = False try: action = sys.argv[1] except: noaction = True +# With no argument, show help if noaction: infos.tyto(target) sys.exit(0) +# Unused argument [action] if not action in actions: - print(':< Unused [action] argument: "%s"'%action) - sys.exit(1) + logs.out("11", action, True) # target diff --git a/src/var/lib/tyto/program/db.py b/src/var/lib/tyto/program/db.py index 8629f42..b721f15 100644 --- a/src/var/lib/tyto/program/db.py +++ b/src/var/lib/tyto/program/db.py @@ -18,7 +18,7 @@ #********************************************************************** import os, sys -import args, logs, tyto +import args, logs, tyto, domain action = args.action target = args.target @@ -45,62 +45,69 @@ domain_conf = in_dir # Settings for domain, check if db is not corrupted -domain_values = ( - 'domain_dir', - 'domain_conf', - 'domain_articles', - 'domain_files', - 'domain_images', - 'articles_db', - 'html_db', - 'navbars_dir', - 'navbar_load', - 'navbar_load_db', - 'sidebar_load', - 'sidebar_load_db', - 'metas_load', - 'metas_load_db', - 'footer_load', - 'footer_load_db', - 'domain_short', - 'domain_www_url', - 'domain_wip_url', - 'domain_title', - 'domain_about', - 'domain_lang', - 'domain_mail', - 'domain_tags', - 'domain_logo', - 'domain_license', - 'domain_licurl', - 'domain_css', - 'domain_sep', - 'domain_relme', - 'sidebar_title', - 'sidebar_items', - 'srv_root', - 'srv_domain', - 'srv_wip', - 'srv_wip_tpl', - 'srv_wip_images', - 'srv_wip_files', - 'srv_www', - 'srv_www_tpl', - 'srv_www_images', - 'srv_www_files', - 'wip_navbar', - 'wip_sidebar', - 'wip_metas', - 'wip_footer', - 'www_navbar', - 'www_sidebar', - 'www_metas', - 'www_footer', - 'domain_active', - ) +domain_values = \ +( + 'domain_dir', + 'domain_conf', + 'domain_articles', + 'domain_files', + 'domain_images', + 'articles_db', + 'html_db', + 'navbars_dir', + 'navbar_load', + 'navbar_load_db', + 'sidebar_load', + 'sidebar_load_db', + 'metas_load', + 'metas_load_db', + 'footer_load', + 'footer_load_db', + 'domain_short', + 'domain_www_url', + 'domain_wip_url', + 'domain_title', + 'domain_about', + 'domain_lang', + 'domain_mail', + 'domain_tags', + 'domain_logo', + 'domain_license', + 'domain_licurl', + 'domain_css', + 'domain_sep', + 'domain_relme', + 'sidebar_title', + 'sidebar_items', + 'srv_root', + 'srv_domain', + 'srv_wip', + 'srv_wip_tpl', + 'srv_wip_images', + 'srv_wip_files', + 'srv_www', + 'srv_www_tpl', + 'srv_www_images', + 'srv_www_files', + 'wip_navbar', + 'wip_sidebar', + 'wip_metas', + 'wip_footer', + 'www_navbar', + 'www_sidebar', + 'www_metas', + 'www_footer', + 'domain_active', +) # If error in DB, continue process for these options -domain_pass_args = ('new', 'edit', 'show', '') +domain_pass_args = \ +( + 'new', + 'edit', + 'show', + '' +) # Search and set domain conf file # from current directory or from argument @@ -138,9 +145,9 @@ if domain_exists: try: domain_show except: - if incomplete_domain: logs.out("41", domain_short, False) - elif domain_active: logs.out("42", domain_short, False) - else: logs.out("40", domain_short, False) + if incomplete_domain: logs.out("41", domain_short, False) + elif domain_active: logs.out("42", domain_short, False) + else: logs.out("40", domain_short, False) domain_show = True else: @@ -151,34 +158,37 @@ else: #------------------# # ARTICLE DATABASE # #------------------# -#==================# +#================================# # Get post DB from target # # Get some post settings fom uri # #--------------------------------# -if args.target and not args.target in args.pass_targets: - if not action in args.pass_actions: - uri_file = '%s/%s'%(in_dir, args.target) - if not os.path.exists(uri_file): - logs.out("1", uri_file, True) +# Conditions from command line +if args.target \ + and args.action in args.pass_db \ + and not args.target in args.pass_targets: - # Get hash for uri and content file - post_exists = True - uri_id = tyto.get_filesum(uri_file, False) - hash_post = tyto.get_filesum(uri_file, True) + uri_file = '%s/%s'%(in_dir, args.target) + if not os.path.exists(uri_file): logs.out("1", uri_file, True) + + # Get hash for uri and content file + post_exists = True + uri_id = tyto.get_filesum(uri_file, False) + hash_post = tyto.get_filesum(uri_file, True) + # Domain must be valid + domain.domain_needed() - if domain_exists and not incomplete_domain: - # Set DB file for this post - post_db = '%s%s.conf'%(articles_db, uri_id) - if not os.path.exists(post_db): - db_exists = False - else: - if os.stat(post_db).st_size < 1000: - os.remove(post_db) - logs.out('23', post_db, False) - db_exists = False - else: - db_exists = True + # Set DB file for this post + post_db = '%s%s.conf'%(articles_db, uri_id) + if not os.path.exists(post_db): + db_exists = False + else: + if os.stat(post_db).st_size < 1000: + os.remove(post_db) + logs.out('23', post_db, False) + db_exists = False + else: + db_exists = True # Load post database if db_exists: diff --git a/src/var/lib/tyto/program/infos.py b/src/var/lib/tyto/program/infos.py index 518611d..c42fe06 100644 --- a/src/var/lib/tyto/program/infos.py +++ b/src/var/lib/tyto/program/infos.py @@ -55,30 +55,37 @@ def tyto(target): print( '\nUsage: tyto [action] [target] \n' ' After created domain (tyto new domain [URL]),\n' - ' create article\'s file in articles/ directory,\n' - ' do in this order on [target] file: Check > wip > publish\n\n' + ' create article\'s file in articles/ directory.\n' + ' Use this actions on article\'s file: Check > wip > publish\n' + ' Config\'s files are in _configs/ directory,\n' + ' and used to create HTML file in your template.\n\n' '- [action] according to [target]:\n' - ' edit : Edit article\'s source file\n' - ' edit-db : Edit post or domain database\n' + ' edit : Edit article\'s file\n' + ' edit-db : Edit article\'s database (TAKE CARE !)\n' ' edit-wip: Edit html file in wip server\n' ' edit_www: Edit html file in www server\n' - ' new : Create new (domain, sidebar/footer... config file)\n' - ' show : Show content of source file\n' - ' show-db : Show content of post or domain database\n' - ' show-wip: Show html file in wip server\n' - ' show-www: Show html file in www server\n\n' + ' new : Create new (domain, sidebar/footer... config\'s file)\n' + ' show : Show content of config\'s file\n' + ' show-db : Show content of article\'s database\n' + ' show-wip: Show HTML file in wip server\n' + ' show-www: Show HTML file in www server\n\n' ' check : Check if article is valid\n' ' wip : Create page in wip server\n' ' publish : Create page in www server\n\n' - '- [target] according to [action]\n' + \ - ' [file] : File uri of an article\n' - ' footer : Create footer config/HTML code file\n' - ' metas : Create metas config/HTML code file\n' - ' navbar : Create navbar config/HTML code file\n' - ' sidebar : Create sidebar config/HTML code file\n\n' - '# Examples:\n' - ' tyto new sidebar: create default source file /_configs/tyto.sidebar\n' - ' tyto edit navbar: edit file: /_configs/tyto.navbar\n' - ' tyto wip sidebar: create sidebar.html in wip server\n' - ' tyto publish navbar: create navbar.html in www server' + '- [target] according to [action]\n' + ' domain : Create/Show config file\n' + ' [file] : URI of an article\'s file (autocompletion is your friend)\n' + ' footer : Create/Show footer HTML config\'s file\n' + ' metas : Create/Show metas HTML config\'s file\n' + ' navbar : Create/Show navbar config\'s file\n' + ' sidebar : Create/Show sidebar config\'s file\n\n' + 'Examples:\n' + '- Check article\'s syntax: tyto check mysubdir/index.tyto\n' + '- Create default _configs/tyto.sidebar: tyto new sidebar\n' + '- Edit _configs/tyto.navbar: tyto edit navbar\n' + '- Edit index.html in www server: tyto edit-www index.tyto\n' + '- Create sidebar.html in wip server: tyto wip sidebar\n' + '- Show sidebar.html in wip server: tyto show-wip sidebar\n' + '- Create navbar.html in www server: tyto publish navbar\n' + '- Show navbar.html in www server: tyto show-www navbar\n' ) diff --git a/src/var/lib/tyto/program/logs.py b/src/var/lib/tyto/program/logs.py index 1b00544..9606b59 100644 --- a/src/var/lib/tyto/program/logs.py +++ b/src/var/lib/tyto/program/logs.py @@ -43,6 +43,7 @@ def out(nbr, value, out): '11' : ':< %sUnused argument%s: %s'%(CR, CS, value), '12' : ':< %sUnused "%s"%s in article\'s header'%(CR, value, CS), '13' : ':< %sNo file or directory%s here (deleted ?)'%(CR, CS), + '14' : ':< %sMismatch%s program start'%(CR, CS), '19' : ':D Article %swip%s on: %s'%(CG, CS, value), '20' : ':D Article %scheck%s on: %s'%(CG, CS, value), '21' : ':D %sValid%s article. Ready to wip'%(CG, CS), diff --git a/src/var/lib/tyto/program/new.py b/src/var/lib/tyto/program/new.py index f6cab67..a5b1b90 100644 --- a/src/var/lib/tyto/program/new.py +++ b/src/var/lib/tyto/program/new.py @@ -42,3 +42,5 @@ def manage_new(target): } actions[target](option) + else: + logs.out("11", '"%s" with "%s"'%(target, option), True) diff --git a/src/var/lib/tyto/program/show.py b/src/var/lib/tyto/program/show.py index 4e916cb..cbed2c0 100644 --- a/src/var/lib/tyto/program/show.py +++ b/src/var/lib/tyto/program/show.py @@ -18,7 +18,7 @@ #********************************************************************** import os, importlib -import args, db, logs, tyto, check +import args, db, logs, domain, tyto, check #======================# @@ -30,7 +30,8 @@ import args, db, logs, tyto, check #----------------------# def manage_show(target): # Domain configuration must exists - if not db.domain_exists: sys.exit(43) + #if not db.domain_exists: sys.exit(43) + domain.domain_needed() do = False actions_read = ('show', 'show-db', 'show-wip', 'show-www') @@ -87,10 +88,9 @@ def manage_show(target): # Post has database elif db.db_exists: do = { - "db" : db.post_db, + "db" : db.post_db, "wip" : db.post_wip, "www" : db.post_www - } if args.action in actions_wip: target = "wip" elif args.action in actions_www: target = "www" @@ -98,7 +98,8 @@ def manage_show(target): #print('> show: target', target) - if not do: logs.out("28", 'yet', True) + if not do: + logs.out("11", '"%s" with "%s"'%(args.target, args.action), True) # Read lines of, or edit file if args.action in actions_read: read_lines(do[target])