Skip to content

Commit

Permalink
Merge pull request #3 from Kraymer/#2/filter_by_date
Browse files Browse the repository at this point in the history
Filter by date.
Closes #2
  • Loading branch information
Kraymer authored Dec 31, 2020
2 parents bf6f03b + 83ba814 commit bcd3949
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
Binary file modified docs/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ arrow
blessed
click
click_log
dateparser
gitpython
1 change: 0 additions & 1 deletion tests/fixtures/child

This file was deleted.

32 changes: 29 additions & 3 deletions treeage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Lists contents of directories in a tree-like format with age metric indicated for each file.
Expand All @@ -8,6 +9,7 @@

import click
import click_log
import dateparser

from treeage.core import TreeageCore

Expand All @@ -17,9 +19,25 @@
click_log.basic_config(logger)


def parse_date(text):
"""Return POSIX timestamp obtained from parsing date and time from given
date string.
Return None if no text given.
"""
if text:
return dateparser.parse(text).timestamp()


@click.command(
context_settings=dict(help_option_names=["-h", "--help"]),
help=__doc__,
epilog=(
"""\b\nExamples:
treeage --maxdepth 2 --before "01 jan 2018"../qifqif
treeage --include "*.py" --after "3 month ago" .
"""
),
)
@click.argument("directory", type=click.Path(exists=True), nargs=1)
@click.option(
Expand All @@ -35,10 +53,18 @@
"--include",
"include_glob",
metavar="GLOB",
help=("Search only files whose base name matches GLOB (using wildcard matching)"),
help=("List only files whose base name matches GLOB (using wildcard matching)"),
)
@click.option(
"--before", metavar="DATE", help=("List only files whose age is older than DATE")
)
@click.option(
"--after", metavar="DATE", help=("List only files whose age is lower than DATE")
)
@click_log.simple_verbosity_option(logger)
@click.version_option(__version__)
def treeage_cli(directory, maxdepth, include_glob):
def treeage_cli(directory, maxdepth, include_glob, before, after):
directory = os.path.abspath(directory)
TreeageCore(directory, maxdepth, include_glob).dump()
TreeageCore(
directory, maxdepth, include_glob, parse_date(before), parse_date(after)
).dump()
28 changes: 21 additions & 7 deletions treeage/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

logger = logging.getLogger(__name__)
term = terminal.Terminal()
all_dates = {}


def repo_path_for(filename):
Expand Down Expand Up @@ -68,9 +67,12 @@ def abbr_date(date):


class TreeageCore:
def __init__(self, root, maxdepth, include_glob):
def __init__(self, root, maxdepth, include_glob, before, after):
"""Render aged tree for dirpath"""
self.all_dates = {}
self.maxdepth = maxdepth
self.before = before
self.after = after
self.root = root
self.repo_path = repo_path_for(self.root)
self.repo = Repo(self.repo_path)
Expand Down Expand Up @@ -121,7 +123,7 @@ def _date(self, path):
filedate = mean(lines_dates)
else: # time of most recent content modification.
filedate = os.stat(path).st_mtime
all_dates[path] = filedate
self.all_dates[path] = filedate
return filedate

def tree_format(self, parent, dir_name):
Expand All @@ -138,6 +140,16 @@ def render_tree(self, tree):
"""Render tree"""
name = tree["name"]
children = tree["children"]
if self.after:
children = [x for x in children if not x["date"] or x["date"] > self.after]
self.all_dates = {
k: v for (k, v) in self.all_dates.items() if v > self.after
}
if self.before:
children = [x for x in children if not x["date"] or x["date"] < self.before]
self.all_dates = {
k: v for (k, v) in self.all_dates.items() if v < self.before
}
res = [name] + fp.reduce(
lambda l, r: l + r,
map(lambda arg: self.render(len(children))(*arg), enumerate(children)),
Expand All @@ -164,8 +176,8 @@ def render_date(self, date):
if not date:
return ""
rgb_max = 255
min_date = min(all_dates.values())
max_date = max(all_dates.values())
min_date = min(self.all_dates.values())
max_date = max(self.all_dates.values())

step_lin = (max_date - min_date) / rgb_max
try:
Expand Down Expand Up @@ -198,8 +210,10 @@ def prefix_date(self, tree):
fullpath = ""
for i in range(0, index + 1, 4):
fullpath = os.path.join(fullpath, walk[i])
if fullpath in all_dates:
res.append("{} {}".format(self.render_date(all_dates[fullpath]), line))
if fullpath in self.all_dates:
res.append(
"{} {}".format(self.render_date(self.all_dates[fullpath]), line)
)
else:
res.append(" " + line)
return res

0 comments on commit bcd3949

Please sign in to comment.