Skip to content

Commit 1abce98

Browse files
committed
pre_commit run all
1 parent 42d1fb6 commit 1abce98

10 files changed

+58
-78
lines changed

.github/workflows/github-actions-test.yaml

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@ jobs:
66
steps:
77
- name: Check out repository code
88
uses: actions/checkout@v2
9-
with:
9+
with:
1010
python-version: '3.x'
1111
# - name: List files in the repository
1212
# run: |
13-
# ls
13+
# ls
1414
# pwd
1515
- name: Install dependencies
1616
run: |
1717
python -m pip install --upgrade setuptools wheel
1818
sudo apt install wkhtmltopdf
19-
- name: Install package
19+
- name: Install package
2020
run: python -m pip install ./
21-
- name: Run test
21+
- name: Run test
2222
run: python ./tests/test_cli.py
23-
- name: Run coverage
24-
run: |
23+
- name: Run coverage
24+
run: |
2525
python -m pip install --upgrade coverage
2626
python -m coverage run --source=code_to_pdf -m code_to_pdf ./
2727
python -m coverage xml -i
2828
- uses: codecov/codecov-action@v1
2929
with:
3030
token: c70aa5e5-5e97-45e5-8c04-5fe781a1f5e0
3131
files: ./coverage.xml
32-
- name: Run Pylint
33-
run: |
34-
python -m pip install pylint
32+
- name: Run Pylint
33+
run: |
34+
python -m pip install pylint
3535
python -m pylint --exit-zero code_to_pdf

LICENSE

-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22-

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
# PDF code proyect generator
66

7-
Passing a lot of hours in front of a screen is tired. Sometimes we need to analyze libraries that used in our application and in order to give a rest to our eyes this program could be usefull. Its imput is a folder that contain files of code, generating pdf pages of highlighted source. Also, at the beginning an index is added. To known the name of every file a header is added in all of them
7+
Passing a lot of hours in front of a screen is tired. Sometimes we need to analyze libraries that used in our application and in order to give a rest to our eyes this program could be usefull. Its imput is a folder that contain files of code, generating pdf pages of highlighted source. Also, at the beginning an index is added. To known the name of every file a header is added in all of them
88

99
![Image conversion](./docs/conversion.svg)
10-

pseudo.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ def add_html(self, html, relative_path):
44

55
def get_pages(self):
66
return 0
7-
7+
8+
89
class TOC:
910
def add_entry(self, relative_path, n_page):
1011
pass
@@ -14,12 +15,10 @@ def code_to_html(file):
1415
return ""
1516

1617

17-
18-
1918
def main(path_to_code, output_pdf):
2019
pdf_creator = PDFCreator()
2120
toc = TOC()
22-
21+
2322
for file, relative_path in os.walk(path_to_code):
2423

2524
html = code_to_html(file)
@@ -30,4 +29,3 @@ def main(path_to_code, output_pdf):
3029

3130
# toc + pdf_creator => output_pdf
3231
join_PDFs(pdf_creator.full_output, toc.render_page(), output_pdf)
33-

setup.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
long_description = fh.read()
55

