Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
- Added an 'ignore' setting - .gitignore style patterns to be ignored…
Browse files Browse the repository at this point in the history
… by the zettel parser.

- Now dependent on pathspec

This closes #4
  • Loading branch information
sthesing committed May 31, 2017
1 parent 98fb4fd commit b3ed453
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from codecs import open
from os import path

version = '0.3.0-exp'
version = '0.3.0'

here = path.abspath(path.dirname(__file__))

Expand Down Expand Up @@ -76,7 +76,7 @@
# your project is installed. For an analysis of "install_requires" vs pip's
# requirements files see:
# https://packaging.python.org/en/latest/requirements.html
install_requires=['PyYAML'],
install_requires=['PyYAML', 'pathspec'],

# List additional groups of dependencies here (e.g. development
# dependencies). You can install these using the following syntax,
Expand Down
5 changes: 5 additions & 0 deletions zettels/examples/zettels.cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ rootdir: examples/Zettelkasten
indexfile: examples/index.yaml
outputformat: '{0[1]}'
prettyformat: '{0[0]:<30}| {0[1]}'
ignore: {
#temporal files, hidden files
'*~',
'.*',
}
51 changes: 44 additions & 7 deletions zettels/zettelparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import linecache
import logging
import os
import pathspec
import pkg_resources
import shlex
import subprocess
Expand All @@ -45,21 +46,56 @@ class Zettelparser:
"""

@staticmethod
def _list_files(dirname):
def _list_files_bak(dirname):
# Returns a list of files in the specified directory
files = []
for root, _, filenames in os.walk(dirname):
for f in filenames:
if not f.endswith("~"):
files.append(os.path.join(root, f))
return files

@staticmethod
def _ignorify(patterns=['*~']):
"""
pathspec implements gitignore style pattern matching. However,
it doesn't ignore patterns, it *matches* patterns.
So every pattern needs to be reversed by adding a '!' (or removing)
it.
"""
patterns = list(patterns)
# First, add everything
reversed_patterns = ['*']

# Then reverse every single pattern
for p in patterns:
# If the pattern starts with '!', remove the '!'
if p.startswith('!'):
reversed_patterns.append(p.replace('!', '', 1))
else:
reversed_patterns.append('!' + p)

return reversed_patterns

@staticmethod
def _list_files(dirname, ignore_patterns=None):
# Reverse pattern
ignore_patterns = Zettelparser._ignorify(ignore_patterns)

spec = pathspec.PathSpec.from_lines('gitwildmatch', ignore_patterns)
matches = spec.match_tree(dirname)
files = []
for match in matches:
files.append(os.path.join(dirname, match))

return files

@staticmethod
def _get_updated_files(dirname, index=None):
def _get_updated_files(dirname, index=None, ignore_patterns=None):


#Get the current list of files
files = Zettelparser._list_files(dirname)
files = Zettelparser._list_files(dirname, ignore_patterns)

if index:
for f in files:
Expand All @@ -79,10 +115,10 @@ def _get_updated_files(dirname, index=None):
return files

@staticmethod
def _grep_files(dirname, index=None):
def _grep_files(dirname, index=None, ignore_patterns=None):
# Calls grep to get the yaml-Blocks and markdown-Links as specified
# in the file "zettels-grep-patterns"
files = Zettelparser._get_updated_files(dirname, index)
files = Zettelparser._get_updated_files(dirname, index, ignore_patterns)

# Call grep only, if there are any updated files
if not files:
Expand Down Expand Up @@ -137,7 +173,7 @@ def _parse_metadata(rootdir, for_yaml, index):
return index

@staticmethod
def update_index(rootdir, index=None):
def update_index(rootdir, index=None, ignore_patterns=None):
"""
Update/build an index for the specified directory.
Expand All @@ -151,11 +187,12 @@ def update_index(rootdir, index=None):
:param rootdir: the directory containing the Zettel files.
:param index: An existing index, if available.
:param ignore_patterns: a list of gitignore-style patterns to be ignored by grep
:return: The index in dictionary format.
"""
logger.debug("Updating index:")
# get the list of updated files and the grep output
files, grepoutput = Zettelparser._grep_files(rootdir, index)
files, grepoutput = Zettelparser._grep_files(rootdir, index, ignore_patterns)

# generate a empty index, if necessary
if not index:
Expand Down
17 changes: 9 additions & 8 deletions zettels/zettels.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Zettels is a command line tool implementing Niklas Luhmann's system of a "Zettelkasten".
"""

