diff --git a/generator/__init__.py b/generator/__init__.py index 53d2a24..ec5e58a 100644 --- a/generator/__init__.py +++ b/generator/__init__.py @@ -1 +1 @@ -__all__ = ["config","model","webengine"] +__all__ = ["file_parser","tree_parser","container_model","content_model","website_config"] diff --git a/generator/config.py b/generator/config.py new file mode 100644 index 0000000..4c09160 --- /dev/null +++ b/generator/config.py @@ -0,0 +1 @@ +# Copyleft (c) 2016 Cocobug All Rights Reserved. diff --git a/generator/config/__init__.py b/generator/config/__init__.py deleted file mode 100644 index a9a2c5b..0000000 --- a/generator/config/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__all__ = [] diff --git a/generator/model/container_model.py b/generator/container_model.py similarity index 100% rename from generator/model/container_model.py rename to generator/container_model.py diff --git a/generator/model/content_model.py b/generator/content_model.py similarity index 100% rename from generator/model/content_model.py rename to generator/content_model.py diff --git a/generator/file_parser.py b/generator/file_parser.py new file mode 100644 index 0000000..a0948f7 --- /dev/null +++ b/generator/file_parser.py @@ -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.+): (?P.*)") +re_text_line=re.compile("") + +if __name__ == '__main__': + + parse_file("sites/example_website/_config.txt") diff --git a/generator/model/__init__.py b/generator/model/__init__.py deleted file mode 100644 index a9a2c5b..0000000 --- a/generator/model/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__all__ = [] diff --git a/generator/parser/__init__.py b/generator/parser/__init__.py deleted file mode 100644 index 0588c5c..0000000 --- a/generator/parser/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__all__ = ["fileparser","treeparser"] diff --git a/generator/parser/fileparser.py b/generator/parser/fileparser.py deleted file mode 100644 index c2e376c..0000000 --- a/generator/parser/fileparser.py +++ /dev/null @@ -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 diff --git a/generator/parser/treeparser.py b/generator/tree_parser.py similarity index 50% rename from generator/parser/treeparser.py rename to generator/tree_parser.py index 4575624..dec433b 100644 --- a/generator/parser/treeparser.py +++ b/generator/tree_parser.py @@ -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() diff --git a/generator/webengine/__init__.py b/generator/webengine/__init__.py deleted file mode 100644 index a9a2c5b..0000000 --- a/generator/webengine/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__all__ = [] diff --git a/sites/example_website/Poetry/index.txt b/sites/example_website/Poetry/index.txt index 20d8c20..816266a 100644 --- a/sites/example_website/Poetry/index.txt +++ b/sites/example_website/Poetry/index.txt @@ -3,6 +3,9 @@ title_en: Poetry category_en: Poetry category_fr: Poésie author_*: Max + +---- + __en__ Random poetry translated __/en__ diff --git a/sites/example_website/Poetry/poetry1.txt b/sites/example_website/Poetry/poetry1.txt index 633dfc7..945c25c 100644 --- a/sites/example_website/Poetry/poetry1.txt +++ b/sites/example_website/Poetry/poetry1.txt @@ -1,6 +1,8 @@ title_*: Rose author_*: Max +---- + __en__ A Rose flew Flower of beauty diff --git a/sites/example_website/_config.txt b/sites/example_website/_config.txt index 5f4b0fb..82028ce 100644 --- a/sites/example_website/_config.txt +++ b/sites/example_website/_config.txt @@ -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 diff --git a/sites/example_website/article1.txt b/sites/example_website/article1.txt index ab170f1..f43ef61 100644 --- a/sites/example_website/article1.txt +++ b/sites/example_website/article1.txt @@ -4,6 +4,8 @@ author_*: Max date: 10/10/10 +---- + __fr__ Contenu __/fr__ diff --git a/sites/example_website/index.txt b/sites/example_website/index.txt index db7ee0d..9b13d72 100644 --- a/sites/example_website/index.txt +++ b/sites/example_website/index.txt @@ -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__ diff --git a/vkyweb.py b/vkyweb.py index 6228ec9..a3d09c7 100644 --- a/vkyweb.py +++ b/vkyweb.py @@ -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')