Re: [Zim-wiki] howto strip metadata header

2012-03-02 Thread hansbkk
On Fri, Mar 2, 2012 at 10:49 PM, Jaap Karssenberg <
jaap.karssenb...@gmail.com> wrote:

>
> You might want to use the HeadersDict class from zim.config. The read()
> method actually parses the lines and only strips lines that are actually
> headers. Much safer, because it will not change files that did not have
> headers in the first place.
>
> I would also recommend the File object from zim.fs as it does all kind of
> checking for safe reading / writing on the files. But that is for the more
> paranoid ;)
>

Just because I'm paranoid doesn't mean I haven't lost tons of data over the
years 

OK then I'll stick to my in-editor method for now, as I do now have a mixed
back of files with and without headers, and I don't know from programming
(what's a class? what's a method? - no please don't answer 8-)
___
Mailing list: https://launchpad.net/~zim-wiki
Post to : zim-wiki@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp


Re: [Zim-wiki] howto strip metadata header

2012-03-02 Thread Jaap Karssenberg
On Fri, Mar 2, 2012 at 3:47 PM, Adam Porter  wrote:

> This will process a directory tree and remove the first 4 lines of
> each file.  I did a quick test on a copy of my tree, but please make
> sure to back up your data first, and if it blows up, it's your fault.
>


You might want to use the HeadersDict class from zim.config. The read()
method actually parses the lines and only strips lines that are actually
headers. Much safer, because it will not change files that did not have
headers in the first place.

I would also recommend the File object from zim.fs as it does all kind of
checking for safe reading / writing on the files. But that is for the more
paranoid ;)

-- Jaap
___
Mailing list: https://launchpad.net/~zim-wiki
Post to : zim-wiki@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp


Re: [Zim-wiki] howto strip metadata header

2012-03-02 Thread hansbkk
On Fri, Mar 2, 2012 at 9:55 PM, Adam Porter  wrote:

> Of course, then I remembered that old adage, "Those who don't know
> Unix are doomed to reinvent it, poorly."  :)
>
> $ find . -iname "*.txt" | while read f; do tail -n +5 "$f" | cat >"$f";
> done
>
>

On Fri, Mar 2, 2012 at 9:55 PM, Adam Porter  wrote:

> Of course, then I remembered that old adage, "Those who don't know
> Unix are doomed to reinvent it, poorly."  :)
>
> $ find . -iname "*.txt" | while read f; do tail -n +5 "$f" | cat >"$f";
> done
>
>
I'm impressed (7 minutes coding!), will give the python version a try since
I'm on windows, otherwise will fire up Cygwin to try the bash one.

What I did in the meantime was load all the .txt files up into my coding
editor and regex-S&R'd on the beginning three "strings+.*+\n", replacing
with nothing. A kludge, but it worked.

Thanks. . .
___
Mailing list: https://launchpad.net/~zim-wiki
Post to : zim-wiki@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp


Re: [Zim-wiki] howto strip metadata header

2012-03-02 Thread Adam Porter
Of course, then I remembered that old adage, "Those who don't know
Unix are doomed to reinvent it, poorly."  :)

$ find . -iname "*.txt" | while read f; do tail -n +5 "$f" | cat >"$f"; done

On Fri, Mar 2, 2012 at 08:47, Adam Porter  wrote:
> This will process a directory tree and remove the first 4 lines of
> each file.  I did a quick test on a copy of my tree, but please make
> sure to back up your data first, and if it blows up, it's your fault.
> :)

___
Mailing list: https://launchpad.net/~zim-wiki
Post to : zim-wiki@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp


Re: [Zim-wiki] howto strip metadata header

2012-03-02 Thread Adam Porter
This will process a directory tree and remove the first 4 lines of
each file.  I did a quick test on a copy of my tree, but please make
sure to back up your data first, and if it blows up, it's your fault.
:)
#! /usr/bin/env python

# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# 
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 
# The name of the author may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import logging as log
import argparse
import os
import sys

def process_directory(dir):

oldcwd = os.getcwd()

for root, dirs, files in os.walk(dir, onerror=log.error):

log.info("Processing directory %s" % root)

if not files:
log.info("No files in directory; skipping")
continue

os.chdir(root)


# Iterate over existing files
for file in sorted(files):

# Skip non-files
if not os.path.isfile(file):
log.debug("Skipping non-file: %s" % file)
continue
	
	if not '.txt' in file:
		continue

log.debug("Processing: %s" % file)

if options.write:
		f = open(file, 'r+')

		lines = f.readlines()
		
		f.close()
		
		f = open(file, 'w')
		
		i = 0
		for line in lines:
		if i > 3:
			f.write(line)

		i += 1 
		
		f.close()
		
	else:
		f = open(file, 'r')
		
		i = 0
		for line in f.readlines():
		if i > 3:
			print line

		i += 1
		
		f.close()
		

