File: genkey.py

package info (click to toggle)
referencer 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,028 kB
  • ctags: 2,265
  • sloc: ansic: 32,973; cpp: 12,149; python: 1,314; xml: 1,258; sh: 1,154; makefile: 252
file content (135 lines) | stat: -rw-r--r-- 3,414 bytes parent folder | download | duplicates (3)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
 
#  Generate Bob08 Alice99 Alice99b type keys

import os
import referencer
from referencer import _

import gobject
import gtk

referencer_plugin_info = {
	"author":    "John Spray",  
	"version":   "1.1.2",
	"longname":  _("Generate keys from metadata"),
	"ui":
		"""
		<ui>
			<menubar name='MenuBar'>
				<menu action='ToolsMenu'>
					<placeholder name ='PluginToolsActions'>
						<menuitem action='_plugin_genkey_genkey'/>
					</placeholder>
				</menu>
			</menubar>

			<popup name='DocPopup'>
				<placeholder name='PluginDocPopupActions'>
					<menuitem action='_plugin_genkey_genkey'/>
				</placeholder>
			</popup>
		</ui>
		"""
	}

referencer_plugin_actions = []

action = {
	"name":"_plugin_genkey_genkey",
	"label":_("Generate Key"),
	"tooltip":_("Generate keys for the selected documents from their metadata"),
	"icon":"_stock:gtk-edit",
	"callback":"do_genkey",
	"sensitivity":"sensitivity_genkey",
	"accelerator":"<control>g"
}
referencer_plugin_actions.append (action)

def sensitivity_genkey (library, documents):
	if (len(documents) > 0):
		return True
	else:
	 	return False

# library is always Nil, it's only there for future proofing
# documents is a list of referencer.document
def do_genkey (library, documents):
	empty = True
	s = ""
	assigned_keys = {}

	format = referencer.pref_get ("genkey_format")
	if (len(format)==0):
		format = "%a%y"

	# Prompt the user for the key format
	dialog = gtk.Dialog (buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
	dialog.set_has_separator (False)
	dialog.vbox.set_spacing (6)
	dialog.set_default_response(gtk.RESPONSE_ACCEPT)
	hbox = gtk.HBox (spacing=6)
	label = gtk.Label ("Key format:")
	entry = gtk.Entry ()
	entry.set_text (format)
	entry.set_activates_default(True)
	dialog.vbox.pack_start (hbox)
	hbox.pack_start (label)
	hbox.pack_start (entry)
	
	label = gtk.Label ("Markers:\n\t%y = two-digit year\n\t%Y = four-digit year\n\t%a = first author's surname\n\t%t = title without spaces\n\t%w = first meaningful word of title")
	dialog.vbox.pack_start (label)

	dialog.show_all ()
	response = dialog.run ()
	dialog.hide ()

	if (response == gtk.RESPONSE_REJECT):
		return False
	
	format = entry.get_text ()

	for document in documents:
		author = document.get_field ("author")
		author = author.split("and")[0].split(",")[0].split(" ")[0]
		year = document.get_field ("year")
		title = document.get_field ("title")
		for c in ":-[]{},+/*.?":
			title = title.replace(c, '')
		title_capitalized = "".join([w[0].upper()+w[1:] for w in title.split()])
		first_word = [w for w in title.split() if 
			w.lower() not in ('a', 'an', 'the')][0]
		title = title.replace(' ', '')

		shortYear = year
		if len(year) == 4:
			shortYear = year[2:4]

		key = format
		key = key.replace ("%y", shortYear)
		key = key.replace ("%Y", year)
		key = key.replace ("%a", author)
		key = key.replace ("%t", title)
		key = key.replace ("%T", title_capitalized)
		key = key.replace ("%w", first_word)

		# Make the key unique within this document set
		append = "b"
		if assigned_keys.has_key (key):
			key += append
		
		# Assumes <26 identical keys
		while assigned_keys.has_key (key):
			append = chr(ord(append[0]) + 1)
			key = key[0:len(key) - 1]
			key += append

		print key
		assigned_keys[key] = True
			
		document.set_key (key)
	

	referencer.pref_set ("genkey_format", format)

	return True