This file is indexed.

/usr/share/lyx/lyx2lyx/LyX.py is in lyx-common 2.2.2-1.

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
# This file is part of lyx2lyx
# -*- coding: utf-8 -*-
# Copyright (C) 2002-2015 The LyX Team
# Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>
# Copyright (C) 2002-2006 José Matos <jamatos@lyx.org>
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

" The LyX module has all the rules related with different lyx file formats."

from parser_tools import get_value, check_token, find_token, \
     find_tokens, find_end_of
import os.path
import gzip
import locale
import sys
import re
import time

try:
    import lyx2lyx_version
    version__ = lyx2lyx_version.version
except: # we are running from build directory so assume the last version
    version__ = '2.2'

default_debug__ = 2

####################################################################
# Private helper functions

def find_end_of_inset(lines, i):
    " Find beginning of inset, where lines[i] is included."
    return find_end_of(lines, i, "\\begin_inset", "\\end_inset")

def minor_versions(major, last_minor_version):
    """ Generate minor versions, using major as prefix and minor
    versions from 0 until last_minor_version, plus the generic version.

    Example:

      minor_versions("1.2", 4) ->
      [ "1.2", "1.2.0", "1.2.1", "1.2.2", "1.2.3"]
    """
    return [major] + [major + ".%d" % i for i in range(last_minor_version + 1)]


# End of helper functions
####################################################################


# Regular expressions used
format_re = re.compile(r"(\d)[\.,]?(\d\d)")
fileformat = re.compile(r"\\lyxformat\s*(\S*)")
original_version = re.compile(r".*?LyX ([\d.]*)")
original_tex2lyx_version = re.compile(r".*?tex2lyx ([\d.]*)")

##
# file format information:
#  file, supported formats, stable release versions
format_relation = [("0_06",    [200], minor_versions("0.6" , 4)),
                   ("0_08",    [210], minor_versions("0.8" , 6) + ["0.7"]),
                   ("0_10",    [210], minor_versions("0.10", 7) + ["0.9"]),
                   ("0_12",    [215], minor_versions("0.12", 1) + ["0.11"]),
                   ("1_0",     [215], minor_versions("1.0" , 4)),
                   ("1_1",     [215], minor_versions("1.1" , 4)),
                   ("1_1_5",   [216], ["1.1", "1.1.5","1.1.5.1","1.1.5.2"]),
                   ("1_1_6_0", [217], ["1.1", "1.1.6","1.1.6.1","1.1.6.2"]),
                   ("1_1_6_3", [218], ["1.1", "1.1.6.3","1.1.6.4"]),
                   ("1_2",     [220], minor_versions("1.2" , 4)),
                   ("1_3",     [221], minor_versions("1.3" , 7)),
                   # Note that range(i,j) is up to j *excluded*.
                   ("1_4", list(range(222,246)), minor_versions("1.4" , 5)),
                   ("1_5", list(range(246,277)), minor_versions("1.5" , 7)),
                   ("1_6", list(range(277,346)), minor_versions("1.6" , 10)),
                   ("2_0", list(range(346,414)), minor_versions("2.0" , 8)),
                   ("2_1", list(range(414,475)), minor_versions("2.1" , 5)),
                   ("2_2", list(range(475,509)), minor_versions("2.2" , 0))
                  ]

####################################################################
# This is useful just for development versions                     #
# if the list of supported formats is empty get it from last step  #
if not format_relation[-1][1]:
    step, mode = format_relation[-1][0], "convert"
    convert = getattr(__import__("lyx_" + step), mode)
    format_relation[-1] = (step,
                           [conv[0] for conv in convert],
                           format_relation[-1][2])
#                                                                  #
####################################################################

def formats_list():
    " Returns a list with supported file formats."
    formats = []
    for version in format_relation:
        for format in version[1]:
            if format not in formats:
                formats.append(format)
    return formats


def format_info():
    " Returns a list with supported file formats."
    out = """Major version:
	minor versions
	formats
"""
    for version in format_relation:
        major = str(version[2][0])
        versions = str(version[2][1:])
        if len(version[1]) == 1:
            formats = str(version[1][0])
        else:
            formats = "%s - %s" % (version[1][-1], version[1][0])
        out += "%s\n\t%s\n\t%s\n\n" % (major, versions, formats)
    return out + '\n'


