This file is indexed.

/usr/lib/python/astrometry/util/yanny.py is in astrometry.net 0.46-0ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
#
# yanny.py
#
# Python library for reading & writing yanny files.
#
# B. A. Weaver, NYU, 2008-10-20
#
# $Id: yanny.py 128984 2011-12-22 19:52:34Z weaver $
#
"""Python library for reading & writing yanny files.

yanny is an object-oriented interface to FTCL/yanny data files following
these specifications_.

The format of the returned object is similar to that returned by
``read_yanny()`` in the efftickle perl package (in the yannytools product).

Currently multidimensional arrays are only supported for type ``char``, but a
close reading of the specifications indicates that multidimensional arrays
were only ever intended to be supported for type ``char``.

.. _specifications: http://www.sdss3.org/dr8/software/par.php
"""

__author__ = 'Benjamin Weaver <benjamin.weaver@nyu.edu>'

__version__ = '$Revision: 128984 $'.split(': ')[1].split()[0]

__all__ = [ 'yanny', 'read_yanny', 'write_yanny', 'write_yanny_append' ]

__docformat__ = "restructuredtext en"

#
# Modules
#
import re
import os
import os.path
import datetime
import numpy

#
# Classes
#
class yanny(dict):
    """An object interface to a yanny file.

    Most users will use the convenience functions defined in this package, but
    this object provides a somewhat more powerful way of reading &
    writing the data in a yanny file.

    Attributes
    ----------
    np : bool
        If True, data in a yanny file will be converted into a NumPy record
        array.
    debug : bool
        If True, some simple debugging statements will be turned on.
    _filename : str
        The name of a yanny parameter file.
    _contents : str
        The complete contents of a yanny parameter file.
    _struct_type_caches : dict
        A dictionary of dictionaries, one dictionary for every structure
        definition in a yanny parameter file.  Contains the types of
        each column
    _struct_isarray_caches : dict
        A dictionary of dictionaries, one dictionary for every structure
        definition in a yanny parameter file.  Contains a boolean value
        for every column.
    _enum_cache : dict
        Initially ``None``, this attribute is initialized the first time
        the ``isenum()`` method is called.  The keyword is the name of the
        enum type, the value is a list of the possible values of that type.

    Parameters
    ----------
    filename : str
        The name of a yanny file.
    np : bool, optional
        If True, data in a yanny file will be converted into a NumPy record
        array. Default is False
    debug : bool, optional
        If True, some simple debugging statements will be turned on. Default
        is False.
    """
    @staticmethod
    def get_token(string):
        """Removes the first 'word' from string.

        If the 'word' is enclosed in double quotes, it returns the
        contents of the double quotes. If the 'word' is enclosed in
        braces, it returns the contents of the braces, but does not
        attempt to split the array.  If the 'word' is the last word of the
        string, remainder is set equal to the empty string.  This is
        basically a wrapper on some convenient regular expressions.
        """
        if string[0] == '"':
            (word, remainder) = re.search(r'^"([^"]*)"\s*(.*)',
                string).groups()
        elif string[0] == '{':
            (word, remainder) = re.search(r'^\{\s*([^}]*)\s*\}\s*(.*)',
                string).groups()
        else:
            try:
                (word, remainder) = re.split(r'\s+',string,1)
            except ValueError:
                #print "Problem with string: %s" % string
                (word, remainder) = (string, '')
        if remainder is None:
            remainder = ''
        return (word,remainder)
    @staticmethod
    def protect(x):
        """Used to appropriately quote string that might contain whitespace.

        This method is mostly for internal use by the yanny object.
        """
        s = str(x)
        if len(s) == 0 or re.search(r'\s+',s) is not None:
            return '"' + s + '"'
        else:
            return s
    @staticmethod
    def dtype_to_struct(dt,structname='mystruct',enums=dict()):
        """Convert a NumPy dtype object describing a record array to
        a typedef struct statement.

        The second argument is the name of the structure.
        If any of the columns are enum types, enums must
        be a dictionary with the keys the column names, and the values
        are a tuple containing the name of the enum type as the first item
        and a tuple or list of possible values as the second item.
        """
        dtmap = {'i2':'short','i4':'int','i8':'long','f4':'float',
            'f8':'double'}
        lines = list()
        for e in enums:
            lines.append('typedef enum {')
            for n in enums[e][1]:
                lines.append("    %s,"%n)
            lines[-1] = lines[-1].strip(',')
            lines.append('} %s;' % enums[e][0].upper())
            lines.append('')
        lines.append('typedef struct {')
        for c in dt.names:
            if dt[c].kind == 'V':
                t = dt[c].subdtype[0].str[1:]
                l = dt[c].subdtype[1][0]
                s = dt[c].subdtype[0].itemsize
            else:
                t = dt[c].str[1:]
                l = 0
                s = dt[c].itemsize
            line = '    '
            if t[0] == 'S':
                if c in enums:
                    line += enums[c][0].upper()
                else:
                    line += 'char'
            else:
                line += dtmap[t]
            line += ' %s' % c
            if l > 0:
                line += "[%d]" % l
            if t[0] == 'S' and c not in enums:
                line += "[%d]" % s
            line += ';'
            lines.append(line)
        lines.append('} %s;' % structname.upper())
        return "\n".join(lines)
    def __init__(self,filename=None,np=False,debug=False):
        """Create a yanny object using a yanny file.

        Create a yanny object using a yanny file, filename.  If the file exists,
        it is read, & the dict structure of the object will be basically the
        same as that returned by ``read_yanny()`` in the efftickle package.

        If the file does not exist, or if no filename is given, a blank
        structure is returned.  Other methods allow for subsequent writing
        to the file.
        """
        #
        # The symbol hash is inherited from the old read_yanny
        #
        self['symbols'] = dict()
        #
        # Create special attributes that contain the internal status of the object
        # this should prevent overlap with keywords in the data files
        #
        self._filename = ''
        self._contents = ''
        #
        # Since the re is expensive, cache the structure types keyed by the field.
        # Create a dictionary for each structure found.
        #
        self._struct_type_caches = dict()
        self._struct_isarray_caches = dict()
        self._enum_cache = None
        #
        # Optionally convert numeric data into NumPy arrays
        #
        self.np = np
        #
        # Turn on simple debugging
        #
        self.debug = debug
        #
        # If the file exists, read it
        #
        if filename is not None:
            if os.access(filename,os.R_OK):
                self._filename = filename
                f = open(filename,'r')
                self._contents = f.read()
                f.close()
                self._parse()
        return
    def __str__(self):
        """Implement the ``str()`` function for yanny objects.

        Simply prints the current contents of the yanny file.
        """
        return self._contents
    def __eq__(self,other):
        """Test two yanny objects for equality.

        Two yanny objects are assumed to be equal if their contents are equal.
        """
        if isinstance(other,yanny):
            return str(other) == str(self)
        return False
    def type(self,structure,variable):
        """Returns the type of a variable defined in a structure.

        Returns ``None`` if the structure or the variable is undefined.
        """
        if structure not in self:
            return None
        if variable not in self.columns(structure):
            return None
        defl = filter(lambda x: x.find(structure.lower()) > 0,
            self['symbols']['struct'])
        defu = filter(lambda x: x.find(structure.upper()) > 0,
            self['symbols']['struct'])
        if len(defl) != 1 and len(defu) != 1:
            return None
        elif len(defl) == 1:
            definition = defl
        else:
            definition = defu
        #
        # Added code to cache values to speed up parsing large files.
        # 2009.05.11 / Demitri Muna, NYU
        # Find (or create) the cache for this structure.
        #
        try:
            cache = self._struct_type_caches[structure]
        except KeyError:
            self._struct_type_caches[structure] = dict()
            cache = self._struct_type_caches[structure] # cache for one struct type
        #
        # Lookup (or create) the value for this variable
        #
        try:
            var_type = cache[variable]
        except KeyError:
            if self.debug:
                print variable
            typere = re.compile(r'(\S+)\s+%s([[<].*[]>]|);' % variable)
            (typ,array) = typere.search(definition[0]).groups()
            var_type = typ + array.replace('<','[').replace('>',']')
            cache[variable] = var_type
        return var_type
    def basetype(self,structure,variable):
        """Returns the bare type of a variable, stripping off any array
        information."""
        typ = self.type(structure,variable)
        if self.debug:
            print variable, typ
        try:
            return typ[0:typ.index('[')]
        except ValueError:
            return typ
    def isarray(self,structure,variable):
        """Returns True if the variable is an array type.

        For character types, this means a two-dimensional array,
        *e.g.*: ``char[5][20]``.
        """
        try:
            cache = self._struct_isarray_caches[structure]
        except KeyError:
            self._struct_isarray_caches[structure] = dict()
            cache = self._struct_isarray_caches[structure]
        try:
            result = cache[variable]
        except KeyError:
            typ = self.type(structure,variable)
            character_array = re.compile(r'char[[<]\d*[]>][[<]\d*[]>]')
            if ((character_array.search(typ) is not None) or
                (typ.find('char') < 0 and (typ.find('[') >= 0
                or typ.find('<') >= 0))):
                cache[variable] = True
            else:
                cache[variable] = False
            result = cache[variable]
        return result
    def isenum(self,structure,variable):
        """Returns true if a variable is an enum type.
        """
        if self._enum_cache is None:
            self._enum_cache = dict()
            if 'enum' in self['symbols']:
                for e in self['symbols']['enum']:
                    m = re.search(r'typedef\s+enum\s*\{([^}]+)\}\s*(\w+)\s*;',e).groups()
                    self._enum_cache[m[1]] = re.split(r',\s*',m[0].strip())
            else:
                return False
        return self.basetype(structure,variable) in self._enum_cache
    def array_length(self,structure,variable):
        """Returns the length of an array type or 1 if the variable is not
        an array.

        For character types, this is the length of a two-dimensional
        array, *e.g.*, ``char[5][20]`` has length 5.
        """
        if self.isarray(structure,variable):
            typ = self.type(structure,variable)
            return int(typ[typ.index('[')+1:typ.index(']')])
        else:
            return 1
    def char_length(self,structure,variable):
        """Returns the length of a character field.

        *e.g.* ``char[5][20]`` is an array of 5 strings of length 20.
        Returns ``None`` if the variable is not a character type. If the
        length is not specified, *i.e.* ``char[]``, it returns the length of
        the largest string.
        """
        typ = self.type(structure,variable)
        if typ.find('char') < 0:
            return None
        try:
            return int(typ[typ.rfind('[')+1:typ.rfind(']')])
        except ValueError:
            if self.isarray(structure,variable):
                return max([max(map(len,r)) for r in self[structure][variable]])
            else:
                return max(map(len,self[structure][variable]))
    def dtype(self,structure):
        """Returns a NumPy dtype object suitable for describing a table as
        a record array.

        Treats enums as string, which is what the IDL reader does.
        """
        dt = list()
        dtmap = {'short':'i2', 'int':'i4', 'long':'i8', 'float':'f',
            'double':'d' }
        for c in self.columns(structure):
            typ = self.basetype(structure,c)
            if typ == 'char':
                d = "S%d" % self.char_length(structure,c)
            elif self.isenum(structure,c):
                d = "S%d" % max(map(len,self._enum_cache[typ]))
            else:
                d = dtmap[typ]
            if self.isarray(structure,c):
                dt.append((c,d,(self.array_length(structure,c),)))
            else:
                dt.append((c,d))
        dt = numpy.dtype(dt)
        return dt
    def convert(self,structure,variable,value):
        """Converts value into the appropriate (Python) type.

        * ``short`` & ``int`` are converted to Python ``int``.
        * ``long`` is converted to Python ``long``.
        * ``float`` & ``double`` are converted to Python ``float``.
        * Other types are not altered.

        There may be further conversions into NumPy types, but this is the
        first stage.
        """
        typ = self.basetype(structure,variable)
        if (typ == 'short' or typ == 'int'):
            if self.isarray(structure,variable):
                return map(int, value)
            else:
                return int(value)
        if typ == 'long':
            if self.isarray(structure,variable):
                return map(long, value)
            else:
                return long(value)
        if (typ == 'float' or typ == 'double'):
            if self.isarray(structure,variable):
                return map(float, value)
            else:
                return float(value)
        return value
    def tables(self):
        """Returns a list of all the defined structures.

        This is just the list of keys of the object with the 'internal'
        keys removed.
        """
        foo = self['symbols'].keys()
        foo.remove('struct')
        foo.remove('enum')
        return foo
    def columns(self,table):
        """Returns an ordered list of column names associated with a particular
        table.

        The order is the same order as they are defined in the yanny file.
        """
        foo = list()
        if table in self['symbols']:
            return self['symbols'][table]
        return foo
    def size(self,table):
        """Returns the number of rows in a table.
        """
        foo = self.columns(table)
        return len(self[table][foo[0]])
    def pairs(self):
        """Returns a list of keys to keyword/value pairs.

        Equivalent to doing ``self.keys()``, but with all the data tables &
        other control structures stripped out.
        """
        p = list()
        foo = self.tables()
        for k in self.keys():
            if k == 'symbols' or k in foo:
                continue
            p.append(k)
        return p
    def row(self,table,index):
        """Returns a list containing a single row from a specified table in column order

        If index is out of range, it returns an empty list.

        If the yanny object instance is set up for NumPy record arrays, then
        a single row can be obtained with::

            >>> row0 = par['TABLE'][0]
        """
        datarow = list()
        if table in self and index >= 0 and index < self.size(table):
            for c in self.columns(table):
                datarow.append(self[table][c][index])
        return datarow
    def set_filename(self,newfile):
        """Updates the filename associated with the yanny object.

        Use this if the object was created with no filename.
        """
        self._filename = newfile
        return
    def list_of_dicts(self, table):
        """Construct a list of dictionaries.

        Takes a table from the yanny object and constructs a list object
        containing one row per entry. Each item in the list is a dictionary
        keyed by the struct value names.

        If the yanny object instance is set up for NumPy record arrays, then
        the same functionality can be obtained with::

            >>> foo = par['TABLE'][0]['column']
        """
        return_list = list()
        d = dict()

        struct_fields = self.columns(table) # I'm assuming these are in order...

        for i in range(self.size(table)):
            one_row = self.row(table, i) # one row as a list
            j = 0
            for key in struct_fields:
                d[key] = one_row[j]
                j = j + 1

            return_list.append(dict(d)) # append a new dict (copy of d)

        return return_list
    def new_dict_from_pairs(self):
        """Returns a new dictionary of keyword/value pairs.

        The new dictionary (*i.e.*, not a yanny object) contains the keys
        that ``self.pairs()`` returns. There are two reasons this is convenient:

        * the key 'symbols' that is part of the yanny object will not be present
        * a simple yanny file can be read with no further processing

        Example
        -------

        Read a yanny file and return only the pairs::

            >>> new_dict = yanny.yanny(file).new_dict_from_pairs()

        added: Demitri Muna, NYU 2009-04-28
        """
        new_dictionary = dict()
        for key in self.pairs():
            new_dictionary[key] = self[key]
        return new_dictionary
    def write(self,*args):
        """Write a yanny object to a file.

        This assumes that the filename used to create the object was not that
        of a pre-existing file.  If a file of the same name is detected,
        this method will *not* attempt to overwrite it, but will print a warning.
        This also assumes that the special 'symbols' key has been properly
        created.  This will not necessarily make the file very human-readable,
        especially if the data lines are long.  If the name of a new file is
        given, it will write to the new file (assuming it doesn't exist).
        If the writing is successful, the data in the object will be updated.
        """
        if len(args) > 0:
            newfile = args[0]
        else:
            if len(self._filename) > 0:
                newfile = self._filename
            else:
                print "ERROR: No filename specified!"
                return
        basefile = os.path.basename(newfile)
        timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')
        contents = "#\n# %s\n#\n# Created by yanny.py\n#\n# %s\n#\n" % (basefile,timestamp)
        #
        # Print any key/value pairs
        #
        for key in self.pairs():
            contents += "%s %s\n" % (key,self[key])
        #
        # Print out enum definitions
        #
        if len(self['symbols']['enum']) > 0:
            contents += "\n" + "\n\n".join(self['symbols']['enum']) + "\n"
        #
        # Print out structure definitions
        #
        if len(self['symbols']['struct']) > 0:
            contents += "\n" + "\n\n".join(self['symbols']['struct']) + "\n"
        contents += "\n"
        #
        # Print out the data tables
        #
        for sym in self.tables():
            columns = self.columns(sym)
            for k in range(self.size(sym)):
                line = list()
                line.append(sym)
                for col in columns:
                    if self.isarray(sym,col):
                        datum = '{' + ' '.join(map(self.protect,self[sym][col][k])) + '}'
                    else:
                        datum = self.protect(self[sym][col][k])
                    line.append(datum)
                contents += "%s\n" % ' '.join(line)
        #
        # Actually write the data to file
        #
        if os.access(newfile,os.F_OK):
            print "%s exists, aborting write!" % newfile
            print "For reference, here's what would have been written:"
            print contents
        else:
            f = open(newfile,'w')
            print >> f, contents
            f.close()
            self._contents = contents
            self._filename = newfile
            self._parse()
        return
    def append(self,datatable):
        """Appends data to an existing FTCL/yanny file.

        Tries as much as possible to preserve the ordering & format of the
        original file.  The datatable should adhere to the format of the
        yanny object, but it is not necessary to reproduce the 'symbols'
        dictionary.  It will not try to append data to a file that does not
        exist.  If the append is successful, the data in the object will be updated.
        """
        if len(self._filename) == 0:
            print "No filename is set for this object. Use the set_filename method to set the filename!"
            return
        if type(datatable) != dict:
            print "Data to append is not of the correct type. Use a dict!"
            return
        timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')
        contents = ''
        #
        # Print any key/value pairs
        #
        for key in datatable.keys():
            if key.upper() in self.tables() or key == 'symbols':
                continue
            contents += "%s %s\n" % (key, datatable[key])
        #
        # Print out the data tables
        #
        for sym in self.tables():
            if sym.lower() in datatable:
                datasym = sym.lower()
            else:
                datasym = sym
            if datasym in datatable:
                columns = self.columns(sym)
                for k in range(len(datatable[datasym][columns[0]])):
                    line = list()
                    line.append(sym)
                    for col in columns:
                        if self.isarray(sym,col):
                            datum = '{' + ' '.join(map(self.protect,datatable[datasym][col][k])) + '}'
                        else:
                            datum = self.protect(datatable[datasym][col][k])
                        line.append(datum)
                    contents += "%s\n" % ' '.join(line)
        #
        # Actually write the data to file
        #
        if len(contents) > 0:
            contents = ("# Appended by yanny.py at %s.\n" % timestamp) + contents
            if os.access(self._filename,os.W_OK):
                f = open(self._filename,'a')
                print >> f, contents
                f.close()
                self._contents += contents
                self._parse()
            else:
                print "%s does not exist, aborting append!" % self._filename
                print "For reference, here's what would have been written:"
                print contents
        else:
            print "Nothing to be appended!"
        return
    def _parse(self):
        """Converts text into tables that users can use.

        This method is for use internally by the yanny object.  It is not
        meant to be called by users.

        Parsing proceeds in this order:

        #. Lines that end with a backslash character ``\`` are reattached
           to following lines.
        #. Structure & enum definitions are identified, saved into the
           'symbols' dictionary & stripped from the contents.
        #. Structure definitions are interpreted.
        #. At this point, the remaining lines of the original file can only
           contain these things:

           * 'blank' lines, including lines that only contain comments
           * keyword/value pairs
           * structure rows

        #. The remaining lines are scanned sequentially.

           #. 'Blank' lines are identified & ignored.
           #. Whitespace & comments are stripped from non-blank lines.
           #. Empty double braces ``{{}}`` are converted into empty double
              quotes ``""``.
           #. If the first word on a line matches the name of a structure,
              the line is broken up into tokens & each token or set of tokens
              (for arrays) is converted to the appropriate Python type.
           #. If the first word on a line does not match the name of a
              structure, it must be a keyword, so this line is interpreted
              as a keyword/value pair.  No further processing is done to
              the value.

        #. At the conclusion of parsing, if ``self.np`` is ``True``, the
           structures are converted into NumPy record arrays.
        """
        #
        # there are five things we might find
        # 1. 'blank' lines including comments
        # 2. keyword/value pairs (which may have trailing comments)
        # 3. enumeration definitions
        # 4. structure definitions
        # 5. data
        #
        lines = self._contents
        #
        # Reattach lines ending with \
        #
        lines = re.sub(r'\\\s*\n',' ',lines)
        #
        # Find structure & enumeration definitions & strip them out
        #
        self['symbols']['struct'] = re.findall(r'typedef\s+struct\s*\{[^}]+\}\s*\w+\s*;',lines)
        self['symbols']['enum'] = re.findall(r'typedef\s+enum\s*\{[^}]+\}\s*\w+\s*;',lines)
        lines = re.sub(r'typedef\s+struct\s*\{[^}]+\}\s*\w+\s*;','',lines)
        lines = re.sub(r'typedef\s+enum\s*\{[^}]+\}\s*\w+\s*;','',lines)
        #
        # Interpret the structure definitions
        #
        typedefre = re.compile(r'typedef\s+struct\s*\{([^}]+)\}\s*(\w*)\s*;')
        for typedef in self['symbols']['struct']:
            typedefm = typedefre.search(typedef)
            (definition,name) = typedefm.groups()
            self[name.upper()] = dict()
            self['symbols'][name.upper()] = list()
            definitions = re.findall(r'\S+\s+\S+;',definition)
            for d in definitions:
                d = d.replace(';','')
                (datatype,column) = re.split(r'\s+',d)
                column = re.sub(r'[[<].*[]>]$','',column)
                self['symbols'][name.upper()].append(column)
                self[name.upper()][column] = list()
        comments = re.compile(r'^\s*#') # Remove lines containing only comments
        blanks = re.compile(r'^\s*$') # Remove lines containing only whitespace
        trailing_comments = re.compile(r'\s*\#.*$') # Remove trailing comments
        double_braces = re.compile(r'\{\s*\{\s*\}\s*\}') # Double empty braces get replaced with empty quotes
        if len(lines) > 0:
            for line in lines.split('\n'):
                if self.debug:
                    print line
                if len(line) == 0:
                    continue
                if comments.search(line) is not None:
                    continue
                if blanks.search(line) is not None:
                    continue
                #
                # Remove leading & trailing blanks & comments
                #
                line = line.strip()
                line = trailing_comments.sub('',line)
                line = double_braces.sub('""',line)
                #
                # Now if the first word on the line does not match a
                # structure definition it is a keyword/value pair
                #
                (key, value) = self.get_token(line)
                uckey = key.upper()
                if uckey in self['symbols'].keys():
                    #
                    # Structure data
                    #
                    for column in self['symbols'][uckey]:
                        if len(value) > 0 and blanks.search(value) is None:
                            (data,value) = self.get_token(value)
                            if self.isarray(uckey,column):
                                #
                                # An array value
                                # if it's character data, it won't be
                                # delimited by {} unless it is a multidimensional
                                # string array.  It may or may not be delimited
                                # by double quotes
                                #
                                # Note, we're assuming here that the only
                                # multidimensional arrays are string arrays
                                #
                                arraydata = list()
                                while len(data) > 0:
                                    (token, data) = self.get_token(data)
                                    arraydata.append(token)
                                self[uckey][column].append(
                                    self.convert(uckey,column,arraydata))
                            else:
                                #
                                # A single value
                                #
                                self[uckey][column].append(
                                    self.convert(uckey,column,data))
                        else:
                            break
                else:
                    #
                    # Keyword/value pair
                    #
                    self[key] = value
        #
        # If self.np is True, convert tables into NumPy record arrays
        #
        if self.np:
            for t in self.tables():
                record = numpy.zeros((self.size(t),),dtype=self.dtype(t))
                for c in self.columns(t):
                    record[c] = self[t][c]
                self[t] = record
        return

