cco.common.psu: directly use zope.app.wsgi

This commit is contained in:
Helmut Merz 2024-10-04 14:44:16 +02:00
parent 9cc93008be
commit 994dc8ea3e
2 changed files with 28 additions and 43 deletions

View file

@ -1,31 +1,31 @@
# psu - paster shell utilities # psu - python shell utilities
# use this from (e.g.): # use this from a Python command line (in a loops / bluebrem virtual environment).
#
# bin/paster shell deploy8.ini
# #
# then: # then:
# #
# from cco.common import psu # from cco.common import psu
# from custom.config import myproject as config # import config
# psu.setup(root, 'path/to/loopsRoot', config) # psu.setup('path/to/loopsRoot', config, zopeconf='zope-0.conf')
# obj = psu.byuid('578457950') # obj = psu.byuid('578457950')
# #
import os import os
from transaction import commit, abort from transaction import commit, abort
from zope.app import wsgi
from zope.app.authentication.principalfolder import Principal from zope.app.authentication.principalfolder import Principal
from zope.app.component.hooks import setSite
from zope.app.container.contained import ObjectAddedEvent, ObjectRemovedEvent
from zope.cachedescriptors.property import Lazy from zope.cachedescriptors.property import Lazy
from zope.catalog.interfaces import ICatalog from zope.catalog.interfaces import ICatalog
from zope.component.hooks import setSite
from zope.container.contained import ObjectAddedEvent, ObjectRemovedEvent
from zope.copypastemove.interfaces import IContainerItemRenamer from zope.copypastemove.interfaces import IContainerItemRenamer
from zope import component from zope import component
from zope.event import notify from zope.event import notify
from zope.exceptions.interfaces import DuplicationError from zope.exceptions.interfaces import DuplicationError
from zope.interface import Interface
from zope.lifecycleevent import ObjectCreatedEvent, ObjectModifiedEvent from zope.lifecycleevent import ObjectCreatedEvent, ObjectModifiedEvent
from zope.publisher.browser import TestRequest as BaseTestRequest from zope.publisher.browser import TestRequest as BaseTestRequest
from zope.security.management import getInteraction, newInteraction, endInteraction from zope.security.management import getInteraction, newInteraction, endInteraction
from zope.interface import Interface #from zope.pluggableauth.plugins.principalfolder import PrincipalInfo
from cybertools.util.date import date2TimeStamp, strptime from cybertools.util.date import date2TimeStamp, strptime
from cybertools.util.jeep import Jeep from cybertools.util.jeep import Jeep
@ -38,8 +38,10 @@ os.environ['NLS_LANG'] = 'German_Germany.UTF8'
sc = Jeep() # shortcuts sc = Jeep() # shortcuts
def setup(root, loopsRootPath='sites/loops', config=None): def setup(loopsRootPath='sites/loops', config=None, zopeconf='zope.conf'):
global sm, smdefault, intids, pau, loopsRoot, sc global conn, sm, smdefault, intids, pau, loopsRoot, sc
conn = wsgi.config(zopeconf).open()
root = conn.root()['Application']
setSite(root) setSite(root)
sm = component.getSiteManager(root) sm = component.getSiteManager(root)
smdefault = sm['default'] smdefault = sm['default']
@ -83,7 +85,7 @@ def notifyRemoved(obj):
def delete(container, name, docommit=True): def delete(container, name, docommit=True):
obj = container.get(name) obj = container.get(name)
if obj is None: if obj is None:
print '*** Object', name, 'not found!' print('*** Object', name, 'not found!')
return return
notifyRemoved(obj) notifyRemoved(obj)
del container[name] del container[name]
@ -93,14 +95,14 @@ def delete(container, name, docommit=True):
def rename(container, old, new, docommit=True): def rename(container, old, new, docommit=True):
obj = container.get(old) obj = container.get(old)
if obj is None: if obj is None:
print '*** Object', old, 'not found!' print('*** Object', old, 'not found!')
return return
renamer = IContainerItemRenamer(container) renamer = IContainerItemRenamer(container)
if new != old: if new != old:
try: try:
renamer.renameItem(old, new) renamer.renameItem(old, new)
except DuplicationError: except DuplicationError:
print '*** Object', new, 'already exists!' print('*** Object', new, 'already exists!')
# container[new] = obj # container[new] = obj
# notifyAdded(obj) # notifyAdded(obj)
notifyModification(obj) notifyModification(obj)
@ -110,7 +112,7 @@ def rename(container, old, new, docommit=True):
def move(source, target, name): def move(source, target, name):
obj = source.get(name) obj = source.get(name)
if obj is None: if obj is None:
print '*** Object', name, 'not found!' print('*** Object', name, 'not found!')
return return
#notifyRemoved(obj) #notifyRemoved(obj)
#del source[name] #del source[name]
@ -124,14 +126,14 @@ def get(container, obj):
name = obj name = obj
obj = container.get(name) obj = container.get(name)
if obj is None: if obj is None:
print '*** Object', name, 'not found!' print('*** Object', name, 'not found!')
return None return None
return adapted(obj) return adapted(obj)
# startup, loop, finish... # startup, loop, finish...
def startup(msg, **kw): def startup(msg, **kw):
print '***', msg print('***', msg)
step = kw.pop('step', 10) step = kw.pop('step', 10)
return Jeep(count=0, step=step, message=msg, **kw) return Jeep(count=0, step=step, message=msg, **kw)
@ -148,20 +150,20 @@ def update(fct, obj, info):
objInfo = obj.context.__name__ objInfo = obj.context.__name__
except: except:
objInfo = obj objInfo = obj
print '*** Processing object # %i: %s' % (info.count, objInfo) print('*** Processing object # %i: %s' % (info.count, objInfo))
if info.get('updated'): if info.get('updated'):
print '*** updated: %i.' % info.updated print('*** updated: %i.' % info.updated)
if info.get('errors'): if info.get('errors'):
print '*** errors: %i.' % info.error print('*** errors: %i.' % info.error)
commit() commit()
return fct(obj, info) return fct(obj, info)
def finish(info): def finish(info):
print '*** count: %i.' % info.count print('*** count: %i.' % info.count)
if info.get('updated'): if info.get('updated'):
print '*** updated: %i.' % info.updated print('*** updated: %i.' % info.updated)
if info.get('errors'): if info.get('errors'):
print '*** errors: %i.' % info.error print('*** errors: %i.' % info.error)
commit() commit()
def stop_condition(info): def stop_condition(info):

View file

@ -1,23 +1,6 @@
# # cco.common.util
# Copyright (c) 2019 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
""" """ Utility functions.
Utility functions.
""" """
import xlrd import xlrd
@ -26,7 +9,7 @@ import mmap
from datetime import timedelta, datetime from datetime import timedelta, datetime
from logging import getLogger from logging import getLogger
from lxml import etree from lxml import etree
from urllib import urlencode from urllib.parse import urlencode
from urllib2 import urlopen from urllib2 import urlopen
from zope import component from zope import component