def get_end_format():
    " Returns the more recent file format available."
    # this check will fail only when we have a new version
    # and there is no format change yet.
    if format_relation[-1][1]:
      return format_relation[-1][1][-1]
    return format_relation[-2][1][-1]


def get_backend(textclass):
    " For _textclass_ returns its backend."
    if textclass == "linuxdoc" or textclass == "manpage":
        return "linuxdoc"
    if textclass.startswith("docbook") or textclass.startswith("agu-"):
        return "docbook"
    return "latex"


def trim_eol(line):
    " Remove end of line char(s)."
    if line[-1] != '\n' and line[-1] != '\r':
        # May happen for the last line of a document
        return line
    if line[-2:-1] == '\r':
        return line[:-2]
    else:
        return line[:-1]


def get_encoding(language, inputencoding, format, cjk_encoding):
    " Returns enconding of the lyx file"
    if format > 248:
        return "utf8"
    # CJK-LyX encodes files using the current locale encoding.
    # This means that files created by CJK-LyX can only be converted using
    # the correct locale settings unless the encoding is given as commandline
    # argument.
    if cjk_encoding == 'auto':
        return locale.getpreferredencoding()
    elif cjk_encoding:
        return cjk_encoding
    from lyx2lyx_lang import lang
    if inputencoding == "auto" or inputencoding == "default":
        return lang[language][3]
    if inputencoding == "":
        return "latin1"
    if inputencoding == "utf8x":
        return "utf8"
    # python does not know the alias latin9
    if inputencoding == "latin9":
        return "iso-8859-15"
    return inputencoding