log.debug("Finished processing directory %s" % root)

os.chdir(oldcwd)


if __name__ == "__main__":

parser = argparse.ArgumentParser(description="Unzim")

parser.add_argument('directory', nargs='+')
parser.add_argument("-w", "--write", dest="write", action="store_true", help="Actually overwrite the files (otherwise print to stdout)")
parser.add_argument("-v", "--verbose", action="count", dest="verbose", help="Print more output (up to -vv)")

options = parser.parse_args()

if options.verbose == 1:
LOG_LEVEL = log.INFO
elif options.verbose >=2:
LOG_LEVEL = log.DEBUG
else:
LOG_LEVEL = log.WARNING

log.basicConfig(level=LOG_LEVEL, format="%(levelname)s: %(message)s")

log.debug("Options: %s" % options)

if not options.directory:
log.critical("You need to specify the directories to process, silly.  :)")
parser.print_usage()
sys.exit(2)

# Verify arguments are directories
quit = False
for dir in options.directory:
if not os.path.isdir(dir):
log.critical("%s is not a directory" % dir)
quit = True
if quit:
sys.exit(2)

# Iterate over directories
consistent = True
for dir in options.directory:
if not process_directory(dir):  # Returns false when inconsistencies are found
consistent = False
___
Mailing list: https://launchpad.net/~zim-wiki
Post to : zim-wiki@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp


Re: [Zim-wiki] howto strip metadata header

2012-02-29 Thread hansbkk
 On Wed, Feb 29, 2012 at 4:13 PM, Jaap Karssenberg <
jaap.karssenb...@gmail.com> wrote:

> I would say there is no way to do this, as zim will put them back as soon
> as you edit a page.
>

Thanks Jaap, I understand and am not asking for any changes to zim other
than putting it out there for general discussion.

My actual request (not just to you but to fello zim users, so Jaap do feel
free to ignore this part) is how to do this on a whole tree of files at
once, independently of zim itself. The fact that zim will put it back at
next edit isn't a problem for me at this point.

I believe your export-to-pMarkdown code already does this, but in this case
I'm not looking to do any syntax conversion.

And now that that functionality is available in a published release, I'll
try to make some time and do some further testing, and try to be helpful
bringing more of the Pandoc community around to seeing how Zim can be a
front-end editor and meta-organizer for some of their doc projects.
___
Mailing list: https://launchpad.net/~zim-wiki
Post to : zim-wiki@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp


Re: [Zim-wiki] howto strip metadata header

2012-02-29 Thread hansbkk
On Wed, Feb 29, 2012 at 3:05 PM, Jaap Karssenberg <
jaap.karssenb...@gmail.com> wrote:

>
> Let's start with the obvious question: why ? Zim needs these headers to
> function properly, and plan is to use them more in the future.
>

I haven't seen anything useful for my use of Zim, it's just annoying kruft
for me so far whenever I'm dealing with the files outside of Zim.

More importantly, I have use cases where I'd like to use Zim as
transparently as possible, not messing with the contents of files produced
by or used later in the toolchain by other text processing tools.

Since I already started to address the issue in the other thread, let's
continue there.

--

In the meantime with this thread, I'd appreciate responses from anyone with
suggestions as to my actual question, which was "how" rather than "why".
___
Mailing list: https://launchpad.net/~zim-wiki
Post to : zim-wiki@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp


Re: [Zim-wiki] howto strip metadata header

2012-02-29 Thread Jaap Karssenberg
On Wed, Feb 29, 2012 at 7:57 AM,  wrote:

> I'd like to be able to strip out the four-line (including the lat blank
> one) from all files in a given filesystem branch, or an entire notebook
> data tree.
>

Let's start with the obvious question: why ?

Zim needs these headers to function properly, and plan is to use them more
in the future.

-- Jaap
___
Mailing list: https://launchpad.net/~zim-wiki
Post to : zim-wiki@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp


[Zim-wiki] howto strip metadata header

2012-02-28 Thread hansbkk
 I'd like to be able to strip out the four-line (including the lat blank
one) from all files in a given filesystem branch, or an entire notebook
data tree.

I haven't come across a way to do this within Zim, so consider this a
wishlist proposal, along with a way to disable creating these lines in the
first place, on a per-notebook basis.

I can probably figure out how to kludge something together with some text
tools (regex-based search and replace), but I'm hoping someone in the Zim
community knows of an easy, ideally "canned" multi-platform way to do this.
Note although I'm on windoze, I do have Cygwin and other gnutool
collections available to me, along with Python.

TIA. . .
___
Mailing list: https://launchpad.net/~zim-wiki
Post to : zim-wiki@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp