Skip to content

Commit

Permalink
Reformat files
Browse files Browse the repository at this point in the history
  • Loading branch information
amartya-dev committed Nov 22, 2020
1 parent 81cc0df commit 403a459
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 61 deletions.
39 changes: 16 additions & 23 deletions djangify/djangify.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import argparse
import logging

from .processing_utils import process_line
from djangify.processing_utils import process_line

# Set default logging level to INFO
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)

# Initialize global variable APP_NAME
APP_NAME = ""
Expand Down Expand Up @@ -94,13 +94,18 @@ def process_file(directory: str, filepath: str, fname: str, app_name: str = APP_
# write the processed line to the newly created file
f.write(temp)
except IOError:
logging.error('An error occurred trying to read the file.')
logging.error("An error occurred trying to read the file.")
finally:
# Close the file to save changes
f.close()

logging.info("Succeeded.. Generated Modified_Files/" + fname +
"." + extension + " in the directory passed.")
logging.info(
"Succeeded.. Generated Modified_Files/"
+ fname
+ "."
+ extension
+ " in the directory passed."
)


def main():
Expand All @@ -112,36 +117,24 @@ def main():

# Defines Argument Parser and fefines flags and expected inputs
parser = argparse.ArgumentParser(
description='Converts specified html files or '
'all html files to django format within '
'a \n specified directory.'
description="Converts specified html files or "
"all html files to django format within "
"a \n specified directory."
)
# Defines the -f flag, standing for files, to gey file nameof the HTML
# file to convert
parser.add_argument(
'files',
metavar='f',
type=str,
nargs='*',
help='provide file names to convert'
"files", metavar="f", type=str, nargs="*", help="provide file names to convert"
)
# Defines the -a flag, for defining the APP_NAME, you want the file
# converted to, for.
parser.add_argument(
'-a',
dest='app_name',
type=str,
nargs='?',
help='provide django app name'
"-a", dest="app_name", type=str, nargs="?", help="provide django app name"
)
# Defines the -d flag, standing for directory, which accepts the path
# to a directory containing the files to be translated
parser.add_argument(
'-d',
dest='base_directory',
type=str,
nargs='?',
help='Provide base directory'
"-d", dest="base_directory", type=str, nargs="?", help="Provide base directory"
)

# Parse the Arguments from the user
Expand Down
30 changes: 17 additions & 13 deletions djangify/processing_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python

import re


Expand All @@ -19,7 +21,7 @@ def check_line(line: str):
form of (True, word) is added
"""

key_words = ['src', 'href', 'url']
key_words = ["src", "href", "url"]
out = list()
for word in key_words:
if line.__contains__(word):
Expand Down Expand Up @@ -48,8 +50,10 @@ def contains_url(line: str):
True if it contains URL and False if no URL in 'line'
"""

URL = r"(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))" \
r"([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?"
URL = (
r"(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))"
r"([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?"
)
if re.match(URL, line):
return True
else:
Expand Down Expand Up @@ -78,20 +82,20 @@ def get_index(line: str, word: str):

index = line.find(word)

if word in ['url']:
start = (index + len(word) + 2)
if word in ["url"]:
start = index + len(word) + 2
quote = line[start - 1]
if quote not in ['\'', '"']:
start = (index + len(word) + 1)
if quote not in ["'", '"']:
start = index + len(word) + 1
quote = line[start - 1]
if quote == '(':
end = line.find(')', start)
if quote == "(":
end = line.find(")", start)
else:
end = line.find(quote, start)
else:
end = line.find(quote, start)
else:
start = (index + len(word) + 2)
start = index + len(word) + 2
quote = line[start - 1]
end = line.find(quote, start)

Expand Down Expand Up @@ -120,7 +124,7 @@ def djangify(line: str, app_name: str):
if contains_url(line):
return line
# Don't change the line if it contains placeholder URL like '#'
if line == '#':
if line == "#":
return line
# If line links to an internal file, make it Django compatible by loading
# from static directory appended with app_name
Expand Down Expand Up @@ -153,8 +157,8 @@ def process_line(line: str, app_name: str):
if instances:
for instance in instances:
index = get_index(buffer, instance[1])
out = djangify(buffer[index[0]: index[1]], app_name)
text = buffer[: index[0]] + out + buffer[index[1]:]
out = djangify(buffer[index[0] : index[1]], app_name)
text = buffer[: index[0]] + out + buffer[index[1] :]
buffer = text

return buffer
46 changes: 21 additions & 25 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
from setuptools import setup, find_packages
from setuptools import setup, find_packages


with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name ='djangify',
version ='1.0.0',
author ='amartya',
author_email ='[email protected]',
url ='https://github.com/L4TTiCe/djangify',
description ='A Python script that converts HTML Files / Templates to Django compatible HTML Templates.',
long_description = long_description,
long_description_content_type ="text/markdown",
license ='MIT',
packages = find_packages(),
entry_points ={
'console_scripts': [
'djangify = djangify.djangify:main'
]
},
classifiers =[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
keywords ='djangify django templates',
zip_safe = False
)
setup(
name="djangify",
version="1.0.0",
author="amartya",
author_email="[email protected]",
url="https://github.com/L4TTiCe/djangify",
description="A Python script that converts HTML Files / Templates to Django compatible HTML Templates.",
long_description=long_description,
long_description_content_type="text/markdown",
license="MIT",
packages=find_packages(),
entry_points={"console_scripts": ["djangify = djangify.djangify:main"]},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
keywords="djangify django templates",
zip_safe=False,
)

0 comments on commit 403a459

Please sign in to comment.