Skip to content

Commit ec4a5f1

Browse files
committed
Initial Committ
1 parent 9effcc2 commit ec4a5f1

File tree

7 files changed

+339
-0
lines changed

7 files changed

+339
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
build
3+
mathematical.egg-info
4+
*/__pycache__
8.18 KB
Binary file not shown.

dist/domdf_python_tools-0.1.0.tar.gz

3.81 KB
Binary file not shown.

domdf_python_tools/__init__.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Compiled 2018-2019 by Dominic Davis-Foster <[email protected]>
2+
3+
# check_dependencies based on https://stackoverflow.com/a/29044693/3092681
4+
# Copyright 2015 TehTechGuy
5+
# Licensed under CC-BY-SA
6+
#
7+
# as_text from https://stackoverflow.com/a/40935194
8+
# Copyright 2016 User3759685
9+
# Available under the MIT License
10+
#
11+
# chunks from https://stackoverflow.com/a/312464/3092681
12+
# Copytight 2008 Ned Batchelder
13+
# Licensed under CC-BY-SA
14+
#
15+
#
16+
# terminal.py
17+
# Copyright 2014-2019 Dominic Davis-Foster <[email protected]>
18+
#
19+
# get_terminal_size, _get_terminal_size_windows, _get_terminal_size_tput and _get_terminal_size_linux
20+
# from https://gist.github.com/jtriley/1108174
21+
# Copyright 2011 jtriley
22+
#
23+
# paths.py
24+
# Copyright 2018-2019 Dominic Davis-Foster <[email protected]>
25+
#
26+
# copytree based on https://stackoverflow.com/a/12514470/3092681
27+
# Copyright 2012 atzz
28+
# Licensed under CC-BY-SA
29+
#
30+
31+
__all__ = ["paths", "terminal"]
32+
33+
__author__ = "Dominic Davis-Foster"
34+
__copyright__ = "Copyright 2014-2019 Dominic Davis-Foster"
35+
36+
__license__ = "LGPL"
37+
__version__ = "0.1.0"
38+
__email__ = "[email protected]"
39+
40+
41+
import sys
42+
43+
from . import *
44+
45+
pyversion = int(sys.version[0]) # Python Version
46+
47+
48+
def as_text(value):
49+
if value is None:
50+
return ""
51+
return str(value)
52+
53+
54+
def str2tuple(input_string, sep=","):
55+
return tuple(int(x) for x in input_string.split(sep))
56+
57+
58+
def tuple2str(input_tuple, sep=","):
59+
return sep.join(input_tuple)
60+
61+
62+
def chunks(l, n):
63+
"""Yield successive n-sized chunks from l."""
64+
for i in range(0, len(l), n):
65+
yield l[i:i + n]
66+
67+
68+
def check_dependencies(dependencies, prt=True):
69+
from pkgutil import iter_modules
70+
modules = set(x[1] for x in iter_modules())
71+
72+
missing_modules = []
73+
for requirement in dependencies:
74+
if not requirement in modules:
75+
missing_modules.append(requirement)
76+
77+
if prt:
78+
if len(missing_modules) == 0:
79+
print("All modules installed")
80+
else:
81+
print("""\rThe following modules are missing.
82+
Please check the documentation.""")
83+
print(missing_modules)
84+
print("")
85+
86+
else:
87+
return missing_modules
88+
89+
90+
def list2string(the_list, sep=","):
91+
return list2str(the_list, sep)
92+
93+
94+
def list2str(the_list, sep=","):
95+
return sep.join([str(x) for x in the_list])

domdf_python_tools/paths.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# paths.py
5+
"""Functions for paths and files"""
6+
#
7+
# Copyright 2018-2019 Dominic Davis-Foster <[email protected]>
8+
#
9+
# copytree based on https://stackoverflow.com/a/12514470/3092681
10+
# Copyright 2012 atzz
11+
# Licensed under CC-BY-SA
12+
#
13+
# This program is free software; you can redistribute it and/or modify
14+
# it under the terms of the GNU Lesser General Public License as published by
15+
# the Free Software Foundation; either version 3 of the License, or
16+
# (at your option) any later version.
17+
#
18+
# This program is distributed in the hope that it will be useful,
19+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
# GNU Lesser General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU Lesser General Public License
24+
# along with this program; if not, write to the Free Software
25+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26+
# MA 02110-1301, USA.
27+
#
28+
#
29+
import os
30+
31+
def parent_path(path):
32+
return os.path.abspath(os.path.join(path,os.pardir))
33+
34+
def copytree(src, dst, symlinks=False, ignore=None):
35+
"""
36+
37+
Because shutil.copytree is borked
38+
39+
:param src:
40+
:param dst:
41+
:param symlinks:
42+
:param ignore:
43+
:return:
44+
"""
45+
46+
import shutil
47+
48+
for item in os.listdir(src):
49+
s = os.path.join(src, item)
50+
d = os.path.join(dst, item)
51+
if os.path.isdir(s):
52+
return shutil.copytree(s, d, symlinks, ignore)
53+
else:
54+
return shutil.copy2(s, d)
55+
56+
def relpath(path):
57+
"""
58+
59+
:param path:
60+
:return:
61+
"""
62+
63+
if os.path.normpath(os.path.abspath(path)).startswith(os.path.normpath(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))):
64+
return os.path.relpath(os.path.abspath(path))
65+
else:
66+
return os.path.abspath(path)
67+
68+
def maybe_make(directory):
69+
"""
70+
makes a directory only if it doesn't already exist
71+
72+
:param directory:
73+
:return:
74+
"""
75+
76+
if not os.path.exists(directory):
77+
os.makedirs(directory)

