def get_config(conf_file=None, logger=set_default_logger()): """ :param conf_file: configparser compliant configuration file :param logger: Log handler, typically from calling module :return: configparser.ConfigParser """ os_type = platform.system() if os_type == "Windows": if conf_file is None: conf_file = pathlib.PureWindowsPath('default.conf') else: conf_file = pathlib.PureWindowsPath(conf_file) else: if conf_file is None: conf_file = pathlib.PurePosixPath('default.conf') else: conf_file = pathlib.PurePosixPath(conf_file) config = configparser.ConfigParser(comment_prefixes='/', allow_no_value=True) try: config.read_file(open(conf_file.as_posix())) except Exception as e: logger.warning("Failed to parse configuration file %s" % conf_file.name) logger.error("Except raised: %s" % e) finally: return config
def _handle_present(self, result, link_stat): target_path = self.get_taskparam('target_path') if not target_path: raise AnsibleOptionsError( "When mode is 'present', 'target_path' parameter must be set") link_type = self.get_taskparam('link_type') ## check if target path exists and what type it is (file, dir, ...) trgt_stat = self._query_win_filepath( target_path, 'target_path', relpath_root=pathlib.PureWindowsPath(link_stat['path']).parent) trgt_stat = trgt_stat['stat'] if pathlib.PureWindowsPath( trgt_stat['path']) == pathlib.PureWindowsPath( link_stat['path']): raise AnsibleOptionsError( "Given link and target path are identical,"\ " this does not make sense" ) if not trgt_stat['exists']: raise AnsibleOptionsError( "Invalid target path param: path must exists on target"\ " system, but given path '{}' does not".format(target_path) ) getattr(self, '_handle_present_' + link_type)(result, link_stat, trgt_stat)
def test_serialize_pathlib(self): # Pure path objects work in all platforms. self.assertSerializedEqual(pathlib.PurePosixPath()) self.assertSerializedEqual(pathlib.PureWindowsPath()) path = pathlib.PurePosixPath("/path/file.txt") expected = ("pathlib.PurePosixPath('/path/file.txt')", {"import pathlib"}) self.assertSerializedResultEqual(path, expected) path = pathlib.PureWindowsPath("A:\\File.txt") expected = ("pathlib.PureWindowsPath('A:/File.txt')", {"import pathlib"}) self.assertSerializedResultEqual(path, expected) # Concrete path objects work on supported platforms. if sys.platform == "win32": self.assertSerializedEqual(pathlib.WindowsPath.cwd()) path = pathlib.WindowsPath("A:\\File.txt") expected = ("pathlib.PureWindowsPath('A:/File.txt')", {"import pathlib"}) self.assertSerializedResultEqual(path, expected) else: self.assertSerializedEqual(pathlib.PosixPath.cwd()) path = pathlib.PosixPath("/path/file.txt") expected = ("pathlib.PurePosixPath('/path/file.txt')", {"import pathlib"}) self.assertSerializedResultEqual(path, expected) field = models.FilePathField(path=pathlib.PurePosixPath("/home/user")) string, imports = MigrationWriter.serialize(field) self.assertEqual( string, "models.FilePathField(path=pathlib.PurePosixPath('/home/user'))", ) self.assertIn("import pathlib", imports)
def test_wsl2_full_path2windows_path(): assert wsl2_full_path2windows_path(p.Path("/mnt") / "c" / "home" / "ykanya") \ == p.PureWindowsPath(r"C:\\") / "home" / "ykanya" assert wsl2_full_path2windows_path(p.Path("/mnt") / "z" / "lib") \ == p.PureWindowsPath(r"z:\\") / "lib" assert wsl2_full_path2windows_path((p.Path("/mnt") / "c")) \ == p.PureWindowsPath(r"C:\\") with pytest.raises(UsageError): wsl2_full_path2windows_path((p.Path("/mt") / "c"))
def _regexp_paths_csv_validator(_, name: str, value: str) -> List[Pattern[str]]: patterns = [] for val in _csv_validator(_, name, value): patterns.append( re.compile( str(pathlib.PureWindowsPath(val)).replace("\\", "\\\\") + "|" + pathlib.PureWindowsPath(val).as_posix())) return patterns
def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]: """Transforms a comma separated list of regular expressions paths.""" patterns: List[Pattern[str]] = [] for pattern in _csv_transformer(value): patterns.append( re.compile( str(pathlib.PureWindowsPath(pattern)).replace("\\", "\\\\") + "|" + pathlib.PureWindowsPath(pattern).as_posix())) return patterns
def _handle_specifics_presub(self, cfg, my_subcfg, cfgpath_abs): real_path = self._get_mandatory_subkey( my_subcfg, 'real_path', cfgpath_abs, validate_fn=key_validator_trueish) real_path = pathlib.PureWindowsPath(real_path) # check that real_path does not exist on target or exists and is dir stat = self.pluginref.exec_module('ansible.windows.win_stat', modargs={'path': str(real_path)}) if stat['stat']['exists'] and not stat['stat']['isdir']: raise AnsibleOptionsError( "{}: bad 'real_path' parameter value, path already exists"\ " and is not a directory: {}".format( '.'.join(cfgpath_abs), real_path ) ) ## ## note: on default create a symlink on c drive when remote fs is ## situated on another drive, because non-c-drive jenkins homes ## can get easily lead to a lot of problems, like for example ## like win-docker, which only seems to properly handle volumes ## on drive c: ## if not my_subcfg['no_autolinking']: link_path = my_subcfg.get('link_path', None) real_path_on_c = real_path.drive.lower() == 'c:' ## if user explicitly set a link path, use that, if not default it if not link_path: if real_path_on_c: # real path is on c:, so we dont do linking on default link_path = None else: # real path is not on 'c:', we need a symlink on 'c:', # default to same path as real_path link_path = pathlib.PureWindowsPath('c:\\') \ / real_path.relative_to(real_path.anchor) link_path = str(link_path) my_subcfg['link_path'] = link_path link_path_on_c = \ pathlib.PureWindowsPath(link_path).drive.lower() == 'c:' my_subcfg['_none_on_c'] = not real_path_on_c and not link_path_on_c return my_subcfg
def test_is_excluded(subtests): default_excludes = re.compile(main.DEFAULT_EXCLUDES) cases = ( ('no exclude', pathlib.PurePosixPath('.git'), [], False), ('.git exclude', pathlib.PurePosixPath('.git'), [default_excludes], True), ('subdir exclude', pathlib.PurePosixPath('sub/dir/.git'), [default_excludes], True), ('windows include', pathlib.PureWindowsPath(r'windows\foo'), [default_excludes], False), ('windows exclude', pathlib.PureWindowsPath(r'windows\.git'), [default_excludes], True), ) for name, path, excludes, expected in cases: with subtests.test(name=name): assert processing._is_excluded(path, excludes) is expected
def _query_win_filepath(self, fp, param_name, relpath_root=None): fp = pathlib.PureWindowsPath(fp) if not fp.is_absolute(): if not relpath_root: raise AnsibleOptionsError( "Invalid param '{}': paths must be absolut, but given"\ " value is not: {}".format(param_name, fp) ) fp = pathlib.PureWindowsPath(str(relpath_root)) / fp return self.exec_module('ansible.windows.win_stat', modargs={'path': str(fp)})
def mkdir(path_from_argu): the_path = pathlib.PureWindowsPath(path_from_argu) try: pathlib.Path.mkdir(pathlib.Path(the_path)) except: print(path_from_argu + " project directory is exists") exit()
def test_get_credentials_path_win(self, windll): importlib.reload(platform) def get_folder_path(_a, _b, _c, _d, path_buf): path_buf.value = r"c:\Users\edgedb\AppData\Local" windll.shell32 = mock.Mock() windll.shell32.SHGetFolderPathW = get_folder_path with mock.patch("pathlib.PureWindowsPath.exists", lambda x: True, create=True): self.assertEqual( str(credentials.get_credentials_path("test")), r"c:\Users\edgedb\AppData\Local" r"\EdgeDB\config\credentials\test.json", ) with mock.patch( "pathlib.PureWindowsPath.exists", _MockExists(), create=True), mock.patch( 'pathlib.PureWindowsPath.home', lambda: pathlib.PureWindowsPath(r"c:\Users\edgedb"), create=True, ): self.assertEqual( str(credentials.get_credentials_path("test")), r"c:\Users\edgedb\.edgedb\credentials\test.json", )
def _record_archive_entry(self, archive, entry, sizes: Sizes) -> None: if self.is_entry_directory(entry): return entry_name = self.get_entry_name(entry) entry_path = pathlib.PureWindowsPath(entry_name) recorder_factory = classify_path(entry_path) if recorder_factory is None: return name = self._name + (entry_name, ) if recorder_factory is RegularFileRecorder: # Optimization: Don't extract the file just to get its size. sizes.record_size( name=name, size=self.get_entry_size(entry), type="file", ) else: with tempfile.NamedTemporaryFile( suffix=entry_path.suffix) as temp_file: with self.open_entry(archive=archive, entry=entry) as entry_file: shutil.copyfileobj(entry_file, temp_file) temp_file.flush() recorder = recorder_factory(name=name, path=pathlib.Path(temp_file.name)) recorder.record(sizes=sizes)
def to_posix_path(code_path): """ Change the code_path to be of unix-style if running on windows when supplied with an absolute windows path. Parameters ---------- code_path : str Directory in the host operating system that should be mounted within the container. Returns ------- str Posix equivalent of absolute windows style path. Examples -------- >>> to_posix_path('/Users/UserName/sam-app') /Users/UserName/sam-app >>> to_posix_path('C:\\\\Users\\\\UserName\\\\AppData\\\\Local\\\\Temp\\\\mydir') /c/Users/UserName/AppData/Local/Temp/mydir """ return re.sub( "^([A-Za-z])+:", lambda match: posixpath.sep + match.group().replace(":", "").lower(), pathlib.PureWindowsPath( code_path).as_posix()) if os.name == "nt" else code_path
def get_ethereum_income(file, start_value): total = 0 # Gather mining data from ethermine (my mining pool) # https://ethermine.org/api/miner with open(pathlib.PureWindowsPath(path + file), 'r') as f: total_NOK, total_ETH = 0, 0 USD_NOK_exchange = start_value #First day of the year for line in f.read().split('\n')[1:]: new_line = [ string.replace('"', '') for string in line.split(',')[1:] ] amount = int(new_line[2]) / 10**18 ## Calculate income date = unix_time_to_date(new_line[4]) if (USD_NOK_dict.get(date)): USD_NOK_exchange = USD_NOK_dict[date] result = ethereum_dict[date] * amount * USD_NOK_exchange total_NOK += result total_ETH += amount return total_NOK, total_ETH
def find_specific_cell(): for row in range(2, 21): for column in "B": # Here you can add or reduce the columns cell_name = "{}{}".format(column, row) #tcStatus = currentSheet[cell_name].value if currentSheet[cell_name].value != "Passed": print(" I am executing no run & failed test cases") #cell_namer = "{}{}".format('A', row) cell_namer = "{}{}".format('A', row) cellstatus = "{}{}".format("D", row) print("Test Case Executing", currentSheet[cell_namer].value) tctorun = currentSheet[cell_namer].value base_dir = pathlib.PureWindowsPath( r'D:\svn\DanpheEMR\SystemTest') filenametorun = base_dir / tctorun print(filenametorun) try: exec(open(filenametorun).read()) currentSheet[cell_name].value = "Passed" except Exception: currentSheet[cell_name].value = "Failed" currentSheet[ cellstatus].value = currentSheet[cellstatus].value + 1 pass theFile.save('SmokeSanityExecutionResultV1.48.8.xlsx') print("Updating status") print(currentSheet) print(cell_name) print(currentSheet[cell_name].value)
def update_output(value): #dir = pathlib.PureWindowsPath(r"C:/Users/tfischle/Github/DtkTrunk_master/Regression/Generic/71_Generic_RngPerCore_FromSerializedPop") dir = pathlib.PureWindowsPath(r"C:\Users\tfischle\Github\DtkTrunk_master\Regression\Generic\13_Generic_Individual_Properties") serialized_file = "state-00015.dtk" path = str(dir) + '/' + serialized_file change_serialized_population.setFile(path) return 'Loaded file:' + path
def main(): dirName = 'input/maps/'; # Get the list of all files in directory tree at given path listOfFiles = getListOfFiles(dirName) for i in range(len(listOfFiles)): b = pathlib.PureWindowsPath(listOfFiles[i]) listOfFiles[i] = b.as_posix() # Print the files for elem in listOfFiles: print(elem) print() print( "The", dirName.split("/")[-2], "folder contains", listOfFiles.__len__(), "files", sep = " ", end="\n\n" ) while True: None
def do_open(self, arg): """Open image by ID with default image viewer: \\open 213 | \\open url 213 Sends Windowsified path to socket on Windows to open image""" action = '' if not arg.isdigit(): arg = arg.split() if len(arg) > 2: print('Expected 2 parameters, got ' + str(len(arg))) return False action, arg = arg img = self.last_results.get(int(arg)) if not img: img = query_by_id([arg]) if not img: print('Image with ID {} does not exist.'.format(arg)) return False img = img.pop() if action.endswith('url'): path = img.original_link else: path = pathlib.PureWindowsPath(img.group.path, img.filename) if self._run_socket('start "" "{}"'.format(path)): print('Opening...', path)
def parseRow(self, r): row = super().parseRow(r) if "\\" in row["coords_path"]: row["coords_path"] = pathlib.PureWindowsPath( row["coords_path"]).as_posix() return row
def parseRow(self, r): row = {} for (col_name, _, caster), r in zip(self.COLS, r): if r is None: row[col_name] = None else: row[col_name] = caster(r) if "\\" in row["image_path"]: row["image_path"] = pathlib.PureWindowsPath( row["image_path"]).as_posix() return { "experiment_num": row["experiment_num"], "experiment_id": row["experiment_id"], "date": "{date_year}-{date_month:02d}-{date_day:02d}".format(**row), "medium": row["medium"], "strain": row["strain"], "image_path": row["image_path"], "image_mode": row["image_mode"], "num_channels": row["num_channels"], "num_slices": row["num_slices"], "num_frames": row["num_frames"], "file_mode": row["file_mode"], "outlined": bool(row["outlined"]), "verified": bool(row["verified"]), "analysed": bool(row["analysed"]), }
def read_trivial_batch(file): """Find DOSBox command in batch file and return its argument list. Returns a tuple: The first element is a directory used by batch file to invoke DOSBox. If batch contains no relevant information, it's 'None'. The second element is an argument list. """ line_num = 1 with open(file, 'r') as bat_file: lines = bat_file.readlines(512) assert lines, 'error processing .bat file (not enough lines)' new_path = None for line in lines: this_line = argsplit_windows(line) if not this_line: continue first_word = this_line[0] if first_word.lower() in ('echo', '@echo'): continue if first_word.lower() in ('cd', '@cd'): # This works only for a single 'CD', but no game uses more than # one (so far). If we'll ever find one, then it's time to # implement more serious batch interpreter instead. new_path = winpathlib.to_posix_path(this_line[1]) assert new_path, 'error processing .bat ' + \ 'file: no directory named ' + \ '{}'.format(this_line[1]) win_path = pathlib.PureWindowsPath(first_word) exe = win_path.parts[-1] if exe.lower() in ('dosbox', 'dosbox.exe'): return new_path, this_line[1:] assert False, 'error processing .bat file (line {})'.format(line_num) return None, []
def read_results_file(self, results_file_path): """ Parse the the JSON results file and return the parsed data. """ if not os.path.exists(results_file_path): results = [] else: with open(results_file_path, "r", encoding="utf8") as results_file: results = json.load(results_file) # The results file data is expected to be a list of metadata dictionaries if not isinstance(results, list) or not all(isinstance(a, dict) for a in results): raise ValueError("Results file is invalid: {}".format(results_file_path)) # Replace {MALAWARE_REPO} with full input file. for result in results: # Add results_file_path for relative paths. # NOTE: Using PureWindowsPath to help convert a Windows path using \ # into / path. # This helps in-case the test case was originally made with a Windows machine # but is being tested on Linux. input_file_path = pathlib.PureWindowsPath(result[INPUT_FILE_PATH]).as_posix() # expand environment variables input_file_path = os.path.expandvars(input_file_path) # resolve variables if self.malware_repo: input_file_path = input_file_path.format(MALWARE_REPO=self.malware_repo) input_file_path = os.path.join(os.path.dirname(results_file_path), input_file_path) input_file_path = os.path.abspath(input_file_path) result[INPUT_FILE_PATH] = input_file_path return results
def projects_from_solution(fn, *, exclude=None): ''' Find project filenames from a solution. Parameters ---------- fn : str, pathlib.Path Solution filename exclude : list or None Exclude certain extensions. Defaults to excluding .tcmproj ''' with open(fn, 'rt') as f: solution_text = f.read() if exclude is None: exclude = ('.tcmproj', ) projects = [ pathlib.PureWindowsPath(match[1]) for match in SLN_PROJECT_RE.findall(solution_text) ] solution_path = pathlib.Path(fn).parent return [(solution_path / pathlib.Path(project)).absolute() for project in projects if project.suffix not in exclude]
def get_results(self, dest_dir): self._log("READING RESULTS") common_files_dir = (pathlib.PureWindowsPath(self._data_dir).parent / "Common" / "Files") src = os.path.join(common_files_dir, str(self._results_name)) tmp = src + ".tmp" with open(src, encoding="utf-16", errors="ignore") as source: with open(tmp, encoding="iso-8859-1", mode="w") as temp: for line in source: try: temp.write(line) except (UnicodeEncodeError, UnicodeDecodeError): # skips spurious bytes pass # **Atomic rename** target_dir = default(dest_dir, self._working_dir) shutil.move(tmp, target_dir) os.rename(os.path.join(target_dir, str(self._results_name) + ".tmp"), os.path.join(target_dir, str(self._results_name))) os.remove(src)
def populate_USD_NOK_conversion(file): # Gather USD to NOK conversion from # https://data.norges-bank.no/api/data/EXR/B.USD.NOK.SP?StartPeriod=2017&EndPeriod=2018&format=csv-:-comma-true-flat with open(pathlib.PureWindowsPath(path + file), 'r') as f: for line in f.read().split('\n'): line = line.split(',') USD_NOK_dict[line[5]] = float(line[6])
def CapMode(): print("Welcome to Cap mode!") parsed.FAR = input("What's path to your far archive: ") mode = input( "Enter what you want to do with the archive [add/rename/replace/list files/extract" + "/extract all files]: ") if mode.lower() == "add": parsed.Replace = input( "Good! you want to add a file. Now tell me, what is the path of the file you " + "want to add: ") parsed.FilePath = input( "And where do you want to add this file in the archive: ") Compress = input( "Do you want the newly added file to be compressed [Y/N]: ") parsed.compress = True if Compress.lower() == "y" else False parsed.add = True elif mode.lower() == "replace": parsed.FilePath = input( "Good! you want to replace a file. Now tell me, what is the path in the " + "archive of the file you want to replace: ") parsed.Replace = input("And where is the new file: ") Compress = input("Do you want the new file to be compressed [Y/N]: ") parsed.compress = True if Compress.lower() == "y" else False parsed.replace = True elif mode.lower() == "rename": parsed.FilePath = str( pathlib.PureWindowsPath( input( "Good! you want to rename a file. Now tell me, what is the path in the " + "archive of the file you want to rename: "))) tempPath = parsed.FilePath.rsplit( '\\', 1)[1] if parsed.FilePath.find('\\') != -1 else parsed.FilePath parsed.Replace = input( f"And what is the new filename for {tempPath} (With the extension): " ) parsed.rename = True elif mode.lower() == "extract": parsed.FilePath = input( "Good! you want to extract a file. Now tell me, what is the path " + "in the archive of the file you want to extract: ") parsed.Replace = input("And where do you want to extract this file: ") parsed.Xtract = True elif mode.lower() == "extract all files": FARName = os.path.basename(parsed.FAR) FARName = FARName.rsplit('.', 1)[0] parsed.Replace = input( "Good! you want to extract all files. Now tell me, in which folder do you " + "want to extract all files (or leave blank to automatically extract to the " + f"files to the {FARName} folder): ") parsed.XAll = True elif mode.lower() == "list files": parsed.listf = True
def test_basic(): print("\n# test_basic") ts = TAINTED_STRING tainted_path = pathlib.Path(ts) tainted_pure_path = pathlib.PurePath(ts) tainted_pure_posix_path = pathlib.PurePosixPath(ts) tainted_pure_windows_path = pathlib.PureWindowsPath(ts) ensure_tainted( tainted_path, tainted_pure_path, tainted_pure_posix_path, tainted_pure_windows_path, pathlib.Path("foo") / ts, ts / pathlib.Path("foo"), tainted_path.joinpath("foo", "bar"), pathlib.Path("foo").joinpath(tainted_path, "bar"), pathlib.Path("foo").joinpath("bar", tainted_path), str(tainted_path), # TODO: Tainted methods and attributes # https://docs.python.org/3.8/library/pathlib.html#methods-and-properties ) if os.name == "posix": tainted_posix_path = pathlib.PosixPath(ts) ensure_tainted(tainted_posix_path, ) if os.name == "nt": tainted_windows_path = pathlib.WindowsPath(ts) ensure_tainted(tainted_windows_path, )
def do_query(self, arg): """Query for images with given tags: \\query tag1 tag2 tag3...""" terminal_width = shutil.get_terminal_size()[0] results = query_results(arg.split()) print() for img in results: self.last_results[img.id] = img img_id = term_str('> {:^8} <').format(img.id).bold() print('{:{fill}^{width}}'.format(img_id, fill='-', width=terminal_width + img_id.len_special())) print(img.group.name.capitalize(), pathlib.PureWindowsPath(img.group.path, img.filename)) print(*get_image_tags(img)) arrow_length = int(terminal_width * 0.2) print('{}{:{fill}^{width}}{}'.format('<' * arrow_length, 'END', '>' * arrow_length, fill='-', width=terminal_width - 2 * arrow_length))
def weed_number(path, model, fullpath): col_img, hsv_img, gray_img = load_image(path) # testet done bin_hsv_img = segmention(hsv_img) # tested done missing fine tuning lst = feature_ex(col_img, hsv_img, bin_hsv_img) # almost done need colorbin values and test x = np.array(lst) maxPath = os.path.join(fullpath, 'max.npy') minPath = os.path.join(fullpath, 'min.npy') max = np.loadtxt(maxPath) min = np.loadtxt(minPath) x_norm = np.zeros(x.shape) np.seterr(divide='ignore', invalid='ignore') x_norm = (x - min) / (max - min) x_norm = np.nan_to_num(x_norm) WeedNumberScore = model.predict(x_norm) filename = "test_data.csv" filepath = os.path.join(fullpath, filename) p = pathlib.PureWindowsPath(filepath) fp = os.fspath(p) with open(p, 'a') as nf: nf.write(WeedNumberScore) #result = model.score(X_test, Y_test) return WeedNumberScore
def update_file_input(*args): global VideoInput, autofilesave_dir_path, VideoInputQuoted, VideoOutput, autosavefilename input_entry.configure(state=NORMAL) input_entry.delete(0, END) VideoInput = str(input_dnd.get()).replace("{", "").replace("}", "") file_extension = pathlib.Path(VideoInput).suffix if file_extension == '.hevc' or file_extension == '.mkv' or file_extension == '.mp4' or file_extension == '.ts': autofilesave_file_path = pathlib.PureWindowsPath(VideoInput) # Command to get file input location autofilesave_dir_path = autofilesave_file_path.parents[0] # Final command to get only the directory VideoInputQuoted = '"' + VideoInput + '"' input_entry.insert(0, str(input_dnd.get()).replace("{", "").replace("}", "")) filename = pathlib.Path(VideoInput) VideoOut = filename.with_suffix('.json') autosavefilename = VideoOut.name VideoOutput = str(VideoOut) input_entry.configure(state=DISABLED) output_entry.configure(state=NORMAL) output_entry.delete(0, END) output_entry.configure(state=DISABLED) output_entry.configure(state=NORMAL) output_entry.insert(0, VideoOut) output_entry.configure(state=DISABLED) output_button.configure(state=NORMAL) start_button.configure(state=NORMAL) else: messagebox.showinfo(title="Wrong File Type", message="Try Again With a Supported File Type!\n\nIf this is a " "file that should be supported, please let me know.")