zope tutorial added
--HG-- branch : sandbox
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
import logging
|
||||
import sys
|
||||
import os
|
||||
import getopt
|
||||
from optparse import OptionParser
|
||||
|
||||
# local imports
|
||||
|
||||
@@ -24,96 +24,21 @@ logging.basicConfig(level=logging.DEBUG,
|
||||
format="%(asctime)s %(levelname).3s %(process)d:%(thread)d %(message)s",
|
||||
datefmt="%H:%M:%S")
|
||||
|
||||
# definitions
|
||||
|
||||
class Usage(Exception):
|
||||
pass
|
||||
|
||||
class Error(Exception):
|
||||
pass
|
||||
|
||||
def main(argv = [__file__]):
|
||||
try:
|
||||
# check for parameters
|
||||
LOG.debug("starting '%s %s'", argv[0], " ".join(argv[1:]))
|
||||
script_name = os.path.basename(argv[0])
|
||||
try:
|
||||
opts, args = getopt.getopt(argv[1:], "hfgp", \
|
||||
["help", "force", "gui", "preview"])
|
||||
except getopt.error, err:
|
||||
raise Usage(err)
|
||||
LOG.debug("opts: %s, args: %s", opts, args)
|
||||
o_overwrite = False
|
||||
o_gui = False
|
||||
o_preview = False
|
||||
for o, a in opts:
|
||||
if o in ("-h", "--help"):
|
||||
usage(script_name)
|
||||
return 0
|
||||
elif o in ("-f", "--force"):
|
||||
o_overwrite = True
|
||||
elif o in ("-p", "--preview"):
|
||||
o_preview = True
|
||||
elif o in ("-g", "--gui"):
|
||||
o_gui = True
|
||||
if len(args) == 2:
|
||||
src_dir = args[0]
|
||||
dest_dir = args[1]
|
||||
elif len(args) == 1 :
|
||||
src_dir = args[0]
|
||||
dest_dir = args[0]
|
||||
elif len(args) == 0 :
|
||||
src_dir = None
|
||||
dest_dir = None
|
||||
o_gui = True
|
||||
else:
|
||||
raise Usage("more than two arguments provided")
|
||||
# call method with appropriate arguments
|
||||
if src_dir and not os.path.exists(src_dir):
|
||||
raise Error("Source directory not found [%s], aborting" % (src_dir, ))
|
||||
if dest_dir and not os.path.exists(dest_dir):
|
||||
LOG.warn("Destination directory not found [%s]", dest_dir)
|
||||
if not o_preview:
|
||||
LOG.info("Creating destination directory [%s]", dest_dir)
|
||||
os.makedirs(dest_dir)
|
||||
if o_gui:
|
||||
gui(src_dir, dest_dir, o_overwrite)
|
||||
else:
|
||||
cli(src_dir, dest_dir, o_preview, o_overwrite)
|
||||
LOG.debug("Done.")
|
||||
return 0
|
||||
except Error, err:
|
||||
LOG.error(err)
|
||||
return 1
|
||||
except Usage, err:
|
||||
LOG.error(err)
|
||||
LOG.info("for usage use -h or --help")
|
||||
return 2
|
||||
# check for parameters
|
||||
LOG.debug("starting '%s %s'", argv[0], " ".join(argv[1:]))
|
||||
script_name = os.path.basename(argv[0])
|
||||
# parse arguments and options
|
||||
usage = "usage: %prog [options] arg"
|
||||
version = "%prog 1.0"
|
||||
parser = OptionParser(usage, version)
|
||||
parser.add_option("-f", "--file", dest="filename",
|
||||
help="read configuration from FILENAME")
|
||||
parser.add_option("-p", "--port", default="8001", type="int",
|
||||
help="server port [default: %default]")
|
||||
|
||||
|
||||
def gui(src_dir, dest_dir, o_overwrite):
|
||||
""" graphical user interface """
|
||||
print src_dir, dest_dir, o_overwrite
|
||||
|
||||
|
||||
def cli(src_dir, dest_dir, o_preview, o_overwrite):
|
||||
""" command line interface """
|
||||
print src_dir, dest_dir, o_preview, o_overwrite
|
||||
|
||||
|
||||
def usage(script_name):
|
||||
print
|
||||
print "usage: %s [options] [src_dir [dest_dir]]" % (script_name,)
|
||||
print """
|
||||
src_dir source directory to search for MOD/MOI
|
||||
dest_dir destination directory for MPG files
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
-f, --force override files with same name in destination directory
|
||||
-g, --gui force interactive mode
|
||||
-p, --preview preview only, don't copy, don't create non-existent directories
|
||||
"""
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv))
|
||||
|
||||
52
src/tutorial.py
Normal file
52
src/tutorial.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# ZODB Tutorial
|
||||
|
||||
""" zodb.tutorial """
|
||||
|
||||
# imports
|
||||
|
||||
from ZODB.FileStorage import FileStorage
|
||||
from ZODB.DB import DB
|
||||
from persistent import Persistent
|
||||
import transaction
|
||||
|
||||
# Persistent objects
|
||||
|
||||
class Account(Persistent):
|
||||
def __init__(self):
|
||||
self.balance = 0.0
|
||||
|
||||
def deposit(self, amount):
|
||||
self.balance += amount
|
||||
|
||||
def cash(self, amount):
|
||||
assert amount < self.balance
|
||||
self.balance -= amount
|
||||
|
||||
# Configuration
|
||||
|
||||
storage = FileStorage(r"e:\temp\data.zodb")
|
||||
db = DB(storage)
|
||||
connection = db.open()
|
||||
root = connection.root()
|
||||
|
||||
print root.keys()
|
||||
|
||||
# Storing objects
|
||||
|
||||
root['account-1'] = Account()
|
||||
if "account-2" not in root:
|
||||
root['account-2'] = Account()
|
||||
|
||||
# Transactions
|
||||
root['account-2'].deposit(5)
|
||||
transaction.commit()
|
||||
print root.keys()
|
||||
|
||||
del root['account-1']
|
||||
print root.keys()
|
||||
# ['account-2']
|
||||
transaction.abort()
|
||||
print root.keys()
|
||||
# ['account-1', 'account-2']
|
||||
|
||||
print root['account-2'].balance
|
||||
Reference in New Issue
Block a user