66
setuptools.setup(
7-
name="code_to_pdf", # Replace with your own username
7+
name="code_to_pdf", # Replace with your own username
88
version="0.0.2",
99
author="Isidro Arias",
1010
author_email="[email protected]",
1111
description="This convert a software proyect to a pdf book",
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
1414
url="https://github.com/isidroas/code_to_pdf",
15-
project_urls={
16-
"Bug Tracker": "https://github.com/isidroas/code_to_pdf/issues",
17-
},
15+
project_urls={"Bug Tracker": "https://github.com/isidroas/code_to_pdf/issues"},
1816
classifiers=[
1917
"Programming Language :: Python :: 3",
2018
"License :: OSI Approved :: MIT License",
@@ -23,14 +21,7 @@
2321
package_dir={"": "src"},
2422
packages=setuptools.find_packages(where="src"),
2523
python_requires=">=3.6",
26-
install_requires=[
27-
'pygments',
28-
'pdfkit',
29-
'PyPDF2',
30-
'reportlab',
31-
'jinja2',
32-
'art',
33-
],
34-
entry_points = {'console_scripts':['code_to_pdf=code_to_pdf.__main__:main']},
35-
include_package_data= True
24+
install_requires=["pygments", "pdfkit", "PyPDF2", "reportlab", "jinja2", "art"],
25+
entry_points={"console_scripts": ["code_to_pdf=code_to_pdf.__main__:main"]},
26+
include_package_data=True,
3627
)

src/code_to_pdf/html_generator.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,28 @@ def code_to_html(input_code: str):
2222
license_match = re.search(REGEX_PATTERN, file_str)
2323

2424
new_header = REGEX_SUB.format(input_code)
25-
# sub_str = new_header + sub_str
25+
# sub_str = new_header + sub_str
2626
starting_line = 1
2727

2828
if license_match:
2929
if license_match.start() == 0:
30-
starting_line = len(re.findall('\n', license_match[0]))
31-
file_str = re.sub(REGEX_PATTERN,'', file_str)
30+
starting_line = len(re.findall("\n", license_match[0]))
31+
file_str = re.sub(REGEX_PATTERN, "", file_str)
3232

3333
formater = HtmlFormatter(
34-
full=True, style="colorful", linenos=True, wrapcode=True, linesparator=True, linenostart=starting_line
34+
full=True,
35+
style="colorful",
36+
linenos=True,
37+
wrapcode=True,
38+
linesparator=True,
39+
linenostart=starting_line,
3540
)
3641
try:
3742
lexer = get_lexer_for_filename(input_code)
3843
except pygments.util.ClassNotFound:
39-
logging.warning('Unable to guess lexer for file {}, using python'.format(input_code) )
44+
logging.warning(
45+
"Unable to guess lexer for file {}, using python".format(input_code)
46+
)
4047
lexer = get_lexer_by_name("python")
4148
output_str = highlight(file_str, lexer, formater)
4249

src/code_to_pdf/pdf_generator.py

+16-31
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020
}
2121

2222

23-
24-
2523
class PDFCreator:
2624
def __init__(self):
27-
self.page_number=1
28-
self.full_pdf = tempfile.NamedTemporaryFile(suffix='.pdf', delete=False).name
25+
self.page_number = 1
26+
self.full_pdf = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False).name
2927
print(self.full_pdf)
3028

