def install_extras(mkdocs_yml, theme=None): with open(mkdocs_yml) as f: config, indent, bsi = load_yaml_guess_indent(f) if theme is None: if 'theme' not in config: raise ValueError('no theme specified in mkdocs.yml; pass ' + '--theme instead') theme = config['theme'] if isinstance(theme, abc.Mapping): theme = theme['name'] theme_dir = get_theme_dir(theme) docs_dir = config.get('docs_dir', 'docs') for path, prop in [('css', 'extra_css'), ('js', 'extra_javascript')]: files = os.listdir(os.path.join(theme_dir, path)) if not files: # pragma: no cover continue extras = config.setdefault(prop, []) for f in files: relpath = os.path.join(path, f) src = os.path.join(theme_dir, relpath) dst = os.path.join(docs_dir, relpath) _makedirs(os.path.dirname(dst)) shutil.copyfile(src, dst) if relpath not in extras: extras.append(relpath) with open(mkdocs_yml, 'w') as f: yaml.round_trip_dump(config, f, indent=indent, block_seq_indent=bsi)
def add(source_path: Path, image: str): """ Add an environment variable. :param source_path: A path containing specification files. :param image: The container image to identify files to edit. """ for path in source_path.rglob('*'): if path.is_file(): if path.suffix == '.json': with open(str(path)) as json_file: json_data = json.load(json_file) specification_image = json_data['transform']['image'] if specification_image == image: print(f'updating file {path}') environment = json_data['transform']['env'] environment['NEW_SETTING'] = 'value' json.dump(json_data, open(str(path), 'w'), indent=2) elif path.suffix == '.yaml': with open(str(path), 'r') as yaml_file: data, indent, block_seq_indent = load_yaml_guess_indent(yaml_file, preserve_quotes=True) specification_image = data['transform']['image'] if specification_image == image: print(f'updating file {path}') environment = data['transform']['env'] environment['NEW_SETTING'] = 'value' ruamel.yaml.round_trip_dump(data, open(str(path), 'w'), explicit_start=True)
def round_trip_single(self, inp, drop_comment=False): explicit_start = self.first_non_empty_line(inp) == '---' explicit_end = self.last_non_empty_line(inp) == '...' indent = self._args.indent if indent == 'auto': from ruamel.yaml.util import load_yaml_guess_indent _, indent, block_seq_indent = load_yaml_guess_indent(inp) if self._args.block_seq_indent: block_seq_indent = int(self._args.block_seq_indent) else: indent = int(indent) block_seq_indent = int(self._args.block_seq_indent) if block_seq_indent is None: block_seq_indent = 0 if False: loader = ruamel.yaml.SafeLoader if drop_comment else \ ruamel.yaml.RoundTripLoader code = ruamel.yaml.load(inp, loader) dumper = ruamel.yaml.SafeDumper if drop_comment else \ ruamel.yaml.RoundTripDumper else: loader = ruamel.yaml.RoundTripLoader code = ruamel.yaml.load(inp, loader) if drop_comment: drop_all_comment(code) dumper = ruamel.yaml.RoundTripDumper res = ruamel.yaml.dump( code, Dumper=dumper, indent=indent, block_seq_indent=block_seq_indent, explicit_start=explicit_start, explicit_end=explicit_end, ) return res
def save_authorization(config_file, authorization): ruamel.yaml.YAML().allow_duplicate_keys = True from ruamel.yaml.util import load_yaml_guess_indent config, ind, bsi = load_yaml_guess_indent(open(config_file)) config['trakt']['authorization'] = { 'access_token': None, 'token_type': None, 'expires_in': None, 'refresh_token': None, 'scope': None, 'created_at': None } config['trakt']['authorization']['access_token'] = authorization[ 'access_token'] config['trakt']['authorization']['token_type'] = authorization[ 'token_type'] config['trakt']['authorization']['expires_in'] = authorization[ 'expires_in'] config['trakt']['authorization']['refresh_token'] = authorization[ 'refresh_token'] config['trakt']['authorization']['scope'] = authorization['scope'] config['trakt']['authorization']['created_at'] = authorization[ 'created_at'] print('| Saving authorization information to {}'.format(config_file)) ruamel.yaml.round_trip_dump(config, open(config_file, 'w'), indent=ind, block_seq_indent=bsi)
def update_manifest(self): print "Updating manifest" config, ind, bsi = load_yaml_guess_indent(open(self.MANIFEST_FILE)) dependencies = config['dependencies'] for dep in dependencies: if dep['name'] in [ "leptonica", "tesseract", "opencv", "gs9", "python-gs", "tessdata" ]: try: if (dep['name'] == "leptonica"): dep['uri'] = URI + self.leptonica_file_name dep['md5'] = self.leptonica_md5 elif (dep['name'] == "opencv"): dep['uri'] = URI + self.opencv_file_name dep['md5'] = self.opencv_md5 elif (dep['name'] == "gs9"): dep['uri'] = URI + self.ghostscript_file_name dep['md5'] = self.ghostscript_md5 elif (dep['name'] == "tesseract"): dep['uri'] = URI + self.tesseract_file_name dep['md5'] = self.tesseract_md5 elif (dep['name'] == "tessdata"): dep['uri'] = URI + self.tessdata_file_name dep['md5'] = self.tessdata_md5 elif (dep['name'] == "python-gs"): dep['uri'] = URI + self.gs_file_name dep['md5'] = self.gs_md5 except: pass ruamel.yaml.round_trip_dump(config, open(self.MANIFEST_FILE, 'w'), indent=ind, block_seq_indent=bsi)
def save_access_token(self, config_file): from ruamel.yaml.util import load_yaml_guess_indent config, ind, bsi = load_yaml_guess_indent(open(config_file)) config['trakt']['access_token'] = self.access_token ruamel.yaml.round_trip_dump(config, open(config_file, 'w'), indent=ind, block_seq_indent=bsi)
def get_conf(self): ''' load yaml configure ''' try: with open(self.conf_file, 'r') as stream: self.data, self.ind, self.bsi = load_yaml_guess_indent(stream) except: raise
def loadyml(path): directory = "travis_ymls" yaml_path = os.path.join(os.path.dirname(__file__), directory, path) with open(yaml_path, 'r') as f: try: doc, ind, bsi = load_yaml_guess_indent(f, preserve_quotes=True) return doc except: print("YAML Parsing Error...", yaml_path)
def get_configured_domain_name(): """Return the currently configured domain name.""" if not is_setup(): return None with open(SERVER_NAME_PATH) as config_file: config, _, _ = load_yaml_guess_indent(config_file) return config['server_name']
def execute_yaml(**kwargs): code = kwargs['code'] kv = kwargs['kv'] import os # call replaceable rcode = replaceable(code, kv) file_path = find_file_path(kwargs['lookahead']) if file_path == None: msg = "[DEBUG] I don't know how to edit!" print msg return msg #################################### # Warning: We install ruamel.yaml #################################### if exist_ruamel == False: logging.error(err_msg) distro = jeju.do.detect_dist() os_ = distro['distname'].split(' ')[0].lower() if os_ == "ubuntu" or os_ == "debian": cmd = "apt-get update;apt-get install -y gcc python-dev;pip install ruamel.yaml" elif os_ == "centos" or os_ == "redhat": cmd = "yum install -y python-devel gcc;pip install ruamel.yaml" else: return {'input':rcode, 'output':'[ERROR] Install ruamel.yaml'} import os os.system(cmd) # redundant import ruamel.yaml from ruamel.yaml.util import load_yaml_guess_indent config, ind, bsi = load_yaml_guess_indent(open(file_path)) config2, ind2, bsi2 = load_yaml_guess_indent(rcode) # Overwrite config2 to config config.update(config2) ruamel.yaml.round_trip_dump(config, open(file_path, 'w'), indent=ind, block_seq_indent=bsi) return {'input': rcode, 'output':open(file_path,'r').read()}
def update_metadata(timestamp, branch, revision): properties, indent, block_seq_indent = load_yaml_guess_indent( open(module_metadata)) properties['commit']['timestamp'] = timestamp properties['commit']['branch'] = branch properties['commit']['revision'] = revision yaml.round_trip_dump(properties, open(module_metadata, 'w'), indent=indent, block_seq_indent=block_seq_indent) sp.run(['git', 'add', module_metadata])
def _modify_config(cfg_file, param, param_new_value=False): # open file config, ind, bsi = ymlutil.load_yaml_guess_indent(open(cfg_file)) if param is 'reset_progress': config['admin']['reset_progress'] = param_new_value elif param is 'update': config['optional']['update'] = param_new_value with io.open(cfg_file, 'w') as conffile: yaml.dump(config, conffile)
def load_yaml(path, mode='r'): """ Extract contents and indent details of a YAML file. Returns a tuple (data, indentation, block_seq_indent) that can be conviently used when writing: YAML().block_seq_indent YAML().indent """ with open(path, mode) as fhdle: (data, ind, bsi) = load_yaml_guess_indent(fhdle) return (data, ind, bsi)
def get_turn_configuration() -> (List[str], str, bool): """Return TurnConfiguration if setup else empty.""" for file_path, managed in ((OVERRIDDEN_TURN_CONF_PATH, False), (TURN_CONF_PATH, True)): if is_non_empty_file(file_path): with open(file_path) as config_file: config, _, _ = load_yaml_guess_indent(config_file) return (TurnConfiguration(None, config['turn_uris'], config['turn_shared_secret']), managed) return (TurnConfiguration(), True)
def create_deploy_yaml_file( yaml_file_path: str, env: bool, secrets_file_path: str, service_name: str, cloud_sql_instances: str, ) -> str: """ Merge env vars to app yaml. """ from ruamel.yaml import YAML, util yaml = YAML() yaml_file_name = yaml_file_path.split("/")[-1] file_dir = "/".join(yaml_file_path.split("/")[:-1]) # Open and read base app-engine.yaml file config = util.load_yaml_guess_indent(open(yaml_file_path))[0] if service_name: print(f"Using service_name: {service_name}") config["service"] = service_name else: print(f"Using service_name: {config['service']}") # Open and read secret env variables if secrets_file_path is None: secrets_file_path = get_default_secrets_path(env) print(f"Using secrets_file_path: {secrets_file_path}") env_vars = dotenv_values(secrets_file_path) # update yaml in memory before writing new file with changes config["env_variables"] = ({} if "env_variables" not in config else config["env_variables"]) for key, val in env_vars.items(): config["env_variables"][key] = val # If cloud_sql_instances provided, set beta_settings to it. if cloud_sql_instances: config["beta_settings"]["cloud_sql_instances"] = cloud_sql_instances deploy_file_path = f"{file_dir}/deploy-{yaml_file_name}" # write to new file the updated yaml file with open(deploy_file_path, "w") as out_file: yaml.dump(config, out_file) return deploy_file_path
def yaml_processor(entity): """ Unserialize raw POST data in YAML format to a Python data structure. :param entity: raw POST data """ body = entity.fp.read() try: _datavars, ind, bsi = load_yaml_guess_indent(body) cherrypy.serving.request.unserialized_data = _datavars except ValueError: raise cherrypy.HTTPError(400, 'Invalid YAML document') cherrypy.serving.request.raw_body = body
def round_trip_save(self, file_name): from ruamel.yaml.util import load_yaml_guess_indent with open(file_name) as inp: _, indent, block_seq_indent = load_yaml_guess_indent( inp, preserve_quotes=not self._args.smart_string) if self._args.block_seq_indent: block_seq_indent = int(self._args.block_seq_indent) if self._args.indent: indent = int(self._args.indent) backup_file_name = file_name + '.orig' if not os.path.exists(backup_file_name): os.rename(file_name, backup_file_name) ruamel.yaml.round_trip_dump(_, sys.stdout, indent=indent, block_seq_indent=block_seq_indent)
def readConfig(self, path): yamlContent, ind, bsi = load_yaml_guess_indent( open(path.decode('utf-8'))) self.git_project_name = yamlContent['git_project_name'] self.project_name = yamlContent['project_name'] self.git_branch = yamlContent['git_branch'] self.test_options = yamlContent['test_options'] self.review_members = yamlContent['review_members'] self.project_pm = yamlContent['project_pm'] self.project_developers = yamlContent['project_developers'] self.poject_comment = yamlContent['poject_comment'] self.project_ui = yamlContent['project_ui'] self.project_pr_diff = yamlContent['project_pr_diff'] self.yamlContent = yamlContent self.ind = ind self.bsi = bsi
def get_yaml_json(filename, dir_path=DATA_PATH, filter=None): ''' 获取yaml文件数据 :param filename: 文件名称 :param dir_path: 文件路径 :param filter: 一级关键字 :return: ''' file_url = os.path.join(dir_path, filename) try: with open(file_url, encoding='UTF-8') as fp: data, ind, bsi = load_yaml_guess_indent(fp) if filter in data and filter != None: return data[filter] return data except Exception as error: raise error
def _update_template_file(self, flavor, datanodes, kafkas): resources_dir = '_resources_{}-{}'.format(flavor, self._cluster) dest_dir = '{}/{}'.format(os.getcwd() + '/cli', resources_dir) if os.path.isdir(dest_dir): pnda_env_yaml = dest_dir + "/pnda_env.yaml" config, ind, bsi = load_yaml_guess_indent(open(pnda_env_yaml)) parameters = config['parameters'] if datanodes > parameters['dataNodes']: parameters['dataNodes'] = datanodes if kafkas > parameters['kafkaNodes']: parameters['kafkaNodes'] = kafkas ruamel.yaml.round_trip_dump(config, open(pnda_env_yaml, 'w'), indent=ind, block_seq_indent=bsi) else: CONSOLE.error('Stack %s does not exist', self._cluster) sys.exit(1)
def install_extras(mkdocs_yml, theme=None): with open(mkdocs_yml) as f: config, indent, bsi = load_yaml_guess_indent(f) if theme is None and 'theme' not in config: raise ValueError('no theme specified in mkdocs.yml; pass ' + '--theme instead') theme_dir = get_theme_dir(config.get('theme', theme)) docs_dir = config.get('docs_dir', 'docs') for path, prop in [('css', 'extra_css'), ('js', 'extra_javascript')]: files = os.listdir(os.path.join(theme_dir, path)) if not files: continue extras = config.setdefault(prop, []) for f in files: relpath = os.path.join(path, f) shutil.copyfile(os.path.join(theme_dir, relpath), os.path.join(docs_dir, relpath)) if relpath not in extras: extras.append(relpath) yaml.round_trip_dump(config, open(mkdocs_yml, 'w'), indent=indent, block_seq_indent=bsi)
def update(source_path: Path, old_image: str, new_image: str): """ Update to the new image all pipeline specifications containing the old image. :param source_path: Path containing specification files. :param old_image: The image to replace. :param new_image: The replacement image. """ for path in source_path.rglob('*'): if path.is_file(): if path.suffix == '.json': with open(str(path)) as json_file: json_data = json.load(json_file) try: image = json_data['transform']['image'] except KeyError: # file is not a pipeline specification. continue if image == old_image: print(f'updating file {path}') json_data['transform']['image'] = new_image json.dump(json_data, open(str(path), 'w'), indent=2) elif path.suffix == '.yaml': with open(str(path), 'r') as open_file: data, indent, block_seq_indent = load_yaml_guess_indent( open_file, preserve_quotes=True) try: image = data['transform']['image'] except KeyError: # file is not a pipeline specification. continue if image == old_image: print(f'updating file {path}') data['transform']['image'] = new_image ruamel.yaml.round_trip_dump(data, open(str(path), 'w'), explicit_start=True)
def pre_install_pnda(self, node_counts): ''' Use the Openstack heatclient API to launch a stack that PNDA can be installed on The stack is defined in template files in the flavor specific heat-template directory ''' # Generate template files self._generate_template_file(self._flavor, node_counts['datanodes'], node_counts['opentsdb_nodes'], node_counts['kafka_nodes'], node_counts['zk_nodes']) stack_name = self._cluster heat_session = self._get_heat_session() templates_path = os.getcwd() + '/cli/' + '_resources_{}-{}'.format( self._flavor, self._cluster) template_file = templates_path + "/pnda.yaml" env_file = templates_path + "/pnda_env.yaml" config, ind, bsi = load_yaml_guess_indent(open(env_file)) parameters = config['parameters'] parameters['keyName'] = node_counts['keyname'] # remove extra parmeters for heat template exclude_section = [ 'INFRASTRUCTURE_TYPE', 'SSH_KEY', 'OS_USER', 'networkCidr', 'KEYSTONE_AUTH_URL', 'KEYSTONE_USER', 'KEYSTONE_PASSWORD', 'KEYSTONE_TENANT', 'KEYSTONE_AUTH_URL', 'KEYSTONE_AUTH_VERSION', 'KEYSTONE_REGION_NAME' ] for param in exclude_section: if param in parameters: del parameters[param] ruamel.yaml.round_trip_dump(config, open(env_file, 'w'), indent=ind, block_seq_indent=bsi) env_param = [env_file] tpl_files, tpl_template = template_utils.process_template_path( template_file) env_files, env_template = template_utils.process_multiple_environments_and_files( env_paths=env_param) files_all = dict(list(tpl_files.items()) + list(env_files.items())) try: status = heat_session.stacks.create(stack_name=stack_name, template=tpl_template, files=files_all, environment=env_template, timeout_mins=120) stack_id = status['stack']['id'] stack_status = 'CREATING' stack_status_new = None except exc.HTTPConflict as exp: error_state = exp.error CONSOLE.error("Stack already exist : %s %s", error_state, stack_name) sys.exit(1) except exc.HTTPBadRequest as exp: error_state = exp.error CONSOLE.error("Bad request stack creation failed: %s", error_state) sys.exit(1) while stack_status in ['CREATE_IN_PROGRESS', 'CREATING']: time.sleep(5) if stack_status != stack_status_new: if stack_status_new is not None: stack_status = stack_status_new CONSOLE.info('Stack is: %s', stack_status) else: CONSOLE.debug('Stack is: %s', stack_status) stack_status_body = heat_session.stacks.get(stack_id) stack_status_new = stack_status_body.stack_status if stack_status != 'CREATE_COMPLETE': CONSOLE.error('Stack did not come up, status is: %s', stack_status) sys.exit(1) self.fill_instance_map()
import ruamel.yaml from ruamel.yaml.util import load_yaml_guess_indent import subprocess CONFIG_DIR = '/home/rodrigo/Documents/catkin_ws/src/grasping/config/' LOCAL_DIR = './config/' CLASSIFIER_DIR = LOCAL_DIR + 'classifiers/' CONFIG_FILE = CONFIG_DIR + 'config.yaml' angles = [4, 5, 6] classifiers = ['DCH_72_split4_beer_drill_svm_auto.yaml', 'DCH_72_split5_beer_drill_svm_auto.yaml', 'DCH_72_split45_beer_drill_svm_auto.yaml'] for c in classifiers: for a in angles: config, ind, bsi = load_yaml_guess_indent(open(CONFIG_FILE)) config['grasper']['angleSplits'] = a config['grasper']['predictions']['classifier'] = CLASSIFIER_DIR + c ruamel.yaml.round_trip_dump(config, open(CONFIG_FILE, 'w'), indent=ind, block_seq_indent=bsi) CMD = 'python src/run.py ~/Documents/catkin_ws/ world_set_1 true' process = subprocess.Popen(CMD, shell=True, stdout=subprocess.PIPE) process.wait() time.sleep(10) print 'FINISHED'
# https://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-of-eth0-in-python/30990617 def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa( fcntl.ioctl( s.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]))[20:24]) file_name = '/katacoda/config/master/master-config.yaml' from ruamel.yaml.util import load_yaml_guess_indent config, ind, bsi = load_yaml_guess_indent(open(file_name)) katacodaid = os.environ['KATACODA_HOST_SUBDOMAIN'] katacodahost = os.environ['KATACODA_HOST'] externalip = os.environ['EXTERNALIP'].replace(".", "\.") masterpublicurl = "https://" + katacodaid + "-8443-" + katacodahost + ".environments.katacoda.com" publicurl = masterpublicurl + "/console/" internalip = get_ip_address('eth0').replace(".", "\.") config['assetConfig']['masterPublicURL'] = masterpublicurl config['assetConfig']['publicURL'] = publicurl config['corsAllowedOrigins'].append("//" + katacodaid + "-8443-" + katacodahost + "\.environments\.katacoda\.com:443$") config['corsAllowedOrigins'].append("//" + katacodaid + "-8443-" +
from urlparse import urlparse from plexapi.server import PlexServer import json import os import time import trakt import logging import ruamel.yaml import sys config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.yml') from ruamel.yaml.util import load_yaml_guess_indent config, ind, bsi = load_yaml_guess_indent(open(config_file)) recipe_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'recipes', sys.argv[1] + '.yml') with open(recipe_file) as r: recipe = ruamel.yaml.safe_load(r) log_format = '%(asctime)s - %(levelname)-8s - %(module)-16s - %(message)s' logging.basicConfig(format=log_format) log = logging.getLogger(__name__) log.setLevel(logging.DEBUG if config['debug'] else logging.INFO) t = trakt.Trakt(config) trakt_username = config['trakt']['username'] plex = PlexServer(config['plex']['baseurl'], config['plex']['token'],
def create(): if sys.getdefaultencoding() != 'utf-8': reload(sys) sys.setdefaultencoding('utf-8') path = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(path,'config.yaml') yamlContent,ind,bsi = load_yaml_guess_indent(open(path)) print ('\n') print ('\n') # 'LenzBusiness' 'LenzMember' print('-----------------------------------') print('工程列表:') count = 0 for p in yamlContent['project_list']: count += 1 print(str(count)+'.'+p['prefix']) print('-----------------------------------') repo_index = int(raw_input('请输入工程名称索引:')) print('-----------------------------------') repo_name = yamlContent['project_list'][repo_index - 1]['repo_name'] prefix = yamlContent['project_list'][repo_index - 1]['prefix'] pm_name = '' task_name = '' print ('\n') print('-----------------------------------') print('生成 feature/时间_任务名称') print('例子 feature/20190516_时间打点') print('-----------------------------------') print('-----------------------------------') print('pm列表:') count = 0 for p in yamlContent['pm_list']: count += 1 print(str(count)+'.'+p) pm_index = int(raw_input('请输入PM名字索引:')) pm_name = yamlContent['pm_list'][pm_index-1] print('-----------------------------------') print ('\n') print('-----------------------------------') while task_name == '': task_name = raw_input('请输入任务名称(不要带空格):') print('-----------------------------------') taskName = getGitBranchNameFromTaskName(task_name) date_string = time.strftime("%Y%m%d",time.localtime()) just_test_branch = date_string + '_' + taskName #用作文件名 test_branch = 'feature/' + date_string + '_' + taskName print ('\n') print ('\n') in_text = '' test_options = '' print('-----------------------------------') print('项目测试项:---------一行一个---------') print('相机优化 ') print('主任务列表优化 ') print('最后输入 q 回车 结束输入') print('-----------------------------------') print('请输入项目测试项:') count = 0 while in_text != 'q': count += 1 in_text = raw_input() if in_text != 'q': test_options += str(count) + '.' + in_text test_options += '\n' print('-----------------------------------') print ('\n') #git 打新分支 默认 feature/xxx repo = Repo('~/' + repo_name) master = repo.heads.master currentBranch = repo.head.reference if currentBranch != master: master.checkout() git = repo.git git.checkout('master',b=test_branch) print('切分支成功:') print(test_branch) #yaml文件更新 config = TemplateConfig() config.readConfigFromTemplate() config.git_branch = test_branch config.git_project_name = repo_name config.test_options = test_options config.project_pm = pm_name config.project_name = prefix + ' ' + task_name yaml_name = just_test_branch+'_config.yaml' path = os.path.dirname(os.path.realpath(__file__)) yamlPath = os.path.join(path,'configs/' + yaml_name) if not os.path.isfile(yamlPath): os.system("touch " + yamlPath) path = os.path.dirname(os.path.realpath(__file__)) with io.open(path+'/configs/configs','a',encoding='utf-8') as f: f.write(yaml_name) f.write(u'\n') config.save(yamlPath) print('存储到本地配置成功:') print(test_options)
print(err) except yaml.composer.ComposerError as err: print("Composer Error...", yaml_path) print(err) except yaml.parser.ParserError: print("Malformed YAML...", yaml_path) except yaml.reader.ReaderError: print("Reader Error...", yaml_path) except NotImplementedError: print("Not implemented command...", yaml_path) if args.fix and Smell.unrelated_commands.value in results: modified_yaml = None with open(yaml_path, 'r') as f: try: doc, ind, bsi = load_yaml_guess_indent(f, preserve_quotes=True) if doc: modified_yaml = transform_phases(doc) if modified_yaml: print("Anti-pattern 4 removed.") except yaml.scanner.ScannerError as err: print("Scanner Error...", yaml_path) print(err) except yaml.composer.ComposerError as err: print("Composer Error...", yaml_path) print(err) except yaml.parser.ParserError: print("Malformed YAML...", yaml_path) except yaml.reader.ReaderError: print("Reader Error...", yaml_path)
def populate( template, output, credentials_key, vstring, polarion_project_id, polarion_url, template_format, ): with open(template, 'r') as template_file: input_yaml, indent, block_indent = load_yaml_guess_indent(template_file) # first update credentials fields input_yaml['username'] = credentials[credentials_key]['username'] input_yaml['password'] = credentials[credentials_key]['password'] version = Version(vstring) replacement = None # avoid UnboundLocal below # First handle xunit import nested values if XUNIT_HEADER not in input_yaml: logger.info('Skipping [%s] in polarion_tools.local.yaml template, missing', XUNIT_HEADER) else: for KEY in [TEMPLATE_ID, GROUP_ID, TESTRUN_TITLE, TESTRUN_ID]: # replacement is different for each field if KEY == TEMPLATE_ID: # There's no error if the template_format doesn't have {} replacement = template_format.format(version.series().replace('.', '')) elif KEY in [TESTRUN_TITLE, TESTRUN_ID]: replacement = vstring.replace('.', '_') # stupid polarion not allowing . elif KEY == GROUP_ID: # z-stream for group ID replacement = version.series(n=3) # now apply the replacement if input_yaml[XUNIT_HEADER].get(KEY, None) not in ['', None]: # Only set empty values, if the template has a value don't change it logger.info('SKIP [%s][%s] in polarion_tools.local.yaml template, already set', XUNIT_HEADER, KEY) else: input_yaml[XUNIT_HEADER][KEY] = replacement logger.info('Setting key [%s] in polarion_tools.local.yaml template, to %s', KEY, replacement) # top level keys not in xunit for KEY in [POLARION_URL, POLARION_PROJECT_ID]: if KEY == POLARION_PROJECT_ID: replacement = polarion_project_id elif KEY == POLARION_URL: replacement = polarion_url # check replacement and current value if replacement is None: logger.info('SKIP [%s] in polarion_tools.local.yaml template, no value passed', KEY) continue elif input_yaml.get(KEY, None) is not None: logger.info('SKIP [%s] in polarion_tools.local.yaml template, value already set', KEY) continue else: logger.info('Setting key [%s] in polarion_tools.local.yaml template', KEY) input_yaml[KEY] = replacement with open(output, 'w') as output_file: ruamel.yaml.round_trip_dump(input_yaml, output_file, indent=indent, block_seq_indent=block_indent) return 0
def check_for_attribute(config, attribute, parent=None, test_list=None, options="", default=None, do_print=True, default_is_none=False, var_type="str", throw=False, save=True): message = "" endline = "" text = "{} attribute".format( attribute) if parent is None else "{} sub-attribute {}".format( parent, attribute) if config is None or attribute not in config: message = "| Config Error: {} not found".format(text) if parent and save == True: ruamel.yaml.YAML().allow_duplicate_keys = True from ruamel.yaml.util import load_yaml_guess_indent new_config, ind, bsi = load_yaml_guess_indent( open(Config.config_path)) endline = "\n| {} sub-attribute {} added to config".format( parent, attribute) if parent not in new_config: new_config = {parent: {attribute: default}} elif not new_config[parent]: new_config[parent] = {attribute: default} elif attribute not in new_config[parent]: new_config[parent][attribute] = default else: endLine = "" ruamel.yaml.round_trip_dump(new_config, open(Config.config_path, 'w'), indent=ind, block_seq_indent=bsi) elif not config[attribute] and config[attribute] != False: message = "| Config Error: {} is blank".format(text) elif var_type == "bool": if isinstance(config[attribute], bool): return config[attribute] else: message = "| Config Error: {} must be either true or false".format( text) elif var_type == "int": if isinstance(config[attribute], int) and config[attribute] > 0: return config[attribute] else: message = "| Config Error: {} must an integer > 0".format(text) elif test_list is None or config[attribute] in test_list: return config[attribute] else: message = "| Config Error: {}: {} is an invalid input".format( text, config[attribute]) if default is not None or default_is_none: message = message + " using {} as default".format(default) message = message + endline if (default is None and not default_is_none) or throw: if len(options) > 0: message = message + "\n" + options sys.exit(message) if do_print: print(message) if attribute in config and config[ attribute] and test_list is not None and config[ attribute] not in test_list: print(options) return default
def guess(s): from ruamel.yaml.util import load_yaml_guess_indent x, y, z = load_yaml_guess_indent(dedent(s)) return y, z
def read(self): path = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(path, 'config.yaml') yamlContent, ind, bsi = load_yaml_guess_indent(open(path)) self.config = yamlContent
def populate( template, output, credentials_key, vstring, polarion_project_id, polarion_url, template_format, ): with open(template, 'r') as template_file: input_yaml, indent, block_indent = load_yaml_guess_indent( template_file) # first update credentials fields input_yaml['username'] = credentials[credentials_key]['username'] input_yaml['password'] = credentials[credentials_key]['password'] version = Version(vstring) replacement = None # avoid UnboundLocal below # First handle xunit import nested values if XUNIT_HEADER not in input_yaml: logger.info( 'Skipping [%s] in polarion_tools.local.yaml template, missing', XUNIT_HEADER) else: for KEY in [TEMPLATE_ID, GROUP_ID, TESTRUN_TITLE, TESTRUN_ID]: # replacement is different for each field if KEY == TEMPLATE_ID: # There's no error if the template_format doesn't have {} replacement = template_format.format(version.series().replace( '.', '')) elif KEY in [TESTRUN_TITLE, TESTRUN_ID]: replacement = vstring.replace( '.', '_') # stupid polarion not allowing . elif KEY == GROUP_ID: # z-stream for group ID replacement = version.series(n=3) # now apply the replacement if input_yaml[XUNIT_HEADER].get(KEY, None) not in ['', None]: # Only set empty values, if the template has a value don't change it logger.info( 'SKIP [%s][%s] in polarion_tools.local.yaml template, already set', XUNIT_HEADER, KEY) else: input_yaml[XUNIT_HEADER][KEY] = replacement logger.info( 'Setting key [%s] in polarion_tools.local.yaml template, to %s', KEY, replacement) # top level keys not in xunit for KEY in [POLARION_URL, POLARION_PROJECT_ID]: if KEY == POLARION_PROJECT_ID: replacement = polarion_project_id elif KEY == POLARION_URL: replacement = polarion_url # check replacement and current value if replacement is None: logger.info( 'SKIP [%s] in polarion_tools.local.yaml template, no value passed', KEY) continue elif input_yaml.get(KEY, None) is not None: logger.info( 'SKIP [%s] in polarion_tools.local.yaml template, value already set', KEY) continue else: logger.info( 'Setting key [%s] in polarion_tools.local.yaml template', KEY) input_yaml[KEY] = replacement with open(output, 'w') as output_file: ruamel.yaml.round_trip_dump(input_yaml, output_file, indent=indent, block_seq_indent=block_indent) return 0