Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Add YAML output support
Browse files Browse the repository at this point in the history
  • Loading branch information
coldfix committed Feb 20, 2014
1 parent ba8c43e commit 58777c4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
54 changes: 51 additions & 3 deletions madseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
madseq - MAD-X sequence parser/transformer.
Usage:
madseq.py [-j <json>] [-s <slice>] [-m] [-o <output>] [<input>]
madseq.py [-j <json>] [-y <yaml>] [-s <slice>] [-m] [-o <output>] [<input>]
madseq.py (--help | --version)
Options:
-o <output>, --output=<output> Set output file
-j <json>, --json=<json> Set JSON output file
-y <yaml>, --yaml=<yaml> Set YAML output file
-s <slice>, --slice=<slice> Select slicing
-m, --makethin Apply a MAKETHIN like transformation
-h, --help Show this help
Expand Down Expand Up @@ -51,7 +52,10 @@ def cast(type):
True
"""
return lambda value: None if value is None else type(value)
def constructor(value):
return None if value is None else type(value)
constructor.cls = type
return constructor

@cast
class stri(str):
Expand Down Expand Up @@ -590,11 +594,48 @@ def json_adjust_element(elem):
[(k,v) for k,v in elem.args.items() if v is not None]),


#----------------------------------------
# YAML serialization utility:
#----------------------------------------

class Yaml(object):
def __init__(self):
import yaml
import pydicti
self.yaml = yaml
self.dict = pydicti.odicti

def dump(self, data, stream=None, **kwds):
yaml = self.yaml
class Dumper(yaml.SafeDumper):
pass
def _dict_representer(dumper, data):
return dumper.represent_mapping(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
data.items())
def _stri_representer(dumper, data):
return dumper.represent_str(data)
def _Value_representer(dumper, data):
return dumper.represent_str(data.value)
def _Decimal_representer(dumper, data):
return dumper.represent_scalar(u'tag:yaml.org,2002:float',
str(data).lower())

Dumper.add_representer(self.dict, _dict_representer)
Dumper.add_representer(stri.cls, _stri_representer)
Dumper.add_representer(Symbolic, _Value_representer)
Dumper.add_representer(Identifier, _Value_representer)
Dumper.add_representer(Composed, _Value_representer)
Dumper.add_representer(decimal.Decimal, _Decimal_representer)

return yaml.dump(data, stream, Dumper,
default_flow_style=False, **kwds)

#----------------------------------------
# main
#----------------------------------------

def transform(elem, json_file=None,
def transform(elem, json_file=None, yaml_file=None,
typecast=Typecast.preserve,
slicing=None):
"""Transform sequence."""
Expand Down Expand Up @@ -654,6 +695,12 @@ def transform(elem, json_file=None,
separators=(',', ' : '),
cls=ValueEncoder)

if yaml_file:
yaml = Yaml()
yaml.dump(
list(chain.from_iterable(map(json_adjust_element, elems))),
open(yaml_file, 'wt'))

return (optics +
[Text('! Sequence definition for %s:' % first.name),
first] +
Expand Down Expand Up @@ -714,6 +761,7 @@ def main(argv=None):
typecast = Typecast.multipole if args['--makethin'] else Typecast.preserve
transformation = partial(transform,
json_file=args['--json'],
yaml_file=args['--yaml'],
typecast=typecast,
slicing=args['--slice'])

Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setup(
name='madseq',
version='0.1',
version='0.2',
description='Parser/transformator for MAD-X sequences',
long_description=long_description,
author='Thomas Gläßle',
Expand All @@ -36,6 +36,9 @@
'pydicti[odicti]>=0.0.3',
'docopt'
],
extras_require={
'yaml': ['pyyaml']
},
test_suite='nose.collector',
tests_require=['nose'],
classifiers=[
Expand Down

0 comments on commit 58777c4

Please sign in to comment.