From 698860f91098c448c958e366a65f76ceef38575d Mon Sep 17 00:00:00 2001 From: Malphaet Date: Tue, 14 Mar 2017 19:52:00 +0100 Subject: [PATCH] Progress still made, nearly done parsing tree/files --- generator/file_parser.py | 35 +++++--- generator/tree_parser.py | 90 +++++++++++++++---- .../Poetry/subarticle1/index_en.txt | 9 ++ .../Poetry/subarticle1/index_fr.txt | 7 ++ .../Poetry/subarticle2_en/index.txt | 13 +++ .../Poetry/subarticle2_fr/index.txt | 7 ++ 6 files changed, 134 insertions(+), 27 deletions(-) create mode 100644 sites/example_website/Poetry/subarticle1/index_en.txt create mode 100644 sites/example_website/Poetry/subarticle1/index_fr.txt create mode 100644 sites/example_website/Poetry/subarticle2_en/index.txt create mode 100644 sites/example_website/Poetry/subarticle2_fr/index.txt diff --git a/generator/file_parser.py b/generator/file_parser.py index 4832d05..8f062d5 100644 --- a/generator/file_parser.py +++ b/generator/file_parser.py @@ -60,14 +60,11 @@ class WebPage(object): 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) + var,lang=create_lang(var) if var in self.reserved: setattr(self,var,value) else: - if var in self.variables: - self.variables[var][lang]=value - else: - self.variables[var]={lang:value} + add_to_table(var,lang,value,self.variables) self.list_of_lang.add(lang) def get_variable(self,varname,filter_lang="*"): @@ -82,13 +79,31 @@ class WebPage(object): return self.variable[varname].items()[0] raise KeyError("The variable "+varname+" doens't exist in te language "+filter_lang) -def create_lang(var,value): + def export(self): + "Export the Webobject in every language" + exp={} + for lang in self.langagues: + exp[lang]=self.get_text(lang) + return exp + +def add_to_table(var,lang,value,table): + "For now it works in tandem with create_lang and add to a dict with the lang" + if var in table: + table[var][lang]=value + else: + table[var]={lang:value} + return table + +def create_lang(var): """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 + s_var=var.rsplit('_',1) + + if len(s_var)==2: + if "" in s_var: + return var,"*" + return s_var[0],s_var[1] + return var ,"*" def parse_file(file_name): "Parse a file and return a webpage object" diff --git a/generator/tree_parser.py b/generator/tree_parser.py index ccef876..a5b1e5c 100644 --- a/generator/tree_parser.py +++ b/generator/tree_parser.py @@ -3,17 +3,19 @@ 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 __init__(self,website_path,path=""): + self.website_path=website_path self.name="" self.generation="" - self.path="" + self.path=path + self.full_path=os.path.join(website_path,path) self.get_config() + self.make_tree() - 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 make_tree(self): + "Walk the directory and subdirectories and make the tree from it, note that the WebTree only have one Subtree, containing other subtrees" + self.tree=WebSubTree(self.website_path,"") def get_config(self): "Get the basic config from the website" @@ -28,13 +30,13 @@ class WebTree(object): def convert_local(self,files): pass - def print_webtree(self): + def print_webtree(self,prefix=""): "Print a representation of the Tree" print self - print self.name + self.tree.print_webtree(prefix) def __repr__(self): - return "WebTree of website "+self.website_path + return "{} @ {}".format(self.__class__.__name__,self.full_path) def __contain__(self,var): "Define the __in__ membership in the variables" @@ -46,8 +48,10 @@ class WebTree(object): class WebSubTree(WebTree): "Share some methods with the main tree, only describes one folder" - def __init__(self): - pass + def __init__(self,website_path,path): + self.subtree={} + self.nodes={} + WebTree.__init__(self,website_path,path) def get_variable(self,varname,filter_lang): "Get the variable, either in it's config or one of the above" @@ -56,10 +60,58 @@ class WebSubTree(WebTree): except: self.parent_node.get_variable(varname,filter_lang) -class WebNode(object): - "A webfile, as seen from the tree, contains a link to a WebPage, generated from itself" - def __init__(self): + def get_config(self): + "Try to init a config, if not just use an empty variable set" + try: + super.get_config(self) + except: + self.variables={} + + def make_tree(self): + """List all directories, make Nodes or Subtree from it and init them + For every subtree/node informations will be stored on the langagues used + (Subtree[[en]->Subtree,[fr]->Subtree])* """ + self.subtree={} + self.nodes={} + + for f in os.listdir(self.full_path): + name=os.path.join(self.full_path,f) + path=os.path.join(self.path,f) + if os.path.isdir(name): + var,lang=file_parser.create_lang(f) + self.subtree=file_parser.add_to_table(var,lang,WebSubTree(self.website_path,path),self.subtree) + else: + var,lang=file_parser.create_lang(os.path.splitext(f)[0]) + self.nodes=file_parser.add_to_table(var,lang,WebNode(self.website_path,path),self.nodes) + #print self, self.subtree + #print self.nodes + + def get_all_nodes(self,filter_lang="*"): + "Get all nodes in the following language" pass + + def get_node(self,name,filter_lang=""): + "Get a node with a specific name" + pass + + def print_webtree(self,prefix=""): + "Print the tree and recuse" + for item,langs in self.nodes.iteritems(): + for lang,node in langs.iteritems(): + print "{} [{}] {}".format(prefix,lang,node) + for item,langs in self.subtree.iteritems(): + for lang,node in langs.iteritems(): + print "{} [{}] {}".format(prefix,lang,node) + node.print_webtree(prefix+" ") + +class WebNode(WebSubTree): + "A webfile, as seen from the tree, contains a link to a WebPage/Webfile in every langague" + def __init__(self,website_path,path): + self.website_path=website_path + self.path=path + self.full_path=os.path.join(website_path,path) + self.content={} + def get_variable(self,varname,filter_lang): "Get the corresponding variable, first in the Webnode, then the config and repeating up until the Root" try: @@ -70,6 +122,11 @@ class WebNode(object): except: self.parent_node.get_variable(varname,filter_lang) + def open_node(self): + "Open the file in the node, for now the parse is left linked, but later versions will ditch it asap" + self.parse=file_parser.parse_file(self.full_path) + self.variables=self.parse.variables + print self.parse.export() 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): @@ -81,5 +138,4 @@ def makeWebsite(website): if __name__ == '__main__': # Do all the nominal tests s=makeWebsite("sites/example_website") - s.print_webtree() - s.make_walk() + s.print_webtree(" > ") diff --git a/sites/example_website/Poetry/subarticle1/index_en.txt b/sites/example_website/Poetry/subarticle1/index_en.txt new file mode 100644 index 0000000..ae371bb --- /dev/null +++ b/sites/example_website/Poetry/subarticle1/index_en.txt @@ -0,0 +1,9 @@ +title_en: Poetry +category_en: Subology +author_*: Max + +---- + +__en__ +SUBARTICLE ONE +__/en__ diff --git a/sites/example_website/Poetry/subarticle1/index_fr.txt b/sites/example_website/Poetry/subarticle1/index_fr.txt new file mode 100644 index 0000000..6dfbf61 --- /dev/null +++ b/sites/example_website/Poetry/subarticle1/index_fr.txt @@ -0,0 +1,7 @@ +title_fr: Poésie +category_fr: Subologie +author_: Max + +---- + +SUBARTICLE UN (Tout est sensé etre en francais puisque l'article est _fr) diff --git a/sites/example_website/Poetry/subarticle2_en/index.txt b/sites/example_website/Poetry/subarticle2_en/index.txt new file mode 100644 index 0000000..f41e1c6 --- /dev/null +++ b/sites/example_website/Poetry/subarticle2_en/index.txt @@ -0,0 +1,13 @@ +title_fr: Ferugination +title_en: Ferogo +category_*: Malo +author_*: Max + +---- + +__en__ +I aim to misbehave +__/en__ +__fr__ +Huehuehue +__/fr__ diff --git a/sites/example_website/Poetry/subarticle2_fr/index.txt b/sites/example_website/Poetry/subarticle2_fr/index.txt new file mode 100644 index 0000000..e6219d6 --- /dev/null +++ b/sites/example_website/Poetry/subarticle2_fr/index.txt @@ -0,0 +1,7 @@ +title_fr: Ferugination +category_*: Malo +author_*: Max + +---- + +On vera bien