domdf_python_tools/terminal.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# terminal.py
5+
"""Useful functions for terminal-based programs"""
6+
#
7+
# Copyright 2014-2019 Dominic Davis-Foster <[email protected]>
8+
#
9+
# get_terminal_size, _get_terminal_size_windows, _get_terminal_size_tput and _get_terminal_size_linux
10+
# from https://gist.github.com/jtriley/1108174
11+
# Copyright 2011 jtriley
12+
#
13+
# This program is free software; you can redistribute it and/or modify
14+
# it under the terms of the GNU Lesser General Public License as published by
15+
# the Free Software Foundation; either version 3 of the License, or
16+
# (at your option) any later version.
17+
#
18+
# This program is distributed in the hope that it will be useful,
19+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
# GNU Lesser General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU Lesser General Public License
24+
# along with this program; if not, write to the Free Software
25+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26+
# MA 02110-1301, USA.
27+
#
28+
#
29+
30+
__author__ = "Dominic Davis-Foster"
31+
__copyright__ = "Copyright 2014-2019 Dominic Davis-Foster"
32+
33+
__license__ = "GPL"
34+
__version__ = "0.1.0"
35+
__email__ = "[email protected]"
36+
37+
import os
38+
import shlex
39+
import struct
40+
import platform
41+
import subprocess
42+
from . import pyversion
43+
44+
45+
def clear(): # clear the display
46+
os.system('cls' if os.name == 'nt' else 'clear')
47+
# works for Windows and UNIX, but does not clear Python Shell
48+
49+
def br(): # Line Break
50+
print("")
51+
52+
def entry(text_to_print):
53+
if pyversion == 3:
54+
return input(text_to_print)
55+
elif pyversion == 2:
56+
return raw_input(text_to_print)
57+
58+
59+
def get_terminal_size():
60+
""" getTerminalSize()
61+
- get width and height of console
62+
- works on linux,os x,windows,cygwin(windows)
63+
originally retrieved from:
64+
http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
65+
"""
66+
current_os = platform.system()
67+
tuple_xy = None
68+
if current_os == 'Windows':
69+
tuple_xy = _get_terminal_size_windows()
70+
if tuple_xy is None:
71+
tuple_xy = _get_terminal_size_tput()
72+
# needed for window's python in cygwin's xterm!
73+
if current_os in ['Linux', 'Darwin'] or current_os.startswith('CYGWIN'):
74+
tuple_xy = _get_terminal_size_linux()
75+
if tuple_xy is None:
76+
print("default")
77+
tuple_xy = (80, 25) # default value
78+
return tuple_xy
79+
80+
81+
def _get_terminal_size_windows():
82+
try:
83+
from ctypes import windll, create_string_buffer
84+
# stdin handle is -10
85+
# stdout handle is -11
86+
# stderr handle is -12
87+
h = windll.kernel32.GetStdHandle(-12)
88+
csbi = create_string_buffer(22)
89+
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
90+
if res:
91+
(bufx, bufy, curx, cury, wattr,
92+
left, top, right, bottom,
93+
maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
94+
sizex = right - left + 1
95+
sizey = bottom - top + 1
96+
return sizex, sizey
97+
except:
98+
pass
99+
100+
101+
def _get_terminal_size_tput():
102+
# get terminal width
103+
# src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
104+
try:
105+
cols = int(subprocess.check_call(shlex.split('tput cols')))
106+
rows = int(subprocess.check_call(shlex.split('tput lines')))
107+
return (cols, rows)
108+
except:
109+
pass
110+
111+
112+
def _get_terminal_size_linux():
113+
def ioctl_GWINSZ(fd):
114+
try:
115+
import fcntl
116+
import termios
117+
cr = struct.unpack('hh',
118+
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
119+
return cr
120+
except:
121+
pass
122+
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
123+
if not cr:
124+
try:
125+
fd = os.open(os.ctermid(), os.O_RDONLY)
126+
cr = ioctl_GWINSZ(fd)
127+
os.close(fd)
128+
except:
129+
pass
130+
if not cr:
131+
try:
132+
cr = (os.environ['LINES'], os.environ['COLUMNS'])
133+
except:
134+
return None
135+
return int(cr[1]), int(cr[0])
136+
137+
if __name__ == "__main__":
138+
sizex, sizey = get_terminal_size()
139+
print('width =', sizex, 'height =', sizey)

setup.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from setuptools import setup, find_packages
2+
3+
with open("README.md", "r") as fh:
4+
long_description = fh.read()
5+
6+
setup(
7+
name="domdf_python_tools",
8+
version="0.1.0",
9+
author='Dominic Davis-Foster',
10+
author_email="[email protected]",
11+
packages=find_packages(),
12+
license="OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
13+
url="https://github.com/domdfcoding/domdf_python_tools",
14+
description='Helpful functions for Python',
15+
long_description=long_description,
16+
long_description_content_type="text/markdown",
17+
classifiers=[
18+
"Programming Language :: Python :: 3",
19+
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
20+
"Operating System :: OS Independent",
21+
"Development Status :: 4 - Beta",
22+
],
23+
24+
)

0 commit comments

Comments
 (0)