diff --git a/src/usr/bin/tyto b/src/usr/bin/tyto index 35cccb2..6c23b5a 100755 --- a/src/usr/bin/tyto +++ b/src/usr/bin/tyto @@ -35,20 +35,24 @@ if not __name__ == "__main__": sys.exit(1) import args -import check, domain, wip, html, publish +action = args.set_action() +target = args.set_target() -noarg = False -target = '' -option = '' +import check, domain, html, new, publish, show, wip # Command start argument actions = { 'check' : check.manage_check, 'domain' : domain.manage_domain, + 'edit' : show.manage_show, + 'editdb' : show.manage_show, 'footer' : html.manage_configs, 'metas' : html.manage_configs, 'navbar' : html.manage_configs, + 'new' : new.manage_new, 'publish' : publish.manage_publish, + 'show' : show.manage_show, + 'showdb' : show.manage_show, 'sidebar' : html.manage_configs, 'wip' : wip.manage_wip } @@ -57,7 +61,7 @@ actions = { # Argument 1 #----------- try: - actions[sys.argv[1]](args.target, args.option) + actions[action](target) except KeyError: - print(':< No action for', sys.argv[1]) + print(':< No action for "%s", with "%s"'%(action, target)) sys.exit(1) diff --git a/src/var/lib/tyto/program/args.py b/src/var/lib/tyto/program/args.py index d401a62..fd63acb 100644 --- a/src/var/lib/tyto/program/args.py +++ b/src/var/lib/tyto/program/args.py @@ -25,67 +25,53 @@ import sys actions = ( 'check', 'domain', - 'footer', - 'metas', - 'navbar', + 'edit', + 'editdb', + 'new', + 'show', + 'showdb', 'wip', - 'publish', - 'sidebar' + 'publish' ) -# Dict for Options -options = { - '-a' : 'Add', 'add' : 'Add', - '-c' : 'Create', 'create' : 'Create', - '-d' : 'DB', 'db' : 'DB', - '-e' : "Edit", 'edit' : "Edit", - '-F' : "Force", 'force' : "Force", - '-n' : "New", 'new' : "New", - '-R' : "Remove", 'remove' : "Remove", - '-s' : "Show", 'show' : 'Show', - 'again' : 'Again' - } - -# Second argument in command -#--------------------------- -arguments = ( - '-a', 'add', - '-c', 'create', - '-d', 'db', - '-e', 'edit', - '-F', 'force', - '-n', 'new', - '-R', 'remove', - '-s', 'show', - 'again' - ) - - pass_actions = ( - 'domain' + 'new' ) -option = '' +pass_targets = ( + 'domain', + 'footer', + 'metas', + 'navbar', + 'sidebar' + ) + +action = '' target = '' +# action +#------- try: sys.argv[1] + action = sys.argv[1] except: print(':< Needed [action] argument') sys.exit(1) -if not sys.argv[1] in actions: +if not action in actions: print(':< Unused [action] argument: "%s"'%sys.argv[1]) sys.exit(1) -# Check and set arguments from command line -#------------------------------------------ -for i in range(2,4): - try: - sys.argv[i] - if sys.argv[i] in arguments: - option = options[sys.argv[i]] - else: - target = sys.argv[i] - except: - continue +# target +#------- +try: + sys.argv[2] + target = sys.argv[2] +except: + target = '' + +def set_action(): + return(action) + +def set_target(): + return(target) diff --git a/src/var/lib/tyto/program/check.py b/src/var/lib/tyto/program/check.py index 0d1282e..6f84f15 100644 --- a/src/var/lib/tyto/program/check.py +++ b/src/var/lib/tyto/program/check.py @@ -31,58 +31,24 @@ post_err = False # Manage Argument 'check' # # Start checking article # #-------------------------#-------------------------------------------- -def manage_check(target, option): +def manage_check(target): # target needed if not target: logs.out("5", '', True) - - # Manage option - #-------------- - if option == 'Edit': - curr_hash = tyto.get_filesum(db.uri_file, True) - tyto.edit_file(db.uri_file) - new_hash = tyto.get_filesum(db.uri_file, True) - if curr_hash != new_hash: - ask = '' - try: - ask = input('-> Check your changes ? ') - except KeyboardInterrupt: - print('') - logs.out("255", '', True) - if not ask in ['y', 'Y']: - logs.out("255", '', True) - else: - option = 'Force' - - elif option == 'Show': - article_raw = open(db.uri_file).read() - for line in article_raw.rsplit('\n'): - print(line) - return - - # Just read the DB from command - elif option == 'DB': - if db.db_exists: - article_db = open(db.post_db).read() - for line in article_db.rsplit('\n'): - print(line) - return - else: - logs.out("25", db.uri_file, True) + + global date_wip, hash_wip, date_www, hash_www + date_wip = hash_wip = date_www = hash_www = '' # Article has DB if db.db_exists: # ... but domain needs to be active and ready if not db.domain_active: sys.exit(1) if db.incomplete_domain: sys.exit(1) - - # ... was already check and not changed - if db.hash_chk == db.hash_post and not option == 'Force': - logs.out("20", db.date_chk, True) - # ... Set values for wip and www from DB - global date_wip, hash_wip, date_www, hash_www - date_wip = hash_wip = date_www = hash_www = '' + # ... was already check and not changed + if db.hash_chk == db.hash_post: + logs.out("20", db.date_chk, True) + # ... Set values for wip and www from DB date_wip = db.date_wip hash_wip = db.hash_wip date_www = db.date_www @@ -140,7 +106,6 @@ def manage_check(target, option): # Count words in article. Quotes, block-codes, icode = 1 per each post_words = len(article_bottom.strip().split(" ")) - # Check for valid contents check_content(post_bottom) post_bottom = article_bottom.rsplit('\n') @@ -148,7 +113,10 @@ def manage_check(target, option): check_headers(post_header) # Exit if unused needed tags - if post_err: logs.out("7", '', True) + if post_err: + if db.db_exists and os.path.exists(db.post_db): + os.remove(db.post_db) + logs.out("7", '', True) # No error create_database() @@ -214,8 +182,8 @@ def check_icodes(article): icode_m2 = line.count(tyto.words_tags[9][1]) if icode_m1 != icode_m2: logs.out("8", 'inline-code: line %s. %s %s'%(ln, - tyto.words_tags[9][0], tyto.words_tags[9][1] - ), True + tyto.words_tags[9][0], tyto.words_tags[9][1] + ), True ) @@ -307,6 +275,7 @@ def check_headers(post_header): for tag in need_headers: if not need_headers[tag]: logs_out("6", tag, False) + post_err = True ### @@ -315,11 +284,9 @@ def check_headers(post_header): #-------------------------------------# for ln, line in enumerate(post_header, 1): if line.startswith('-----'): break - - + # Set each optional tags #----------------------- - # ABBR #----- tag = tyto.headers[8] # abbr: @@ -351,7 +318,7 @@ def check_headers(post_header): if not post_err: web_link = '%s'%( - 'abbr', abbr_long, abbr_alt + 'abbr', abbr_long, abbr_alt ) globals()['abbr_%s'%stat_abbrs] = ( '!%s'%abbr_short, web_link @@ -406,7 +373,7 @@ def check_headers(post_header): logs.out("2", "Line %s (Name, %s)"%(ln, tag), False) post_err = True if not isin(r'\b_%s%s\b'%(tag, image_name), post_bottom): - logs.out("6", "_%s%s"%(tag, image_name), False) + logs.out("6", '_%s%s'%(tag, image_name), False) post_err = True image_uri = post_header[ln].lstrip() @@ -466,9 +433,9 @@ def check_headers(post_header): if not post_err: globals()['raw_%s'%stat_raws] = ( - '_%s%s'%(tag, raw_name), - web_uri, - raw_alt + '_%s%s'%(tag, raw_name), + web_uri, + raw_alt ) # FILE @@ -514,6 +481,7 @@ def check_headers(post_header): # snpic (set image to share to social networks) #---------------------------------------------- if snpic_name: continue + snpic_url = '%s/template/%s'%(db.domain_www_url, db.domain_logo) tag = tyto.headers[11] # snpic: if line.startswith(tag): @@ -527,11 +495,8 @@ def check_headers(post_header): snpic_url = '%s%s'%(db.domain_www_url, web_uri) break if not snshare: - logs.out("12", '%s %s'%(tyto.headers[6], snpic_name), True) - - # Exit if error in optional tags - #------------------------------- - if post_err: logs.out("7", '', True) + logs.out("12", '%s %s'%(tyto.headers[6], snpic_name), False) + post_err = True #================================# @@ -675,7 +640,9 @@ def check_content(post_bottom): set_tags = (ptag_set) if match: continue - else: logs.out("12", "%s %s"%(htag, ptag_set), True) + else: + logs.out("12", "%s %s"%(htag, ptag_set), False) + post_err = True # Template Tags (warning for not paired symbols) @@ -686,7 +653,7 @@ def check_content(post_bottom): if tpl1 != tpl2: logs.out("22", '"%s", "%s"'%(tag[0], tag[1]), False) - + post_err = True #===============================================# diff --git a/src/var/lib/tyto/program/db.py b/src/var/lib/tyto/program/db.py index 66be81d..82b2971 100644 --- a/src/var/lib/tyto/program/db.py +++ b/src/var/lib/tyto/program/db.py @@ -20,17 +20,29 @@ import os, sys import args, logs, tyto +action = args.action +target = args.target + + +#======================# +#----------------------# +# DOMAIN Configuration # +#----------------------# +#======================# # Settings #--------- domain_active = False +incomplete_domain = False +db_exists = False +post_exists = False + home_dir = os.path.expanduser('~') -try: - in_dir = os.getcwd() -except: - logs.out("13", '', False) + +try: in_dir = os.getcwd() +except: logs.out("13", '', False) domain_conf = in_dir -incomplete_domain = False + # Settings for domain, check if db is not corrupted domain_values = ( @@ -93,15 +105,8 @@ domain_pass_args = ('New', 'Edit', 'Show', '') # Search and set domain conf file # from current directory or from argument #---------------------------------------- -if '/articles' in in_dir: - domain_conf = in_dir.rsplit('/articles')[0] -else: - try: - args.target - if 'articles/' in args.arguments: - domain_conf = in_dir.rsplit('articles/')[0] - except: - domain_conf = in_dir +if '/articles' in in_dir: domain_conf = in_dir.rsplit('/articles')[0] +else: domain_conf = in_dir conf_dir = domain_conf + "/" domain_conf = '%s/tyto_domain.conf'%domain_conf @@ -109,54 +114,57 @@ domain_conf = '%s/tyto_domain.conf'%domain_conf if os.path.exists(domain_conf): domain_exists = True else: domain_exists = False - -#====================================================================== -#------------------------# -# Check if domain is set # -#------------------------# -#====================================================================== stdout = '%s %s domain "%s" in %s' # Show domain status if domain_exists: exec(open(domain_conf).read()) + # Check if domain conf is valid for conf in domain_values: try: eval(str(conf)) except: incomplete_domain = True - logs.out('10', conf, False) + logs.out("10", conf, False) - # Stop process if error, continue if '-n' - if not args.option in domain_pass_args: - if incomplete_domain: - sys.exit(1) + # Stop process if error, continue if 'new' + if not action in args.pass_actions: + if incomplete_domain: sys.exit(1) try: domain_active except: domain_active = 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) + # Internal process. No need to show when import reloaded + 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) + domain_show = True + else: logs.out("43", domain_conf, False) + domain_show = True - -#====================================================================== -#--------------------------------# +#==================# +#------------------# +# ARTICLE DATABASE # +#------------------# +#==================# # Get post DB from target # # Get some post settings fom uri # #--------------------------------# -#====================================================================== -if args.target: - if not sys.argv[1] in args.pass_actions: +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) # Get hash for uri and content file - uri_id = tyto.get_filesum(uri_file, False) - hash_post = tyto.get_filesum(uri_file, True) + post_exists = True + uri_id = tyto.get_filesum(uri_file, False) + hash_post = tyto.get_filesum(uri_file, True) if domain_exists and not incomplete_domain: @@ -175,4 +183,4 @@ if args.target: # Load post database if db_exists: exec(open(post_db).read(),globals()) - + diff --git a/src/var/lib/tyto/program/domain.py b/src/var/lib/tyto/program/domain.py index b26bec3..82b1307 100644 --- a/src/var/lib/tyto/program/domain.py +++ b/src/var/lib/tyto/program/domain.py @@ -17,63 +17,42 @@ #********************************************************************** -import os, sys, locale +import os, sys, locale, importlib import logs, db, tyto, html #==========================# # Manage Argument 'domain' # +# target: 3rd argument # #--------------------------# -def manage_domain(target, option): - if option == 'Edit': - if db.domain_exists: - print(":D Edit domain configuration file:", db.domain_conf) - tyto.edit_file(db.domain_conf) - else: - sys.exit(1) - - elif option == 'New': - if not db.domain_exists: - create_domain(target, option) - else: - if not db.incomplete_domain: - ask = input(' ├ Edit the domain configuration with the form ? ') - if ask in ['y', 'Y']: create_domain(target, option) - else: logs.out("255", '', True) - else: - create_domain(target, option) - - elif option == 'Show': - if db.domain_exists: - domain_db = open(db.domain_conf).read() - for line in domain_db.rsplit('\n'): - print(line) - else: - sys.exit(1) - - -#=====================# -# Check target if URL # -#---------------------# -def isurl(target): - global url - if target.startswith('http'): url = target - else: logs.out("2", target, True) +def manage_domain(target): + if not db.domain_exists or db.incomplete_domain: + create_domain(target) + else: + try: + ask = input(' ├ Edit the domain configuration with the form ? ') + except KeyboardInterrupt: + print('') + logs.out("255", '', True) + if ask in ['y', 'Y']: create_domain(target) + else: logs.out("255", '', True) #=====================# # Create a new domain # #---------------------# -def create_domain(target, option): - if target: isurl(target) +def create_domain(target): + if target and not target.startswith('http'): + logs.out("51", '"%s" -> http(s)://...'%target, False) + target = '' print('\n' ' ┌──────────────────────────────────────────────┐\n' ' │ Configure a new domain for current directory │\n' - ' │ Answer Y = yes. Default value = (default) │\n' + ' │ Answer Y/y = yes. Enter to set {default} │\n' ' │ Empty Answer cancel process, except for │\n' - ' │ - "Optional" │\n' - ' │ - "(default) value │\n' + ' │ - [Optional] │\n' + ' │ - {default} value │\n' ' ├──────────────────────────────────────────────┘' ) @@ -89,72 +68,82 @@ def create_domain(target, option): valid_url = ('http://', 'https://') ask = '' try: - ask = input(' ├ [http(s)://...] URL to website ? ("%s") '%domain_www_url) + ask = input(' ├ [http(s)://...] URL to website ? {%s} '%domain_www_url) except KeyboardInterrupt: print('') logs.out("255", '', True) if ask: if not ask.startswith(valid_url): - logs.out("2", 'http(s)://...', True) - isurl(ask) - domain_www_url = url - elif not domain_www_url: + logs.out("2", '"%s" -> http(s)://...'%ask, True) + domain_www_url = ask + if not domain_www_url: logs.out("255", '', True) + # Remove unused last / if exists + if domain_www_url[-1] == "/": + domain_www_url = domain_www_url[:-1] + + domain_proto = domain_www_url.rsplit('://')[0] domain_short = domain_www_url.rsplit('://')[1] # Prefix wip #----------- + domain_wip_url = '' + # Set domain with www-wip. try: - try_wip_url = target.rsplit('.') - try_wip_url = 'https://www-wip.%s.%s'%( - try_wip_url[1], try_wip_url[2] - ) + domain_wip_url = db.domain_wip_url except: - try_wip_url = 'https://www-wip.%s'%domain_short - - try: domain_wip_url = db.domain_wip_url - except: domain_wip_url = try_wip_url + wip_url = domain_short.rsplit('.', 2) + wip_url[0] = '%s://www-wip'%domain_proto + for t in wip_url: + if domain_wip_url: + domain_wip_url = '%s.%s'%(domain_wip_url, t) + else: + domain_wip_url = t ask = '' try: - ask = input(' ├ URL to wip ? ("%s") '%domain_wip_url) + ask = input(' ├ URL to wip ? {%s} '%domain_wip_url) except KeyboardInterrupt: print('') logs.out("255", '', True) if ask: - isurl(ask) + if not ask.startswith(valid_url): + logs.out("2", '"%s" -> http(s)://...'%ask, True) domain_wip_url = ask + # Remove unused last / if exists + if domain_wip_url[-1] == "/": + domain_wip_url = domain_wip_url[:-1] + + # Register variables in domain database global navbars_conf db_dir = '%s/.local/tyto/%s/'%(db.home_dir, domain_short) db_dir_html = '%shtml/'%db_dir navbars_conf = '%sarticles/_configs/'%db.conf_dir - conf_domain = \ - 'domain_dir = "%s"\n'%db.conf_dir + \ - 'domain_conf = "%s"\n'%db.domain_conf + \ - 'domain_articles = "%sarticles/"\n'%db.conf_dir + \ - 'domain_files = "%sarticles/files/"\n'%db.conf_dir + \ - 'domain_images = "%sarticles/images/"\n'%db.conf_dir + \ - 'articles_db = "%sarticles/"\n'%db_dir + \ - 'html_db = "%s"\n'%db_dir_html + \ - 'navbars_dir = "%s"\n'%navbars_conf + \ - 'navbar_load = "%styto.navbar"\n'%navbars_conf + \ - 'navbar_load_db = "%snavbar_load.conf"\n'%db_dir_html + \ - 'sidebar_load = "%styto.sidebar"\n'%navbars_conf + \ - 'sidebar_load_db = "%ssidebar_load.conf"\n'%db_dir_html + \ - 'metas_load = "%styto.metas.html"\n'%navbars_conf + \ - 'metas_load_db = "%smetas_load.conf"\n'%db_dir_html + \ - 'footer_load = "%styto.footer.html"\n'%navbars_conf + \ - 'footer_load_db = "%sfooter_load.conf"\n'%db_dir_html + \ - '\ndomain_short = "%s"\n'%domain_short + \ - 'domain_www_url = "%s"\n'%domain_www_url + \ - 'domain_wip_url = "%s"\n'%domain_wip_url - - tyto.set_file(db.domain_conf, True, conf_domain) - - + set_f = 'domain_dir = "%s"\n'%db.conf_dir + \ + 'domain_conf = "%s"\n'%db.domain_conf + \ + 'domain_articles = "%sarticles/"\n'%db.conf_dir + \ + 'domain_files = "%sarticles/files/"\n'%db.conf_dir + \ + 'domain_images = "%sarticles/images/"\n'%db.conf_dir + \ + 'articles_db = "%sarticles/"\n'%db_dir + \ + 'html_db = "%s"\n'%db_dir_html + \ + 'navbars_dir = "%s"\n'%navbars_conf + \ + 'navbar_load = "%styto.navbar"\n'%navbars_conf + \ + 'navbar_load_db = "%snavbar_load.conf"\n'%db_dir_html + \ + 'sidebar_load = "%styto.sidebar"\n'%navbars_conf + \ + 'sidebar_load_db = "%ssidebar_load.conf"\n'%db_dir_html + \ + 'metas_load = "%styto.metas.html"\n'%navbars_conf + \ + 'metas_load_db = "%smetas_load.conf"\n'%db_dir_html + \ + 'footer_load = "%styto.footer.html"\n'%navbars_conf + \ + 'footer_load_db = "%sfooter_load.conf"\n'%db_dir_html + \ + '\ndomain_short = "%s"\n'%domain_short + \ + 'domain_www_url = "%s"\n'%domain_www_url + \ + 'domain_wip_url = "%s"\n'%domain_wip_url + tyto.set_file(db.domain_conf, True, set_f) + print(' ├ Created new database') + # Get srv root #------------- try: domain_srv = db.domain_srv @@ -162,7 +151,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ System server ? ("%s") '%domain_srv) + ask = input(' ├ System server ? {%s} '%domain_srv) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -173,28 +162,28 @@ def create_domain(target, option): root_srv_dom = '%s/%s'%(domain_srv, domain_short) srv_wip_tpl = "%s/wip/template/"%root_srv_dom srv_www_tpl = "%s/www/template/"%root_srv_dom - srvs = '# Servers directories\n' + \ - 'srv_root = "%s/"\n'%domain_srv + \ - 'srv_domain = "%s/"\n'%root_srv_dom + \ - 'srv_wip = "%s/wip/"\n'%root_srv_dom + \ - 'srv_wip_tpl = "%s"\n'%srv_wip_tpl + \ - 'srv_wip_images = "%s/wip/images/"\n'%root_srv_dom + \ - 'srv_wip_files = "%s/wip/files/"\n'%root_srv_dom + \ - 'srv_www = "%s/www/"\n'%root_srv_dom + \ - 'srv_www_tpl = "%s"\n'%srv_www_tpl + \ - 'srv_www_images = "%s/www/images/"\n'%root_srv_dom + \ - 'srv_www_files = "%s/www/files/"\n'%root_srv_dom + \ - '\n' + \ - '# Servers files\n' + \ - 'wip_navbar = "%snavbar.html"\n'%srv_wip_tpl + \ - 'wip_sidebar = "%ssidebar.html"\n'%srv_wip_tpl + \ - 'wip_metas = "%smetas.html"\n'%srv_wip_tpl + \ - 'wip_footer = "%sfooter.html"\n'%srv_wip_tpl + \ - 'www_navbar = "%snavbar.html"\n'%srv_www_tpl + \ - 'www_sidebar = "%ssidebar.html"\n'%srv_www_tpl + \ - 'www_metas = "%smetas.html"\n'%srv_www_tpl + \ - 'www_footer = "%sfooter.html"\n'%srv_www_tpl - tyto.set_file(db.domain_conf, False, srvs) + set_f = '# Servers directories\n' + \ + 'srv_root = "%s/"\n'%domain_srv + \ + 'srv_domain = "%s/"\n'%root_srv_dom + \ + 'srv_wip = "%s/wip/"\n'%root_srv_dom + \ + 'srv_wip_tpl = "%s"\n'%srv_wip_tpl + \ + 'srv_wip_images = "%s/wip/images/"\n'%root_srv_dom + \ + 'srv_wip_files = "%s/wip/files/"\n'%root_srv_dom + \ + 'srv_www = "%s/www/"\n'%root_srv_dom + \ + 'srv_www_tpl = "%s"\n'%srv_www_tpl + \ + 'srv_www_images = "%s/www/images/"\n'%root_srv_dom + \ + 'srv_www_files = "%s/www/files/"\n'%root_srv_dom + \ + '\n' + \ + '# Servers files\n' + \ + 'wip_navbar = "%snavbar.html"\n'%srv_wip_tpl + \ + 'wip_sidebar = "%ssidebar.html"\n'%srv_wip_tpl + \ + 'wip_metas = "%smetas.html"\n'%srv_wip_tpl + \ + 'wip_footer = "%sfooter.html"\n'%srv_wip_tpl + \ + 'www_navbar = "%snavbar.html"\n'%srv_www_tpl + \ + 'www_sidebar = "%ssidebar.html"\n'%srv_www_tpl + \ + 'www_metas = "%smetas.html"\n'%srv_www_tpl + \ + 'www_footer = "%sfooter.html"\n'%srv_www_tpl + tyto.set_file(db.domain_conf, False, set_f) # Get title domain @@ -204,7 +193,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ Domain title ? ("%s") '%domain_title) + ask = input(' ├ Domain title ? {%s} '%domain_title) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -213,10 +202,9 @@ def create_domain(target, option): if '"' in domain_title: domain_title = domain_title.replace('"', '') - tyto.set_file(db.domain_conf, False, - '# Domain user\'s settings\n' + \ - 'domain_title = "%s"'%domain_title - ) + set_f = '# Domain user\'s settings\n' + \ + 'domain_title = "%s"'%domain_title + tyto.set_file(db.domain_conf, False, set_f) # Get Description domain @@ -226,7 +214,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ Domain Description ? ("%s") '%domain_about) + ask = input(' ├ Domain Description ? {%s} '%domain_about) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -235,10 +223,8 @@ def create_domain(target, option): if '"' in domain_about: domain_about = domain_about.replace('"', '') - tyto.set_file(db.domain_conf, False, - 'domain_about = "%s"'%domain_about - ) - + set_f = 'domain_about = "%s"'%domain_about + tyto.set_file(db.domain_conf, False, set_f) # Get Lang domain #---------------- @@ -247,7 +233,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ [2 characters] Website language ? ("%s") '%domain_lang) + ask = input(' ├ [2 char] Website language ? {%s} '%domain_lang) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -256,9 +242,8 @@ def create_domain(target, option): else: logs.out("3", ask, True) elif not domain_lang: logs.out("255", '', True) - tyto.set_file(db.domain_conf, False, - 'domain_lang = "%s"'%domain_lang - ) + set_f = 'domain_lang = "%s"'%domain_lang + tyto.set_file(db.domain_conf, False, set_f) # Get mail domain @@ -268,7 +253,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ Webmaster\'s mail ? ("%s") '%domain_mail) + ask = input(' ├ Webmaster\'s mail ? {%s} '%domain_mail) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -277,10 +262,9 @@ def create_domain(target, option): domain_mail = ask elif not domain_mail: logs.out("255", '', True) - tyto.set_file(db.domain_conf, False, - 'domain_mail = "%s"'%domain_mail - ) - + set_f = 'domain_mail = "%s"'%domain_mail + tyto.set_file(db.domain_conf, False, set_f) + # Get Tags domain #---------------- @@ -289,18 +273,17 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ [comma separated] Domain tags ? ("%s") '%domain_tags) + ask = input(' ├ [comma separated] Domain tags ? {%s} '%domain_tags) except KeyboardInterrupt: print('') logs.out("255", '', True) if ask: domain_tags = ask elif not domain_tags: logs.out("255", '', True) - tyto.set_file(db.domain_conf, False, - 'domain_tags = "%s"'%domain_tags - ) - - + set_f = 'domain_tags = "%s"'%domain_tags + tyto.set_file(db.domain_conf, False, set_f) + + # Get logo's website #------------------- try: domain_logo = db.domain_logo @@ -308,17 +291,17 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ logo filename ? ("%s") '%domain_logo) + ask = input(' ├ logo filename ? {%s} '%domain_logo) except KeyboardInterrupt: print('') logs.out("255", '', True) if ask: domain_logo = ask - tyto.set_file(db.domain_conf, False, - 'domain_logo = "%s"\n'%domain_logo + \ - 'wip_logo = "%s%s"\n'%(srv_wip_tpl, domain_logo) + \ - 'www_logo = "%s%s"\n'%(srv_www_tpl, domain_logo) - ) + set_f = 'domain_logo = "%s"\n'%domain_logo + \ + 'wip_logo = "%s%s"\n'%(srv_wip_tpl, domain_logo) + \ + 'www_logo = "%s%s"\n'%(srv_www_tpl, domain_logo) + tyto.set_file(db.domain_conf, False, set_f) + # Get License domain @@ -328,7 +311,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ Domain License ? ("%s") '%domain_license) + ask = input(' ├ Domain License ? {%s} '%domain_license) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -337,10 +320,9 @@ def create_domain(target, option): if '"' in domain_license: domain_license = domain_license.replace('"', '') - tyto.set_file(db.domain_conf, False, - 'domain_license = "%s"'%domain_license - ) - + set_f = 'domain_license = "%s"'%domain_license + tyto.set_file(db.domain_conf, False, set_f) + # Get License URL (optionnal) #---------------------------- @@ -349,7 +331,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ Optional. License URL ? ("%s") '%domain_licurl) + ask = input(' ├ [Optional] License URL ? {%s} '%domain_licurl) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -357,9 +339,8 @@ def create_domain(target, option): if not ask.startswith('http'): logs.out("3", ask, True) domain_licurl = ask - tyto.set_file(db.domain_conf, False, - 'domain_licurl = "%s"'%domain_licurl - ) + set_f = 'domain_licurl = "%s"'%domain_licurl + tyto.set_file(db.domain_conf, False, set_f) # CSS Prefix @@ -369,7 +350,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ [alnum] Prefix CSS ? ("%s") '%domain_css) + ask = input(' ├ [alnum] Prefix CSS ? {%s} '%domain_css) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -377,9 +358,8 @@ def create_domain(target, option): if not ask.isalnum(): logs.out("3", ask, True) domain_css = ask.lower() - tyto.set_file(db.domain_conf, False, - 'domain_css = "%s"'%domain_css - ) + set_f = 'domain_css = "%s"'%domain_css + tyto.set_file(db.domain_conf, False, set_f) # Titles webpage separator @@ -389,7 +369,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ [1 character] Pages titles separator ? ("%s") '%domain_sep) + ask = input(' ├ [1 char] Pages titles separator ? {%s} '%domain_sep) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -397,9 +377,8 @@ def create_domain(target, option): if not len(ask) == 1: logs.out("3", ask, True) domain_sep = ask - tyto.set_file(db.domain_conf, False, - 'domain_sep = "%s"'%domain_sep - ) + set_f = 'domain_sep = "%s"'%domain_sep + tyto.set_file(db.domain_conf, False, set_f) # Profile for rel="me" (optionnal) @@ -409,7 +388,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ Optional. Profile URL ? ("%s") '%domain_relme) + ask = input(' ├ [Optional] rel="me" URL ? {%s} '%domain_relme) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -417,9 +396,8 @@ def create_domain(target, option): if not ask.startswith('http'): logs.out("3", ask, True) domain_relme = ask - tyto.set_file(db.domain_conf, False, - 'domain_relme = "%s"'%domain_relme - ) + set_f = 'domain_relme = "%s"'%domain_relme + tyto.set_file(db.domain_conf, False, set_f) # Sidebar Title @@ -429,7 +407,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ Sidebar title ? ("%s") '%sidebar_title) + ask = input(' ├ Sidebar title ? {%s} '%sidebar_title) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -437,9 +415,8 @@ def create_domain(target, option): if '"' in sidebar_title: sidebar_title = sidebar_title.replace('"', '') - tyto.set_file(db.domain_conf, False, - 'sidebar_title = "%s"'%(sidebar_title) - ) + set_f = 'sidebar_title = "%s"'%sidebar_title + tyto.set_file(db.domain_conf, False, set_f) # Sidebar Items @@ -449,7 +426,7 @@ def create_domain(target, option): ask = '' try: - ask = input(' ├ [max=16] Sidebar Items ? ("%s") '%sidebar_items) + ask = input(' ├ [max=16] Sidebar Items ? {%s} '%sidebar_items) except KeyboardInterrupt: print('') logs.out("255", '', True) @@ -457,9 +434,8 @@ def create_domain(target, option): if not ask.isdigit(): logs.out("3", ask, True) elif int(ask) in range(1,17): sidebar_items = int(ask) - tyto.set_file(db.domain_conf, False, - 'sidebar_items = %s'%(sidebar_items) - ) + set_f = 'sidebar_items = %d'%sidebar_items + tyto.set_file(db.domain_conf, False, set_f) # Activate Domain after Resumed configuration ? @@ -492,14 +468,14 @@ def create_domain(target, option): tyto.set_file(db.domain_conf, False, '\ndomain_active = True') # Load config - exec(open(db.domain_conf).read(),globals()) + importlib.reload(db) # Create folders from configuration file folders = ( - srv_wip_tpl, srv_wip_images, srv_wip_files, - srv_www_tpl, srv_www_images, srv_www_files, - domain_files, domain_images, navbars_dir, - domain_db, html_db + db.srv_wip_tpl, db.srv_wip_images, db.srv_wip_files, + db.srv_www_tpl, db.srv_www_images, db.srv_www_files, + db.domain_files, db.domain_images, db.navbars_dir, + db.articles_db, db.html_db ) print(' │') @@ -599,7 +575,7 @@ def create_sidebar(option): # Create sidebar_load if create_load: # Create new file, or ask if exists - ask = ' ├ Use default (empty) sidebar configuration ? ' + ask = ' ├ Reset sidebar configuration file ? ' log = ' ├ Create file: %s'%db.sidebar_load res = '' if os.path.exists(db.sidebar_load): @@ -705,7 +681,7 @@ def create_navbar(option): # Create sidebar_load if create_load: # Create new file, or ask if exists - ask = ' ├ Use default (empty) navbar configuration ? ' + ask = ' ├ Reset navbar configuration file ? ' log = ' ├ Create file: %s'%db.navbar_load res = '' if os.path.exists(db.navbar_load): diff --git a/src/var/lib/tyto/program/html.py b/src/var/lib/tyto/program/html.py index fb7488f..6f9079e 100644 --- a/src/var/lib/tyto/program/html.py +++ b/src/var/lib/tyto/program/html.py @@ -28,7 +28,6 @@ import logs, db, tyto, domain def manage_configs(target, option): # Arguments args1 = ('metas', 'sidebar', 'footer', 'navbar') - args2 = ('-n', '-e', '-F', '-c') opts = ('New', 'Edit', 'Force', 'Create') # Check arguments (from form domain) @@ -234,20 +233,16 @@ def create_html_infos_section(process): def create_db_load(file_load, file_db): hash_load = tyto.get_filesum(file_load, True) if not os.path.exists(file_db): - tyto.set_file(file_db, - 'New', - 'file_load_hash = "%s"'%hash_load - ) + set_f = 'file_load_hash = "%s"'%hash_load + tyto.set_file(file_db, True, set_f) return global file_load_hash file_load_hash = '' exec(open(file_db, 'r').read(), globals()) if not file_load_hash == hash_load: - tyto.set_file(file_db, - 'New', - 'file_load_hash = "%s"'%hash_load - ) + set_f = 'file_load_hash = "%s"'%hash_load + tyto.set_file(file_db, True, set_f) #============================================# @@ -354,7 +349,7 @@ def create_sidebar(target): res = input(ask_html) if not res in ['y', 'Y']: return - tyto.set_file(target, "w", sidebar_content) + tyto.set_file(target, True, sidebar_content) logs.out("33", target, False) @@ -434,10 +429,15 @@ def create_navbar(target): ask_html = ' ├ Replace %s ? '%db.wip_navbar res = '' if os.path.exists(db.wip_navbar): - res = input(ask_html) + try: + res = input(ask_html) + except KeyboardInterrupt: + print('') + logs.out("255", '', True) + if not res in ['y', 'Y']: return - tyto.set_file(navbar_file, 'new', menu_html) + tyto.set_file(navbar_file, True, menu_html) print(' ├ Create file: %s'%navbar_file) @@ -520,15 +520,19 @@ def create_user_metas(option): # Create metas_load file according to option #------------------------------------------- if create_load: - ask_load = ' ├ Use default HTML metas configuration ? ' + ask_load = ' ├ Reset metas configuration file ? ' log_load = ' ├ Create file: %s'%db.metas_load res = '' if os.path.exists(db.metas_load): - res = input(ask_load) + try: + res = input(ask_load) + except KeyboardInterrupt: + print('') + logs.out("255", '', True) if not res in ['y', 'Y']: return - tyto.set_file(db.metas_load, 'new', metas_tags) + tyto.set_file(db.metas_load, True, metas_tags) create_html = True create_db_load(db.metas_load, db.metas_load_db) print(log_load) @@ -543,7 +547,11 @@ def create_user_metas(option): res = '' if os.path.exists(db.wip_metas): - res = input(ask_html) + try: + res = input(ask_html) + except KeyboardInterrupt: + print('') + logs.out("255", '', True) if not res in ['y', 'Y']: return user_file = open(db.metas_load, 'r').read() @@ -553,7 +561,7 @@ def create_user_metas(option): if user_metas: user_metas = "%s\n %s"%(user_metas, line) else: user_metas = ' %s'%line - tyto.set_file(db.wip_metas, 'new', user_metas) + tyto.set_file(db.wip_metas, True, user_metas) print(log_html) @@ -629,15 +637,20 @@ def create_user_footer(option): # Create new default footer_load, or ask if exists #------------------------------------------------- if create_load: - ask_load = ' ├ Replace default footer configuration ? ' + ask_load = ' ├ Reset footer configuration file ? ' log_load = ' ├ Create file: %s'%db.footer_load res = '' if os.path.exists(db.footer_load): - res = input(ask_load) + try: + res = input(ask_load) + except KeyboardInterrupt: + print('') + logs.out("255", '', True) + if not res in ['y', 'Y']: return create_html = True - tyto.set_file(db.footer_load, 'new', footer) + tyto.set_file(db.footer_load, True, footer) create_db_load(db.footer_load, db.footer_load_db) print(log_load) @@ -647,7 +660,11 @@ def create_user_footer(option): log_load = ' ├ Create file: %s'%db.wip_footer res = '' if os.path.exists(db.wip_footer): - res = input(ask_load) + try: + res = input(ask_load) + except KeyboardInterrupt: + print('') + logs.out("255", '', True) if not res in ['y', 'Y']: return user_footer = '' @@ -657,7 +674,7 @@ def create_user_footer(option): if user_footer: user_footer = "%s\n %s"%(user_footer, line) else: user_footer = ' %s'%line - tyto.set_file(db.wip_footer, 'new', user_footer) + tyto.set_file(db.wip_footer, True, user_footer) print(log_load) diff --git a/src/var/lib/tyto/program/logs.py b/src/var/lib/tyto/program/logs.py index 87d7d1e..12efbda 100644 --- a/src/var/lib/tyto/program/logs.py +++ b/src/var/lib/tyto/program/logs.py @@ -31,10 +31,11 @@ CG = '\033[1;32m' def out(nbr, value, out): logs = { '1' : ':< %sUnused resource%s: %s'%(CR, CS, value), - '2' : ':< %sIncomplete data%s: "%s"'%(CR, CS, value), + '2' : ':< %sIncomplete data%s: %s'%(CR, CS, value), '3' : ':< %sInvalid data%s: "%s"'%(CR, CS, value), + '4' : ':< %sCannot create file%s: %s'%(CR, CS, value), '5' : ':< %sUnused argument%s: [file]'%(CR, CS), - '6' : ':< %sUnused "%s"%s in article'%(CR, CS, value), + '6' : ':< %sUnused "%s"%s in article'%(CR, value, CS), '7' : ':< Article is %snot valid yet%s'%(CR, CS), '8' : ':< %sNot paired%s: %s'%(CR, CS, value), '9' : ':< Article %shas changed%s. Check it first'%(CR, CS), @@ -60,6 +61,7 @@ def out(nbr, value, out): '41' : ':? %sIncomplete%s domain "%s"'%(CR, CS, value), '42' : ':D %sActive%s domain "%s"'%(CG, CS, value), '43' : ':? %sNo domain%s configured here: %s'%(CY, CS, value), + '51' : ':< %sIncomplete data%s: %s'%(CY, CS, value), '255' : ';) Maybe later...' } diff --git a/src/var/lib/tyto/program/new.py b/src/var/lib/tyto/program/new.py new file mode 100644 index 0000000..7d4e441 --- /dev/null +++ b/src/var/lib/tyto/program/new.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +# Name: Tyto - Littérateur +# Type: Command arguments 'new' manager +# Description: manage all 'new' from command action argument +# file: new.py +# Folder: /var/lib/tyto/program/ +# By echolib (XMPP: im@echolib.re) +# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 + +#------------ +# funny stats +#------------ +# lines: +# functions: +# comments: +#---------------------------------------------------------------------- + +#********************************************************************** + +import sys +import logs, args, db, domain + + +#===============================================# +# manage argument new for # +# - domain: target becomes 3rd command argument # +#-----------------------------------------------# +def manage_new(target): + if target == "domain": + try: target = sys.argv[3] + except: target = '' + domain.manage_domain(target) + diff --git a/src/var/lib/tyto/program/show.py b/src/var/lib/tyto/program/show.py new file mode 100644 index 0000000..43846aa --- /dev/null +++ b/src/var/lib/tyto/program/show.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +# Name: Tyto - Littérateur +# Type: Command arguments 'show', 'showdb' manager +# Description: manage 'show' and 'showdb' from command action argument +# file: show.py +# Folder: /var/lib/tyto/program/ +# By echolib (XMPP: im@echolib.re) +# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 + +#------------ +# funny stats +#------------ +# lines: +# functions: +# comments: +#---------------------------------------------------------------------- + +#********************************************************************** + +import importlib +import args, db, logs, tyto, check + + +#======================# +# From command line: # +# - 'showdb' / 'show' # +# - 'editdb' / 'edit' # +# Show or edit files # +# final html, db, load # +#----------------------# +def manage_show(target): + # Domain configuration must exists + if not db.domain_exists: sys.exit(43) + + # Conditions to filter processes show, or edit + actions_target_ct = ('show', 'edit') + actions_target_db = ('showdb', 'editdb') + actions_read = ('show', 'showdb') + actions_edit = ('edit', 'editdb') + + # target in command is an uri file + if args.action in actions_target_ct and db.post_exists: + target = "post" + do = {"post" : db.uri_file} + + # Get hash when edit to ask to check again if change + if args.action == "edit": + curr_hash = tyto.get_filesum(db.uri_file, True) + + # Set DB from article + elif args.action in actions_target_db and db.db_exists: + target = "post" + do = {"post" : db.post_db} + + # Target is a registred name to access file + elif target in args.pass_targets: + try: + # File is a "db" from showdb, editdb + if args.action in actions_target_db: + do = { + 'domain' : db.domain_conf, + 'footer' : db.footer_load, + 'metas' : db.metas_load, + 'navbar' : db.navbar_load, + 'sidebar' : db.sidebar_load, + } + + # File is a source, or final file from show, edit + elif args.action in actions_target_ct: + do = { + 'domain' : db.domain_conf, + 'footer' : db.wip_footer, + 'metas' : db.wip_metas, + 'navbar' : db.wip_navbar, + 'sidebar' : db.wip_sidebar, + } + except: + logs.out("41", '%s configuration'%target, True) + else: + return + + # Read lines of, or edit file + if args.action in actions_read: read_lines(do[target]) + elif args.action in actions_edit: tyto.edit_file(do[target]) + + # After editing article, if change, ask to check again + if args.action == "edit": + new_hash = tyto.get_filesum(db.uri_file, True) + if curr_hash != new_hash: + ask = '' + try: ask = input('-> Check your changes ? ') + except KeyboardInterrupt: + print('') + logs.out("255", '', True) + + if not ask in ['y', 'Y']: + logs.out("255", '', True) + + # Reload post DB (if edited article, and check it if ask "y") + importlib.reload(db) + check.manage_check(db.uri_file) + + +#============================================# +# Generic function to read lines from a file # +#--------------------------------------------# +def read_lines(f): + if not f: return # Maybe + + datas = open(f).read() + for line in datas.rsplit('\n'): + print(line) diff --git a/src/var/lib/tyto/program/tyto.py b/src/var/lib/tyto/program/tyto.py index 339c241..6d7abe1 100644 --- a/src/var/lib/tyto/program/tyto.py +++ b/src/var/lib/tyto/program/tyto.py @@ -182,17 +182,21 @@ def edit_file(edit_file): ) -#================# -# Create a file # -# Or append text # -#----------------# +#=================# +# Create a file # +# Or append text # +# new: True/false # +#-----------------# def set_file(path, new, text): if new: opt = "w" else: opt = "a" - file = open(path, opt) - file.write(text + '\n') - file.close() + try: + file = open(path, opt) + file.write(text + '\n') + file.close() + except: + logs.out("4", db.domain_conf, True) #==========================#