Minor improvements

This commit is contained in:
Malphaet 2017-03-14 12:45:33 +01:00
parent f9f442e7ce
commit b32b5c2ef1
2 changed files with 61 additions and 10 deletions

View File

@ -25,20 +25,30 @@ class WebPage(object):
setattr(self,key,"")
def set_reserved(self,var,value=""):
"Set a reserved variable"
setattr(self,self.reserved_prefix+key,value)
def get_reserved(self,var):
"Get a reserved variable"
getattr(self,self.reserved_prefix+var)
def add_content(self,text,lang):
"Add a line of content, with the appropriates langues"
self.content.append([text,lang])
def get_next_line(self,filter_lang="*"):
"Get a line of text, with a filter if needed"
for line,lang in self.content:
if filter_lang in lang:
if self.match_with_lang(lang,filter_lang):
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):
"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=""
@ -47,10 +57,6 @@ class WebPage(object):
text+=os.linesep
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):
"""Add the folowing variable and update the necessary constants
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.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):
"""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("*"):
# print l
#print index.content
#print index.get_text("*")
#print index.get_text("fr")
print "All text only"
print index.get_text("*")
print "Fr text only"
print index.get_text("fr")
print "En text only"
print index.get_text("en")

View File

@ -7,7 +7,7 @@ class WebTree(object):
self.website_path=path
self.name=""
self.generation=""
self.variable={"/":{}}
self.path=""
self.get_config()
def make_walk(self,subpath=""):
@ -17,28 +17,58 @@ class WebTree(object):
def get_config(self):
"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):
pass
def print_webtree(self):
"Print a representation of the Tree"
print self
print self.name
def __repr__(self):
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):
"Share some methods with the main tree, only describes one folder"
def __init__(self):
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):
"A webfile, as seen from the tree, contains a link to a WebPage, generated from itself"
def __init__(self):
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):
"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
s=makeWebsite("sites/example_website")
s.print_webtree()
s.list_dir()
s.make_walk()