Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 01792a2eb8
Fetching contributors…

Cannot retrieve contributors at this time

executable file 164 lines (129 sloc) 4.882 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#!/usr/bin/env python

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>
#
# Author: Francois Boulogne <fboulogne at sciunto dot org>, 2012

#System...
import sys
import os
import getopt

import logging


#our lib...
from ZimArchivist import zimnotes
from ZimArchivist import utils
from ZimArchivist import timechecker
from ZimArchivist import archive
from ZimArchivist import processtext
from ZimArchivist.timechecker import TimeChecker



#############
# Main
#############

def usage():
    """ Print usage... """
    usage = """
zimarchivist --cache -d ~/Notes

Actions:
--cache: make a cache
Arg:
-d: Zim notes directory
Option:
-f: Zim file path (Otherwise, the notebook is processed)


--clean: clean the cache by removing unnecessary archives
Arg:
-d: Zim notes directory

Other options:
--no-timecheck: Do not check if zim file has been modified
since the last time.
"""

    print(usage)


if __name__ == '__main__':
    try:
        os.makedirs(os.path.expanduser('~/.zimarchivist'), exist_ok=True)
    except:
        print('Impossible to create ~/.zimarchivist/, Exiting...')
        sys.exit(2)

    lock_file = "~/.zimarchivist/zimarchivist.lock"

    utils.create_pidfile(lock_file)

    log_filename = os.path.expanduser('~/.zimarchivist/zimarchivist.log')
    logging.basicConfig(filename=log_filename, filemode='w', level=logging.DEBUG)

    
    zim_root = None
    zim_archive_path = None
    zim_file_path = None
    
    try:
        opts, args = getopt.getopt(sys.argv[1:],"hd:f:", ['help', 'clean', 'cache'])
    except getopt.GetoptError:
        logging.critical('Wrong option')
        usage()
        sys.exit(2)

        
    action_make_archive = False
    action_clean_archive = False
    checktime = True
        
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            logging.debug("Option -h")
            usage()
            sys.exit(0)
        elif opt in '--log':
            pass
            #TODO
            #logging.basicConfig(filename=log_filename, filemode='w', level=logging.DEBUG)
        elif opt in '--clean':
            logging.debug("Option --clean")
            action_clean_archive = True
        elif opt in '--cache':
            logging.debug("Option --cache")
            action_make_archive = True
        elif opt in '--no-timecheck':
            logging.debug("Option --no-timecheck")
            checktime = False
        elif opt in '-d':
            logging.debug("Option -d: " + str(arg))
            zim_root = os.path.realpath(arg)
        elif opt in '-f':
            logging.debug("Option -f: " + str(arg))
            zim_file_path = os.path.realpath(arg)

    if action_clean_archive or action_make_archive:
        #Check if we know paths...
        if zim_root == None:
            logging.critical('Missing Notebook filepath. Exiting')
            usage()
            sys.exit(2)
        zim_archive_path = os.path.join(zim_root, '.Archive')
    else:
        usage()
        sys.exit(2)

    if action_make_archive:
        #Create the .Archive file
        if not (os.path.isdir(zim_archive_path)):
            try:
                os.mkdir(zim_archive_path)
            except OSError:
                logging.critical('could not make ', zim_archive_path)
                sys.exit(1)
            except IOError:
                logging.critical('could not make ', zim_archive_path)
                sys.exit(1)
        
        
        if zim_file_path == None:
            zim_files = zimnotes.get_zim_files(zim_root)
        else:
            zim_files = [zim_file_path]

        logging.info('Processing zim files')

        timechecker = TimeChecker('~/.zimarchivist/time.db', zim_root)
        #remove zimroot
        #zimnotes.process_zim_file(timechecker, zim_root, zim_files, process_text, checktime, 1, '/tmp' )
        zimnotes.process_zim_file(timechecker, zim_root, zim_files, processtext.process_text, checktime, 3 , zim_archive_path)


    if action_clean_archive:
        logging.info('Cache cleaning')
        zim_files = zimnotes.get_zim_files(zim_root)
        archive.clean_archive(zim_files, zim_archive_path)


    utils.release_pidfile(lock_file)
Something went wrong with that request. Please try again.