# -*- coding: utf-8
# Copyright 2004-2006 by Vahur Rebas
"""This is a container for errors and error."""
import Globals
from Globals import Acquisition, Persistent
from AccessControl import ClassSecurityInfo
from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2
from Acquisition import aq_base, aq_inner, aq_parent, Implicit
from zope.interface import implements
from interfaces import IErrors
from permissions import *
import re
import logging
logger=logging.getLogger("korpus")
class Errors(Persistent, BTreeFolder2,Implicit):
""" errors """
meta_type = 'Errors'
security = ClassSecurityInfo()
security.declareObjectPublic()
id = 'Errors'
implements(IErrors)
def __init__(self):
""" init method """
BTreeFolder2.__init__(self, 'Errors')
security.declarePrivate('manage_afterAdd')
def manage_afterAdd(self, item, container):
""" manage after add """
from zExceptions import BadRequest
try:
self.setupErrorsCatalog()
except BadRequest:
pass
security.declareProtected(perm_manage, 'setupErrorsCatalog')
def setupErrorsCatalog(self):
""" setup errors catalog """
from Products.ZCatalog.ZCatalog import ZCatalog
cat = ZCatalog('errors_catalog', 'Catalog for marked areas')
cat.addIndex('pointer', 'FieldIndex')
cat.addIndex('document', 'FieldIndex')
extra = {'default_encoding': 'utf-8', 'use_normalizer': 0, 'dedicated_storage': 1, 'splitter_casefolding': 1, 'index_unknown_languages': 1}
cat.addIndex('content', 'TextIndexNG3', extra=extra)
cat.addIndex('code', 'FieldIndex')
cat.addIndex('author', 'FieldIndex')
cat.addIndex('is_deleted', 'FieldIndex')
cat.addColumn('pointer', 'FieldIndex')
cat.addColumn('document', 'FieldIndex')
cat.addColumn('content', 'FieldIndex')
cat.addColumn('code', 'FieldIndex')
cat.addColumn('author', 'FieldIndex')
cat.addColumn('is_deleted', 'FieldIndex')
self._setObject('errors_catalog', cat)
security.declareProtected(perm_manage, 'checkForErrors')
def checkForErrors(self, REQUEST):
""" check for errors. cleanup errors that doesn't have real code object to match """
not_valid = 0
for x in self.objectValues('Error'):
code = x.getProperty('code')
code_obj = self.Marks.getCode(code)
if not code_obj:
not_valid += 1
self.deleteError(x.getId())
x.reindex_object()
print "Found %s not valid error(s)" % not_valid
return "ok"
security.declareProtected(perm_edit_tree, 'addNewError')
def addNewError(self, pointer, content, document, code, author, pre, post):
""" add new error """
#logger.info("Harry katsetused-13")
#logger.info(pointer)
#logger.info(content)
#logger.info(document)
#logger.info(code)
#logger.info(author)
#logger.info(pre)
#logger.info(post)
id = self.generateId(prefix='error_', suffix='_item', rand_ceiling=999999999999)
e = Error(id, pointer, content, document, code, author)
e.addPreContext(pre)
e.addPostContext(post)
self._setObject(id, e)
return getattr(self, id)
security.declareProtected(perm_mark_document, 'getDocumentMarksHTML')
def getDocumentMarksHTML(self, doc_id, uname):
""" return marks for document. output in HTML for marking page """
res = ''
count = 0
marks = self.getDocumentMarks(doc_id)
for x in marks:
pointer = x.getProperty('pointer')
code = x.getProperty('code')
mark = self.Marks.getCode(code)
#print code, uname, x, mark.isPublished(code)
if (not mark.isPublished(code) and not code.startswith(uname)) and (not code.startswith('global_') and not code.startswith('text_')):
continue
count = count + 1
start = pointer.split(';')[0].split('#')[0]
end = pointer.split(';')[1].split('#')[0]
res += '
' % str(count)
try:
res += mark.getTitle().decode('utf-8')
except AttributeError:
res += 'gone'
res += '' % (str(count), str(count), pointer)
res += '' % (str(count), str(count), code)
res += '' % (str(count), str(count), x.getId())
res += '
'
return res.encode('utf-8')
security.declareProtected(perm_view_document, 'getDocumentMarks')
def getDocumentMarks(self, doc_id, uname=''):
""" return marks for document """
q = {'document': doc_id, 'is_deleted': 0}
q_res = self.errors_catalog(q)
res = []
[ res.append(x.getObject()) for x in q_res ]
marks = []
for x in res:
if uname:
code = x.getProperty('code')
mark = self.Marks.getCode(code)
#if not mark.isPublished(code) and not code.startswith(uname):
if (not mark.isPublished(code) and not code.startswith(uname)) and (not code.startswith('global_') and not code.startswith('text_')):
continue
marks.append(x)
marks.sort()
#print "result:", marks
return marks
security.declareProtected(perm_view_document, 'queryErrors')
def queryErrors(self, query, is_deleted=0):
""" query for errors """
query['is_deleted'] = is_deleted
res = self.errors_catalog(query)
#print "error res", res
return res
security.declareProtected(perm_mark_document, 'deleteError')
def deleteError(self, err_id):
""" delete error """
x = getattr(self, err_id)
x._setDeleted()
security.declareProtected(perm_manage, 'get_error_authors')
def get_error_authors(self):
""" how many who has marked """
d = {}
counted = {}
for x in self.objectValues('Error'):
a = x.getProperty('author')
if not d.has_key(a):
d[a] = 0
if not counted.has_key(a):
counted[a] = {'total': 0, 'counted': []}
d[a] = d[a] + 1
nw = 0
if x.document not in counted[a]['counted']:
nw = getattr(self.Documents, x.document).get_n_of_words()
counted[a]['counted'].append(x.document)
counted[a]['total'] += nw
msg = ""
for x in d.keys():
msg += x+": "+str(d[x])+" sonu:"+str(counted[x]['total'])+" dokumente:"+str(len(counted[x]['counted']))
msg += "\n"
return msg
Globals.InitializeClass(Errors)
from OFS.SimpleItem import SimpleItem
from OFS.PropertyManager import PropertyManager
from Products.ZCatalog.CatalogPathAwareness import CatalogAware
from zExceptions import BadRequest
class Error(Persistent, SimpleItem, CatalogAware, PropertyManager):
""" error. binds Mark and Document """
meta_type = 'Error'
security = ClassSecurityInfo()
security.declareObjectPublic()
manage_options = PropertyManager.manage_options+SimpleItem.manage_options
def __init__(self, _id, pointer, content, document, code, author):
self.default_catalog = 'errors_catalog'
self.id = _id
self._setProperty('pointer', pointer, 'string')
self._setProperty('document', document, 'string')
self._setProperty('content', content, 'string')
self._setProperty('code', code, 'string')
self._setProperty('author', author, 'string')
self._setProperty('is_deleted', 0, 'boolean')
self.pre = []
self.post = []
def __cmp__(self, other):
""" -100 siis kui left on ennem right-i """
left = self.getProperty('pointer')
right = other.getProperty('pointer')
left_start, left_end = left.split(';')
right_start, right_end = right.split(';')
left_xpath, left_off = left_start.split('#')
right_xpath, right_off = right_start.split('#')
left_x_comp = left_xpath.split('/')
right_x_comp = right_xpath.split('/')
if len(left_x_comp)==len(right_x_comp):
for x in range(len(left_x_comp)):
if not left_x_comp[x]: continue
ln = re.search('[0-9]+', left_x_comp[x])
rn = re.search('[0-9]+', right_x_comp[x])
if not ln: # /P/...
if not rn: continue
if rn: # /P[2]/..
return -100
if not rn: # /P/.. vs. /P[2]/..
return 100
# /P[2]/.. vs. /P[3]/..
if int(ln.group())>int(rn.group()):
return 100
if int(ln.group())len(right_x_comp):
return 100
else:
return -100
left_off = int(left_off.split(':')[1])
right_off = int(right_off.split(':')[1])
if left_off>right_off:
return 100
if left_off 499:
if not hasattr(self, '_funny_stuffs'):
self._funny_stuffs = []
self._funny_stuffs.append('content too long!')
self._funny_stuffs = self._funny_stuffs
dbid = self.sqls.newError(
pointer=self.pointer,
documentid=self.document,
before="", #self.pre,
content="", #contentDB,
after="", #self.post,
code=self.code,
author=self.author,
)
self._db_id = dbid[0][0]
security.declareProtected(perm_view_document, 'isDeleted')
def isDeleted(self):
return self.getProperty('is_deleted', 0)
security.declareProtected(perm_mark_document, 'addPreContext')
def addPreContext(self, pre):
""" add pre context. in array. closest in first place """
self.pre = pre
security.declareProtected(perm_mark_document, 'addPostContext')
def addPostContext(self, post):
""" add post context. in array. closest in first place """
self.post = post
security.declareProtected(perm_view_document, 'getPreContext')
def getPreContext(self, level=2):
""" get pre context """
return ' '.join(self.pre[level:])
security.declareProtected(perm_view_document, 'getPostContext')
def getPostContext(self, level=2):
""" get post context """
return ' '.join(self.post[:level])
security.declareProtected(perm_view_document, 'getContent')
def getContent(self):
""" return content """
return self.content
security.declareProtected(perm_mark_document, '_setDeleted')
def _setDeleted(self):
""" mark error as deleted """
try:
self._updateProperty('is_deleted', 1)
except BadRequest:
self._setProperty('is_deleted', 1)
self.reindex_object()
Globals.InitializeClass(Error)