(v, level)
| 123 | |
| 124 | # Emits all output tokens without intervening whitespaces. |
| 125 | def GenerateTokens(v, level): |
| 126 | if isinstance(v, str): |
| 127 | yield '"' + ''.join(TranslateToGnChars(v)) + '"' |
| 128 | |
| 129 | elif isinstance(v, bool): |
| 130 | yield 'true' if v else 'false' |
| 131 | |
| 132 | elif isinstance(v, int): |
| 133 | yield str(v) |
| 134 | |
| 135 | elif isinstance(v, list): |
| 136 | yield '[' |
| 137 | for i, item in enumerate(v): |
| 138 | if i > 0: |
| 139 | yield ',' |
| 140 | for tok in GenerateTokens(item, level + 1): |
| 141 | yield tok |
| 142 | yield ']' |
| 143 | |
| 144 | elif isinstance(v, dict): |
| 145 | if level > 0: |
| 146 | yield '{' |
| 147 | for key in sorted(v): |
| 148 | if not isinstance(key, str): |
| 149 | raise ValueError('Dictionary key is not a string.') |
| 150 | if not key or key[0].isdigit() or not key.replace('_', '').isalnum(): |
| 151 | raise ValueError('Dictionary key is not a valid GN identifier.') |
| 152 | yield key # No quotations. |
| 153 | yield '=' |
| 154 | for tok in GenerateTokens(v[key], level + 1): |
| 155 | yield tok |
| 156 | if level > 0: |
| 157 | yield '}' |
| 158 | |
| 159 | else: # Not supporting float: Add only when needed. |
| 160 | raise ValueError('Unsupported type when printing to GN.') |
| 161 | |
| 162 | can_start = lambda tok: tok and tok not in ',}]=' |
| 163 | can_end = lambda tok: tok and tok not in ',{[=' |
no test coverage detected
searching dependent graphs…