Skip to content

Commit

Permalink
Basic configuration setup
Browse files Browse the repository at this point in the history
  • Loading branch information
thvitt committed Feb 18, 2019
1 parent 2d31087 commit f9d3d28
Show file tree
Hide file tree
Showing 14 changed files with 537 additions and 187 deletions.
2 changes: 1 addition & 1 deletion logging.yaml
Expand Up @@ -13,7 +13,7 @@ handlers:
root:
handlers:
- console
level: INFO
level: WARNING

loggers:
graph:
Expand Down
4 changes: 3 additions & 1 deletion src/macrogen/__init__.py
@@ -1,2 +1,4 @@
from .graph import macrogenesis_graphs, BiblSource
from .graph import macrogenesis_graphs
from macrogen.bibliography import BiblSource
from .uris import Reference, Witness, AmbiguousRef, Inscription
from .visualize import simplify_graph, write_dot, render_file, render_all
117 changes: 117 additions & 0 deletions src/macrogen/bibliography.py
@@ -0,0 +1,117 @@
import csv
from collections import namedtuple, defaultdict
from typing import Dict, Union, IO

import requests
from lxml import etree

from .config import config
from . import faust

BibEntry = namedtuple('BibEntry', ['uri', 'citation', 'reference', 'weight'])


def _parse_bibliography(bibxml: Union[str, IO]) -> Dict[str, BibEntry]:
"""Parses the bibliography file at url. Returns a dictionary mapping an URI to a corresponding bibliography entry."""
db: Dict[str, BibEntry] = {}
scores = config.bibscores
et = etree.parse(bibxml)
for bib in et.xpath('//f:bib', namespaces=faust.namespaces):
uri = bib.get('uri')
citation = bib.find('f:citation', namespaces=faust.namespaces).text
reference = bib.find('f:reference', namespaces=faust.namespaces).text
db[uri] = BibEntry(uri, citation, reference, scores[uri])
return db


_bib_labels = {
'faust://self': 'Faustedition',
'faust://model/inscription': 'Inskription von',
'faust://orphan/adoption': 'Datierungsinhalt für',
'faust://heuristic': 'künstliche Datierung'
}


class BiblSource:
"""
A bibliographic source in a macrogenesis XML file.
"""

def __init__(self, uri, detail=''):
"""
Creates a bibliographic source.
Args:
uri: should be a faust://bibliography/ URI or one of the special values
detail: detail string like pages
"""
self.uri = uri
if detail is None:
detail = ''
self.detail = detail
self.weight = config.bibliography[uri].weight if uri in config.bibliography else 1

def __eq__(self, other):
if isinstance(other, BiblSource):
return self.uri == other.uri and self.detail == other.detail
else:
return super().__eq__(other)

def __hash__(self):
return hash(self.uri) ^ hash(self.detail)

def __str__(self):
result = self.citation
if self.detail is not None:
result += '\n' + self.detail
return result

@property
def filename(self):
"""
A string representation of the source (w/o detail) that is usable as part of a filename.
"""
if self.uri.startswith('faust://bibliography'):
return self.uri.replace('faust://bibliography/', '')
else:
return self.uri.replace('faust://', '').replace('/', '-')

@property
def citation(self):
"""
String representation of only the citation, w/o detail.
Example:
Bohnenkamp 19994
"""
if self.uri in config.bibliography:
return config.bibliography[self.uri].citation
elif self.uri in _bib_labels:
return _bib_labels[self.uri]
else:
return self.filename

@property
def long_citation(self):
if self.uri in config.bibliography:
return config.bibliography[self.uri].reference
else:
return self.citation


def read_scores(scorefile):
"""
Parses the bibliography score file.
Returns:
Map uri -> score
"""
scores = defaultdict(lambda: 1)
logger = config.getLogger(__name__)
r = csv.reader(scorefile, delimiter='\t')
for row in r:
try:
scores[row[0]] = int(row[1])
except ValueError as e:
logger.warning('Skipping scorefile row %s: %s', row, e)
return scores

0 comments on commit f9d3d28

Please sign in to comment.