-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcsl-count-rights-strings.py
49 lines (40 loc) · 1.5 KB
/
csl-count-rights-strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Python script to check how styles have been licensed
# Author: Rintze M. Zelle
# Version: 2012-05-12
# * Requires lxml library (http://lxml.de/)
#
# Prints the text contents of the cs:rights elements
import os, glob, re, inspect
from lxml import etree
# http://stackoverflow.com/questions/50499
folderPath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentFolderPath = os.path.dirname (folderPath)
path = os.path.join(parentFolderPath, 'styles')
uniqueStrings = {}
styles = []
stylesTested = 0
for stylepath in glob.glob( os.path.join(path, '*.csl') ):
styles.append(os.path.join(stylepath))
for stylepath in glob.glob( os.path.join(path, 'dependent', '*.csl') ):
styles.append(os.path.join(stylepath))
for style in styles:
parsedStyle = etree.parse(style)
styleElement = parsedStyle.getroot()
rightsStrings = []
try:
rightsStrings = styleElement.findall(".//{http://purl.org/net/xbiblio/csl}rights")
for rightsString in rightsStrings:
rightsString = rightsString.text
if rightsString in uniqueStrings:
uniqueStrings[rightsString] += 1
else:
uniqueStrings[rightsString] = 1
stylesTested += 1
except:
print(style)
pass
sortedStrings = sorted(uniqueStrings, key=uniqueStrings.get, reverse=True)
print("Styles tested: " + "%d" % (stylesTested))
print("Rights:")
for string in sortedStrings:
print('"' + string + '"' + ": %d" % (uniqueStrings[string]))