def _quotes_are_needed(string): loader = yaml.BaseLoader('key: ' + string) # Remove the 5 first tokens corresponding to 'key: ' (StreamStartToken, # BlockMappingStartToken, KeyToken, ScalarToken(value=key), ValueToken) for _ in range(5): loader.get_token() try: a, b = loader.get_token(), loader.get_token() if (isinstance(a, yaml.ScalarToken) and a.style is None and isinstance(b, yaml.BlockEndToken) and a.value == string): return False return True except yaml.scanner.ScannerError: return True
def token_generator(buffer): yaml_loader = yaml.BaseLoader(buffer) try: prev = None curr = yaml_loader.get_token() while curr is not None: next = yaml_loader.get_token() nextnext = yaml_loader.peek_token() yield Token(curr.start_mark.line + 1, curr, prev, next, nextnext) prev = curr curr = next except yaml.scanner.ScannerError: pass
def get_model_description(file_path_fmu: str) -> FMUModelDescription: """ Returns the model description including variables_sorted. the parameters, inputs, outputs and other internal variables from the model description file of the fmu using the cosim-cli. Args: file_path_fmu (str): Absolute file path of the FMU file. Returns: FMUModelDescription: NamedTuple that contains model description """ mode = 'inspect' assert os.path.isfile( PATH_TO_COSIM), 'The cosim CLI is not found: %s' % PATH_TO_COSIM assert os.path.isfile( file_path_fmu), 'The fmu file is not found: %s' % file_path_fmu #: Run the cosim to get the result in yaml format result = '' error = '' try: with Popen(args=[PATH_TO_COSIM, mode, file_path_fmu], shell=True, stdout=PIPE, stderr=PIPE) as proc: result = proc.stdout.read() error = proc.stderr.read() except OSError as exception: raise OSError(f'{result}, {error}, {exception}') #: Parse yaml to dictionary result = yaml.BaseLoader(result).get_data() return FMUModelDescription( name=result['name'], uuid=result['uuid'], model_variable=parse_model_variables(result['variables']), description=result['description'], author=result['author'], version=result['version'], )
def token_or_comment_generator(buffer): yaml_loader = yaml.BaseLoader(buffer) try: prev = None curr = yaml_loader.get_token() while curr is not None: next = yaml_loader.get_token() nextnext = (yaml_loader.peek_token() if yaml_loader.check_token() else None) yield Token(curr.start_mark.line + 1, curr, prev, next, nextnext) for comment in comments_between_tokens(curr, next): yield comment prev = curr curr = next except yaml.scanner.ScannerError: pass
def _tokens_from_string(self, source): yaml_loader = pyyaml.BaseLoader(source) curr = yaml_loader.get_token() while curr is not None: yield curr curr = yaml_loader.get_token()