Commit 14e5b897 authored by gijs's avatar gijs
Browse files

More debug info

parent d60faf58
from . import fields
from .utils import info, debug
import datetime
import re
......@@ -7,6 +8,7 @@ import re
import markdown
from django.utils.safestring import mark_safe
def keyFilter (value):
if type(value) is list:
return '--'.join([keyFilter(v) for v in value])
......@@ -70,7 +72,7 @@ class ReverseMultiLink(ReverseLink):
links = getattr(obj, self.linkName)
if type(links) is not list:
# debug(self.linkName, obj.key, target.key, type(links))
debug(self.linkName, obj.key, target.key, type(links))
raise LinkExistsError
else:
links = []
......@@ -88,19 +90,42 @@ class Model(object):
content = None
keyField = 'id'
metadata = {}
def __init__ (self, key=None, metadata=None, content=None):
print('Keyfield {}'.format(self.keyField))
if key:
self.key = key
elif metadata:
self.key = keyFilter(metadata[self.keyField]) if self.keyField in metadata else keyFilter(metadata['pk']) if 'pk' in metadata else None
else:
self.key = self.extractKey(metadata)
self.metadata = {}
if metadata:
self.setMetadata(metadata)
self.empty = True
if metadata or content:
self.fill(metadata=metadata, content=content)
@classmethod
def extractKey(cls, data):
if cls.keyField in data:
return keyFilter(data[cls.keyField])
elif 'pk' in data:
return keyFilter(data['pk'])
else:
raise ValueError("Object doesn't have any key")
def setMetadata(self, metadata=None):
if metadata:
for key in metadata:
self.__setattr__(key, metadata[key])
def fill(self, metadata={}, content=None):
if metadata:
self.empty = False
self.setMetadata(metadata)
if content:
self.empty = False
self.content = content
def __setattr__ (self, name, value):
......@@ -112,7 +137,6 @@ class Model(object):
elif name == 'content':
super().__setattr__('content', value)
elif name in self.metadataFields:
print(name)
if is_link(self.metadataFields[name]):
# If it is a link we also include, the obj
self.metadata[name] = self.metadataFields[name](value, self)
......@@ -163,23 +187,32 @@ class Collection(object):
def get (self, key, instantiate=True):
key = keyFilter(key)
if key in self.index:
debug('Found entry for {}'.format(key))
return self.index[key]
elif key and instantiate:
return self.instantiate(key)
debug('Could not find entry for {}, instantiating'.format(key))
return self.instantiateStub(key)
else:
return None
def has (self, key):
return key in self.index
"""
Register the given model with the collection
"""
def register (self, obj):
if isinstance(obj, self.model):
if obj.key not in self.index:
if not self.has(obj.key):
self.models.append(obj)
self.index[obj.key] = obj
elif self.index[obj.key].empty:
debug('Updating metadata for stub {}'.format(obj.key))
self.index[obj.key].setMetadata(obj.meta)
else:
print('Already have', obj, obj.key)
# Extend the object here
debug('Already have', obj, obj.key)
"""
Instantiate a model for the given key, metadata and content
and register it on the collection.
......@@ -189,6 +222,11 @@ class Collection(object):
self.register(obj)
return obj
def instantiateStub (self, key):
obj = self.model(key=key)
self.register(obj)
return obj
def linkMultiReverse(contentType, reverseName):
return Link(contentType=contentType, reverse=ReverseMultiLink(reverseName))
......
......@@ -3,6 +3,7 @@ import os.path
import urllib
from .models import modelFor, collectionFor, UnknownContentTypeError
from .utils import info, debug, error
from markdown.extensions.toc import TocExtension
from py_etherpad import EtherpadLiteClient
......@@ -24,7 +25,7 @@ def parse_pads ():
padID = pad.publicpadid if pad.is_public else pad.group.groupID + '$' + urllib.parse.quote(pad.name.replace(PAD_NAMESPACE_SEPARATOR, '_'))
source = epclient.getText(padID)['text']
print('\nReading {}'.format(pad.display_slug))
info('Reading {}'.format(pad.display_slug))
if extension in ['.md', '.markdown']:
md = markdown.Markdown(extensions=['extra', 'meta', TocExtension(baselevel=2), 'attr_list'])
......@@ -40,14 +41,24 @@ def parse_pads ():
if meta['type'] == ['biography']:
meta['type'] = ['produser']
collection = collectionFor(meta['type'][0])
collection.register(modelFor(meta['type'][0])(metadata=meta, content=content))
key = modelFor(meta['type'][0]).extractKey(meta)
debug('Extracted key: {}'.format(key))
model = collection.get(key)
if model.empty:
debug('Filling model {}'.format(key))
model.fill(metadata=meta, content=content)
else:
error('Model for key {} already filled'.format(key))
except UnknownContentTypeError as e:
print('Skipped `{}`'.format(name))
print(e)
pass
print('Read {}'.format(pad.display_slug))
info('Read {}'.format(pad.display_slug))
class Command(BaseCommand):
args = ''
......
DEBUG = True
CRED = '\033[91m'
CGREEN = '\033[92m'
CYELLOW = '\033[93m'
CMAGENTA = '\033[95m'
CCYAN = '\033[96m'
CEND = '\033[0m'
def info(*args):
print(*args)
def debug(*args, color=CYELLOW):
if DEBUG:
print(color, *args, CEND)
def error(*args):
print(CRED, *args, CEND)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment