ScribusUtils.py 4.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
#               Guy Oswald OBAMA <guy@nexedi.com>
#
# 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.
#
##############################################################################

from Products.PythonScripts.Utility import allow_class
from ZPublisher.HTTPRequest import FileUpload
from xml.dom.ext.reader import PyExpat
from xml.dom import Node, minidom
from AccessControl import ClassSecurityInfo
from Globals import InitializeClass, get_request
from zipfile import ZipFile, ZIP_DEFLATED
from StringIO import StringIO
from zLOG import LOG
import imghdr
import random
import getopt, sys, os, string
from urllib import quote



class ScribusParser:
  """
  Parses a Scribus file
  """
  
  # Declarative security
  security = ClassSecurityInfo()
  
    
  security.declarePublic('getXmlObjectsProperties')
  def getXmlObjectsProperties(self, xml_string):
    # Create the PyExpat reader
    reader = PyExpat.Reader()
  
    # Create DOM tree from the xml string
    dom_tree = reader.fromString(xml_string)
  
    text_field_list = {}
    
    dom_root = dom_tree.documentElement
    page_object_list = dom_root.getElementsByTagName("PAGEOBJECT")
  
    # Take Xml objects properties
    for page_object in page_object_list:
      text_field_properties = {}
      field_name = None
      for attribute in page_object.attributes:
        node_name  = str(attribute.nodeName)
        node_value = str(attribute.nodeValue)
        if node_name == 'ANNAME':
          if node_value != '':
            field_name = node_value
        else:
            text_field_properties[node_name] = node_value
      if field_name != None:
        text_field_list[field_name] = text_field_properties
  
    return text_field_list   

   
  security.declarePublic('getPropertiesConversion')
  def getPropertiesConversion(self, text_field_list):
  # Get Scribus field properties
  
    field_scribus_properties_dict = {}
  
    for field_name in text_field_list.keys():
      text_field_properties = text_field_list[field_name]
      field_scribus_properties_dict[field_name] = text_field_properties['ANTOOLTIP']
  
    widget_properties_list = []
    index = 1    
  
    while index < len(field_scribus_properties_dict):
      for key, item in field_scribus_properties_dict.items():
        if string.atoi(item[:3]) == index:
          property_field_list = item[4:].split('#')
          widget_properties_buffer = {}
          for property_field in property_field_list:
            property_field_split = property_field.split(':')
            if property_field_split[0] == 'items':
              property_field_split[1] = property_field_split[1].split('|')
            widget_properties_buffer[property_field_split[0]] = property_field_split[1]
          widget_properties_list.append((key, widget_properties_buffer))
          break
      index = index + 1
  
    for key, item in field_scribus_properties_dict.items():
      if string.atoi(item[:3]) == 999:
        property_field_list = item[4:].split('#')
        widget_properties_buffer = {}
        for property_field in property_field_list:
          property_field_split = property_field.split(':')
          widget_properties_buffer[property_field_split[0]] = property_field_split[1]
        widget_properties_list.append((key, widget_properties_buffer))
  
    return widget_properties_list

        
  security.declareProtected('Import/Export objects', 'getContentFile')
  def getContentFile(self, file_descriptor):
    """ Get file content """
    return file_descriptor.read()
  """
  def getContentFile(self, file_path):
    # Verify that the file exist
    if not os.path.isfile(file_path):
      output("ERROR: " + file_path + " doesn't exist.")
      return None
  
    # Get file content
    file_path = os.path.abspath(file_path)
    file_object = open(file_path, 'r')
  
    return file_object.read()
  """
  
InitializeClass(ScribusParser)
allow_class(ScribusParser)