diff --git a/djangify/djangify.py b/djangify/djangify.py index de83af5..8e11a83 100644 --- a/djangify/djangify.py +++ b/djangify/djangify.py @@ -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 = "" @@ -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(): @@ -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 diff --git a/djangify/processing_utils.py b/djangify/processing_utils.py index 5cfce5b..6ef1670 100644 --- a/djangify/processing_utils.py +++ b/djangify/processing_utils.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + import re @@ -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): @@ -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: @@ -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) @@ -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 @@ -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 diff --git a/setup.py b/setup.py index 32e02be..565603b 100644 --- a/setup.py +++ b/setup.py @@ -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 ='amarkaushik1999@gmail.com', - 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="amarkaushik1999@gmail.com", + 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, +)