Skip to content

Commit

Permalink
Fix string handling in Python 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed Dec 17, 2018
1 parent 2c8b3b6 commit e980350
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 38 deletions.
15 changes: 6 additions & 9 deletions knowledge_repo/app/app.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
from __future__ import absolute_import

import multiprocessing
import time
import atexit
import os
import imp
import logging
import traceback
import math
import multiprocessing
import uuid
import mimetypes

from flask import Flask, current_app, render_template, g, request, flash, redirect, url_for, session
import six
from flask import Flask, current_app, render_template, request, session
from flask_login import LoginManager, user_loaded_from_request
from flask_mail import Mail
from flask_migrate import Migrate
from flask_principal import Principal, identity_loaded, identity_changed, Identity, RoleNeed, UserNeed, AnonymousIdentity, PermissionDenied
from flask_principal import Principal, identity_loaded, Identity, AnonymousIdentity, PermissionDenied
from alembic import command
from alembic.migration import MigrationContext
from datetime import datetime
from werkzeug import url_encode

import knowledge_repo
from . import roles, routes
from . import routes
from .auth_provider import KnowledgeAuthProvider
from .proxies import db_session, current_repo, current_user
from .index import update_index, set_up_indexing_timers, time_since_index, time_since_index_check
from .models import db as sqlalchemy_db, Post, User, Tag
from .models import db as sqlalchemy_db, User, Tag
from .utils.auth import AnonymousKnowledgeUser, populate_identity_roles, prepare_user

# Needed to serve svg with correct mime type over https
Expand Down Expand Up @@ -55,7 +52,7 @@ def __init__(self, repo, db_uri=None, debug=None, config=None, **kwargs):

# Load configuration from file or provided object
if config:
if isinstance(config, str):
if isinstance(config, six.string_types):
config = imp.load_source('knowledge_server_config', os.path.abspath(config))
self.config.from_object(config)

Expand Down
5 changes: 4 additions & 1 deletion knowledge_repo/app/auth_providers/oauth2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import posixpath
import json

import six
from flask import request, redirect
from six.moves.urllib.parse import urljoin

from ..models import User
from ..auth_provider import KnowledgeAuthProvider

Expand Down Expand Up @@ -155,7 +158,7 @@ def extract_from_dict(d, key):
key = key[0]
else:
return extract_from_dict(d[key[0]], key[1:])
if isinstance(key, str):
if isinstance(key, six.string_types):
return d[key]
raise RuntimeError("Invalid key type: {}.".format(key))

Expand Down
9 changes: 5 additions & 4 deletions knowledge_repo/app/deploy/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from abc import abstractmethod
from future.utils import with_metaclass

import six
import knowledge_repo
from knowledge_repo.utils.registry import SubclassRegisteringABCMeta

Expand All @@ -28,7 +29,7 @@ def __init__(self,
port=7000,
workers=4,
timeout=60):
assert isinstance(knowledge_builder, (str, types.FunctionType)), \
assert isinstance(knowledge_builder, six.string_types + (types.FunctionType, )), \
u"Unknown builder type {}".format(type(knowledge_builder))
self.knowledge_builder = knowledge_builder
self.host = host
Expand Down Expand Up @@ -56,7 +57,7 @@ def builder_str(self):
if isinstance(self.knowledge_builder, types.FunctionType):
out = []
for nl, cell in zip(self.knowledge_builder.__code__.co_freevars, self.knowledge_builder.__closure__):
if isinstance(cell.cell_contents, str):
if isinstance(cell.cell_contents, six.string_types):
out.append(u'{} = "{}"'.format(nl, cell.cell_contents.replace('"', '\\"')))
else:
out.append(u'{} = {}'.format(nl, cell.cell_contents))
Expand All @@ -66,7 +67,7 @@ def builder_str(self):

@property
def builder_func(self):
if isinstance(self.knowledge_builder, str):
if isinstance(self.knowledge_builder, six.string_types):
knowledge_builder = 'def get_app():\n\t' + self.knowledge_builder.replace('\n', '\t') + '\n\treturn app'
namespace = {}
exec(knowledge_builder, namespace)
Expand Down Expand Up @@ -94,7 +95,7 @@ def write_temp_files(self):
out.append('import knowledge_repo')

out.append(self.builder_str)
if not isinstance(self.knowledge_builder, str):
if not isinstance(self.knowledge_builder, six.string_types):
out.append('app = %s()' % self.knowledge_builder.__name__)
out.append('app.start_indexing()')

Expand Down
6 changes: 4 additions & 2 deletions knowledge_repo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import types
import yaml

import six

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -43,7 +45,7 @@ def update(self, *values, **kwargs):
dict.update(self, value)
elif isinstance(value, types.ModuleType):
self.__update_from_module(value)
elif type(value) == str:
elif isinstance(value, six.string_types):
if os.path.exists(value):
self.__update_from_file(value)
else:
Expand All @@ -61,7 +63,7 @@ def update_defaults(self, *values, **kwargs):
self.DEFAULT_CONFIGURATION.update(value)
elif isinstance(value, types.ModuleType):
self.__defaults_from_module(value)
elif type(value) == str:
elif isinstance(value, six.string_types):
if os.path.exists(value):
self.__defaults_from_file(value)
else:
Expand Down
2 changes: 1 addition & 1 deletion knowledge_repo/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def update_headers(self, **headers):
def thumbnail_uri(self):
thumbnail = self.headers.get('thumbnail')

