Skip to content

Commit

Permalink
Merge pull request #7 from generic-github-user/file-processing
Browse files Browse the repository at this point in the history
Merge file-processing
  • Loading branch information
generic-github-user authored Jul 22, 2022
2 parents d481791 + 200ad71 commit 3701299
Showing 1 changed file with 45 additions and 14 deletions.
59 changes: 45 additions & 14 deletions ap/ap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
from pathlib import Path

import pickle
import copy
import time
import re
import itertools

import re
import string

import warnings
import textwrap

Expand Down Expand Up @@ -93,6 +96,17 @@ def log(content, level=0):
p=' ' * level # prefix
print(p + f'\n{p}~ '.join(textwrap.wrap(content, n)))


# Helper class to make my life easier;
# - supports arbitrary keyword arguments (which are stored as attributes)
# - tracks access and modification
# - enables method chaining for common iterable operations
class node:
def __init__(self, *args, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
created = time.time()

# Very minimalist class for representing a file; aggregated from file
# snapshots, which represent a (possibly nonexistent) file at a particular
# point in time
Expand All @@ -119,7 +133,7 @@ def validate(self):
warnings.warn(f'filenode missing expected attribute: `{k}`')

def __str__(self):
return '\n'.join(f'{a}: {getattr(self, a) if hasattr(self, a) else None}' for a in 'path ext tags'.split())
return 'filenode { '+'\n'.join(f'{a}: {getattr(self, a) if hasattr(self, a) else None}' for a in 'name path ext tags snapshots'.split())+' }'

def print(self):
print(self)
Expand Down Expand Up @@ -152,7 +166,7 @@ def process(self):
current[0].snapshots.append(self)
#current[0].ext = self.ext
current[0].ext = self.ext
current[0].print()
#current[0].print()
else:
log(f'Adding file node for {self.path} ({len(data["files"])} total)', 1)
newnode = filenode(
Expand All @@ -165,7 +179,7 @@ def process(self):
textproc=False
)
data['files'].append(newnode)
newnode.print()
#newnode.print()
self.processed=True

# TODO: refactor using https://docs.python.org/3/library/pathlib.html#pathlib.Path.iterdir
Expand Down Expand Up @@ -218,20 +232,37 @@ def catalog(path='.', limit=1000, i=0, recursive=True, level=0, delay=0.01) -> i
time.sleep(delay)
return i

# Generates a folder containing symlinks to each of the given files and opens
# it using the associated program
def openfiles(files):
dirname = f'/home/alex/Desktop/ap-temp-{time.time()}'
# be careful
fcopy = copy.deepcopy(files)
for f in fcopy:
if sum(f.name == g.name for g in fcopy) > 1:
for i, h in enumerate(list(filter(lambda x: f.name == x.name, fcopy))):
suffix = f'-{i+1}'
#a, b =
h.name = str(Path(h.name).stem+suffix+h.ext)
# h.path += suffix

Path(dirname).mkdir()
for f in fcopy:
Path(os.path.join(dirname, f.name)).symlink_to(f.path)
os.system(f'xdg-open {dirname}')

def tagfile(fnode, types, tag):
if (hasattr(fnode, 'ext') and
fnode.ext.lower()[1:] in types.split() and
'image' not in fnode.tags):
fnode.tags.append('image')

def tagfiles(n=0):
log('Tagging files')
for anode in itertools.islice(data['files'], n):
anode.validate()

if (hasattr(anode, 'ext') and
anode.ext.lower()[1:] in 'gif png jpg jpeg tiff webm'.split() and
'image' not in anode.tags):
anode.tags.append('image')

if (hasattr(anode, 'ext') and
anode.ext.lower()[1:] in 'txt js py sh java css html todo'.split() and
'textlike' not in anode.tags):
anode.tags.append('textlike')
tagfile(anode, 'gif png jpg jpeg tiff webm', 'image')
tagfile(anode, 'txt js py sh java css html todo', 'textlike')

if not hasattr(anode, 'tags'): setattr(anode, 'tags', [])
anode.print()
Expand Down

0 comments on commit 3701299

Please sign in to comment.