<commit_before>'''
Created on Jan 28, 2011

@author: roger
'''
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This work was created by participants in the DataONE project, and is
# jointly copyrighted by participating institutions in DataONE. For
# more information on DataONE, see our web site at http://dataone.org.
#
#   Copyright ${year}
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
Module d1_common.types.exception_serialization
==============================================

Implements serializaton and de-serialization for the DataONE Exception type.
'''

# Stdlib.
import logging
import urllib
try:
  import cjson as json
except:
  import json
try:
  from xml.etree import cElementTree as ETree
except:
  from xml.etree import ElementTree as Etree
import xml.dom.minidom

# App.
import d1_common.const
import serialization_base
import d1_common.types.exceptions


def instanceToSimpleType(instance):
  '''Force instance to a simple type- int, float, or string
  '''
  if isinstance(instance, float):
    return instance
  if isinstance(instance, int):
    return instance
  if isinstance(instance, basestring):
    return instance
  return unicode(instance)


class DataONEExceptionSerialization(serialization_base.Serialization):
  '''Implements serialization of DataONE Exception.
  '''

  def __init__(self, dataone_exception):
    serialization_base.Serialization.__init__(self)

    self.log = logging.getLogger('DataONEExceptionSerialization')

    self.pri = [
      d1_common.const.MIMETYPE_XML,
      d1_common.const.MIMETYPE_APP_XML,
      d1_common.const.MIMETYPE_JSON,
      #d1_common.const.MIMETYPE_CSV,
      #d1_common.const.MIMETYPE_RDF,
      d1_common.const.MIMETYPE_HTML,
      #d1_common.const.MIMETYPE_LOG,
      d1_common.const.MIMETYPE_TEXT,
    ]

    self.dataone_exception = dataone_exception

  def serialize_xml(self, pretty=False, jsonvar=False):
    '''Serialize DataONE Exception to XML.

    :rtype: UTF-8 encoded XML string
    '''
    root = ETree.Element(
      u'error', {
        u'name': self.dataone_exception.name,
        u'errorCode': str(self.dataone_exception.errorCode),
        u'detailCode': str(self.dataone_exception.detailCode)
      }
    )
    try:
      ETree.SubElement(root, u'pid').text = self.dataone_exception.pid
    except AttributeError:
      pass
    ETree.SubElement(root, u'description').text = self.dataone_exception.description
    for v in self.dataone_exception.traceInformation:
      ETree.SubElement(root, u'traceInformation').text = unicode(v)

    doc = ETree.tostring(root, 'utf-8')

    if pretty:
      xml_obj = xml.dom.minidom.parseString(doc)
      doc = xml_obj.toprettyxml(encoding='utf-8')

    return doc

  def serialize_json(self, pretty=False, jsonvar=False):
    '''Serialize DataONE Exception to JSON.

    :rtype: UTF-8 encoded JSON string
    '''
    json_obj = {
     u'name': self.dataone_exception.name,
     u'errorCode': self.dataone_exception.errorCode,
     u'detailCode': str(self.dataone_exception.detailCode),
     u'description': self.dataone_exception.description,
     u'traceInformation': [
       instanceToSimpleType(v)<commit_msg>Blanked DataONEException.traceInformation to make it compatible with the Java stack for the demo.<commit_after>'''
Created on Jan 28, 2011

@author: roger
'''
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This work was created by participants in the DataONE project, and is
# jointly copyrighted by participating institutions in DataONE. For
# more information on DataONE, see our web site at http://dataone.org.
#
#   Copyright ${year}
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
Module d1_common.types.exception_serialization
==============================================

Implements serializaton and de-serialization for the DataONE Exception type.
'''

# Stdlib.
import logging
import urllib
try:
  import cjson as json
except:
  import json
try:
  from xml.etree import cElementTree as ETree
except:
  from xml.etree import ElementTree as Etree
import xml.dom.minidom

# App.
import d1_common.const
import serialization_base
import d1_common.types.exceptions


def instanceToSimpleType(instance):
  '''Force instance to a simple type- int, float, or string
  '''
  if isinstance(instance, float):
    return instance
  if isinstance(instance, int):
    return instance
  if isinstance(instance, basestring):
    return instance
  return unicode(instance)


class DataONEExceptionSerialization(serialization_base.Serialization):
  '''Implements serialization of DataONE Exception.
  '''

  def __init__(self, dataone_exception):
    serialization_base.Serialization.__init__(self)

    self.log = logging.getLogger('DataONEExceptionSerialization')

    self.pri = [
      d1_common.const.MIMETYPE_XML,
      d1_common.const.MIMETYPE_APP_XML,
      d1_common.const.MIMETYPE_JSON,
      #d1_common.const.MIMETYPE_CSV,
      #d1_common.const.MIMETYPE_RDF,
      d1_common.const.MIMETYPE_HTML,
      #d1_common.const.MIMETYPE_LOG,
      d1_common.const.MIMETYPE_TEXT,
    ]

    self.dataone_exception = dataone_exception

  def serialize_xml(self, pretty=False, jsonvar=False):
    '''Serialize DataONE Exception to XML.

    :rtype: UTF-8 encoded XML string
    '''
    root = ETree.Element(
      u'error', {
        u'name': self.dataone_exception.name,
        u'errorCode': str(self.dataone_exception.errorCode),
        u'detailCode': str(self.dataone_exception.detailCode)
      }
    )
    try:
      ETree.SubElement(root, u'pid').text = self.dataone_exception.pid
    except AttributeError:
      pass
    ETree.SubElement(root, u'description').text = self.dataone_exception.description
    #    for v in self.dataone_exception.traceInformation:
    #      ETree.SubElement(root, u'traceInformation').text = unicode(v)
    # TODO: For the Feb 2011 NSF demo, we keep traceInformation blank as
    # the serialization format of traceInformation is different between
    # Python and Java stacks. We only do this for XML because that is
    # the only serialization format used in the Java stack.
    ETree.SubElement(root, u'traceInformation').text = ''

    doc = ETree.tostring(root, 'utf-8')

    if pretty:
      xml_obj = xml.dom.minidom.parseString(doc)
      doc = xml_obj.toprettyxml(encoding='utf-8')

    return doc

  def serialize_json(self, pretty=False, jsonvar=False):
    '''Serialize DataONE Exception to JSON.

    :rtype: UTF-8 encoded JSON string
    '''
    json_obj = {
     u'name': self.dataone_exception.name,
     u'errorCode': self.dataone_exception.errorCode,
     u'detailCode': str(self.dataone_exception.detailCode),
     u'description': self.dataone_exception.description,
     u'traceInformation': [
       instanceToSimpleType(v)
{'fraction_non_alphanumeric': 0.1037113668692616, 'fraction_numerical': 0.007241691452217768, 'mean_word_length': 3.891840607210626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2560087', 'n_tokens_mistral': 2347, 'n_tokens_neox': 2315, 'n_words': 651}