Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update to QC script

  • Loading branch information...
commit 3e85ed1a5e01e847716693a9e2971015c4904a8e 1 parent ac08df6
@rmzelle rmzelle authored
Showing with 87 additions and 20 deletions.
  1. +87 −20 csl-qc.py
View
107 csl-qc.py
@@ -1,54 +1,121 @@
+# Python script for additional style validation
+# Author: Rintze M. Zelle
+# Requires lxml library (http://lxml.de/)
+# Checks
+# - whether ".csl" files conform to naming scheme [a-z0-9] with optional single
+# hyphen delimiters
+# - whether style ID (content of cs:id) matches the URI generated by the Zotero
+# Style Repository:
+# "http://www.zotero.org/styles/" + filename - extension
+# - whether "self" link (value of "href" on cs:link with rel="self") matches the
+# Zotero Style Repository
+# - for independent styles, whether "template" link (value of "href" on cs:link
+# with rel="template") points to the Zotero Style Repository style URI of an
+# independent style.
+# - for dependent styles, whether "independent-parent" link (value of "href" on
+# cs:link with rel="template") points to the Zotero Style Repository style URI
+# of an independent style.
+
import os, glob, re
from lxml import etree
-def parseStyle(independentStyle):
- style = etree.parse (independentStyle)
+path = 'C:\Users\Rintze Zelle\Documents\git\styles\\'
+
+def parseStyle(stylePath):
+ style = etree.parse(stylePath)
styleElement = style.getroot()
metadata = {}
try:
metadata["id"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}id").text
metadata["selfLink"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}link[@rel='self']").attrib.get("href")
+ except:
+ pass
+ try:
metadata["template"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}link[@rel='template']").attrib.get("href")
except:
pass
+ try:
+ metadata["independentParent"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}link[@rel='independent-parent']").attrib.get("href")
+ except:
+ pass
return(metadata)
-path = 'C:\Users\Rintze Zelle\Documents\git\styles\\'
+def checkFileName(fileName):
+ if not(re.match("[a-z0-9](-?[a-z0-9]+)*(.csl)", fileName)):
+ print("Non-conforming filename: " + fileName)
+
metadataList = []
metadata = {}
for independentStyle in glob.glob( os.path.join(path, '*.csl') ):
fileName = os.path.basename(independentStyle)
- if not(re.match("[a-z0-9](-?[a-z0-9]+)*(.csl)", fileName)):
- print("Non-conforming filename: " + fileName)
+ checkFileName(fileName)
metadata = parseStyle(independentStyle)
metadata["fileName"] = fileName
try:
if not(("http://www.zotero.org/styles/"+fileName) == (metadata["selfLink"]+".csl")):
- print("ID - cs:link[@rel=self] mismatch: " + fileName)
+ print("Name Mismatch - Filename & Style URI (value 'href' on cs:link[@rel=self]): " + fileName)
except:
- print("Missing cs:link[@rel=self] value: " + fileName)
+ print("Missing Style URI (value 'href' on cs:link[@rel=self]): " + fileName)
try:
if not(("http://www.zotero.org/styles/"+fileName) == (metadata["id"]+".csl")):
- print("ID - filename mismatch: " + fileName)
+ print("Name Mismatch - Filename & Style ID (content cs:id): " + fileName)
except:
- print("Missing cs:id content: " + fileName)
+ print("Missing Style ID (content cs:id): " + fileName)
metadataList.append(metadata)
-#for set in metadataList:
-# print(set)
-# print(set["fileName"])
+for queryMetadataDict in metadataList:
+ match = True
+ try:
+ if(queryMetadataDict["template"]):
+ match = False
+ for metadataDict in metadataList:
+ if(queryMetadataDict["template"] == metadataDict["selfLink"]):
+ match = True
+ if(match == False):
+ print("Non-existing Style Template URI (value 'href' on cs:link[@rel=template]): " + queryMetadataDict["fileName"])
+ except:
+ pass
-#print(idList)
+metadataListDependents = []
+metadataDependents = {}
+for dependentStyle in glob.glob( os.path.join(path, "dependent", '*.csl') ):
+ fileName = os.path.basename(dependentStyle)
+
+ checkFileName(fileName)
-# Use dict instead? Can use filename as key
-# What do I really want? Easy way to compare id, self-link and filename, and compare values to other sets
+ metadataDependents = parseStyle(dependentStyle)
+ metadataDependents["fileName"] = fileName
+
+ try:
+ if not(("http://www.zotero.org/styles/"+fileName) == (metadataDependents["selfLink"]+".csl")):
+ print("Name Mismatch - Filename & Style URI (value 'href' on cs:link[@rel=self]): (dependent) " + fileName)
+ except:
+ pass
+ try:
+ if not(("http://www.zotero.org/styles/"+fileName) == (metadataDependents["id"]+".csl")):
+ print("Name Mismatch - Filename & Style ID (content cs:id): (dependent) " + fileName)
+ except:
+ print("Missing Style ID (content cs:id): (dependent) " + fileName)
+ try:
+ metadataDependents["independentParent"]
+ except:
+ print("Missing Parent Style ID value 'href' on cs:link[@rel=independent-parent]): (dependent) " + fileName)
+
+ metadataListDependents.append(metadataDependents)
-# Start with independent styles
-# Store filename, ID, self and template links
-# Report error for missing ID and self link
-# Check whether filename conforms to requirements ( ^[a-z0-9][
-# Split ID and self link in "http://www.zotero.org/styles/" and filename part
+for queryMetadataDict in metadataListDependents:
+ match = True
+ try:
+ if(queryMetadataDict["independentParent"]):
+ match = False
+ for metadataDict in metadataList:
+ if(queryMetadataDict["independentParent"] == metadataDict["selfLink"]):
+ match = True
+ if(match == False):
+ print("Non-existing Parent Style URI (value 'href' on cs:link[@rel=independent-parent]): (dependent) " + queryMetadataDict["fileName"])
+ except:
+ pass
Please sign in to comment.
Something went wrong with that request. Please try again.