| 1 |
|
| 2 |
# http://sourceforge.net/mailarchive/message.php?msg_id=25355232 |
| 3 |
|
| 4 |
# http://dret.net/bibconvert/tex2unicode |
| 5 |
|
| 6 |
from warnings import warn |
| 7 |
|
| 8 |
from .. import VARIABLES |
| 9 |
|
| 10 |
|
| 11 |
class CustomDict(dict): |
| 12 |
def __init__(self, args, required=set(), optional=set(), required_or=[]): |
| 13 |
passed_keywords = set(args.keys()) |
| 14 |
missing = required - passed_keywords |
| 15 |
if missing: |
| 16 |
raise TypeError('The following required arguments are missing: ' + |
| 17 |
', '.join(missing)) |
| 18 |
required_or_merged = set() |
| 19 |
for required_options in required_or: |
| 20 |
if not passed_keywords & required_options: |
| 21 |
raise TypeError('Require at least one of: ' + |
| 22 |
', '.join(required_options)) |
| 23 |
required_or_merged |= required_options |
| 24 |
unsupported = passed_keywords - required - optional - required_or_merged |
| 25 |
if unsupported: |
| 26 |
cls_name = self.__class__.__name__ |
| 27 |
warn('The following arguments for {} are '.format(cls_name) + |
| 28 |
'unsupported: ' + ', '.join(unsupported)) |
| 29 |
self.update(args) |
| 30 |
|
| 31 |
def __setattr__(self, name, value): |
| 32 |
self[name] = value |
| 33 |
|
| 34 |
def __getattr__(self, name): |
| 35 |
return self[name] |
| 36 |
|
| 37 |
def __getitem__(self, key): |
| 38 |
try: |
| 39 |
return super(CustomDict, self).__getitem__(key) |
| 40 |
except KeyError: |
| 41 |
raise VariableError |
| 42 |
|
| 43 |
|
| 44 |
class Reference(CustomDict): |
| 45 |
def __init__(self, key, type, **args): |
| 46 |
self.key = key |
| 47 |
self.type = type |
| 48 |
#required_or = [set(csl.VARIABLES)] |
| 49 |
optional = ({'uri', 'container_uri', 'contributor', 'date'} | |
| 50 |
set(VARIABLES)) |
| 51 |
super(Reference, self).__init__(args, optional=optional) |
| 52 |
|
| 53 |
def __repr__(self): |
| 54 |
return '{}({})'.format(self.__class__.__name__, self.key) |
| 55 |
|
| 56 |
|
| 57 |
class VariableError(Exception): |
| 58 |
pass |
| 59 |
|
| 60 |
|
| 61 |
class Name(CustomDict): |
| 62 |
def __init__(self, **args): |
| 63 |
if 'literal' in args: |
| 64 |
required, optional = {'literal'}, set() |
| 65 |
else: |
| 66 |
required = set() |
| 67 |
optional = {'family', 'given', 'dropping-particle', 'non-dropping-particle', |
| 68 |
'suffix'} |
| 69 |
super(Name, self).__init__(args, required, optional) |
| 70 |
|
| 71 |
def parts(self): |
| 72 |
if 'literal' in self: |
| 73 |
return (None, self['literal'], None, None, None) |
| 74 |
else: |
| 75 |
return (self.get('given'), self.get('family'), |
| 76 |
self.get('dropping-particle'), |
| 77 |
self.get('non-dropping-particle'), self.get('suffix')) |
| 78 |
|
| 79 |
|
| 80 |
class DateBase(CustomDict): |
| 81 |
def __init__(self, args, required=set(), optional=set()): |
| 82 |
optional = {'circa'} | optional |
| 83 |
super(DateBase, self).__init__(args, required, optional) |
| 84 |
# defaults |
| 85 |
if 'circa' not in self: |
| 86 |
self['circa'] = False |
| 87 |
|
| 88 |
|
| 89 |
class Date(DateBase): |
| 90 |
def __init__(self, **args): |
| 91 |
required = {'year'} |
| 92 |
optional = {'month', 'day', 'season'} |
| 93 |
if 'day' in args and 'month' not in args: |
| 94 |
raise TypeError('When specifying the day, you should also specify ' |
| 95 |
'the month') |
| 96 |
for key, value in args.items(): |
| 97 |
try: |
| 98 |
args[key] = int(value) |
| 99 |
except ValueError: |
| 100 |
pass |
| 101 |
super(Date, self).__init__(args, required, optional) |
| 102 |
|
| 103 |
def sort_key(self): |
| 104 |
year = self.year |
| 105 |
month = self.get('month', 0) |
| 106 |
day = self.get('day', 0) |
| 107 |
return '{:05}{:02}{:02}'.format(year + 10000, month, day) |
| 108 |
|
| 109 |
def is_nil(self): |
| 110 |
return (self.year == 0 and self.get('month', 0) == 0 and |
| 111 |
self.get('day', 0) == 0) |
| 112 |
|
| 113 |
|
| 114 |
class LiteralDate(DateBase): |
| 115 |
def __init__(self, text, **args): |
| 116 |
self.text = text |
| 117 |
super(LiteralDate, self).__init__(args) |
| 118 |
|
| 119 |
def sort_key(self): |
| 120 |
return self.text |
| 121 |
|
| 122 |
|
| 123 |
class DateRange(DateBase): |
| 124 |
def __init__(self, **args): |
| 125 |
required = {'begin'} |
| 126 |
optional = {'end'} |
| 127 |
super(DateRange, self).__init__(args, required, optional) |
| 128 |
|
| 129 |
def sort_key(self): |
| 130 |
begin = self.begin.sort_key() |
| 131 |
end = self.get('end', Date(year=0)).sort_key() |
| 132 |
return begin + '-' + end |
| 133 |
|
| 134 |
def __eq__(self, other): |
| 135 |
# TODO: for sorting |
| 136 |
raise NotImplementedError |
| 137 |
|
| 138 |
|
| 139 |
class Citation(CustomDict): |
| 140 |
def __init__(self, cites, **kwargs): |
| 141 |
for cite in cites: |
| 142 |
cite.citation = self |
| 143 |
self.cites = cites |
| 144 |
super(Citation, self).__init__(kwargs) |
| 145 |
|
| 146 |
def __repr__(self): |
| 147 |
cites = ', '.join([cite.key for cite in self.cites]) |
| 148 |
return '{}({})'.format(self.__class__.__name__, cites) |
| 149 |
|
| 150 |
|
| 151 |
class CitationItem(CustomDict): |
| 152 |
def __init__(self, key, bibliography=None, **args): |
| 153 |
self.key = key.lower() |
| 154 |
optional = {'locator', 'prefix', 'suffix'} |
| 155 |
super(CitationItem, self).__init__(args, optional=optional) |
| 156 |
|
| 157 |
def __repr__(self): |
| 158 |
return '{}({})'.format(self.__class__.__name__, self.key) |
| 159 |
|
| 160 |
@property |
| 161 |
def bibliography(self): |
| 162 |
return self.citation.bibliography |
| 163 |
|
| 164 |
@property |
| 165 |
def reference(self): |
| 166 |
return self.bibliography.source[self.key] |
| 167 |
|
| 168 |
@property |
| 169 |
def number(self): |
| 170 |
return self.bibliography.keys.index(self.key) + 1 |
| 171 |
|
| 172 |
@property |
| 173 |
def has_locator(self): |
| 174 |
return 'locator' in self |
| 175 |
|
| 176 |
def get_field(self, field): |
| 177 |
string = self.reference.get(field) |
| 178 |
if string is not None: |
| 179 |
return self.bibliography.formatter.preformat(string) |
| 180 |
|
| 181 |
def is_bad(self): |
| 182 |
return self.key not in self.bibliography.keys |
| 183 |
|
| 184 |
|
| 185 |
class Locator(object): |
| 186 |
def __init__(self, label, identifier): |
| 187 |
self.label = label |
| 188 |
self.identifier = identifier |
| 189 |
|
| 190 |
|
| 191 |
class BibliographySource(dict): |
| 192 |
def add(self, entry): |
| 193 |
self[entry.key] = entry |
| 194 |
|
| 195 |
|
| 196 |
from . import bibtex, json |