diff --git a/example.yaml b/example.yaml index 75794d0..afa5596 100644 --- a/example.yaml +++ b/example.yaml @@ -16,13 +16,20 @@ programs: enable_pcre2: false remove_old_files: true # Optional. ignore_hidden_files: true # Optional. - excludes: # Optional. Exculde some files/folders + excludes: # Optional. Exculde some files. Only effected when path is a folder. - data.db # Releative path - /path/to/data.db # Absolute path - type: wildcards rule: "*/*.db" - type: regex rule: "\\d+\\.db" + includes: # Optional. Only include some files. Only effected when path is a folder. + - data.db + - /path/to/data.db + - type: wildcards + rule: "*/*.db" + - type: regex + rule: "\\d+\\.db" - type: leveldb # module plyvel is needed to support this type. This will store leveldb database to a single file database (sqlite3) path: leveldb # path to leveldb. Must be relative path. name: dest # optional. path to the backup files. Shoule be a relative path diff --git a/game_backuper/config.py b/game_backuper/config.py index 6591679..7573aaf 100644 --- a/game_backuper/config.py +++ b/game_backuper/config.py @@ -149,6 +149,20 @@ class BasicConfig: # pylint: enable=unsupported-membership-test, unsubscriptable-object +def parse_ex_or_in_cludes(li: list, enable_pcre2) -> List[Union[str, Regex]]: + r = [] + for i in li: + if isinstance(i, str): + r.append(i) + elif isinstance(i, dict): + t = i['type'] + if t == 'wildcards': + r.append(wildcards_to_regex(i['rule'], use_pcre2=enable_pcre2)) + elif t == "regex": + r.append(Regex(i['rule'], use_pcre2=enable_pcre2)) + return r + + class ConfigPath(BasicOption, NFBasicOption, BasicConfig): def __init__(self, data, cfg, prog): NFBasicOption.__init__(self, cfg, prog) @@ -169,24 +183,26 @@ class ConfigPath(BasicOption, NFBasicOption, BasicConfig): del t if 'excludes' in self.data: if isinstance(self.data['excludes'], list): - r = [] - for i in self.data["excludes"]: - if isinstance(i, str): - r.append(i) - elif isinstance(i, dict): - t = i['type'] - if t == 'wildcards': - r.append(wildcards_to_regex(i['rule'], use_pcre2=self.enable_pcre2)) # noqa: E501 - elif t == "regex": - r.append(Regex(i['rule'], - use_pcre2=self.enable_pcre2)) + r = parse_ex_or_in_cludes(self.data["excludes"], self.enable_pcre2) # noqa: E501 self.__excludes = r return r - def is_exclude(self, b: str, loc: str) -> bool: - e = self.excludes + @property + def includes(self) -> List[Union[str, Regex]]: + t = getattr(self, "__includes", None) + if t is not None: + return t + del t + if 'includes' in self.data: + if isinstance(self.data['includes'], list): + r = parse_ex_or_in_cludes(self.data["includes"], self.enable_pcre2) # noqa: E501 + self.__includes = r + return r + + def is_ex_or_in_clude(self, b: str, loc: str, exclude: bool) -> bool: + e = self.excludes if exclude else self.includes if e is None: - return False + return False if exclude else True if isabs(loc): bl = abspath(loc) rl = relpath(loc, b) @@ -208,6 +224,12 @@ class ConfigPath(BasicOption, NFBasicOption, BasicConfig): return True return False + def is_exclude(self, b: str, loc: str) -> bool: + return self.is_ex_or_in_clude(b, loc, True) + + def is_include(self, b: str, loc: str) -> bool: + return self.is_ex_or_in_clude(b, loc, False) + class ConfigOLeveldb(BasicOption, NFBasicOption, BasicConfig): def __init__(self, data, cfg, prog): @@ -315,6 +337,8 @@ class Program(BasicOption, NFBasicOption): for ii in ll: if i.is_exclude(bp, ii): continue + if not i.is_include(bp, ii): + continue tname = relpath(join(b, join(name, relpath(ii, bp))), b) # noqa: E501 tmp = ConfigNormalFile(tname, ii) del tname