Example #1
0
 def _strip_folder_name(self):
     all_folder_in_order = self.path.split(self.separator)
     result = ""
     for folder_name in all_folder_in_order:
         folder_name = folder_name.strip()
         check_state(len(folder_name) > 0, self.path + "is invalid")
         result += folder_name + self.separator
     self.path = result[:-1]
Example #2
0
 def parse_file(self, content: str) -> File:
     match_result = MarkdownParser.file_line_pattern.match(content)
     check_state(match_result is not None)
     filename = match_result.group(1)
     uri = match_result.group(2)
     tags = self.parse_tag_str(match_result.group(3))
     metadata = json.loads(match_result.group(4))
     file = File(filename, uri, Path(metadata["path"]))
     file.metadata = metadata
     file.add_tags(tags)
     return file
Example #3
0
 def __init__(self, path="", validate_folder_name=True):
     # "/" is reserved for the default separator.
     self.separator = DEFAULT_PATH_SEPARATOR
     # a string in the format of "a/b/c" to represent a path
     # "" can be used
     self.path = path.strip().strip(self.separator).strip()
     if validate_folder_name and self.separator in self.path:
         self._strip_folder_name()
     if validate_folder_name:
         for folder in self.split():
             check_state(verify_folder(folder))
Example #4
0
 def __init__(self, filename: str, uri: str, path: Path):
     # name of file
     self.name = filename.strip()
     check_state(verify_filename(filename))
     # a string act as uri
     self.uri = uri.strip()
     check_state(verify_uri(self.uri))
     # a set for holding customer specified tags (str)
     self.tags = set()
     # metadata for the file, free_formed, open for extension
     # it must be a str to str map
     self.metadata = dict()
     # path of the file
     self.path = path.copy()
     self.metadata["path"] = self.path.path
Example #5
0
    def parse_file_collection_without_metadata_or_tags(self, content: str) -> FileCollection:
        file_col = FileCollection()

        pre_path = Path()
        for line in content.split(MarkdownParser.line_break):
            line = MarkdownParser.convert_space_to_tab(line)
            if "[" in line:
                match_result = MarkdownParser.file_line_without_metadata_pattern.search(line)
                if match_result is not None:
                    filename = match_result.group(1)
                    uri = match_result.group(2)
                    tags = self.parse_tag_str(match_result.group(3))
                    file = File(filename, uri, pre_path)
                    file.add_tags(tags)
                else:
                    match_result = MarkdownParser.file_line_without_tags_or_metadata_pattern.search(line)
                    check_state(match_result is not None)
                    filename = match_result.group(1)
                    uri = match_result.group(2)
                    file = File(filename, uri, pre_path)
                file_col.add_file(file)
            else:
                match_result = MarkdownParser.folder_line_pattern.match(line)
                if match_result is None:
                    continue
                folder_level = match_result.group(1).count("\t") + 1
                folder_name = match_result.group(2)
                if folder_level == pre_path.depth():
                    cur_path = pre_path.sibling(folder_name)
                elif folder_level > pre_path.depth():
                    cur_path = pre_path.child(folder_name)
                else:
                    diff = pre_path.depth() - folder_level
                    cur_path = pre_path.parent_(diff).sibling(folder_name)
                pre_path = cur_path
        return file_col
Example #6
0
    def render_metadata(metadata: dict):
        result = json.dumps(metadata)
        check_state("<" not in result)

        return result
Example #7
0
 def __init__(self, tags_filename: list):
     # [tag1, tag2, ..., filename]
     check_state(len(tags_filename) > 0)
     self.tags_filename = tags_filename
Example #8
0
 def is_child_of(self, other_path) -> bool:
     check_state(self.separator == other_path.separator,
                 "The separator used in two path are not the same")
     return self.path.startswith(other_path.path)
Example #9
0
 def child(self, child_name: str):
     child_name = child_name.strip().strip(self.separator).strip()
     check_state(len(child_name) > 0, child_name + "is invalid")
     if len(self.path) == 0:
         return Path(child_name)
     return Path(self.path + self.separator + child_name)