#
# Functions
#
def read_yanny(filename):
    """Reads the contents of an FTCL/yanny file & returns the data in a hash.

    This is just a convenience wrapper on a yanny object, for use when a
    user is not interested in changing the contents of a yanny object.
    """
    par = yanny(filename)
    return par.copy()

def write_yanny(filename,datatable):
    """Writes the contents of a hash to an FTCL/yanny file.

    Ideally used in conjunction with read_yanny() to create an initial
    dictionary of the appropriate format.
    """
    par = yanny(filename)
    for key in datatable:
        par[key] = datatable[key]
    par.write()
    return

def write_yanny_append(filename,datatable):
    """Appends the contents of a hash to an existing FTCL/yanny file.

    Ideally used in conjunction with read_yanny() to create an initial
    dictionary of the appropriate format.
    """
    par = yanny(filename)
    par.append(datatable)
    return

def main():
    """Used to test the yanny class.
    """
    par = yanny(os.path.join(os.getenv('YANNYTOOLS_DIR'),'data','test.par'),
        np=True,debug=True)
    print par.pairs()
    for p in par.pairs():
        print "%s => %s" % (p, par[p])
    print par.keys()
    print par['symbols'].keys()
    print par['symbols']['struct']
    print par['symbols']['enum']
    print par.tables()
    for t in par.tables():
        print par.dtype(t)
        print "%s: %d entries" % (t,par.size(t))
        print par.columns(t)
        for c in par.columns(t):
            print "%s: type %s" % (c,par.type(t,c))
            print par[t][c]
    if par.isenum('MYSTRUCT','new_flag'):
        print par._enum_cache
    par.write() # This should fail, since test.par already exists.
    datatable = {'status_update': {'state':['SUCCESS', 'SUCCESS'],
        'timestamp':['2008-06-22 01:27:33','2008-06-22 01:27:36']},
        'new_keyword':'new_value'}
    par.set_filename(os.path.join(os.getenv('YANNYTOOLS_DIR'),'data','test_append.par'))
    par.append(datatable) # This should also fail, because test_append.par does not exist
    return

#
# Testing purposes
#
if __name__ == '__main__':
    main()