Skip to content

Commit

Permalink
Hotfix (#45)
Browse files Browse the repository at this point in the history
* Fixed README documentation

* Fixed typo

* Fixed problem with content length

* Fixed issue with displaying observation
  • Loading branch information
andamian authored Oct 11, 2017
1 parent f02e5c1 commit 391c804
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 23 deletions.
38 changes: 26 additions & 12 deletions caom2/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ To create a minimal Simple Observation
--------------------------------------

.. code:: python
import caom2
# WARNING: unicode string parameters are required, no simple strings.
# If you run python2 be aware of this.
observation = caom2.SimpleObservation(u'collection', u'observationID')
observation.planes = caom2.TypedOrderedDict(Plane)
plane = caom2.Plane(u'productID')
# make it compatible with Python 2 and 3
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import sys
from caom2 import SimpleObservation, TypedOrderedDict, Plane, Artifact,\
Part, Chunk, ObservationWriter, ProductType,\
ReleaseType, TypedList
observation = SimpleObservation('collection', 'observationID')
observation.planes = TypedOrderedDict(Plane)
plane = Plane('productID')
observation.planes['productID'] = plane
plane.artifacts = TypedOrderedDict(Artifact)
Expand Down Expand Up @@ -80,8 +82,20 @@ To create a complete Observation
--------------------------------------

.. code:: python
import caom2
# make it compatible with Python 2 and 3
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import datetime
import sys
from caom2 import SimpleObservation, Plane, Artifact, Part, Chunk,\
TypedOrderedDict, ObservationWriter, ProductType, \
ReleaseType, TypedList, Target, TargetPosition, \
TargetType, ObservationIntentType, Instrument, \
Telescope, Environment, DataProductType, Provenance, \
CalibrationLevel, Metrics, Proposal, Point, Slice, Axis,\
ObservableAxis, CoordAxis1D, CoordAxis2D, SpatialWCS,\
SpectralWCS, EnergyTransition, TemporalWCS, CoordFunction1D,\
RefCoord, PolarizationWCS
observation = SimpleObservation('collection', 'observationID')
observation.obs_type = 'flat'
Expand Down
2 changes: 1 addition & 1 deletion caom2/caom2/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def content_length(self):
@content_length.setter
def content_length(self, value):
caom_util.type_check(value, int, "content_length")
caom_util.value_check(value, 0, 1E10, "content_length")
caom_util.value_check(value, 0, 1E32, "content_length")
self._content_length = value

@property
Expand Down
2 changes: 1 addition & 1 deletion caom2/caom2/caom_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def __str__(self):
for k, v in six.iteritems(self)])

def __repr__(self):
return "TypeOrderedDict((%r))," % self._oktypes + (
return "TypedOrderedDict((%r))," % self._oktypes + (
"(".join(
["(%r,%r)" % (k, v) for k, v in six.iteritems(self)]) + ")")

Expand Down
20 changes: 16 additions & 4 deletions caom2/caom2/obs_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,12 @@ def __init__(self, validate=False, write_empty_collections=False,
self._nsmap = {namespace_prefix: self._namespace, "xsi": XSI_NAMESPACE}

def write(self, obs, out):
"""
Writes an observation to an output stream
:param obs: observation to write
:param out: stream or file name to write to
:return:
"""
assert isinstance(obs, observation.Observation), (
"observation is not an Observation")

Expand Down Expand Up @@ -1773,10 +1779,16 @@ def write(self, obs, out):
if self._validate and self._xmlschema:
self._xmlschema.assertValid(obs_element)

doc = etree.tostring(obs_element, encoding='UTF-8',
xml_declaration=True, pretty_print=True)
out.write(doc)
out.flush()
tree = etree.ElementTree(obs_element)
try:
if 'b' not in out.mode:
out.write(etree.tostring(tree, encoding='unicode',
pretty_print=True))
return
except AttributeError:
pass # nothing to do
tree.write(out, encoding='utf-8',
xml_declaration=True, pretty_print=True)

def _add_entity_attributes(self, entity, element):
if self._output_version == 20:
Expand Down
5 changes: 3 additions & 2 deletions caom2/caom2/tests/test_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ def test_all(self):
self.assertEquals("FITS", test_artifact.content_type, "Content type")
self.assertIsNone(test_artifact.content_length,
"Default content length")
test_artifact.content_length = 23
self.assertEquals(23, test_artifact.content_length, "Content length")
test_artifact.content_length = 23000000000000
self.assertEquals(23000000000000,
test_artifact.content_length, "Content length")
test_artifact.product_type = artifact.ProductType.PREVIEW
self.assertEquals(artifact.ProductType.PREVIEW,
test_artifact.product_type,
Expand Down
7 changes: 7 additions & 0 deletions caom2/caom2/tests/test_obs_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ def observation_test(self, obs, validate, write_empty_collections,
returned = reader.read('/tmp/test.xml')
self.compare_observations(obs, returned, version)

# same test when passing file name
writer.write(obs, '/tmp/test1.xml')
xml_file.close()
reader = obs_reader_writer.ObservationReader(True)
returned = reader.read('/tmp/test.xml')
self.compare_observations(obs, returned, version)

def compare_observations(self, expected, actual, version):

assert ((isinstance(expected, observation.SimpleObservation) and
Expand Down
2 changes: 1 addition & 1 deletion caom2/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ edit_on_github = False
github_project = opencadc/caom2tools
install_requires = future lxml aenum
# version should be PEP386 compatible (http://www.python.org/dev/peps/pep-0386)
version = 2.3.0a5
version = 2.3


[entry_points]
3 changes: 1 addition & 2 deletions caom2repo/caom2repo/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ def update(self, observation):
args.observationID)
observation_writer = ObservationWriter()
if args.output:
with open(args.output, 'wb') as obsfile:
observation_writer.write(observation, obsfile)
observation_writer.write(observation, args.output)
else:
observation_writer.write(observation, sys.stdout)
elif args.cmd == 'update':
Expand Down

0 comments on commit 391c804

Please sign in to comment.