Skip to content

Commit

Permalink
Merge pull request #1014 from cscutcher/bugfix/filetime_from_git_and_…
Browse files Browse the repository at this point in the history
…permalinks_python3

permalinks and filetime_from_git: Py3 compatibility fixes. Fixes #1011
  • Loading branch information
justinmayer committed Apr 16, 2018
2 parents 78fa1ec + ffc01ec commit 8d96866
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
26 changes: 19 additions & 7 deletions filetime_from_git/actions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
import base64
import hashlib
import os
import logging
import os

from pelican.utils import strftime
from .utils import string_to_bool
from .utils import datetime_from_timestamp

from .registration import content_git_object_init
from .utils import datetime_from_timestamp
from .utils import string_to_bool


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -79,6 +81,14 @@ def git_sha_metadata(content, git_content):
content.metadata['gitsha_oldest'] = str(git_content.get_oldest_commit())


def update_hash_from_str(hsh, str_input):
"""
Convert a str to object supporting buffer API and update a hash with it.
"""
byte_input = str(str_input).encode("UTF-8")
hsh.update(byte_input)


@content_git_object_init.connect
def git_permalink(content, git_content):
'''
Expand All @@ -95,14 +105,16 @@ def git_permalink(content, git_content):
return

permalink_hash = hashlib.sha1()
permalink_hash.update(str(git_content.get_oldest_commit()))
permalink_hash.update(str(git_content.get_oldest_filename()))
git_permalink_id = base64.urlsafe_b64encode(permalink_hash.digest())
update_hash_from_str(permalink_hash, git_content.get_oldest_commit())
update_hash_from_str(permalink_hash, git_content.get_oldest_filename())
git_permalink_id_raw = base64.urlsafe_b64encode(permalink_hash.digest())
git_permalink_id = git_permalink_id_raw.decode("UTF-8")
permalink_id_metadata_key = content.settings['PERMALINK_ID_METADATA_KEY']

if permalink_id_metadata_key in content.metadata:
content.metadata[permalink_id_metadata_key] = (
','.join((
content.metadata[permalink_id_metadata_key], git_permalink_id)))
content.metadata[permalink_id_metadata_key], git_permalink_id))
)
else:
content.metadata[permalink_id_metadata_key] = git_permalink_id
2 changes: 1 addition & 1 deletion filetime_from_git/content_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_oldest_filename(self):
'''
commit_and_name_iter = self.git.get_commits_and_names_iter(
self.content.source_path)
_commit, name = commit_and_name_iter.next()
_commit, name = next(commit_and_name_iter)
return name

@memoized
Expand Down
8 changes: 5 additions & 3 deletions filetime_from_git/git_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ def _get_commits(self, path):
:returns: Sequence of commit objects. Newest to oldest
.. NOTE ::
If this fails it could be that your gitpython version is out of sync with the git
binary on your distro. Make sure you use the correct gitpython version.
If this fails it could be that your gitpython version is out of
sync with the git binary on your distro.
Make sure you use the correct gitpython version.
Alternatively enabling GIT_FILETIME_FOLLOW may also make your problem go away.
Alternatively enabling GIT_FILETIME_FOLLOW may also make your
problem go away.
'''
return list(self.repo.iter_commits(paths=path))

Expand Down
25 changes: 16 additions & 9 deletions permalinks/permalinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
This plugin enables a kind of permalink which can be used to refer to a piece
of content which is resistant to the file being moved or renamed.
"""
import logging
import itertools
import logging
import os
import os.path

from pelican import signals
from pelican.generators import Generator
from pelican.utils import mkdir_p
from pelican.utils import clean_output_dir
from pelican.utils import mkdir_p

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -53,7 +54,8 @@ def generate_context(self):
'''
self.permalink_output_path = os.path.join(
self.output_path, self.settings['PERMALINK_PATH'])
self.permalink_id_metadata_key = self.settings['PERMALINK_ID_METADATA_KEY']
self.permalink_id_metadata_key = (
self.settings['PERMALINK_ID_METADATA_KEY'])

def generate_output(self, writer=None):
'''
Expand All @@ -79,26 +81,29 @@ def generate_output(self, writer=None):

def get_permalink_ids_iter(self):
'''
Method to get permalink ids from content. To be bound to the class last thing
Method to get permalink ids from content. To be bound to the class last
thing.
'''
permalink_id_key = self.settings['PERMALINK_ID_METADATA_KEY']
permalink_ids_raw = self.metadata.get(permalink_id_key, '')
permalink_ids = self.metadata.get(permalink_id_key, '')

for permalink_id in permalink_ids_raw.split(','):
for permalink_id in permalink_ids.split(','):
if permalink_id:
yield permalink_id.strip()


def get_permalink_ids(self):
'''
Method to get permalink ids from content. To be bound to the class last thing
Method to get permalink ids from content. To be bound to the class last
thing.
'''
return list(self.get_permalink_ids_iter())


def get_permalink_path(self):
"""Get just path component of permalink."""
try:
first_permalink_id = self.get_permalink_ids_iter().next()
first_permalink_id = next(self.get_permalink_ids_iter())
except StopIteration:
return None

Expand Down Expand Up @@ -131,12 +136,14 @@ def add_permalink_methods(content_inst):
permalink_method.__name__,
permalink_method.__get__(content_inst, content_inst.__class__))


def add_permalink_option_defaults(pelicon_inst):
'''
Add perlican defaults
'''
pelicon_inst.settings.setdefault('PERMALINK_PATH', 'permalinks')
pelicon_inst.settings.setdefault('PERMALINK_ID_METADATA_KEY', 'permalink_id')
pelicon_inst.settings.setdefault(
'PERMALINK_ID_METADATA_KEY', 'permalink_id')


def get_generators(_pelican_object):
Expand Down

0 comments on commit 8d96866

Please sign in to comment.