IoFile.py :  » UML » Python-UML-Tool » pyut-1.4.0 » src » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » UML » Python UML Tool 
Python UML Tool » pyut 1.4.0 » src » IoFile.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

__version__ = "$Revision: 1.10 $"
__author__ = "EI5, eivd, Group Burgbacher - Waelti"
__date__ = "2002-1-9"

from mediator import getMediator
from pyutUtils import displayError
import PyutConsts

class IoFile(object):
    """
    To save datas in a compressed file format.

    IoFile is used to save or read Pyut file format who's
    are named *.put.


    Example::
        IoFile = io()
        io.save("myFileName.put", project)  # to save diagram
        io.open("pyFileName.put", project)    # to read file

    :version: $Revision: 1.10 $
    :author:  Deve Roux 
    :contact: droux@eivd.ch 
    """
    def save(self, project):
        """
        To save save diagram in XML file.

        @since 1.0
        @author Deve Roux <droux@eivd.ch>
        """
        import os
        oldpath = os.getcwd()
        path = getMediator().getAppPath()
        os.chdir(path)
        from glob import glob
        candidates = glob("PyutXmlV*.py")
        numbers = [int(s[8:-3]) for s in candidates]
        lastVersion = str(max(numbers))
        print "Using version", lastVersion, " of the exporter"
        module = __import__("PyutXmlV" + lastVersion)
        myXml = module.PyutXml()
        doc = myXml.save(project)
        text = doc.toxml()
        # add attribute encoding = "iso-8859-1"
        # this is not possible with minidom, so we use pattern matching
        text = text.replace(r'<?xml version="1.0" ?>', 
            r'<?xml version="1.0" encoding="iso-8859-1"?>')

        import zlib
        compressed = zlib.compress(text)
        file = open(project.getFilename(), "wb")
        file.write(compressed)
        file.close()
        os.chdir(oldpath)

    #>------------------------------------------------------------------------

    def open(self, filename, project):
        """
        To open a compressed file and create diagram.

        @author Deve Roux
        """
        import os
        #print "IoFile-1"

        #
        oldpath = os.getcwd()
        path = getMediator().getAppPath()
        #print "IoFile-2"
        os.chdir(path)
        from xml.dom.minidom import parseString
        import lang
        #print "IoFile-3"
        lang.importLanguage()
        #print "IoFile-4"
        xmlString = ""
        if filename[-4:]==".put":
            #print "IoFile-5"
            comp = open(filename, "r").read()
            #print "IoFile-6"
            try:
                import zlib
                print zlib.__version__
                #print "1"
                import zlib
                xmlString = zlib.decompress(comp)
            except:
                print "EXCEPTION"
            #print xmlString
            #print "IoFile-7"
        elif filename[-4:]==".xml":
            xmlString = open(filename, "rb").read()
        else:
            displayError(_("Can't open the unidentified file : %s") % filename)
            return
        #try:
        #print "IoFile-8"
        dom = parseString(xmlString)

        # Try to load file for version >=5
        #print "IoFile-9"
        root = dom.getElementsByTagName("PyutProject")
        if len(root)>0:
            root = root[0]
            if root.hasAttribute('version'):
                version = root.getAttribute("version")
                print "Using version", version, " of the importer"
                module = __import__("PyutXmlV" + str(version))
                myXml = module.PyutXml()
            else:
                version = 1
                from PyutXml import PyutXml
                myXml = PyutXml()
            myXml.open(dom, project)
        else:
            root = dom.getElementsByTagName("Pyut")[0]
            if root.hasAttribute('version'):
                version = root.getAttribute("version")
                print "Using version", version, " of the importer"
                module = __import__("PyutXmlV" + str(version))
                myXml = module.PyutXml()
            else:
                version = 1
                from PyutXml import PyutXml
                myXml = PyutXml()
            project.newDocument(PyutConsts.CLASS_DIAGRAM)
            umlFrame = project.getDocuments()[0].getFrame()
            myXml.open(dom, umlFrame)
        #print "IoFile-10"


        os.chdir(oldpath)
        # TODO : put this back
        #except:
        #    #dlg=wxMessageDialog(umlFrame, 
        #    #    _("An error occured while while parsing the file ")
        #    #    + str(fileName) + ".",
        #    #    _("Parse Error !"),
        #    #    wxOK | wxICON_ERROR)
        #    #dlg.ShowModal()
        #    #dlg.Destroy()
        #    displayError(_("An error occured while parsing the file") + \
        #                 str(fileName), _("Parse Error !"))

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.