예제 #1
0
파일: tagging.py 프로젝트: preom/luwak
    def get_fnames_from_tag(self, tagName):
        """ Get filenames with a tag matching tagName. """
        dbManager = DatabaseManager(self.settings)
        conn, cursor = dbManager.get_conn()

        sqlString = 'Select tags.filename, title from tags left join meta on tags.filename=meta.filename where tags.tag="{}"'.format(tagName)

        cursor.execute(sqlString)

        return cursor.fetchall()
예제 #2
0
파일: tagging.py 프로젝트: preom/luwak
    def get_tags(self, fname=None):
        """ Get a list of all the tags in the database as a row object. 

            Attributes:
                fname: Standardized form of the filename stored in the database.

            Returns: 
                [str]: list of tags.

        """
        dbManager = DatabaseManager(self.settings)
        conn, cursor = dbManager.get_conn()

        sqlString = 'Select distinct tag from tags order by tag'

        cursor.execute(sqlString)
        return cursor.fetchall()
예제 #3
0
파일: luwak_utils.py 프로젝트: preom/luwak
def process_generate(*args, **kwargs):
    startTime = time.time()

    CONFIG_FILENAME = Settings.CONFIG_FILENAME

    params = vars(args[0])

    if 'project_path' not in params:
        params['project_path'] = os.getcwd()

    proj_settings = GenerationComponent.load_project_file(params['project_path'])

    iterativeBuidler = IterativeBuilder(proj_settings)
    dbManager = DatabaseManager(proj_settings)

    content_loader = ContentLoader(proj_settings)
    contentReader = ContentReader(proj_settings)
    templater = TemplateCombinator(proj_settings)
    contentWriter = ContentWriter(proj_settings)

    contentFiles = content_loader.get_content_paths()

    if params['do_flush']:
        conn = sqlite3.connect(dbManager.dbFilePath)
        cursor = conn.cursor()
        cursor.execute('delete from records')
        cursor.execute('delete from meta')
        conn.commit()

    contentFiles = iterativeBuidler.content_filter(contentFiles)

    postList = []
    metaList = []

    conn = sqlite3.connect(dbManager.dbFilePath)
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()

    toUpdateList = [] #TODO change name from toUpdateList
    sourceDir = os.path.join(proj_settings['project_path'], proj_settings['source_dir'])
    for fpath in contentFiles:
        relativeFname = os.path.relpath(fpath, sourceDir) 
        cursor.execute('select * from meta where filename=?', (relativeFname,))
        row = cursor.fetchone()
        meta = contentReader.generate_meta(fpath)
        meta['modtime'] = os.path.getmtime(fpath)

        category =  meta.get('category', [None])[0]

        if row:
            if 'category' in row.keys():
                storedCategory = row['category']
            else:
                storedCategory = None

            if storedCategory != category:
                toUpdateList.append((relativeFname, meta, 'update')), 

        else:
            toUpdateList.append((relativeFname, meta, 'add'))


    # Set meta data
    for relativeFname, meta, status in toUpdateList:
        category = meta.get('category', [None])[0]
        title = meta['title'][0]

        try: 
            metaDate = datetime.datetime.strptime(meta['date'][0], '%Y-%m-%d')
        except ValueError:
            print '>>--------------------------------------------------------<<'
            print '>>----------------------- Error --------------------------<<'
            print '>>--------------------------------------------------------<<'
            print ''
            print "File: ", relativeFname
            print "Mandatory meta date could not be parsed"
            print "Make sure that it is in the correct format."
            print "Either `YYYY-MM-DD` (e.g. 2015-07-04)"
            #print "Or `MONTHNAME MM, YYYY` (e.g. July 04, 2015)"
            print ""
            print "Found: ", meta['date']
            print ""
            print '>>--------------------------------------------------------<<'
            print ""
            raise

        dbManager.update_tags(relativeFname, meta.get('tags', []))

        # Important: date value in db must be unique for article navigation linking to work (correct row number); 
        # content file doesn't specify hour, minute, etc. therefore combined with last modifed
        date = datetime.datetime.fromtimestamp(meta['modtime'])
        date = date.replace(year=metaDate.year, month=metaDate.month, day=metaDate.day)

        if status == 'add':
            cursor.execute('insert into meta (filename, category, title, created) values(?, ?, ?, ?)', (relativeFname, category, title, date))
            conn.commit()

            cursor.execute('select count(*) from meta where category=? and title<?', (category, title))
            rowCount = cursor.fetchone()[0]

            # SKIP NULL VALUES:
            if category is None:
                continue

            if rowCount <= 0:
                cursor.execute('select * from meta where category=? order by category, title limit 2 offset ?', (category, rowCount))
                results = cursor.fetchall()
                prev = None
                current = results[0]

                if len(results) > 1:
                    next = results[1]
                else:
                    next = None

            else:
                cursor.execute('select * from meta where category=? order by category, title limit 3 offset ?', (category, rowCount - 1))
                results = cursor.fetchall()
                while (len(results) < 3):
                    results.append(None)

                prev, current, next = results 

            # Link Next
            if next:
                cursorArgs = (next['filename'], current['filename'])
                cursor.execute('update meta set nextFilename=? where filename=?', cursorArgs)

                cursorArgs = (current['filename'], next['filename'])
                cursor.execute('update meta set prevFilename=? where filename=?', cursorArgs)

            # Link previous
            if prev:
                cursorArgs = (prev['filename'], current['filename'])
                cursor.execute('update meta set prevFilename=? where filename=?', cursorArgs)

                cursorArgs = (current['filename'], prev['filename'])
                cursor.execute('update meta set nextFilename=? where filename=?', cursorArgs)

            conn.commit()

    # Write content
    for fpath in contentFiles:
        fname = os.path.basename(fpath)
        htmlContent = contentReader.generate_html(fpath)
        metaContent = contentReader.generate_meta(fpath)

        relativeFname = os.path.relpath(fpath, sourceDir) 
        cursor.execute('select * from meta where filename=?', (relativeFname,))
        row = cursor.fetchone()
        # transform from source filename to output filename
        if row['prevFilename']:
            metaContent['prev'] = [contentWriter.generate_name(row['prevFilename'])]
        if row['nextFilename']:
            metaContent['next'] = [contentWriter.generate_name(row['nextFilename'])]

        html = templater.combine_html_wrapper(htmlContent, fname=fname, meta=metaContent)
        href = contentWriter.output(html, fname)
        postList.append((metaContent['title'][0], href))

    # Generate tag pages
    tagGenerator = CategoryGenerator(proj_settings)
    tags = [t['tag'] for t in tagGenerator.get_tags()]
    tags = [(t, '/tags/{}.html'.format(t)) for t in tags]
    ctx = {'items': tags, 'title': 'Tags'}
    print tags
    html = templater.combine(ctx, templateType="list")
    contentWriter.output_specific(html, 'index.html', 'tags')
    for tag, tagLink in tags:
        articles = []
        for row in tagGenerator.get_fnames_from_tag(tag):
            fname = os.path.basename(row['filename'])
            fname = '/' + contentWriter.generate_name(fname)
            articles.append((row['title'], fname))

        ctx = {'items': articles, 'title': tag}
        html = templater.combine(ctx, templateType="list")

        contentWriter.output_specific(html, '{}.html'.format(tag), 'tags')

    # Generate Index pages
    pgDSource = PaginationDbDataSource(dbManager.dbFilePath)
    paginator = Paginator(pgDSource)
    for pageInfo in paginator.get_generator():
        index_html = templater.combine_index_wrapper(pageInfo)
        contentWriter.output(index_html, pageInfo['currentPage'][1])

    elapsedTime = time.time() - startTime
    print ">> -- Time: {:.4f} -- <<".format(elapsedTime)