##
# Class
#
class LyX_base:
    """This class carries all the information of the LyX file."""

    def __init__(self, end_format = 0, input = "", output = "", error = "",
                 debug = default_debug__, try_hard = 0, cjk_encoding = '',
                 final_version = "", systemlyxdir = '', language = "english",
                 encoding = "auto"):

        """Arguments:
        end_format: final format that the file should be converted. (integer)
        input: the name of the input source, if empty resort to standard input.
        output: the name of the output file, if empty use the standard output.
        error: the name of the error file, if empty use the standard error.
        debug: debug level, O means no debug, as its value increases be more verbose.
        """
        self.choose_io(input, output)

        if error:
            self.err = open(error, "w")
        else:
            self.err = sys.stderr

        self.debug = debug
        self.try_hard = try_hard
        self.cjk_encoding = cjk_encoding

        if end_format:
            self.end_format = self.lyxformat(end_format)

            # In case the target version and format are both specified
            # verify that they are compatible. If not send a warning
            # and ignore the version.
            if final_version:
                message = "Incompatible version %s for specified format %d" % (
                    final_version, self.end_format)
                for version in format_relation:
                    if self.end_format in version[1]:
                        if final_version not in version[2]:
                            self.warning(message)
                            final_version = ""
        elif final_version:
            for version in format_relation:
                if final_version in version[2]:
                    # set the last format for that version
                    self.end_format = version[1][-1]
                    break
            else:
                final_version = ""
        else:
            self.end_format = get_end_format()

        if not final_version:
            for step in format_relation:
                if self.end_format in step[1]:
                    final_version = step[2][1]
        self.final_version = final_version
        self.warning("Final version: %s" % self.final_version, 10)
        self.warning("Final format: %d" % self.end_format, 10)

        self.backend = "latex"
        self.textclass = "article"
        # This is a hack: We use '' since we don't know the default
        # layout of the text class. LyX will parse it as default layout.
        # FIXME: Read the layout file and use the real default layout
        self.default_layout = ''
        self.header = []
        self.preamble = []
        self.body = []
        self.status = 0
        self.encoding = encoding
        self.language = language
        self.systemlyxdir = systemlyxdir


    def warning(self, message, debug_level= default_debug__):
        """ Emits warning to self.error, if the debug_level is less
        than the self.debug."""
        if debug_level <= self.debug:
            self.err.write("Warning: " + message + "\n")


    def error(self, message):
        " Emits a warning and exits if not in try_hard mode."
        self.warning(message)
        if not self.try_hard:
            self.warning("Quitting.")
            sys.exit(1)

        self.status = 2


    def read(self):
        """Reads a file into the self.header and
        self.body parts, from self.input."""

        while True:
            line = self.input.readline()
            if not line:
                self.error("Invalid LyX file.")

            line = trim_eol(line)
            if check_token(line, '\\begin_preamble'):
                while 1:
                    line = self.input.readline()
                    if not line:
                        self.error("Invalid LyX file.")

                    line = trim_eol(line)
                    if check_token(line, '\\end_preamble'):
                        break

                    if line.split()[:0] in ("\\layout",
                                            "\\begin_layout", "\\begin_body"):

                        self.warning("Malformed LyX file:"
                                     "Missing '\\end_preamble'."
                                     "\nAdding it now and hoping"
                                     "for the best.")

                    self.preamble.append(line)

            if check_token(line, '\\end_preamble'):
                continue

            line = line.rstrip()
            if not line:
                continue

            if line.split()[0] in ("\\layout", "\\begin_layout",
                                   "\\begin_body", "\\begin_deeper"):
                self.body.append(line)
                break

            self.header.append(line)

        i = find_token(self.header, '\\textclass', 0)
        if i == -1:
            self.warning("Malformed LyX file: Missing '\\textclass'.")
            i = find_token(self.header, '\\lyxformat', 0) + 1
            self.header[i:i] = ['\\textclass article']

        self.textclass = get_value(self.header, "\\textclass", 0)
        self.backend = get_backend(self.textclass)
        self.format  = self.read_format()
        self.language = get_value(self.header, "\\language", 0,
                                  default = "english")
        self.inputencoding = get_value(self.header, "\\inputencoding",
                                       0, default = "auto")
        self.encoding = get_encoding(self.language,
                                     self.inputencoding, self.format,
                                     self.cjk_encoding)
        self.initial_version = self.read_version()

        # Second pass over header and preamble, now we know the file encoding
        # Do not forget the textclass (Debian bug #700828)
        self.textclass = self.textclass.decode(self.encoding)
        for i in range(len(self.header)):
            self.header[i] = self.header[i].decode(self.encoding)
        for i in range(len(self.preamble)):
            self.preamble[i] = self.preamble[i].decode(self.encoding)

        # Read document body
        while 1:
            line = self.input.readline().decode(self.encoding)
            if not line:
                break
            self.body.append(trim_eol(line))


    def write(self):
        " Writes the LyX file to self.output."
        self.set_version()
        self.set_format()
        self.set_textclass()
        if self.encoding == "auto":
            self.encoding = get_encoding(self.language, self.encoding,
                                         self.format, self.cjk_encoding)
        if self.preamble:
            i = find_token(self.header, '\\textclass', 0) + 1
            preamble = ['\\begin_preamble'] + self.preamble + ['\\end_preamble']
            header = self.header[:i] + preamble + self.header[i:]
        else:
            header = self.header

        for line in header + [''] + self.body:
            self.output.write(line.encode(self.encoding)+"\n")


    def choose_io(self, input, output):
        """Choose input and output streams, dealing transparently with
        compressed files."""

        if output:
            self.output = open(output, "wb")
        else:
            self.output = sys.stdout

        if input and input != '-':
            self.dir = os.path.dirname(os.path.abspath(input))
            try:
                gzip.open(input).readline()
                self.input = gzip.open(input)
                self.output = gzip.GzipFile(mode="wb", fileobj=self.output)
            except:
                self.input = open(input)
        else:
            self.dir = ''
            self.input = sys.stdin


    def lyxformat(self, format):
        " Returns the file format representation, an integer."
        result = format_re.match(format)
        if result:
            format = int(result.group(1) + result.group(2))
        elif format == '2':
            format = 200
        else:
            self.error(str(format) + ": " + "Invalid LyX file.")

        if format in formats_list():
            return format

        self.error(str(format) + ": " + "Format not supported.")
        return None


    def read_version(self):
        """ Searchs for clues of the LyX version used to write the
        file, returns the most likely value, or None otherwise."""

        for line in self.header:
            if line[0] != "#":
                return None

            line = line.replace("fix",".")
            # need to test original_tex2lyx_version first because tex2lyx
            # writes "#LyX file created by tex2lyx 2.2"
            result = original_tex2lyx_version.match(line)
            if not result:
                result = original_version.match(line)
                if result:
                    # Special know cases: reLyX and KLyX
                    if line.find("reLyX") != -1 or line.find("KLyX") != -1:
                        return "0.12"
            if result:
                res = result.group(1)
                if not res:
                    self.warning(line)
                #self.warning("Version %s" % result.group(1))
                return res
        self.warning(str(self.header[:2]))
        return None


    def set_version(self):
        " Set the header with the version used."

        initial_comment = " ".join(["#LyX %s created this file." % version__,
                                    "For more info see http://www.lyx.org/"])

        # Simple heuristic to determine the comment that always starts
        # a lyx file
        if self.header[0].startswith("#"):
            self.header[0] = initial_comment
        else:
            self.header.insert(0, initial_comment)

        # Old lyx files had a two lines comment header:
        # 1) the first line had the user who had created it
        # 2) the second line had the lyx version used
        # later we decided that 1) was a privacy risk for no gain
        # here we remove the second line effectively erasing 1)
        if self.header[1][0] == '#':
            del self.header[1]


    def read_format(self):
        " Read from the header the fileformat of the present LyX file."
        for line in self.header:
            result = fileformat.match(line)
            if result:
                return self.lyxformat(result.group(1))
        else:
            self.error("Invalid LyX File.")
        return None


    def set_format(self):
        " Set the file format of the file, in the header."
        if self.format <= 217:
            format = str(float(self.format)/100)
        else:
            format = str(self.format)
        i = find_token(self.header, "\\lyxformat", 0)
        self.header[i] = "\\lyxformat %s" % format


    def set_textclass(self):
        i = find_token(self.header, "\\textclass", 0)
        self.header[i] = "\\textclass %s" % self.textclass


    #Note that the module will be added at the END of the extant ones
    def add_module(self, module):
      i = find_token(self.header, "\\begin_modules", 0)
      if i == -1:
        #No modules yet included
        i = find_token(self.header, "\\textclass", 0)
        if i == -1:
          self.warning("Malformed LyX document: No \\textclass!!")
          return
        modinfo = ["\\begin_modules", module, "\\end_modules"]
        self.header[i + 1: i + 1] = modinfo
        return
      j = find_token(self.header, "\\end_modules", i)
      if j == -1:
        self.warning("(add_module)Malformed LyX document: No \\end_modules.")
        return
      k = find_token(self.header, module, i)
      if k != -1 and k < j:
        return
      self.header.insert(j, module)


    def get_module_list(self):
      i = find_token(self.header, "\\begin_modules", 0)
      if (i == -1):
        return []
      j = find_token(self.header, "\\end_modules", i)
      return self.header[i + 1 : j]


    def set_module_list(self, mlist):
      modbegin = find_token(self.header, "\\begin_modules", 0)
      newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
      if (modbegin == -1):
        #No modules yet included
        tclass = find_token(self.header, "\\textclass", 0)
        if tclass == -1:
          self.warning("Malformed LyX document: No \\textclass!!")
          return
        modbegin = tclass + 1
        self.header[modbegin:modbegin] = newmodlist
        return
      modend = find_token(self.header, "\\end_modules", modbegin)
      if modend == -1:
        self.warning("(set_module_list)Malformed LyX document: No \\end_modules.")
        return
      newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
      self.header[modbegin:modend + 1] = newmodlist


    def set_parameter(self, param, value):
        " Set the value of the header parameter."
        i = find_token(self.header, '\\' + param, 0)
        if i == -1:
            self.warning('Parameter not found in the header: %s' % param, 3)
            return
        self.header[i] = '\\%s %s' % (param, str(value))


    def is_default_layout(self, layout):
        " Check whether a layout is the default layout of this class."
        # FIXME: Check against the real text class default layout
        if layout == 'Standard' or layout == self.default_layout:
            return 1
        return 0


    def convert(self):
        "Convert from current (self.format) to self.end_format."
        if self.format == self.end_format:
            self.warning("No conversion needed: Target format %s "
                "same as current format!" % self.format, default_debug__)
            return

        mode, conversion_chain = self.chain()
        self.warning("conversion chain: " + str(conversion_chain), 3)

        for step in conversion_chain:
            steps = getattr(__import__("lyx_" + step), mode)

            self.warning("Convertion step: %s - %s" % (step, mode),
                         default_debug__ + 1)
            if not steps:
                self.error("The conversion to an older "
                "format (%s) is not implemented." % self.format)

            multi_conv = len(steps) != 1
            for version, table in steps:
                if multi_conv and \
                   (self.format >= version and mode == "convert") or\
                   (self.format <= version and mode == "revert"):
                    continue

                for conv in table:
                    init_t = time.time()
                    try:
                        conv(self)
                    except:
                        self.warning("An error ocurred in %s, %s" %
                                     (version, str(conv)),
                                     default_debug__)
                        if not self.try_hard:
                            raise
                        self.status = 2
                    else:
                        self.warning("%lf: Elapsed time on %s" %
                                     (time.time() - init_t,
                                      str(conv)), default_debug__ +
                                     1)
                self.format = version
                if self.end_format == self.format:
                    return


    def chain(self):
        """ This is where all the decisions related with the
        conversion are taken.  It returns a list of modules needed to
        convert the LyX file from self.format to self.end_format"""

        self.start =  self.format
        format = self.format
        correct_version = 0

        for rel in format_relation:
            if self.initial_version in rel[2]:
                if format in rel[1]:
                    initial_step = rel[0]
                    correct_version = 1
                    break

        if not correct_version:
            if format <= 215:
                self.warning("Version does not match file format, "
                             "discarding it. (Version %s, format %d)" %
                             (self.initial_version, self.format))
            for rel in format_relation:
                if format in rel[1]:
                    initial_step = rel[0]
                    break
            else:
                # This should not happen, really.
                self.error("Format not supported.")

        # Find the final step
        for rel in format_relation:
            if self.end_format in rel[1]:
                final_step = rel[0]
                break
        else:
            self.error("Format not supported.")

        # Convertion mode, back or forth
        steps = []
        if (initial_step, self.start) < (final_step, self.end_format):
            mode = "convert"
            full_steps = []
            for step in format_relation:
                if  initial_step <= step[0] <= final_step and step[2][0] <= self.final_version:
                    full_steps.append(step)
            if full_steps[0][1][-1] == self.format:
                full_steps = full_steps[1:]
            for step in full_steps:
                steps.append(step[0])
        else:
            mode = "revert"
            relation_format = format_relation[:]
            relation_format.reverse()
            last_step = None

            for step in relation_format:
                if  final_step <= step[0] <= initial_step:
                    steps.append(step[0])
                    last_step = step

            if last_step[1][-1] == self.end_format:
                steps.pop()

        self.warning("Convertion mode: %s\tsteps%s" %(mode, steps), 10)
        return mode, steps


