Skip to content

Commit

Permalink
Updated migrate script
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Sep 14, 2021
1 parent aecc945 commit 407b1e6
Showing 1 changed file with 90 additions and 6 deletions.
96 changes: 90 additions & 6 deletions migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import json
import datetime
import pypandoc
from PySide2 import QtCore

unhandledTemplates = [] # holder for unhandled templates

Expand All @@ -91,7 +92,8 @@ def __init__(self,url="https://wiki.freecadweb.org"):
self.readCache()
self.imagecount = 0
self.images = {}
self.output = os.path.join(os.path.dirname(__file__),"wiki")
self.wikifolder = "wiki"
self.output = os.path.join(os.path.dirname(__file__),self.wikifolder)
if not os.path.exists(self.output):
os.mkdir(self.output)
self.imagefolder = "images"
Expand Down Expand Up @@ -242,6 +244,7 @@ def getAllPages(self,pageset=None):
overwrite = False
if not self.pagenames:
self.getPageNames()
self.getCategories()
pageset = self.pagenames
count = 1
for page in pageset:
Expand All @@ -261,6 +264,9 @@ def getAllPages(self,pageset=None):
return revisions


### CATEGORIES


def getCategories(self):

"""getCategories():
Expand Down Expand Up @@ -332,6 +338,19 @@ def getCategoryContents(self,name):
break
return members

def getPageCategory(self,page):

"""getPageCategory(self,page):
Returns the first category a page belongs to, or None"""

if page in self.pagecontents:
content = self.pagecontents[page]
cats = re.findall("Category:(.*?)[{}\[]",content)
if cats:
return cats[0]
if re.findall("Arch Tools navi",content):
return "Arch"
return None


### IMAGE OPERATIONS
Expand Down Expand Up @@ -500,7 +519,7 @@ def readRevisions(self,filename=None):
jsonfiles = [f for f in files if (f.endswith(".json") and f.startswith("revision"))]
jsonfiles.sort(key=os.path.getmtime, reverse=True)
filename = os.path.join(os.path.dirname(__file__),jsonfiles[0])
print("Opening",filename)
print("Opening",os.path.basename(filename))
if os.path.exists(filename):
with open(filename) as jfile:
data = json.load(jfile)
Expand All @@ -518,7 +537,9 @@ def updateRevisions(self,oldrevision,newrevision):
if oldrevision[page] == revid:
continue
pageset.append(page)
print(len(pageset),"pages (",int((len(pageset)/len(self.pagenames))*100),"% ) to update...")
self.getAllPages(pageset)
return pageset


### MARKDOWN OPERATIONS
Expand Down Expand Up @@ -728,13 +749,55 @@ def writeAllPages(self,overwrite=True,basepath=None):
errors.append(r)
count += 1
self.printProgress()
print("All done!\n")
print("SUMMARY:\n")
if unhandledTemplates:
print("unhandled templates: ",unhandledTemplates)
print("\n")
if errors:
print("page with write errors: ",errors)
return errors



### GENERAL FUNCTIONS
def updateReadme(self):

"""updateReadme():
Updates the main page with a list of available translations"""

mainpage = os.path.join(os.path.dirname(__file__),"README.md")
tfolder = os.path.join(self.output,self.translationfolder)
column = 1
output = ""
for lcode in os.listdir(tfolder):
lfolder = os.path.join(tfolder,lcode)
if (lcode != "en") and os.path.isdir(lfolder) and ("Main_Page.md" in os.listdir(lfolder)):
l = QtCore.QLocale(lcode)
lname = l.languageToString(l.language())
if ("-" in lcode) or ("_" in lcode):
lname += " ("+l.country().name.decode("utf8")+")"
ltr = l.nativeLanguageName()
print(lname)
output += "| ![Flag "+lcode+"]("+self.wikifolder+"/"+self.imagefolder
output += "/Flag-"+lcode+".jpg) ["+lname+" / "+ltr+"]("+self.wikifolder+"/"
output += self.translationfolder+"/"+lcode+"/Main_Page.md) "
if column == 3:
output += "|\n"
column = 1
else:
column += 1
output += "\n\n\n\n## Get involved"
f = open(mainpage,"r")
b = f.read()
f.close()
#b = b.replace("## Get involved",output)
b = re.sub("\|\ \!\[Flag.*?\#\# Get involved",output,b,flags=re.DOTALL|re.MULTILINE)
f = open(mainpage,"w")
f.write(b)
f.close()



### GENERAL FUNCTIONS (USABLE THROUGH COMMAND-LINE)



Expand All @@ -758,10 +821,23 @@ def update():
if not wiki.pagecontents:
wiki.getAllPages()
newrevisions = wiki.getRevisions()
wiki.updateRevisions(oldrevisions,newrevisions)
pageset = wiki.updateRevisions(oldrevisions,newrevisions)
print("Writing",len(pageset),"pages...")
errors = []
count = 1
for page in pageset:
wiki.printProgress(count,len(pageset),"Saving page "+page+"...")
r = wiki.writeMarkdown(page,overwrite=True)
if r:
errors.append(r)
count += 1
self.printProgress()
wiki.writeAllPages()
wiki.getAllImages()
print("All done!")
wiki.updateReadme()
print("All done!\n")
if errors:
print("page with write errors: ",errors)


def test():
Expand Down Expand Up @@ -800,6 +876,14 @@ def writeimages():
wiki.getAllImages()


def updatereadme():

"""updates the README.md with the list of translations"""

wiki = MediaWiki()
wiki.updateReadme()



### COMMAND-LINE USAGE

Expand Down

0 comments on commit 407b1e6

Please sign in to comment.