Skip to content

Added python 3 compatibility #553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:2
FROM python:3
EXPOSE 5000

WORKDIR /usr/src/app
Expand Down
2 changes: 1 addition & 1 deletion cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import inch
import StringIO
import io


def create_cert(filename, name, course_name, date, logo_loc):
Expand Down
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def main(args: Array[String]) {
}

# this will run once
for domain, v in DOMAIN_DATA.iteritems():
for domain, v in list(DOMAIN_DATA.items()):
v["namespace"] = domain
v["full_url"] = "https://www." + v["namespace"]
v["contact_email"] = "admin@" + v["namespace"]
Expand Down
4 changes: 2 additions & 2 deletions courses/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

for l in d:
t = f[:-4]
l = {x[0].lower().replace(" ", "_"): x[1] for x in l.items()}
l = {x[0].lower().replace(" ", "_"): x[1] for x in list(l.items())}
l["retail_price"] = float(l["retail_price"])

link = re.findall(r'<a href=\"(.*?)\">', l["link_code"])[0]
Expand All @@ -26,7 +26,7 @@
data[t].append(l)

encoded = json.dumps(data, sort_keys=True, indent=4)
print encoded
print(encoded)
with open("../courses.json", "w") as e:
e.write(encoded)

8 changes: 4 additions & 4 deletions ideone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,21 @@ def _translate_language_name(self, language_name):

# Check for exact match first including the whole version
# string
for ideone_index, ideone_language in languages.items():
for ideone_index, ideone_language in list(languages.items()):
if ideone_language.lower() == language_name.lower():
return ideone_index

# Check for a match of just the language name without any
# version information
simple_languages = dict((k,v.split('(')[0].strip())
for (k,v) in languages.items())
for ideone_index, simple_name in simple_languages.items():
for (k,v) in list(languages.items()))
for ideone_index, simple_name in list(simple_languages.items()):
if simple_name.lower() == language_name.lower():
return ideone_index

# Give up, but first find a similar name, suggest it and error
# out
language_choices = languages.values() + simple_languages.values()
language_choices = list(languages.values()) + list(simple_languages.values())
similar_choices = difflib.get_close_matches(language_name,
language_choices,
n=3,
Expand Down
34 changes: 16 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
import markdown
import os
import cgi
import urllib
import urllib.request, urllib.parse, urllib.error
import time
import functools
import logging
import binascii

from flask import Flask, render_template, request, make_response, session, Response
import sys
if sys.version_info.major < 3:
reload(sys)
sys.setdefaultencoding('utf8')


from ideone import Ideone

Expand All @@ -40,7 +38,7 @@
help="Default domain when running in development mode",
default=current_domain,
required=True,
choices=constants.DOMAIN_DATA.keys()
choices=list(constants.DOMAIN_DATA.keys())
)

parser.add_argument("-p", "--port", help="port to listen to", default=5000, type=int)
Expand Down Expand Up @@ -100,7 +98,7 @@ def pageurl(value, language):
if value.startswith("http"):
return value
else:
return urllib.quote("/%s/%s" % (language, value.replace(' ', '_')))
return urllib.parse.quote("/%s/%s" % (language, value.replace(' ', '_')))


def _wikify_one(language, pat):
Expand All @@ -125,7 +123,7 @@ def _wikify_one(language, pat):

def wikify(text, language):
text, count = WIKI_WORD_PATTERN.subn(functools.partial(_wikify_one, language), text)
return markdown.markdown(text.decode("utf-8")).strip()
return markdown.markdown(text).strip()


def untab(text):
Expand All @@ -149,7 +147,7 @@ def init_tutorials():
continue

if domain not in constants.DOMAIN_DATA:
logging.warn("skipping domain %s beacause no domain data exists" % domain)
logging.warning("skipping domain %s beacause no domain data exists" % domain)
continue

for language in os.listdir(os.path.join(os.path.dirname(__file__), "tutorials", domain)):
Expand Down Expand Up @@ -188,15 +186,15 @@ def init_tutorials():
tutorial_sections = sections.findall(tutorial_dict["text"])
if tutorial_sections:
text, code, output, solution = tutorial_sections[0]
tutorial_dict["page_title"] = tutorial.decode("utf8")
tutorial_dict["page_title"] = tutorial
tutorial_dict["text"] = wikify(text, language)
tutorial_dict["code"] = untab(code)
tutorial_dict["output"] = untab(output)
tutorial_dict["solution"] = untab(solution)
tutorial_dict["is_tutorial"] = True
else:
if tutorial_file != "Welcome.md":
logging.warn("File %s/%s/%s is not a tutorial", domain, language, tutorial_file)
logging.warning("File %s/%s/%s is not a tutorial", domain, language, tutorial_file)
tutorial_dict["page_title"] = ""
tutorial_dict["text"] = wikify(tutorial_dict["text"], language)
tutorial_dict["code"] = constants.DOMAIN_DATA[domain]["default_code"]
Expand All @@ -205,24 +203,24 @@ def init_tutorials():
for link in links:
if not link in tutorial_data[domain][language]:
tutorial_data[domain][language][link] = {
"page_title" : link.decode("utf8"),
"page_title" : link,
"text": contributing_tutorials,
"code": ""
}

if not "back_chapter" in tutorial_data[domain][language][link]:
tutorial_data[domain][language][link]["back_chapter"] = tutorial.decode("utf-8").replace(" ", "_")
tutorial_data[domain][language][link]["back_chapter"] = tutorial.replace(" ", "_")
elif not link.startswith("http"):
logging.info("Warning! duplicate links to tutorial %s from tutorial %s/%s", link, language, tutorial)

num_links = len(links)
page_index = links.index(link)
if page_index > 0:
if not "previous_chapter" in tutorial_data[domain][language][link]:
tutorial_data[domain][language][link]["previous_chapter"] = links[page_index - 1].decode("utf-8").replace(" ", "_")
tutorial_data[domain][language][link]["previous_chapter"] = links[page_index - 1].replace(" ", "_")
if page_index < (num_links - 1):
if not "next_chapter" in tutorial_data[domain][language][link]:
tutorial_data[domain][language][link]["next_chapter"] = links[page_index + 1].decode("utf-8").replace(" ", "_")
tutorial_data[domain][language][link]["next_chapter"] = links[page_index + 1].replace(" ", "_")


init_tutorials()
Expand Down Expand Up @@ -352,7 +350,7 @@ def progress(language):
@app.route("/<title>", methods=["GET", "POST"])
@app.route("/<language>/<title>", methods=["GET", "POST"])
def index(title, language="en"):
tutorial = title.replace("_", " ").encode("utf-8")
tutorial = title.replace("_", " ")
try:
current_tutorial_data = get_tutorial(tutorial, language)
except KeyError:
Expand All @@ -365,13 +363,13 @@ def index(title, language="en"):
html_title = "%s - %s" % (title.replace("_", " "), title_suffix) if title != "Welcome" else title_suffix

if not "uid" in session:
session["uid"] = os.urandom(16).encode("hex")
session["uid"] = binascii.b2a_hex(os.urandom(16))

uid = session["uid"]

try:
site_links = tutorial_data[get_host()][language]["Welcome"]["links"]
except Exception, e:
except Exception as e:
site_links = []
logging.error("cant get site links for %s %s" % (get_host(), language))

Expand Down
60 changes: 30 additions & 30 deletions markdown/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
DOC_TAG = "div" # Element used to wrap document - later removed

# Placeholders
STX = u'\u0002' # Use STX ("Start of text") for start-of-placeholder
ETX = u'\u0003' # Use ETX ("End of text") for end-of-placeholder
STX = '\u0002' # Use STX ("Start of text") for start-of-placeholder
ETX = '\u0003' # Use ETX ("End of text") for end-of-placeholder
INLINE_PLACEHOLDER_PREFIX = STX+"klzzwxh:"
INLINE_PLACEHOLDER = INLINE_PLACEHOLDER_PREFIX + "%s" + ETX
AMP_SUBSTITUTE = STX+"amp"+ETX
Expand All @@ -86,11 +86,11 @@
-----------------------------------------------------------------------------
"""

RTL_BIDI_RANGES = ( (u'\u0590', u'\u07FF'),
RTL_BIDI_RANGES = ( ('\u0590', '\u07FF'),
# Hebrew (0590-05FF), Arabic (0600-06FF),
# Syriac (0700-074F), Arabic supplement (0750-077F),
# Thaana (0780-07BF), Nko (07C0-07FF).
(u'\u2D30', u'\u2D7F'), # Tifinagh
('\u2D30', '\u2D7F'), # Tifinagh
)


Expand All @@ -109,7 +109,7 @@ def message(level, text):
if level > WARN:
sys.exit(0)
elif level > WARN:
raise MarkdownException, text
raise MarkdownException(text)
else:
warnings.warn(text, MarkdownWarning)

Expand All @@ -123,7 +123,7 @@ def isBlockLevel(tag):
=============================================================================
"""

class AtomicString(unicode):
class AtomicString(str):
"""A string which should not be further processed."""
pass

Expand Down Expand Up @@ -158,22 +158,22 @@ class MarkdownWarning(Warning):

"""

import preprocessors
import blockprocessors
import treeprocessors
import inlinepatterns
import postprocessors
import blockparser
import etree_loader
import odict
from . import preprocessors
from . import blockprocessors
from . import treeprocessors
from . import inlinepatterns
from . import postprocessors
from . import blockparser
from . import etree_loader
from . import odict

# Extensions should use "markdown.etree" instead of "etree" (or do `from
# markdown import etree`). Do not import it by yourself.

etree = etree_loader.importETree()

# Adds the ability to output html4
import html4
from . import html4


class Markdown:
Expand Down Expand Up @@ -325,12 +325,12 @@ def registerExtensions(self, extensions, configs):

"""
for ext in extensions:
if isinstance(ext, basestring):
if isinstance(ext, str):
ext = load_extension(ext, configs.get(ext, []))
if isinstance(ext, Extension):
try:
ext.extendMarkdown(self, globals())
except NotImplementedError, e:
except NotImplementedError as e:
message(ERROR, e)
else:
message(ERROR, 'Extension "%s.%s" must be of type: "markdown.Extension".' \
Expand All @@ -356,7 +356,7 @@ def set_output_format(self, format):
self.serializer = self.output_formats[format.lower()]
except KeyError:
message(CRITICAL, 'Invalid Output Format: "%s". Use one of %s.' \
% (format, self.output_formats.keys()))
% (format, list(self.output_formats.keys())))

def convert(self, source):
"""
Expand All @@ -370,12 +370,12 @@ def convert(self, source):

# Fixup the source text
if not source.strip():
return u"" # a blank unicode string
return "" # a blank unicode string
try:
source = unicode(source)
source = str(source)
except UnicodeDecodeError:
message(CRITICAL, 'UnicodeDecodeError: Markdown only accepts unicode or ascii input.')
return u""
return ""

source = source.replace(STX, "").replace(ETX, "")
source = source.replace("\r\n", "\n").replace("\r", "\n") + "\n\n"
Expand All @@ -384,14 +384,14 @@ def convert(self, source):

# Split into lines and run the line preprocessors.
self.lines = source.split("\n")
for prep in self.preprocessors.values():
for prep in list(self.preprocessors.values()):
self.lines = prep.run(self.lines)

# Parse the high-level elements.
root = self.parser.parseDocument(self.lines).getroot()

# Run the tree-processors
for treeprocessor in self.treeprocessors.values():
for treeprocessor in list(self.treeprocessors.values()):
newRoot = treeprocessor.run(root)
if newRoot:
root = newRoot
Expand All @@ -412,7 +412,7 @@ def convert(self, source):
message(CRITICAL, 'Failed to strip top level tags.')

# Run the text post-processors
for pp in self.postprocessors.values():
for pp in list(self.postprocessors.values()):
output = pp.run(output)

return output.strip()
Expand Down Expand Up @@ -443,13 +443,13 @@ def convertFile(self, input=None, output=None, encoding=None):
input_file = codecs.open(input, mode="r", encoding=encoding)
text = input_file.read()
input_file.close()
text = text.lstrip(u'\ufeff') # remove the byte-order mark
text = text.lstrip('\ufeff') # remove the byte-order mark

# Convert
html = self.convert(text)

# Write to file or stdout
if isinstance(output, (str, unicode)):
if isinstance(output, str):
output_file = codecs.open(output, "w", encoding=encoding)
output_file.write(html)
output_file.close()
Expand Down Expand Up @@ -482,7 +482,7 @@ def getConfig(self, key):

def getConfigInfo(self):
""" Return all config settings as a list of tuples. """
return [(key, self.config[key][1]) for key in self.config.keys()]
return [(key, self.config[key][1]) for key in list(self.config.keys())]

def setConfig(self, key, value):
""" Set a config setting for `key` with the given `value`. """
Expand All @@ -501,8 +501,8 @@ def extendMarkdown(self, md, md_globals):
* md_globals: Global variables in the markdown module namespace.

"""
raise NotImplementedError, 'Extension "%s.%s" must define an "extendMarkdown"' \
'method.' % (self.__class__.__module__, self.__class__.__name__)
raise NotImplementedError('Extension "%s.%s" must define an "extendMarkdown"' \
'method.' % (self.__class__.__module__, self.__class__.__name__))


def load_extension(ext_name, configs = []):
Expand Down Expand Up @@ -542,7 +542,7 @@ def load_extension(ext_name, configs = []):
# If the module is loaded successfully, we expect it to define a
# function called makeExtension()
try:
return module.makeExtension(configs.items())
return module.makeExtension(list(configs.items()))
except AttributeError:
message(CRITICAL, "Failed to initiate extension '%s'" % ext_name)

Expand Down
2 changes: 1 addition & 1 deletion markdown/blockparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def parseBlocks(self, parent, blocks):

"""
while blocks:
for processor in self.blockprocessors.values():
for processor in list(self.blockprocessors.values()):
if processor.test(parent, blocks[0]):
processor.run(parent, blocks)
break
Expand Down
2 changes: 1 addition & 1 deletion markdown/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def parse_options():
'extensions': [],
'encoding': None }, CRITICAL
else:
print OPTPARSE_WARNING
print(OPTPARSE_WARNING)
return None, None

parser = optparse.OptionParser(usage="%prog INPUTFILE [options]")
Expand Down
Loading