Commit ea7af2e2 authored by brandt73's avatar brandt73
Browse files

Merge branch 'rbrandt-implement-file-mask' into 'master'

implements file mask selection argument

See merge request !13
parents 5dca40c9 57dabedb
Pipeline #276059 passed with stages
in 13 seconds
...@@ -14,7 +14,15 @@ _Types of changes_: ...@@ -14,7 +14,15 @@ _Types of changes_:
* `Fixed` for any bug fixes. * `Fixed` for any bug fixes.
* `Security` in case of vulnerabilities. * `Security` in case of vulnerabilities.
## [Unreleased] ## [Released]
## [0.8.3] - 2022-02-16
### Added
- Introduced new `file_mask` option to limit the files to process. This is a string value which is used
in a `glob.glob()` call to select only files matching the selected criteria. Example:
`"file_mask": "*.csv"` will only process files with the `.csv` extension.
## [0.8.2] - 2022-02-16 ## [0.8.2] - 2022-02-16
......
...@@ -24,6 +24,8 @@ as an option with all incoming `check` and `receive` requests. See agent ...@@ -24,6 +24,8 @@ as an option with all incoming `check` and `receive` requests. See agent
[usage description](mover/description.md#Options) for details. [usage description](mover/description.md#Options) for details.
* `default_destination_dir`: Default target directory for copy or move operations. See agent * `default_destination_dir`: Default target directory for copy or move operations. See agent
[usage description](mover/description.md#Options) for details. [usage description](mover/description.md#Options) for details.
* `file_mask`: Limits the selection of files to a specified file mask. See agent
[usage description](mover/description.md#Options) for details.
## Usage ## Usage
......
VERSION = (0, 8, 2)
VERSION = (0, 8, 3)
__version__ = ".".join(map(str, VERSION)) __version__ = ".".join(map(str, VERSION))
......
...@@ -42,6 +42,8 @@ successfully moved files from the source path. ...@@ -42,6 +42,8 @@ successfully moved files from the source path.
function that will be supplied as a function that will be supplied as a
`key` to `sorted()`. Example: `lambda file: os.path.getmtime(file)` sorts files by last modification. `key` to `sorted()`. Example: `lambda file: os.path.getmtime(file)` sorts files by last modification.
* `order_reverse`: Boolean, reverses the order from `order` if set to "true". * `order_reverse`: Boolean, reverses the order from `order` if set to "true".
* `file_mask:` option to limit the files to process. This is a string value which is used
in a `glob.glob()` call to select only files matching the selected criteria.
### Full Example ### Full Example
...@@ -55,7 +57,8 @@ successfully moved files from the source path. ...@@ -55,7 +57,8 @@ successfully moved files from the source path.
"modified": "not today", "modified": "not today",
"limit": 10, "limit": 10,
"order": "sorted", "order": "sorted",
"order_reverse": "false" "order_reverse": "false",
"file_mask": "*.csv"
} }
} }
``` ```
import glob
import logging import logging
import os import os
import shutil import shutil
...@@ -26,6 +27,7 @@ def move( ...@@ -26,6 +27,7 @@ def move(
order = None order = None
order_reverse = False order_reverse = False
limit = 0 limit = 0
file_mask = None
if len(criteria): if len(criteria):
keys = criteria.keys() keys = criteria.keys()
...@@ -42,6 +44,8 @@ def move( ...@@ -42,6 +44,8 @@ def move(
limit = int(criteria["limit"]) limit = int(criteria["limit"])
if "order" in keys: if "order" in keys:
order = criteria["order"] order = criteria["order"]
if "file_mask" in keys:
file_mask = criteria["file_mask"]
if "order_reverse" in keys: if "order_reverse" in keys:
if type(criteria["order_reverse"]) is bool: if type(criteria["order_reverse"]) is bool:
order_reverse = criteria["order_reverse"] order_reverse = criteria["order_reverse"]
...@@ -55,7 +59,13 @@ def move( ...@@ -55,7 +59,13 @@ def move(
order_reverse = criteria["order_reverse"] > 0 order_reverse = criteria["order_reverse"] > 0
try: try:
source_files = os.listdir(source) if file_mask:
if source[-1] != "/":
source = source + "/"
source_files = [os.path.basename(x) for x in glob.glob(source + file_mask)]
else:
source_files = os.listdir(source)
if order: if order:
# use special keyword "sorted" for simple sorting # use special keyword "sorted" for simple sorting
if order == "sorted": if order == "sorted":
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment