Skip to content

Commit

Permalink
move no suffix option into into -r/-l
Browse files Browse the repository at this point in the history
  • Loading branch information
inconvergent committed Mar 24, 2020
1 parent c00224b commit af87379
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
19 changes: 10 additions & 9 deletions fn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
fn [-m] [-t]
fn -g
fn -p
fn -r [-a|-A] [<dir>]
fn -l [-a|-A] [<dir>]
fn -R [<dir>]
fn -r [-a|-A] [-f] [<dir>]
fn -l [-a|-A] [-f] [<dir>]
fn -s [<dir>]
Expand All @@ -29,12 +28,12 @@
-t return timestamp only.
-r return all files with the most recent prochash.
-R return most recent file name with no suffix.
-l return all files with current git sha.
-s return most recent prochash.
-a show file name only.
-A show absolute paths.
-f remove file suffix. resulting duplicates will be removed.
-h --help show this screen.
--version show version.
Expand Down Expand Up @@ -68,13 +67,15 @@ def handle_path_args(args):
def handle_args(fn, args):
args = handle_path_args(args)
if args['-l']:
return fn.lst(d=args['<dir>'], path_style=args['path_style'])
return fn.lst(d=args['<dir>'],
path_style=args['path_style'],
suffix=not args['-f'])
if args['-r']:
return fn.recent(d=args['<dir>'], path_style=args['path_style'])
return fn.recent(d=args['<dir>'],
path_style=args['path_style'],
suffix=not args['-f'])
if args['-s']:
return fn.recent_prochash(d=args['<dir>'])
if args['-R']:
return fn.recent_nosuffix(d=args['<dir>'])
if args['-p']:
return [fn.get_pid_sha()]
if args['-g']:
Expand All @@ -83,7 +84,7 @@ def handle_args(fn, args):


def main():
args = docopt(__doc__, version='fn 2.2.1')
args = docopt(__doc__, version='fn 2.3.0')

if args['-t']:
print(get_time(milli=args['-m']))
Expand Down
27 changes: 14 additions & 13 deletions fn/fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from .utils import rel_abs_path
from .utils import remove_extension
from .utils import sortfx
from .utils import overlay
from .utils import deduplicate_files

SEP = '-'

Expand Down Expand Up @@ -49,15 +51,19 @@ def name(self, milli=True, postfix=None):
l.append(self.postfix)
return ''.join(l)

def __get_current_files(self, d=None, path_style='rel'):
def __get_current_files(self, d=None, path_style='rel', suffix=True):
if d:
try:
chdir(d)
except FileNotFoundError:
raise ValueError('no folder: {:s}'.format(d))

return rel_abs_path(
d, path_style, sorted(self.tokenizer(glob('*')), key=sortfx))
files = self.tokenizer(glob('*'))
if not suffix:
files = deduplicate_files(
[overlay(f, {'_raw': remove_extension(f['_raw'])}) for f in files])

return rel_abs_path(d, path_style, sorted(files, key=sortfx))

def get_pid_sha(self):
return self.pid_sha
Expand All @@ -75,16 +81,6 @@ def recent(self, **args):
lambda f: f['_raw'],
filter(lambda f: f['prochash'] == prochash, current))

def recent_nosuffix(self, d):
current = list(self.__get_current_files(d, path_style='file'))
if current:
yield remove_extension(current[-1]['_raw'])

def recent_prochash(self, d):
current = list(self.__get_current_files(d))
if current:
yield current[-1]['prochash']

def lst(self, **args):
self.__is_git()
files = list(self.__get_current_files(**args))
Expand All @@ -95,3 +91,8 @@ def lst(self, **args):
lambda x: x['_raw'],
filter(lambda x: x['gitsha'] == prochash, files))

def recent_prochash(self, d):
current = list(self.__get_current_files(d))
if current:
yield current[-1]['prochash']

8 changes: 8 additions & 0 deletions fn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def rel_abs_path(d, path, files):
yield overlay(f, {'_raw': normpath(fx(f['_raw']))})


def deduplicate_files(files):
d = set()
for f in files:
if f['_raw'] not in d:
d.update([f['_raw']])
yield f


def get_time(milli=True, sep='-'):
now = datetime.now()
if milli:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


setup(name='fn',
version='2.2.1',
version='2.3.0',
description='fn',
url='https://github.com/inconvergent/fn',
license='MIT License',
Expand Down

0 comments on commit af87379

Please sign in to comment.