# Part of an unfinished attempt to make lyx2lyx gave a more
# structured view of the document.
#    def get_toc(self, depth = 4):
#        " Returns the TOC of this LyX document."
#        paragraphs_filter = {'Title' : 0,'Chapter' : 1, 'Section' : 2,
#                             'Subsection' : 3, 'Subsubsection': 4}
#        allowed_insets = ['Quotes']
#        allowed_parameters = ('\\paragraph_spacing', '\\noindent',
#                              '\\align', '\\labelwidthstring',
#                              "\\start_of_appendix", "\\leftindent")
#        sections = []
#        for section in paragraphs_filter.keys():
#            sections.append('\\begin_layout %s' % section)

#        toc_par = []
#        i = 0
#        while 1:
#            i = find_tokens(self.body, sections, i)
#            if i == -1:
#                break

#            j = find_end_of(self.body,  i + 1, '\\begin_layout', '\\end_layout')
#            if j == -1:
#                self.warning('Incomplete file.', 0)
#                break

#            section = self.body[i].split()[1]
#            if section[-1] == '*':
#                section = section[:-1]

#            par = []

#            k = i + 1
#            # skip paragraph parameters
#            while not self.body[k].strip() or self.body[k].split()[0] \
#                      in allowed_parameters:
#                k += 1

#            while k < j:
#                if check_token(self.body[k], '\\begin_inset'):
#                    inset = self.body[k].split()[1]
#                    end = find_end_of_inset(self.body, k)
#                    if end == -1 or end > j:
#                        self.warning('Malformed file.', 0)

#                    if inset in allowed_insets:
#                        par.extend(self.body[k: end+1])
#                    k = end + 1
#                else:
#                    par.append(self.body[k])
#                    k += 1

#            # trim empty lines in the end.
#            while par and par[-1].strip() == '':
#                par.pop()

#            toc_par.append(Paragraph(section, par))

#            i = j + 1

#        return toc_par


class File(LyX_base):
    " This class reads existing LyX files."

    def __init__(self, end_format = 0, input = "", output = "", error = "",
                 debug = default_debug__, try_hard = 0, cjk_encoding = '',
                 final_version = '', systemlyxdir = ''):
        LyX_base.__init__(self, end_format, input, output, error,
                          debug, try_hard, cjk_encoding, final_version,
                          systemlyxdir)
        self.read()


# FIXME: header settings are completely outdated, don't use like this
#class NewFile(LyX_base):
#    " This class is to create new LyX files."
#    def set_header(self, **params):
#        # set default values
#        self.header.extend([
#            "#LyX xxxx created this file."
#            "For more info see http://www.lyx.org/",
#            "\\lyxformat xxx",
#            "\\begin_document",
#            "\\begin_header",
#            "\\textclass article",
#            "\\language english",
#            "\\inputencoding auto",
#            "\\font_roman default",
#            "\\font_sans default",
#            "\\font_typewriter default",
#            "\\font_default_family default",
#            "\\font_sc false",
#            "\\font_osf false",
#            "\\font_sf_scale 100",
#            "\\font_tt_scale 100",
#            "\\graphics default",
#            "\\paperfontsize default",
#            "\\papersize default",
#            "\\use_geometry false",
#            "\\use_amsmath 1",
#            "\\cite_engine basic",
#            "\\use_bibtopic false",
#            "\\use_indices false",
#            "\\paperorientation portrait",
#            "\\secnumdepth 3",
#            "\\tocdepth 3",
#            "\\paragraph_separation indent",
#            "\\defskip medskip",
#            "\\quotes_language english",
#            "\\papercolumns 1",
#            "\\papersides 1",
#            "\\paperpagestyle default",
#            "\\tracking_changes false",
#            "\\end_header"])

#        self.format = get_end_format()
#        for param in params:
#            self.set_parameter(param, params[param])


#    def set_body(self, paragraphs):
#        self.body.extend(['\\begin_body',''])

#        for par in paragraphs:
#            self.body.extend(par.asLines())

#        self.body.extend(['','\\end_body', '\\end_document'])


# Part of an unfinished attempt to make lyx2lyx gave a more
# structured view of the document.
#class Paragraph:
#    # unfinished implementation, it is missing the Text and Insets
#    # representation.
#    " This class represents the LyX paragraphs."
#    def __init__(self, name, body=[], settings = [], child = []):
#        """ Parameters:
#        name: paragraph name.
#        body: list of lines of body text.
#        child: list of paragraphs that descend from this paragraph.
#        """
#        self.name = name
#        self.body = body
#        self.settings = settings
#        self.child = child

#    def asLines(self):
#        """ Converts the paragraph to a list of strings, representing
#        it in the LyX file."""

#        result = ['','\\begin_layout %s' % self.name]
#        result.extend(self.settings)
#        result.append('')
#        result.extend(self.body)
#        result.append('\\end_layout')

#        if not self.child:
#            return result

#        result.append('\\begin_deeper')
#        for node in self.child:
#            result.extend(node.asLines())
#        result.append('\\end_deeper')

#        return result