Config barely read, progress ?

This commit is contained in:
Malphaet 2017-03-13 14:03:59 +01:00
parent bf77a0044a
commit e0587ddb82
17 changed files with 118 additions and 26 deletions

View File

@ -1 +1 @@
__all__ = ["config","model","webengine"]
__all__ = ["file_parser","tree_parser","container_model","content_model","website_config"]

1
generator/config.py Normal file
View File

@ -0,0 +1 @@
# Copyleft (c) 2016 Cocobug All Rights Reserved.

View File

@ -1 +0,0 @@
__all__ = []

73
generator/file_parser.py Normal file
View File

@ -0,0 +1,73 @@
# Copyleft (c) 2016 Cocobug All Rights Reserved.
# coding=utf-8
import os,sys,tree_parser
import re
import traceback
class WebPage(object):
"A webpage object, with some variables and all localisations"
def __init__(self,path):
self.path=path
self.name=os.path.split(path)[-1]
self.variables={}
self.list_of_lang=set()
self.reserved=["model"]
self.init_reserved()
def init_reserved(self):
for key in reserved:
self.setattr(key,"")
def add_variable(self,var,value):
"""Add the folowing variable and update the necessary constants
Takes a variable (with an eventual langague tag) and it's value"""
var,lang,value=create_lang(var,value)
if var in reserved:
self.setattr(var,value)
else:
if var in self.variables:
self.variables[var][lang]=value
else:
self.variables[var]={lang:value}
self.list_of_lang.add(lang)
def create_lang(var,value):
"""Takes a variable (with an eventual language tag) and it's value and return var,lang,value
Only the last _ determines the language, note that it could give the impression _ is ok to use in variables. It is not."""
var=var.rsplit('_',1)
if len(var)==2:
return var[0],var[1],value
return var[0],"*",value
def parse_file(file_name):
"Parse a file and return a webpage object"
page=WebPage(file_name)
isconfig=True # The firsts lines are config lines
with open(file_name) as f:
while True:
line=f.readline()
if not line:
break
try:
if line.startswith("----"): # Not really a good practice, but meh
isconfig=False
if isconfig and line!=os.linesep: # Now parsing config files
var,value=re_config_line.match(line).groups()
page.add_variable(var,value)
else: # The config lines are now parsed, will now enter the nightmare of standart lines
except re.error:
print "Error parsing",file_name,"contain a non parsable line:"
print " >",line
except:
traceback.print_exc()
re_config_line=re.compile("(?P<variable>.+): (?P<value>.*)")
re_text_line=re.compile("")
if __name__ == '__main__':
parse_file("sites/example_website/_config.txt")

View File

@ -1 +0,0 @@
__all__ = []

View File

@ -1 +0,0 @@
__all__ = ["fileparser","treeparser"]

View File

@ -1,13 +0,0 @@
# Copyleft (c) 2016 Cocobug All Rights Reserved.
import os,sys,treeparser
class WebPage(object):
"A webpage object, with some variables and all localisations"
def __init__(self):
pass
def parse_file(file_object):
"Parse a file object and return a webpage object"
pass

View File

@ -1,18 +1,36 @@
# Copyleft (c) 2016 Cocobug All Rights Reserved.
import os,sys,fileparser
import os,sys,file_parser
class WebTree(object):
"The tree of all website evolves during runtime as the files are requested"
def __init__(self,path):
self.website_path=path
def list_dir(self,subpath=""):
"List all files in the directory"
pass
self.name=""
self.generation=""
self.variable={"/":{}}
self.get_config()
def make_walk(self,subpath=""):
"Walk the directory and subdirectories and make the tree from it"
for (dirpath, dirnames, filenames) in os.walk(os.path.join(self.website_path,subpath)):
print dirpath, dirnames, filenames
def get_config(self):
"Get the basic config from the website"
file_parser.parse_file(os.path.join(self.website_path,"_config.txt"))
def convert_local(self,files):
pass
class WebSubTree(Tree):
def print_webtree(self):
print self
print self.name
def __repr__(self):
return "WebTree of website "+self.website_path
class WebSubTree(WebTree):
"Share some methods with the main tree, only describes one folder"
def __init__(self):
pass
@ -25,11 +43,13 @@ class WebNode(object):
def makeWebsite(website):
"Take a string of the folder name containing the website, will proceed to generate all needed Objects and return the Webtree"
if os.path.isdir(website):
webt=Webtree(website)
webt=WebTree(website)
return webt
else:
raise IOError("Unable to find the specified website")
if __name__ == '__main__':
# Do all the nominal tests
pass
s=makeWebsite("sites/example_website")
s.print_webtree()
s.list_dir()

View File

@ -1 +0,0 @@
__all__ = []

View File

@ -3,6 +3,9 @@ title_en: Poetry
category_en: Poetry
category_fr: Poésie
author_*: Max
----
__en__
Random poetry translated
__/en__

View File

@ -1,6 +1,8 @@
title_*: Rose
author_*: Max
----
__en__
A Rose flew
Flower of beauty

View File

@ -1,7 +1,12 @@
variable_that_will_be_accesible_in_the_whole_project: Very good
variablethatwillbeaccesibleinthewholeproject: Very good
en_en: English
en_fr: Anglais
fr_fr: Français
fr_en: French
model: dual
generation: dynamic
----
Be wary that _ is a reserved character
Technically those lines are not comments but since the text content of config files is ignored, it acts like one

View File

@ -4,6 +4,8 @@ author_*: Max
date: 10/10/10
----
__fr__
Contenu
__/fr__

View File

@ -11,6 +11,8 @@ category_fr: Liste des catégories
legal_*: Copyleft (c) Cocobug
----
__en__
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
__/en__

View File

@ -3,6 +3,7 @@
# Copyleft (c) 2016 Cocobug All Rights Reserved.
import argparse
from generator import *
parser = argparse.ArgumentParser(description='Website on-the-fly & static generator')
subparsers = parser.add_subparsers(help='Commands')