def test_relations_default(): e1 = Element("e1", c2=["e3", "e4"]) e2 = Element("e2", c2="e3") e3 = Element("e3") e4 = Element("e4") c1 = Collection("c1", [e1, e2]) c2 = Collection("c2", [e3, e4]) r = Relations(c1, c2) assert r.collection == "c2" assert r.key is None p = Project([e1, e2, e3, e4]) c1(p) c2(p) r(p) assert e3 in getattr(e1, "c2", []) assert e4 in getattr(e1, "c2", []) assert getattr(e2, "c2", []) is e3
def test_markdown(): elt = Element("a.md") elt._contents = "# H1\n\nLorem Ipsum\n" project = Project([elt]) M = Markdown() M(project) assert elt._contents == '<h1 id="h1">H1</h1>\n\n<p>Lorem Ipsum</p>\n'
def test_markdown_metadata(): elt = Element("a.md") elt._contents = "---\nfoo: test a\nbar: test b\n---\n# H1\n\nLorem Ipsum\n" project = Project([elt]) M = Markdown() M(project) assert elt._contents == '<h1 id="h1">H1</h1>\n\n<p>Lorem Ipsum</p>\n' assert getattr(elt, "foo") == "test a" assert getattr(elt, "bar") == "test b"
def test_drop_simple_pattern(): d1 = Element(PurePath("a/b/c")) d2 = Element(PurePath("a/b/f")) d3 = Element(PurePath("a/k/c")) d4 = Element(PurePath("a/k/d/c")) project = Project([d1, d2, d3, d4]) D = Drop("a/b/c") D(project) assert len(project.elements) == 3 assert d1 not in project.elements assert d2 in project.elements assert d3 in project.elements assert d4 in project.elements
def test_drop_multiple_wildcard_pattern(): d1 = Element(PurePath("a/b/c")) d2 = Element(PurePath("a/b/f")) d3 = Element(PurePath("a/k/c")) d4 = Element(PurePath("a/k/d/c")) project = Project([d1, d2, d3, d4]) D = Drop("**/c") D(project) assert len(project.elements) == 1 assert d1 not in project.elements assert d2 in project.elements assert d3 not in project.elements assert d4 not in project.elements
def test_keep_simple_pattern(): d1 = Element(PurePath("a/b/c")) d2 = Element(PurePath("a/b/f")) d3 = Element(PurePath("a/k/c")) d4 = Element(PurePath("a/k/d/c")) project = Project([d1, d2, d3, d4]) K = Keep("a/b/c") K(project) assert len(project.elements) == 1 assert d1 in project.elements assert d2 not in project.elements assert d3 not in project.elements assert d4 not in project.elements
def test_keep_multiple_wildcard_pattern(): d1 = Element(PurePath("a/b/c")) d2 = Element(PurePath("a/b/f")) d3 = Element(PurePath("a/k/c")) d4 = Element(PurePath("a/k/d/c")) project = Project([d1, d2, d3, d4]) K = Keep("**/c") K(project) assert len(project.elements) == 3 assert d1 in project.elements assert d2 not in project.elements assert d3 in project.elements assert d4 in project.elements
def __call__(self, project: Project): csv.field_size_limit(sys.maxsize) # Parsing elements for element in project.elements: # Valid selected CSV file if (self.pattern and element._path.match(self.pattern) or self.pattern is None and element._path.suffix.lower() == ".csv"): # Parsing CSV file records = csv.reader(element._contents.splitlines(), delimiter=self.delimiter) # Header line headers = next(records) # Iterating records for i, row in enumerate(records): # Parsing CSV data into metadata metadata = dict(zip(headers, row)) # Extracting main column contents contents = (metadata.pop(self.contents_column) if self.contents_column else None) # New filename building filename = (metadata.get(self.filename_column) if self.filename_column else str(i)) filepath = element._path.with_name(filename) new_file = Element(filepath, **metadata) new_file._contents = contents project.elements.append(new_file) # Removing original CSV file project.elements.remove(element)
def add(self, element: Element): if element in self._members: logging.debug( f"Element {element._filename} is already in {self.name} Collection." ) return # Adding to internal element list self._members.append(element) # Create element collections attribute if it does not exists already if hasattr(element, "collections"): if isinstance(element.collections, list): element.collections = set(element.collections) elif not isinstance(element.collections, set): element.collections = {element.collections} else: setattr(element, "collections", set()) # Reverse-adding collection name and various attributes to element element.collections.add(self.name)
def __call__(self, project: Project): project.elements.append( Element(self.directory / Path(self.filename), global_metadata=project.metadata, **self.metadata))