31-
32-
def add_html(self, input_html: str, header_name: str):
29+
def add_html(self, input_html: str, header_name: str):
3330
"""Generate numerized pdf pages from a html file
3431
3532
:param input_html: html in string
@@ -41,8 +38,7 @@ def add_html(self, input_html: str, header_name: str):
4138
pdf_reader = PyPDF2.PdfFileReader(input_pdf)
4239
pdf_writer = PyPDF2.PdfFileWriter()
4340

44-
45-
if os.path.isfile(self.full_pdf) and os.path.getsize(self.full_pdf)>0:
41+
if os.path.isfile(self.full_pdf) and os.path.getsize(self.full_pdf) > 0:
4642
# It is a non empty file
4743

4844
pdf_reader_full = PyPDF2.PdfFileReader(self.full_pdf)
@@ -54,23 +50,23 @@ def add_html(self, input_html: str, header_name: str):
5450
page = pdf_reader.getPage(n_page)
5551

5652
## Create
57-
temp_file = tempfile.NamedTemporaryFile(suffix="pdf", delete=False)
53+
temp_file = tempfile.NamedTemporaryFile(suffix="pdf", delete=False)
5854
c = canvas.Canvas(temp_file.name)
5955
width, height = A4
6056
y = height * 0.03
6157
self.page_number += n_page
6258
x = width * 0.92
6359
c.drawString(x, y, str(self.page_number))
6460

65-
if n_page==0:
61+
if n_page == 0:
6662
# Print file header for the first page
6763
# TODO: add this header in html. This is not mantenible because
6864
# the positions depends on the margins
69-
textobject = c.beginText(width*0.075, height*0.97)
65+
textobject = c.beginText(width * 0.075, height * 0.97)
7066
textobject.setFillGray(0.8)
7167
c.setFont("Helvetica", 10)
7268
textobject.textLine("#")
73-
textobject.textLine('# ' + header_name)
69+
textobject.textLine("# " + header_name)
7470
textobject.textLine("#")
7571
c.drawText(textobject)
7672

@@ -84,21 +80,12 @@ def add_html(self, input_html: str, header_name: str):
8480

8581
pdf_writer.addPage(page)
8682

87-
self.page_number +=1
88-
89-
83+
self.page_number += 1
9084

91-
with open(self.full_pdf, 'wb') as file:
85+
with open(self.full_pdf, "wb") as file:
9286
pdf_writer.write(file)
9387

94-
95-
96-
97-
98-
99-
100-
101-
def save_in_path(self, path:str):
88+
def save_in_path(self, path: str):
10289
shutil.copyfile(self.full_pdf, path)
10390

10491
@staticmethod
@@ -119,7 +106,6 @@ def merge_pdfs(pdf_list, output_pdf):
119106
with open(output_pdf, "wb") as file:
120107
pdf_writer.write(file)
121108

122-
123109
@staticmethod
124110
def add_blank_page(pdf_file):
125111
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
@@ -132,15 +118,14 @@ def add_blank_page(pdf_file):
132118
with open(pdf_file, "wb") as file:
133119
pdf_writer.write(file)
134120

135-
136121
@staticmethod
137122
def number_of_pages(pdf_file):
138123
""" Get number of pages of a PDF """
139124
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
140125
return pdf_reader.getNumPages()
141126

142127

143-
if __name__=="__main__":
128+
if __name__ == "__main__":
144129
pdfcreator = PDFCreator()
145130
html_1 = """
146131
<html>
@@ -163,7 +148,7 @@ def number_of_pages(pdf_file):
163148
</body>
164149
</html>
165150
"""
166-
pdfcreator.add_html(html_1, 'HTML 1')
167-
pdfcreator.add_html(html_2, 'HTML 2')
168-
pdfcreator.add_html(html_3, 'HTML 3')
169-
pdfcreator.save_in_path('./pruebapdf.pdf')
151+
pdfcreator.add_html(html_1, "HTML 1")
152+
pdfcreator.add_html(html_2, "HTML 2")
153+
pdfcreator.add_html(html_3, "HTML 3")
154+
pdfcreator.save_in_path("./pruebapdf.pdf")

src/code_to_pdf/temporal.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ def __init__(self):
99
self.temp_folder = file
1010
os.mkdir(self.temp_folder)
1111
logging.info(f"Temporal files will be written in {self.temp_folder}")
12-
12+
1313
self.sub_temp_folder = os.path.join(self.temp_folder, "tmp")
1414
os.mkdir(self.sub_temp_folder)
15-
16-
15+
1716
def get_temp_folder(self):
1817
return self.temp_folder
19-
20-
18+
2119
def get_temp_file(self, suffix=".pdf"):
22-
with tempfile.NamedTemporaryFile(suffix=suffix, dir=self.sub_temp_folder) as file:
20+
with tempfile.NamedTemporaryFile(
21+
suffix=suffix, dir=self.sub_temp_folder
22+
) as file:
2323
return file.name

test_git.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from git import Repo
22
import os
33

4-
PATH = '/home/isidro/wat'
4+
PATH = "/home/isidro/wat"
55

66

77
def get_git_info(path):
88
repo = Repo(path)
99
is_dirty = repo.is_dirty()
10-
commit = str(repo.head.commit) + '*' if is_dirty else ''
10+
commit = str(repo.head.commit) + "*" if is_dirty else ""
1111
datetime = repo.head.commit.committed_datetime.strftime("%Y-%m-%d %H:%M:%S")
1212
branch = repo.active_branch.name
1313
return commit, datetime, branch
1414

15+
1516
print(get_git_info(PATH))
16-
#repo = Repo()
17+
# repo = Repo()

tests/test_cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from code_to_pdf.run import main
33

4-
main(['../'])
4+
main(["../"])
55

6-
#os.system("code_to_pdf ../")
6+
# os.system("code_to_pdf ../")

0 commit comments

Comments
 (0)