Ignore:
Timestamp:
Oct 24, 2017 11:53:41 AM (4 years ago)
Author:
vondreele
Message:

replace old CifFile? with new py 2/7/3.6 compliant code
fix cif file import phase & powder file
fix CemComp? restraint editing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CifFile/yapps3_compiled_rt.py

    r469 r3137  
    1111# Modified for PyCIFRW by JRH to allow external scanner
    1212#
     13# To maximize python3/python2 compatibility
     14from __future__ import print_function
     15from __future__ import unicode_literals
     16from __future__ import division
     17from __future__ import absolute_import
     18
    1319""" Detail of JRH modifications.
    1420
     
    3541
    3642import sys, re
    37 try:
    38     import StarScan
    39     have_star_scan = True
    40 except ImportError:
     43
     44
     45# For normal installation this module is "CifFile.yapps3_compiled_rt"
     46# and StarScan is an extension module within the parent CifFile module.
     47if __name__.startswith('CifFile.'):
     48    try:
     49        from . import StarScan
     50        have_star_scan = True
     51    except ImportError:
     52        have_star_scan = False
     53# Otherwise assume this is imported from the yapps3/yapps2.py script
     54# that is executed from Makefile to generate YappsStarParser sources.
     55else:
     56    assert __name__ == 'yapps3_compiled_rt', "Unexpected module name."
     57    assert sys.argv[0].endswith('yapps2.py'), (
     58        "This should be reached only when running yapps2.py in Makefile.")
    4159    have_star_scan = False
    4260
     
    87105        self.pos = 0
    88106        self.ignore = ignore
    89         self.scantype = scantype
     107        self.scantype = scantype
    90108        self.first_line_number = 1
    91         if self.scantype == "flex" and have_star_scan:
    92             StarScan.prepare(input)
    93             self.scan = self.compiled_scan
    94             self.token = self.compiled_token
    95             self.__del__ = StarScan.cleanup
     109        if self.scantype == "flex" and have_star_scan:
     110            StarScan.prepare(input)
     111            self.scan = self.compiled_scan
     112            self.token = self.compiled_token
     113            self.__del__ = StarScan.cleanup
    96114        elif self.scantype == "flex":
    97             print "Warning: using Python scanner"
    98             self.scantype = "standard"
     115            print("WARNING: using Python scanner although C scanner requested")
     116            self.scantype = "standard"
    99117        if self.scantype != "flex":
    100             self.scan = self.interp_scan
    101             self.token = self.interp_token
    102        
     118            self.scan = self.interp_scan
     119            self.token = self.interp_token
     120
    103121        if patterns is not None:
    104122            # Compile the regex strings into regex objects
     
    167185   
    168186    def compiled_token(self,i,restrict=0):
    169         try:
    170             return StarScan.token(i)
     187        try:
     188            return StarScan.token(i)
    171189        except IndexError:
    172             raise NoMoreTokens()
     190            raise NoMoreTokens()
    173191   
    174192    def __repr__(self):
    175193        """Print the last 10 tokens that have been scanned in"""
    176194        output = ''
    177         if self.scantype != "flex":
     195        if self.scantype != "flex":
    178196            for t in self.tokens[-10:]:
    179197                output = '%s\n  (@%s)  %s  =  %s' % (output,t[0],t[2],repr(t[3]))
    180198        else:
    181             out_tokens = StarScan.last_ten()
    182             for t in out_tokens:
     199            out_tokens = StarScan.last_ten()
     200            for t in out_tokens:
    183201                output = '%s\n  (~line %s)  %s  =  %s' % (output,t[0],t[2],repr(t[3]))
    184202        return output
     
    237255
    238256    def compiled_scan(self,restrict):
    239         token = StarScan.scan()
    240         print "Calling compiled scan, got %s" % `token`
    241         if token[2] not in restrict:
    242             msg = "Bad Token"
    243             if restrict:
    244                 msg = "Trying to find one of "+join(restrict,", ")
     257        token = StarScan.scan()
     258        print("Calling compiled scan, got %s" % repr(token))
     259        if token[2] not in restrict:
     260            msg = "Bad Token"
     261            if restrict:
     262               msg = "Trying to find one of "+join(restrict,", ")
    245263            raise SyntaxError(self.pos,msg)
    246264        self.tokens.append(token)
    247         self.restrictions.append(restrict)
    248         return
     265        self.restrictions.append(restrict)
     266        return
    249267
    250268class Parser:
     
    341359
    342360    # Now print the string, along with an indicator
    343     print >>sys.stderr, '> ',text
    344     print >>sys.stderr, '> ',' '*p + '^'
     361    print('> ',text,file=sys.stderr)
     362    print('> ',' '*p + '^',file=sys.stderr)
    345363   
    346364def print_error(input, err, scanner):
     
    350368    line_number = scanner.get_line_number()
    351369    column_number = scanner.get_column_number()
    352     print >>sys.stderr, '%d:%d: %s' % (line_number, column_number, err.msg)
     370    print('%d:%d: %s' % (line_number, column_number, err.msg),file=sys.stderr)
    353371
    354372    context = err.context
     
    358376    while context:
    359377        # TODO: add line number
    360         print >>sys.stderr, 'while parsing %s%s:' % (context.rule, tuple(context.args))
     378        print('while parsing %s%s:' % (context.rule, tuple(context.args)),file=sys.stderr)
    361379        print_line_with_pointer(input, context.scanner.get_prev_char_pos(context.tokenpos))
    362380        context = context.parent
     
    365383    try:
    366384        return getattr(parser, rule)()
    367     except SyntaxError, e:
     385    except SyntaxError as e:
    368386        input = parser._scanner.input
    369387        print_error(input, e, parser._scanner)
    370388    except NoMoreTokens:
    371         print >>sys.stderr, 'Could not complete parsing; stopped around here:'
    372         print >>sys.stderr, parser._scanner
     389        print('Could not complete parsing; stopped around here:',file=sys.stderr)
     390        print(parser._scanner,file=sys.stderr)
Note: See TracChangeset for help on using the changeset viewer.