__version__ = '0.3.0-exp'
__version__ = '0.3.0'
__author__ = 'Stefan Thesing'

# Libraries
Expand Down Expand Up @@ -105,9 +105,10 @@ def _read_settings(f):
exit()
indexfile = settings['indexfile']
indexfile = os.path.abspath(os.path.expanduser(indexfile))
outputformat = settings['outputformat']
prettyformat = settings['prettyformat']
return rootdir, indexfile, outputformat, prettyformat
outputformat = settings['outputformat']
prettyformat = settings['prettyformat']
ignore_patterns = settings['ignore']
return rootdir, indexfile, outputformat, prettyformat, ignore_patterns
else:
print("There seems to be a problem with your settings \
file. Zettels expected to receive a dictionary or other \
Expand All @@ -134,13 +135,13 @@ def _query(args):

# Next, let's read the settings file. _read_settings(settings) does the
# error handling
rootdir, indexfile, outputformat, prettyformat = _read_settings(args.settings)
rootdir, indexfile, outputformat, prettyformat, ignore_patterns = _read_settings(args.settings)
# If we're still running, we have valid settings.
logger.debug("Root dir: " + rootdir)
logger.debug("Index file: " + indexfile)

if args.update:
index = Zettelparser.update_index(rootdir)
index = Zettelparser.update_index(rootdir, ignore_patterns=ignore_patterns)
logger.debug("Writing index to file " + indexfile)
Zettelparser.write_index(index, indexfile)
logger.debug("Done")
Expand Down Expand Up @@ -217,12 +218,12 @@ def _parse(args):

# Next, let's read the settings file. _read_settings(settings) does the
# error handling
rootdir, indexfile, _, _ = _read_settings(args.settings)
rootdir, indexfile, _, _, ignore_patterns = _read_settings(args.settings)
# If we're still running, we have valid settings.
logger.debug("Root dir: " + rootdir)
logger.debug("Index file: " + indexfile)

index = Zettelparser.update_index(rootdir)
index = Zettelparser.update_index(rootdir, ignore_patterns=ignore_patterns)
logger.debug("Writing index to file " + indexfile)
Zettelparser.write_index(index, indexfile)
logger.debug("Done")
Expand Down
10 changes: 8 additions & 2 deletions zettels/zettels_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def generate_settings():
indexfile = settings_base_dir + '/index.yaml'
outputformat = '{0[1]}'
prettyformat = '{0[0]:<30}| {0[1]}'
ignore = ['*~', '.*']

# Ask the user

Expand Down Expand Up @@ -67,8 +68,13 @@ def generate_settings():
f.write("# see https://github.com/sthesing/Zettels\n")
f.write('rootdir: ' + rootdir + '\n')
f.write('indexfile: ' + indexfile + '\n')
f.write('outputformat: \'' + outputformat + '\'')
f.write('prettyformat: \'' + prettyformat + '\'')
f.write('outputformat: \'' + outputformat + '\'\n')
f.write('prettyformat: \'' + prettyformat + '\'\n')
f.write('ignore: {\n')
f.write(' # temporal files, hidden files\n')
for p in ignore:
f.write(' \''+ p + '\',\n')
f.write('}\n')
f.close()

print("Settings written to '" + os.path.join(settings_base_dir, 'zettels.cfg.yaml') + "'.")
Expand Down

0 comments on commit b3ed453

Please sign in to comment.