Skip to content

Commit

Permalink
changes for file sorting feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottDillman committed Nov 6, 2024
1 parent e809902 commit f494c00
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
- add exmaple yaml settings file and exclude settings from git
- call python3 instead of pythion in shell scripts and make
- defualt to compiled docx, get name from parent directory
- fix time difference
- fix time difference

## 0.1.3 2024-11-06
- Add support for file sorting by age,name,size
- Added file sorting config to yaml
2 changes: 1 addition & 1 deletion srt2docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
)

## update me on major changes
__version__ = "0.1.2"
__version__ = "0.1.3"
__contact__ = "[email protected]"
__web__ = "https://dreamcyclesetudios.com"

Expand Down
32 changes: 30 additions & 2 deletions srt2docx_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,44 @@
import inspect
import os
from docx.enum.style import WD_STYLE_TYPE

import time

def round_to_secs(dt: datetime) -> datetime:
"""round to nearest second"""
return timedelta(seconds=int(dt.total_seconds()))

def age(x):
return time.time() - os.path.getmtime(x)

def getbytes(x):
return int(os.path.getsize(x))

def readFiles(values) -> list:
"""read files from cwd"""
logger.info("Glob in effect is: [{}]".format(values.settings.filetypes.glob))
files = list(Path().glob(values.settings.filetypes.glob))
logger.info("Sorting in effect is: [{}][{}]".format(values.settings.sort.type,values.settings.sort.direction))

key = None;
reverse = False

if values.settings.sort.direction == 'descending':
reverse = True
else:
reverse = False

## sort files here based on settings
match values.settings.sort.type:
case "name":
key = os.path.normpath
case "size":
key = getbytes
case "age":
key = age
case _:
key = os.path.normpath

files = sorted(list(Path().glob(values.settings.filetypes.glob)),key=key, reverse=reverse)

return files


Expand Down
6 changes: 6 additions & 0 deletions srt2docx_settings_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ settings:

## single file mode, false for multi-file
single_file: true
## file sorting
sort:
## alpha, size, age
type: alpha
## ascending or descending
direction: descending

## table settings
table:
Expand Down

0 comments on commit f494c00

Please sign in to comment.