@@ -62,10 +62,15 @@ class Card(_Verify):
6262 # This regex helps delete leading zeros from numbers, otherwise
6363 # Python might evaluate them as octal values (this is not-greedy, however,
6464 # so it may not strip leading zeros from a float, which is fine)
65- _number_FSC_RE = re .compile (r'(?P<sign>[+-])?0*?(?P<digt>{})' .format (
66- _digits_FSC ))
67- _number_NFSC_RE = re .compile (r'(?P<sign>[+-])? *0*?(?P<digt>{})' .format (
68- _digits_NFSC ))
65+ _number_FSC_RE = re .compile (rf'(?P<sign>[+-])?0*?(?P<digt>{ _digits_FSC } )' )
66+ _number_NFSC_RE = \
67+ re .compile (rf'(?P<sign>[+-])? *0*?(?P<digt>{ _digits_NFSC } )' )
68+
69+ # Used in cards using the CONTINUE convention which expect a string
70+ # followed by an optional comment
71+ _strg = r'\'(?P<strg>([ -~]+?|\'\'|) *?)\'(?=$|/| )'
72+ _comm_field = r'(?P<comm_field>(?P<sepr>/ *)(?P<comm>(.|\n)*))'
73+ _strg_comment_RE = re .compile (f'({ _strg } )? *{ _comm_field } ?' )
6974
7075 # FSC commentary card string which must contain printable ASCII characters.
7176 # Note: \Z matches the end of the string without allowing newlines
@@ -96,7 +101,7 @@ class Card(_Verify):
96101 # since a greedy match will find a single-quote after
97102 # the comment separator resulting in an incorrect
98103 # match.
99- r'\'(?P<strg>([ -~]+?|\'\'|)) *?\'(?=$|/| ) |'
104+ rf' { _strg } |'
100105 r'(?P<bool>[FT])|'
101106 r'(?P<numr>' + _numr_FSC + r')|'
102107 r'(?P<cplx>\( *'
@@ -111,17 +116,13 @@ class Card(_Verify):
111116 _value_NFSC_RE = re .compile (
112117 r'(?P<valu_field> *'
113118 r'(?P<valu>'
114- r'\'(?P<strg>([ -~]+?|\'\'|) *?)\'(?=$|/| ) |'
119+ rf' { _strg } |'
115120 r'(?P<bool>[FT])|'
116121 r'(?P<numr>' + _numr_NFSC + r')|'
117122 r'(?P<cplx>\( *'
118123 r'(?P<real>' + _numr_NFSC + r') *, *'
119124 r'(?P<imag>' + _numr_NFSC + r') *\))'
120- r')? *)'
121- r'(?P<comm_field>'
122- r'(?P<sepr>/ *)'
123- r'(?P<comm>(.|\n)*)'
124- r')?$' )
125+ fr')? *){ _comm_field } ?$' )
125126
126127 _rvkc_identifier = r'[a-zA-Z_]\w*'
127128 _rvkc_field = _rvkc_identifier + r'(\.\d+)?'
@@ -725,19 +726,6 @@ def _parse_value(self):
725726 if self ._check_if_rvkc (self ._image ):
726727 return self ._value
727728
728- if len (self ._image ) > self .length :
729- values = []
730- for card in self ._itersubcards ():
731- value = card .value .rstrip ().replace ("''" , "'" )
732- if value and value [- 1 ] == '&' :
733- value = value [:- 1 ]
734- values .append (value )
735-
736- value = '' .join (values )
737-
738- self ._valuestring = value
739- return value
740-
741729 m = self ._value_NFSC_RE .match (self ._split ()[1 ])
742730
743731 if m is None :
@@ -789,21 +777,22 @@ def _parse_comment(self):
789777 if self .keyword in Card ._commentary_keywords or self ._invalid :
790778 return ''
791779
792- if len (self ._image ) > self .length :
793- comments = []
794- for card in self ._itersubcards ():
795- if card .comment :
796- comments .append (card .comment )
797- comment = '/ ' + ' ' .join (comments ).rstrip ()
798- m = self ._value_NFSC_RE .match (comment )
799- else :
800- m = self ._value_NFSC_RE .match (self ._split ()[1 ])
801-
780+ valuecomment = self ._split ()[1 ]
781+ m = self ._value_NFSC_RE .match (valuecomment )
782+ comment = ''
802783 if m is not None :
803- comment = m .group ('comm' )
804- if comment :
805- return comment .rstrip ()
806- return ''
784+ # Don't combine this if statement with the one above, because
785+ # we only want the elif case to run if this was not a valid
786+ # card at all
787+ if m .group ('comm' ):
788+ comment = m .group ('comm' ).rstrip ()
789+ elif '/' in valuecomment :
790+ # The value in this FITS file was not in a valid/known format. In
791+ # this case the best we can do is guess that everything after the
792+ # first / was meant to be the comment
793+ comment = valuecomment .split ('/' , 1 )[1 ].strip ()
794+
795+ return comment
807796
808797 def _split (self ):
809798 """
@@ -817,6 +806,34 @@ def _split(self):
817806 else :
818807 image = self .image
819808
809+ # Split cards with CONTINUE cards
810+ if len (self ._image ) > self .length :
811+ values = []
812+ comments = []
813+ keyword = None
814+ for card in self ._itersubcards ():
815+ kw , vc = card ._split ()
816+ if keyword is None :
817+ keyword = kw
818+
819+ # Should match a string followed by a comment; if not it
820+ # might be an invalid Card, so we just take it verbatim
821+ m = self ._strg_comment_RE .match (vc )
822+ if not m :
823+ return kw , vc
824+
825+ value = m .group ('strg' ) or ''
826+ value = value .rstrip ().replace ("''" , "'" )
827+ if value and value [- 1 ] == '&' :
828+ value = value [:- 1 ]
829+ values .append (value )
830+ comment = m .group ('comm' )
831+ if comment :
832+ comments .append (comment .rstrip ())
833+
834+ valuecomment = f"'{ '' .join (values )} ' / { ' ' .join (comments )} "
835+ return keyword , valuecomment
836+
820837 if self .keyword in self ._special_keywords :
821838 keyword , valuecomment = image .split (' ' , 1 )
822839 else :
0 commit comments