if not thumbnail or not isinstance(thumbnail, str):
if not thumbnail or not isinstance(thumbnail, six.string_types):
return None

if ':' not in thumbnail: # if thumbnail points to a local reference
Expand Down
3 changes: 2 additions & 1 deletion knowledge_repo/postprocessors/extract_images.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import posixpath
import re
import six
import logging

from ..postprocessor import KnowledgePostProcessor
Expand All @@ -21,7 +22,7 @@ def update_thumbnail_uri(self, kp, images, image_mapping):
thumbnail = kp.headers.get('thumbnail', 0)

# if thumbnail is a number, select the nth image in this post as the thumbnail
if isinstance(thumbnail, str) and thumbnail.isdigit():
if isinstance(thumbnail, six.string_types) and thumbnail.isdigit():
thumbnail = int(thumbnail)
if isinstance(thumbnail, int):
if len(images) > 0:
Expand Down
11 changes: 3 additions & 8 deletions knowledge_repo/repositories/folder.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
from __future__ import print_function
from builtins import input

import os
import shutil
import logging
import re
import git
import socket
import time
from io import open

from knowledge_repo._version import __git_uri__
import six

from ..post import KnowledgePost
from ..repository import KnowledgeRepository
from ..utils.exec_code import get_module_for_source
from ..utils.types import str_types
from ..utils.encoding import encode

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -72,7 +67,7 @@ def path(self):

@path.setter
def path(self, path):
assert isinstance(path, str), "The path specified must be a string."
assert isinstance(path, six.string_types), "The path specified must be a string."
path = os.path.abspath(os.path.expanduser(path))
if not os.path.exists(path):
path = os.path.abspath(path)
Expand Down
5 changes: 2 additions & 3 deletions knowledge_repo/repositories/gitrepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
from io import open

import git
import six
import yaml

from knowledge_repo._version import __git_uri__
from ..repository import KnowledgeRepository
from ..utils.exec_code import get_module_for_source
from ..utils.types import str_types
from ..utils.encoding import encode

Expand Down Expand Up @@ -94,7 +93,7 @@ def path(self):

@path.setter
def path(self, path):
assert isinstance(path, str), "The path specified must be a string."
assert isinstance(path, six.string_types), "The path specified must be a string."
path = os.path.abspath(os.path.expanduser(path))
if not os.path.exists(path):
path = os.path.abspath(path)
Expand Down
19 changes: 10 additions & 9 deletions knowledge_repo/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import datetime
from collections import OrderedDict
from enum import Enum
import uuid

import six

from . import config_defaults
from .post import KnowledgePost
Expand Down Expand Up @@ -49,7 +50,7 @@ def for_uri(cls, uri, *args, **kwargs):
def for_uris(cls, uri):
# Import this within this method so as not to cause import resolution problems
from .repositories.meta import MetaKnowledgeRepository
if isinstance(uri, str):
if isinstance(uri, six.string_types):
uris = {'': uri}
else:
uris = uri
Expand Down Expand Up @@ -99,14 +100,14 @@ def uris(self):
# It assumes that self.uri is either a string or a dictionary mapping
# of form:
# {<mountpoint>: <KnowledgeRepositoryInstance>}
if isinstance(self.uri, str):
if isinstance(self.uri, six.string_types):
return {'': self.uri}

elif isinstance(self.uri, dict):
uri_dict = {}

def add_uris(uri_dict, uri, parent=''):
if isinstance(uri, str):
if isinstance(uri, six.string_types):
uri_dict[parent] = uri
elif isinstance(uri, dict):
for mountpoint, u in uri.items():
Expand All @@ -127,14 +128,14 @@ def revisions(self):
# This method provides a mapping from uri to revision for this repository
# and/or any nested repositories. This is most useful when checking if an
# update is required server side.
if isinstance(self.uri, str):
if isinstance(self.uri, six.string_types):
return {self.uri: self.revision}

elif isinstance(self.uri, dict):
revision_dict = {}

def add_revisions(revision_dict, uri):
if isinstance(uri, str):
if isinstance(uri, six.string_types):
revision_dict[uri] = KnowledgeRepository.for_uri(uri).revision
elif isinstance(uri, dict):
for u in uri.values():
Expand Down Expand Up @@ -189,13 +190,13 @@ def post(self, path, revision=None):
return KnowledgePost(path=path, repository=self, revision=revision or self._kp_get_revision(path))

def dir(self, prefix=None, status=None):
if prefix is None or isinstance(prefix, str):
if prefix is None or isinstance(prefix, six.string_types):
prefixes = [prefix]
else:
prefixes = prefix
assert all([prefix is None or isinstance(prefix, str) for prefix in prefixes]), "All path prefixes must be strings."
assert all([prefix is None or isinstance(prefix, six.string_types) for prefix in prefixes]), "All path prefixes must be strings."
prefixes = [prefix if prefix is None else posixpath.relpath(prefix) for prefix in prefixes]
if isinstance(status, str):
if isinstance(status, six.string_types):
if status == 'all':
status = [self.PostStatus.DRAFT, self.PostStatus.SUBMITTED, self.PostStatus.PUBLISHED, self.PostStatus.UNPUBLISHED]
else:
Expand Down

0 comments on commit e980350

Please sign in to comment.