Created
July 4, 2014 11:31
-
-
Save gborelli/c2ae12055a4f234e6326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
# Questo è un template di base per realizzare degli script per le istanze Zope | |
# | |
# Lo script può essere lanciato nel seguente modo: | |
# ./bin/instance -O<plone_site_id> run <nome_dello_script> [parametri] | |
# | |
# è disponibile un parametro '-cm' (o --commit) opzionale che consente | |
# di eseguire il commit delle modifiche eseguite con lo script o meno. | |
# | |
# Leggere i commenti più sotto per verificare come modificare questo script | |
# secondo le proprie esigenze | |
import argparse | |
import logging | |
import sys | |
from traceback import print_exc | |
import transaction | |
from zope.component.hooks import getSite | |
from DateTime import DateTime | |
from Products.CMFPlone.utils import getToolByName | |
logger = logging.getLogger('Zope script') | |
# Decommentare questa riga per cambiare il livello dei log | |
# logging.basicConfig(level=logging.DEBUG) | |
# Inizializzazione del parser degli argomenti | |
# XXX: Non rimuovere queste due opzioni | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"-c", | |
help="Parametro fisso rappresenta questo script non rimuovere" | |
) | |
parser.add_argument( | |
"-cm", "--commit", action="store_true", help="Salva i dati su Zope") | |
# parser.add_argument( | |
# "-p", "--path", | |
# help="Path per limitare una sezione del sito dove compiere " | |
# "le trasformazioni." | |
# ) | |
# TODO: Aggiungere qui i parametri che ti servono per l'esecuzione dello script | |
# | |
# cfr: | |
# * https://docs.python.org/2/howto/argparse.html | |
# * https://docs.python.org/2/library/argparse.html | |
# | |
# Esempio: | |
# parser.add_argument( | |
# "-p", "--param", | |
# action="store_true", | |
# help="Messaggio d'aiuto") | |
class Application(object): | |
@classmethod | |
def execute_script(cls, *args_, **kwargs): | |
portal = getSite() | |
# put yuor code here | |
@classmethod | |
def main(cls): | |
params = parser.parse_args() | |
try: | |
cls.execute_script(**vars(params)) | |
if params.commit: | |
transaction.commit() | |
logger.warning('Changes Saved') | |
else: | |
logger.warning('Dry Run mode - nothing changed') | |
except: # pylint: disable=W0702 | |
transaction.abort() | |
print_exc() | |
return -1 | |
return 0 | |
@classmethod | |
def run(cls): | |
sys.exit(cls.main()) | |
if __name__ == "__main__": | |
Application.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment