Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
big refactoring - now it will be fun to write memacs modules ;)
Signed-off-by: Armin Wieser <armin.wieser@gmail.com>
- Loading branch information
Showing
11 changed files
with
284 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,63 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Time-stamp: <2011-10-27 15:13:31 aw> | ||
# Time-stamp: <2011-12-19 15:13:31 aw> | ||
|
||
from argparse import ArgumentParser | ||
import os | ||
|
||
EPILOG=""":copyright: (c) 2011 by Karl Voit <tools@Karl-Voit.at> | ||
:license: GPL v2 or any later version | ||
:bugreports: <tools@Karl-Voit.at> | ||
:version: """ | ||
class MemacsArgumentParser(ArgumentParser): | ||
|
||
def __init__(self,prog_version,prog_version_date,description): | ||
version="%prog v" +prog_version + " from " + prog_version_date | ||
def __init__(self,prog_version,prog_version_date,prog_description): | ||
version="%(prog)s v" +prog_version + " from " + prog_version_date | ||
|
||
ArgumentParser.__init__(self, | ||
description=description, | ||
description=prog_description, | ||
add_help=True, | ||
epilog=EPILOG + prog_version + " from "+prog_version_date + "\n") | ||
epilog=EPILOG + prog_version + " from "+prog_version_date + "\n", | ||
) | ||
|
||
self.add_argument('--version', action='version', version=version) | ||
|
||
self.add_argument("-v", "--verbose", dest="verbose", action="store_true", | ||
help="enable verbose mode") | ||
self.add_argument("-v", "--verbose", dest="verbose", action="store_true", help="enable verbose mode") | ||
|
||
self.add_argument("-s", "--suppress-messages", dest="suppressmessages", action="store_true", | ||
help="do not show any log message - helpful when -o not set") | ||
|
||
self.add_argument("-o", "--output", dest="outputfile", | ||
help="Org-mode file that will be generated (see above)." +\ | ||
"If no output file is given, result gets printed to stdout", | ||
metavar="FILE") | ||
|
||
def format_epilog(self,formatter): | ||
""" | ||
overwriting OptionParser's format_epilog for correct formatting of \n | ||
overwriting ArgParser's format_epilog for correct formatting of \n | ||
""" | ||
return "\n" + self.epilog | ||
|
||
def format_description(self,formatter): | ||
""" | ||
overwriting OptionParser's format_description for correct formatting of \n | ||
overwriting ArgParser's format_description for correct formatting of \n | ||
""" | ||
return self.description | ||
|
||
def get_version(self): | ||
return self.version | ||
return self.version | ||
|
||
def parse_args(self, args=None, namespace=None): | ||
""" | ||
overwriting ArgParser's parse_args and do checking default argument outputfile | ||
""" | ||
args = ArgumentParser.parse_args(self, args=args, namespace=namespace) | ||
if args.outputfile: | ||
if not os.path.exists(os.path.dirname(args.outputfile)): | ||
self.error("Output file path(%s) does not exist!" % args.outputfile) | ||
if os.access(args.outputfile, os.W_OK): | ||
self.error("Output file %s is not writeable!" % args.outputfile) | ||
|
||
if args.suppressmessages == True and args.verbose == True: | ||
self.error("cannot set both verbose and suppress-messages") | ||
return args | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Time-stamp: <2011-12-19 15:13:31 aw> | ||
|
||
import logging | ||
import traceback | ||
from common.argparser import MemacsArgumentParser | ||
from common.orgwriter import OrgOutputWriter | ||
from common.loggingsettings import handle_logging | ||
import sys | ||
|
||
class Memacs(object): | ||
|
||
def __init__(self | ||
, prog_version | ||
, prog_version_date | ||
, prog_description | ||
, prog_short_description | ||
, prog_tag | ||
, argv = sys.argv | ||
, write_footer=False | ||
): | ||
self.__prog_version = prog_version | ||
self.__prog_version_date = prog_version_date | ||
self.__prog_description = prog_description | ||
self.__prog_short_description = prog_short_description | ||
self.__prog_tag = prog_tag | ||
self.__write_footer = write_footer | ||
self.__argv = argv | ||
def __init(self): | ||
self._parser = MemacsArgumentParser(prog_version=self.__prog_version, | ||
prog_version_date=self.__prog_version_date, | ||
prog_description=self.__prog_description, | ||
) | ||
# add additional arguments | ||
self._parser_add_arguments() | ||
# parse all arguments | ||
self._parser_parse_args() | ||
# set logging configuration | ||
handle_logging(self._args.verbose, self._args.suppressmessages, self._args.outputfile) | ||
|
||
logging.debug("args specified:") | ||
logging.debug(self._args) | ||
|
||
self._writer = OrgOutputWriter(file_name=self._args.outputfile, | ||
short_description=self.__prog_short_description, | ||
tag=self.__prog_tag); | ||
def __main(self): | ||
pass | ||
|
||
def _parser_add_arguments(self): | ||
""" | ||
does nothing in super class, | ||
In subclass we add arguments to the parser | ||
""" | ||
pass | ||
|
||
def _parser_parse_args(self): | ||
""" | ||
In subclass we do additional parsing on arguments | ||
""" | ||
self._args = self._parser.parse_args(self.__argv[1:]) | ||
|
||
#__parser_add_arguments = _parser_add_arguments | ||
#__parser_parse_args = parser_parse_args | ||
|
||
def handle_main(self): | ||
try: | ||
self.__init() | ||
self._main() | ||
self._writer.close(self.__write_footer) | ||
except KeyboardInterrupt: | ||
logging.info("Received KeyboardInterrupt") | ||
except SystemExit: | ||
# if we get an sys.exit() do exit! | ||
pass | ||
except: | ||
error_lines = traceback.format_exc().splitlines() | ||
logging.error("\n ".join(map(str, error_lines))) | ||
raise # re raise exception |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Time-stamp: <2011-11-02 15:13:31 aw> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import unittest | ||
import sys, os | ||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) | ||
from common.argparser import MemacsArgumentParser | ||
|
||
class TestArgParser(unittest.TestCase): | ||
def setUp(self): | ||
self.prog_version = "0.1" | ||
self.prog_version_date = "2011-12-19" | ||
self.description = "descriptionbla" | ||
self.parser = MemacsArgumentParser( | ||
prog_version=self.prog_version, | ||
description=self.description, | ||
prog_version_date=self.prog_version_date) | ||
self.TMPFOLDER = os.path.normpath(os.path.dirname(os.path.abspath(__file__)) | ||
+ os.path.sep + "tmp") + os.sep | ||
if not os.path.exists(self.TMPFOLDER): | ||
os.makedirs(self.TMPFOLDER) | ||
def test_verbose(self): | ||
""" | ||
testing MemacsArgumentParser's argument verbose | ||
""" | ||
args = self.parser.parse_args('-v'.split()) | ||
args2 = self.parser.parse_args('--verbose'.split()) | ||
|
||
self.assertEqual(args, args2, "-v and --verbose do different things") | ||
self.assertEqual(args.outputfile, None, "verbose - args.outputfile should be None") | ||
self.assertEqual(args.suppressmessages, False, "verbose - args.suppressmessages should be False") | ||
self.assertEqual(args.verbose, True, "verbose - args.verbose should be True") | ||
|
||
def test_suppress(self): | ||
""" | ||
testing MemacsArgumentParser's suppress-messages | ||
""" | ||
args = self.parser.parse_args('-s'.split()) | ||
args2 = self.parser.parse_args('--suppress-messages'.split()) | ||
|
||
self.assertEqual(args, args2, "-s and --suppress-messages do different things") | ||
self.assertEqual(args.outputfile, None, "suppressmessages - args.outputfile should be None") | ||
self.assertEqual(args.suppressmessages, True, "suppressmessages - args.suppressmessages should be True") | ||
self.assertEqual(args.verbose, False, "suppressmessages - args.verbose should be False") | ||
|
||
|
||
def test_outputfile(self): | ||
#args = self.parser.parse_args('-o'.split()) | ||
outputfile_path = self.TMPFOLDER + "outputfile" | ||
outputfile_argument = "-o " + outputfile_path | ||
outputfile_argument2 = "--output " + outputfile_path | ||
args = self.parser.parse_args(outputfile_argument.split()) | ||
args2 = self.parser.parse_args(outputfile_argument2.split()) | ||
self.assertEqual(args, args2, "-o and --output do different things") | ||
|
||
def test_nonexistingoutputdir(self): | ||
outputfile_path = self.TMPFOLDER + "NONEXISTING" + os.sep + "outputfile" | ||
outputfile_argument = "-o " + outputfile_path | ||
|
||
try: | ||
args = self.parser.parse_args(outputfile_argument.split()) | ||
self.assertTrue(False, "parsing was correct altough nonexisting outputfile") | ||
except SystemExit: | ||
pass | ||
|
||
def test_verbose_suppress_both(self): | ||
try: | ||
args = self.parser.parse_args('-s -v'.split()) | ||
self.assertTrue(False, "parsing was correct altough both suppress and verbose was specified") | ||
except SystemExit: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import re | ||
import os | ||
from common.memacs import Memacs | ||
from common.orgformat import OrgFormat | ||
import logging | ||
import time | ||
|
||
DATESTAMP_REGEX = re.compile("([12]\d{3})-([01]\d)-([0123]\d)") | ||
TIMESTAMP_REGEX = re.compile("([12]\d{3})-([01]\d)-([0123]\d)T([012]\d)[.]([012345]\d)([.]([012345]\d))?") | ||
|
||
class FileNameTimeStamps(Memacs): | ||
|
||
def _parser_add_arguments(self): | ||
Memacs._parser_add_arguments(self) | ||
|
||
self._parser.add_argument("-f", "--folder", dest="filenametimestamps_folder", action="append", \ | ||
help="path to a folder to search for filenametimestamps, " + | ||
"multiple folders can be specified: -f /path1 -f /path2") | ||
|
||
self._parser.add_argument("-x", "--exclude", dest="exclude_folder", | ||
help="path to excluding folder, for more excludes use this: -x /path/exclude -x /path/exclude") | ||
|
||
self._parser.add_argument("-l", "--follow-links", dest="follow_links", action="store_true", | ||
help="follow symbolics links, default False") | ||
|
||
def _parser_parse_args(self): | ||
Memacs._parser_parse_args(self) | ||
if not self._args.filenametimestamps_folder: | ||
self._parser.error("no filenametimestamps_folder specified") | ||
|
||
for f in self._args.filenametimestamps_folder: | ||
if not os.path.isdir(f): | ||
self._parser.error("Check the folderlist - one or more aren't folders") | ||
|
||
def _main(self): | ||
print "main called" | ||
for folder in self._args.filenametimestamps_folder: | ||
for rootdir, dirs, files in os.walk(folder, followlinks=self._args.follow_links): | ||
if self._args.exclude_folder and rootdir in self._args.exclude_folder: | ||
logging.info("ignoring dir: " + rootdir) | ||
else: | ||
for file in files: | ||
if DATESTAMP_REGEX.match(file) and file[-1:] != '~': # don't handle emacs tmp files (file~) | ||
link = rootdir + os.sep + file | ||
logging.debug(link) | ||
if TIMESTAMP_REGEX.match(file): | ||
# if we found a timestamp too,take hours,minutes and optionally seconds from this timestamp | ||
orgdate = OrgFormat.strdatetimeiso8601(TIMESTAMP_REGEX.match(file).group()) | ||
logging.debug("found timestamp: " + orgdate) | ||
else: | ||
orgdate = OrgFormat.strdate(DATESTAMP_REGEX.match(file).group()) | ||
orgdate_time_tupel = OrgFormat.datetupeliso8601(DATESTAMP_REGEX.match(file).group()) | ||
file_datetime = time.localtime(os.path.getmtime(link)) | ||
# check if the file - time information matches year,month,day , then update time | ||
if file_datetime.tm_year == orgdate_time_tupel.tm_year and \ | ||
file_datetime.tm_mon == orgdate_time_tupel.tm_mon and \ | ||
file_datetime.tm_mday == orgdate_time_tupel.tm_mday: | ||
logging.debug("found a time in file. setting time from %s to %s", orgdate, OrgFormat.date(file_datetime, True)) | ||
orgdate = OrgFormat.date(file_datetime, True) | ||
# write entry to org file | ||
self._writer.write_org_subitem(orgdate + " " + OrgFormat.link(link=link, description=file)) | ||
|
Oops, something went wrong.