mirror of https://github.com/Malphaet/vkyweb.git
Minor improvements
This commit is contained in:
parent
f9f442e7ce
commit
b32b5c2ef1
|
|
@ -25,20 +25,30 @@ class WebPage(object):
|
||||||
setattr(self,key,"")
|
setattr(self,key,"")
|
||||||
|
|
||||||
def set_reserved(self,var,value=""):
|
def set_reserved(self,var,value=""):
|
||||||
|
"Set a reserved variable"
|
||||||
setattr(self,self.reserved_prefix+key,value)
|
setattr(self,self.reserved_prefix+key,value)
|
||||||
|
|
||||||
def get_reserved(self,var):
|
def get_reserved(self,var):
|
||||||
|
"Get a reserved variable"
|
||||||
getattr(self,self.reserved_prefix+var)
|
getattr(self,self.reserved_prefix+var)
|
||||||
|
|
||||||
def add_content(self,text,lang):
|
def add_content(self,text,lang):
|
||||||
|
"Add a line of content, with the appropriates langues"
|
||||||
self.content.append([text,lang])
|
self.content.append([text,lang])
|
||||||
|
|
||||||
def get_next_line(self,filter_lang="*"):
|
def get_next_line(self,filter_lang="*"):
|
||||||
"Get a line of text, with a filter if needed"
|
"Get a line of text, with a filter if needed"
|
||||||
for line,lang in self.content:
|
for line,lang in self.content:
|
||||||
if filter_lang in lang:
|
if self.match_with_lang(lang,filter_lang):
|
||||||
yield line
|
yield line
|
||||||
|
|
||||||
|
def match_with_lang(self,lang,filter_lang):
|
||||||
|
"""Will make sense if I ever use a translation table and not a clusterf*ck of strings"""
|
||||||
|
for l in lang:
|
||||||
|
if l=="*" or l==filter_lang:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
def get_text(self,filter):
|
def get_text(self,filter):
|
||||||
"Get the whole text matching the filter, note that the * filter will ONLY match if the text is meant for all, not all text"
|
"Get the whole text matching the filter, note that the * filter will ONLY match if the text is meant for all, not all text"
|
||||||
text=""
|
text=""
|
||||||
|
|
@ -47,10 +57,6 @@ class WebPage(object):
|
||||||
text+=os.linesep
|
text+=os.linesep
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def get_lang(self,lang):
|
|
||||||
"Will make sense if I ever use a translation table and not a clusterf*ck of strings"
|
|
||||||
return lang
|
|
||||||
|
|
||||||
def add_variable(self,var,value):
|
def add_variable(self,var,value):
|
||||||
"""Add the folowing variable and update the necessary constants
|
"""Add the folowing variable and update the necessary constants
|
||||||
Takes a variable (with an eventual langague tag) and it's value"""
|
Takes a variable (with an eventual langague tag) and it's value"""
|
||||||
|
|
@ -64,6 +70,17 @@ class WebPage(object):
|
||||||
self.variables[var]={lang:value}
|
self.variables[var]={lang:value}
|
||||||
self.list_of_lang.add(lang)
|
self.list_of_lang.add(lang)
|
||||||
|
|
||||||
|
def get_variable(self,varname,filter_lang="*"):
|
||||||
|
"Get a variable, if * or nothing is used a filter the program will attempt to give a global variable, or will yield one at random"
|
||||||
|
if varname in self.reserved:
|
||||||
|
return getattr(self,varname)
|
||||||
|
if varname in self.variable:
|
||||||
|
if filter_lang in self.variable[varname]:
|
||||||
|
return self.variable[varname][filter_lang]
|
||||||
|
else:
|
||||||
|
if filter_lang=="*":
|
||||||
|
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 create_lang(var,value):
|
||||||
"""Takes a variable (with an eventual language tag) and it's value and return var,lang,value
|
"""Takes a variable (with an eventual language tag) and it's value and return var,lang,value
|
||||||
|
|
@ -137,5 +154,9 @@ if __name__ == '__main__':
|
||||||
#for l in config.get_next_line("*"):
|
#for l in config.get_next_line("*"):
|
||||||
# print l
|
# print l
|
||||||
#print index.content
|
#print index.content
|
||||||
#print index.get_text("*")
|
print "All text only"
|
||||||
#print index.get_text("fr")
|
print index.get_text("*")
|
||||||
|
print "Fr text only"
|
||||||
|
print index.get_text("fr")
|
||||||
|
print "En text only"
|
||||||
|
print index.get_text("en")
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ class WebTree(object):
|
||||||
self.website_path=path
|
self.website_path=path
|
||||||
self.name=""
|
self.name=""
|
||||||
self.generation=""
|
self.generation=""
|
||||||
self.variable={"/":{}}
|
self.path=""
|
||||||
self.get_config()
|
self.get_config()
|
||||||
|
|
||||||
def make_walk(self,subpath=""):
|
def make_walk(self,subpath=""):
|
||||||
|
|
@ -17,28 +17,58 @@ class WebTree(object):
|
||||||
|
|
||||||
def get_config(self):
|
def get_config(self):
|
||||||
"Get the basic config from the website"
|
"Get the basic config from the website"
|
||||||
file_parser.parse_file(os.path.join(self.website_path,"_config.txt"))
|
self.config_path=os.path.join(self.website_path,self.path,"_config.txt")
|
||||||
|
self.config_file=file_parser.parse_file(self.config_path)
|
||||||
|
self.variables=self.config_file.variables
|
||||||
|
|
||||||
|
def get_variable(self,varname,filter_lang="*"):
|
||||||
|
"Get the corresponding variable"
|
||||||
|
self.config_file.get_variable(varname,filter_lang)
|
||||||
|
|
||||||
def convert_local(self,files):
|
def convert_local(self,files):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def print_webtree(self):
|
def print_webtree(self):
|
||||||
|
"Print a representation of the Tree"
|
||||||
print self
|
print self
|
||||||
print self.name
|
print self.name
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "WebTree of website "+self.website_path
|
return "WebTree of website "+self.website_path
|
||||||
|
|
||||||
|
def __contain__(self,var):
|
||||||
|
"Define the __in__ membership in the variables"
|
||||||
|
try:
|
||||||
|
self.get_variable(var)
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
class WebSubTree(WebTree):
|
class WebSubTree(WebTree):
|
||||||
"Share some methods with the main tree, only describes one folder"
|
"Share some methods with the main tree, only describes one folder"
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def get_variable(self,varname,filter_lang):
|
||||||
|
"Get the variable, either in it's config or one of the above"
|
||||||
|
try:
|
||||||
|
self.config_file.get_variable(varname,filter_lang)
|
||||||
|
except:
|
||||||
|
self.parent_node.get_variable(varname,filter_lang)
|
||||||
|
|
||||||
class WebNode(object):
|
class WebNode(object):
|
||||||
"A webfile, as seen from the tree, contains a link to a WebPage, generated from itself"
|
"A webfile, as seen from the tree, contains a link to a WebPage, generated from itself"
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
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:
|
||||||
|
self.file.get_variable(varname,filter_lang)
|
||||||
|
except:
|
||||||
|
try:
|
||||||
|
self.config_file.get_variable(varname,filter_lang)
|
||||||
|
except:
|
||||||
|
self.parent_node.get_variable(varname,filter_lang)
|
||||||
|
|
||||||
def makeWebsite(website):
|
def makeWebsite(website):
|
||||||
"Take a string of the folder name containing the website, will proceed to generate all needed Objects and return the Webtree"
|
"Take a string of the folder name containing the website, will proceed to generate all needed Objects and return the Webtree"
|
||||||
|
|
@ -52,4 +82,4 @@ if __name__ == '__main__':
|
||||||
# Do all the nominal tests
|
# Do all the nominal tests
|
||||||
s=makeWebsite("sites/example_website")
|
s=makeWebsite("sites/example_website")
|
||||||
s.print_webtree()
|
s.print_webtree()
|
||||||
s.list_dir()
|
s.make_walk()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue