def __init__(self, name=None, path=None): if path is None: self.path = self.get_path_from_name(name=name) else: path = Path(path) if not path.endswith(name): path /= name self.path = path self.has_loaded_metadata = False
def download(self, path, version=None, overwrite=False): """ Download tar ball to cache, extract it, remove tar ball. Returns target folder tarball is extracted in. """ if version is None: version = self.get_version() path = Path(path) temp = Path.get_cache_dir() / "Python/temp.tar.gz" download(self.get_tarball_url(version=version), path=temp) temp.unpack(base=path, overwrite=overwrite) temp.delete(error=False) return path / f"{self.name}-{version}"
def _create_todo_dict(self, text, path, line): """ :param generalpackager.Packager self: """ path = Path(path) return { "Package": Markdown.link(text=self.name, url=self.github.url), "Module": self.github_link_path_line(text=path.name(), path=path, line=1), "Message": self.github_link_path_line(text=text, path=path, line=line), }
def download(url, path): """ Todo: Move download to it's own package. """ data = requests.get(url) if data.status_code != 200: raise AttributeError(f"Request for url {url} did not yield a status code of 200.'") path = Path(path) with path.lock(): path.get_parent().create_folder() with open(str(path), "wb") as file: file.write(data.content) return path
def generate_license(self): """ Generate LICENSE by using Packager.license. :param generalpackager.Packager self: """ text = Path(self.localrepo.get_repos_path() / f"generalpackager/generalpackager/licenses/{self.license}" ).text.read() assert "$" in text text = text.replace("$year", str(Date.now().datetime.year)) text = text.replace("$author", self.author) assert "$" not in text return text
def format_file(self, path, write=True): """ Overwrite a file to replace camelCase with under_scores. """ path = Path(path) old_text = path.text.read() new_text = self.replace_camel_case(text=old_text) new_text = self.replace_docstrings(text=old_text) if write: path.text.write(new_text, overwrite=True) else: from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install()) driver.get("https://text-compare.com/") driver.maximize_window() driver.execute_script("arguments[0].value = arguments[1];", driver.find_element_by_id("inputText1"), old_text) driver.execute_script("arguments[0].value = arguments[1];", driver.find_element_by_id("inputText2"), new_text) driver.find_element_by_class_name("compareButtonText").click() while True: pass
def git_changed_files(self): """ Get a list of changed files using local .git folder. """ repo = Repo(str(self.path)) return [ Path(file) for file in re.findall("diff --git a/(.*) " + "b/", repo.git.diff()) ]
def relative_path_is_aesthetic(self, relative_path): """ Relative to package path. False if not defined as a GenerateFile instance. :param generalpackager.Packager self: :param Path or str relative_path: """ relative_path = Path(relative_path).relative(self.path) aesthetic_attr = getattr( self.files_by_relative_path.get(relative_path, None), "aesthetic", None) if aesthetic_attr is None: if relative_path.match(*self.extra_aesthetic): return True # elif relative_path.match(*self.extra_non_aesthetic): # return False else: return False return aesthetic_attr
def test_text_read_write(self): for path in self.paths: text = Path(path).text text.write("foo") self.assertEqual("foo", text.read()) self.assertRaises(FileExistsError, text.write, "bar") text.write("bar", overwrite=True) self.assertEqual("bar", text.read())
def reserve_name(self): """ Reserve a name on PyPI with template files. Untested. :param generalpackager.Packager self: """ path = Path.get_cache_dir() / "python/pypi_reserve" # type: Path packager = type(self)(self.name, path=path) packager.create_blank_locally(install=False) packager.localrepo.upload() path.delete()
def get_repos_path(cls, path=None): """ Try to return absolute path pointing to folder containing repos or None. :rtype: Path """ filt = lambda path: LocalRepo.get_repo_path_child( path) and not path.match("/test") return Path(path).absolute().get_parent(depth=-1, include_self=True, filt=filt, traverse_excluded=True)
def get_repo_path_parent(cls, path=None): """ Iterate self and parents to return first path where LocalRepo can be created or None. :param Path or any path: :rtype: Path """ return Path(path=path).absolute().get_parent( depth=-1, include_self=True, filt=LocalRepo.path_exists, traverse_excluded=True)
def github_link_path_line(self, text, path, line=None): """ :param generalpackager.Packager self: :param text: :param path: :param line: """ if line is None: line = 1 path = Path(path) return self.github_link( text=text, suffix=f"blob/{self.commit_sha}/{path.encode()}#L{line}")
def _compare_local(self, platform, aesthetic): """ :param generalpackager.Packager self: """ unpack_target = Path.get_cache_dir() / "Python" package_path = platform.download(path=unpack_target, overwrite=True) filt = lambda path: not path.match(*self.git_exclude_lines) differing_files = self.path.get_differing_files(target=package_path, filt=filt) return self.filter_relative_filenames(*differing_files, aesthetic=aesthetic)
def download(self, path, overwrite=False): """ Clone a GitHub repo into a path. Creates a folder with Package's name first. Target must be empty. :param generalpackager.Packager self: :param path: :param overwrite: """ if not self.exists(): return path = Path(path) / self.name if path.exists(): if overwrite: path.delete() else: raise AttributeError( f"Clone target exists and overwrite is False.") Repo.clone_from(url=self.url, to_path=path) return path
def get_path_from_name(cls, name=None): """ Get path from name. :param Path or any name: :rtype: Path """ # Check if inside correct repo already path = cls.get_repo_path_parent() if path and (name is None or name == path.name()): return path # if Path.get_working_dir().name() == name: # return Path.get_working_dir() if name is None: name = "generalpackager" # Check if there's a parent that has a repo child that exists repos_path = cls.get_repos_path() if repos_path: return repos_path / name return Path(name).absolute()
def test_text_append(self): for path in self.paths: text = Path(path).text text.append("foo") self.assertEqual("foo", text.read()) text.append("bar") self.assertEqual("foobar", text.read()) text.append("linebreak", newline=True) self.assertEqual("foobar\nlinebreak", text.read())
def _appendAndReadDF(self, obj): Path("df.tsv").spreadsheet.write(pd.DataFrame(), overwrite=True) Path("df.tsv").spreadsheet.append(obj) read = Path("df.tsv").spreadsheet.read(header=False, column=False) return read
def _writeAndReadDF(self, df): header, column = Path("df.tsv").spreadsheet.write(df, overwrite=True) read = Path("df.tsv").spreadsheet.read(header=header, column=column) return read
def get_repo_path_child(cls, folder_path): """ Return whether there's atleast one repo in folder. """ return Path(folder_path).get_child(filt=LocalRepo.path_exists, traverse_excluded=True)
def test_utf8(self): path = Path("foo") path.text.write("├") self.assertEqual("├", path.text.read())
def test_text_replace_regex(self): for path in self.paths: text = Path(path).text text.write("hello this is a test") text.replace({".+": "foo52bar"}, regex=True) self.assertEqual("foo52bar", text.read()) text.replace({".+": "foo52bar"}, regex=True) self.assertEqual("foo52bar", text.read()) text.replace({"\d+": ""}, regex=True) self.assertEqual("foobar", text.read())
def test_text_replace(self): for path in self.paths: text = Path(path).text text.write("hello this is a test") text.replace({"this": "that"}) self.assertEqual("hello that is a test", text.read()) text.replace({"that is": "what"}) self.assertEqual("hello what a test", text.read()) text.replace({"hello": "hi", "a test": "are tests"}) self.assertEqual("hi what are tests", text.read())