Back to index

plone3  3.1.7
scanner.py
Go to the documentation of this file.
00001 import os, re
00002 
00003 IDPATTERN = re.compile(r'\$\Id[^$]*\$')
00004 JSPATTERN = re.compile(r'(?:string:\$\{portal_url\}/([^."]+.js)")|(?:<link href="([^."]*.css)")')
00005 KWS = 'kupu_wysiwyg_support'
00006 ROOT = os.path.dirname(os.path.dirname(__file__))
00007 
00008 def matchFiles(extension, root, paths):
00009     for dirname in paths:
00010         dirname = os.path.join(root, dirname)
00011         files = os.listdir(dirname)
00012         for f in files:
00013             if f.endswith(extension):
00014                 yield f, os.path.join(dirname, f)
00015 
00016 def getFileData(path):
00017     fh = open(path, 'rU')
00018     try:
00019         return fh.read()
00020     finally:
00021         fh.close()
00022 
00023 def getKWS(root):
00024     return os.path.join(root, 'plone', 'kupu_plone_layer', KWS+'.html')
00025 
00026 def scanFile(path):
00027     '''Scan a single file returning all the Id strings it contains'''
00028     ids = IDPATTERN.findall(getFileData(path))
00029     return ids
00030 
00031     
00032 def scanKWS(root=ROOT):
00033     try:
00034         wysiwyg = scanFile(getKWS(root))
00035     except:
00036         return KWS, "cannot open template: run make"
00037 
00038     wysiwyg = dict.fromkeys(wysiwyg)
00039 
00040     for fname, path in matchFiles('.kupu', root, ('default', 'plone')):
00041         for id in scanFile(path):
00042             if id in wysiwyg:
00043                 del wysiwyg[id]
00044     if wysiwyg:
00045         return KWS, "template appears to be out of date: run make"
00046     return KWS, ''
00047 
00048 def scanIds(root=ROOT):
00049     status = {}
00050     wanted = {}
00051     for groups in JSPATTERN.findall(getFileData(getKWS(root))):
00052         for group in groups:
00053             if group:
00054                 wanted[group] = None
00055 
00056     wanted = dict([ (name, None)
00057         for groups in JSPATTERN.findall(getFileData(getKWS(root)))
00058             for name in groups if name])
00059 
00060     for fname, path in matchFiles('.js', root, ('common', os.path.join('plone', 'kupu_plone_layer'))):
00061         if fname in wanted:
00062             for id in scanFile(path):
00063                 status[fname] = id
00064     res = status.items()
00065     res.sort()
00066     return res
00067 
00068 if __name__=='__main__':
00069     print scanKWS('..')
00070     print scanIds('..')