summaryrefslogtreecommitdiffstats
path: root/cobbler/serializer.py
diff options
context:
space:
mode:
authorMichael DeHaan <mdehaan@redhat.com>2006-05-08 15:02:06 -0400
committerJim Meyering <jim@meyering.net>2006-05-08 15:02:06 -0400
commitd4f71b4318fedf374844030095c6c8dd544f0e92 (patch)
treedd5bb88c2587a0ab4ea2638374ad4a71bbd868a9 /cobbler/serializer.py
parent038a4383ccb6230f927960a34288c5cf6fbd3455 (diff)
downloadthird_party-cobbler-d4f71b4318fedf374844030095c6c8dd544f0e92.tar.gz
third_party-cobbler-d4f71b4318fedf374844030095c6c8dd544f0e92.tar.xz
third_party-cobbler-d4f71b4318fedf374844030095c6c8dd544f0e92.zip
Interim checkin while straightening out exceptions. The last_error bit reminded me of the
thing I hated most about Microsoft SDK/DDK programming (that being, last_error and inconsistant error handling), so it had to go.
Diffstat (limited to 'cobbler/serializer.py')
-rw-r--r--cobbler/serializer.py78
1 files changed, 57 insertions, 21 deletions
diff --git a/cobbler/serializer.py b/cobbler/serializer.py
index da4c684..a63cd6e 100644
--- a/cobbler/serializer.py
+++ b/cobbler/serializer.py
@@ -1,28 +1,64 @@
# Michael DeHaan <mdehaan@redhat.com>
import syck # PySyck 0.61 or greater, not syck-python 0.55
-import msg
+import errno
+import os
+
+import utils
def serialize(obj):
- if obj.filename() is None:
- raise Exception("not serializable")
- fd = open(obj.filename(),"w+")
- datastruct = obj.to_datastruct()
- encoded = syck.dump(datastruct)
- fd.write(encoded)
- fd.close()
- return True
+ """
+ Save an object to disk. Object must "implement" Serializable.
+ Will create intermediate paths if it can. Returns True on Success,
+ False on permission errors.
+ """
+ filename = obj.filename()
+ try:
+ fd = open(filename,"w+")
+ except IOError, ioe:
+ basename = os.path.basename(filename)
+ if not os.path.exists(basename):
+ try:
+ os.makedirs(basename)
+ except:
+ raise CobblerException("need_perms", basename)
+ return False
+ try:
+ fd = open(filename,"w+")
+ except:
+ raise CobblerException("need_perms", filename)
+ return False
+ datastruct = obj.to_datastruct()
+ encoded = syck.dump(datastruct)
+ fd.write(encoded)
+ fd.close()
+ return True
def deserialize(obj):
- if obj.filename() is None:
- raise Exception("not serializable")
- try:
- fd = open(obj.filename(),"r")
- except:
- print msg.m("parse_error") % obj.filename()
- return
- data = fd.read()
- datastruct = syck.load(data)
- fd.close()
- obj.from_datastruct(datastruct)
- return True
+ """
+ Populate an existing object with the contents of datastruct.
+ Object must "implement" Serializable. Returns True assuming
+ files could be read and contained decent YAML. Otherwise returns
+ False.
+ """
+ filename = obj.filename()
+ try:
+ fd = open(filename,"r")
+ except IOError, ioe:
+ # if it doesn't exist, that's cool -- it's not a bug until we try
+ # to write the file and can't create it.
+ if not os.path.exists(filename):
+ return True
+ else:
+ raise CobblerException("need_perms",obj.filename())
+ data = fd.read()
+ datastruct = syck.load(data)
+ if type(datastruct) == str:
+ # PySyck returns strings when it chokes on data
+ # it doesn't really throw exceptions
+ raise CobblerException("parse_error",filename)
+ fd.close()
+ obj.from_datastruct(datastruct)
+ return True
+
+