Skip to content

Commit

Permalink
Introduce black formatter (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 8, 2024
1 parent 99b055b commit a73a7eb
Show file tree
Hide file tree
Showing 34 changed files with 2,145 additions and 1,121 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ jobs:
- uses: actions/setup-python@v3
- name: 'Run linters'
run: |
python3 -m pip install ruff rstcheck toml-sort sphinx-rtd-theme
python3 -m pip install black ruff rstcheck toml-sort sphinx-rtd-theme
python3 -m pip freeze
make lint-all
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
Bug tracker at https://github.com/giampaolo/pyftpdlib/issues

Version: 1.5.10 - (UNRELEASED)
==============================

**Enhancements**

* #621: use black formatter.

Version: 1.5.9 - 2023-10-25
===========================

Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PYTHON = python3
TSCRIPT = pyftpdlib/test/runner.py
ARGS =
PYDEPS = \
black \
check-manifest \
coverage \
psutil \
Expand Down Expand Up @@ -152,8 +153,11 @@ test-coverage: ## Run test coverage.
# Linters
# ===================================================================

black: ## Python files linting (via black)
@git ls-files '*.py' | xargs $(PYTHON) -m black --check --safe

ruff: ## Run ruff linter.
@git ls-files '*.py' | xargs $(PYTHON) -m ruff check --config=pyproject.toml --no-cache
@git ls-files '*.py' | xargs $(PYTHON) -m ruff check --no-cache

_pylint: ## Python pylint (not mandatory, just run it from time to time)
@git ls-files '*.py' | xargs $(PYTHON) -m pylint --rcfile=pyproject.toml --jobs=${NUM_WORKERS}
Expand All @@ -165,6 +169,7 @@ lint-toml: ## Linter for pyproject.toml
@git ls-files '*.toml' | xargs toml-sort --check

lint-all: ## Run all linters
${MAKE} black
${MAKE} ruff
${MAKE} lint-rst
${MAKE} lint-toml
Expand All @@ -173,6 +178,9 @@ lint-all: ## Run all linters
# Fixers
# ===================================================================

fix-black:
git ls-files '*.py' | xargs $(PYTHON) -m black

fix-ruff:
@git ls-files '*.py' | xargs $(PYTHON) -m ruff --config=pyproject.toml --no-cache --fix

Expand All @@ -183,6 +191,7 @@ fix-unittests: ## Fix unittest idioms.
@git ls-files '*test_*.py' | xargs $(PYTHON) -m teyit --show-stats

fix-all: ## Run all code fixers.
${MAKE} fix-black
${MAKE} fix-ruff
${MAKE} fix-toml
${MAKE} fix-unittests
Expand Down
7 changes: 4 additions & 3 deletions demo/anti_flood_ftpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
class AntiFloodHandler(FTPHandler):

cmds_per_second = 300 # max number of cmds per second
ban_for = 60 * 60 # 1 hour
ban_for = 60 * 60 # 1 hour
banned_ips = []

def __init__(self, *args, **kwargs):
FTPHandler.__init__(self, *args, **kwargs)
self.processed_cmds = 0
self.pcmds_callback = \
self.ioloop.call_every(1, self.check_processed_cmds)
self.pcmds_callback = self.ioloop.call_every(
1, self.check_processed_cmds
)

def on_connect(self):
# called when client connects.
Expand Down
5 changes: 3 additions & 2 deletions demo/tls_ftpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
from pyftpdlib.servers import FTPServer


CERTFILE = os.path.abspath(os.path.join(os.path.dirname(__file__),
"keycert.pem"))
CERTFILE = os.path.abspath(
os.path.join(os.path.dirname(__file__), "keycert.pem")
)


def main():
Expand Down
15 changes: 11 additions & 4 deletions demo/unix_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def get_server():

def daemonize():
"""A wrapper around python-daemonize context manager."""

def _daemonize():
pid = os.fork()
if pid > 0:
Expand Down Expand Up @@ -170,10 +171,16 @@ def main():
USAGE = "python3 [-p PIDFILE] [-l LOGFILE]\n\n"
USAGE += "Commands:\n - start\n - stop\n - status"
parser = optparse.OptionParser(usage=USAGE)
parser.add_option('-l', '--logfile', dest='logfile',
help='the log file location')
parser.add_option('-p', '--pidfile', dest='pidfile', default=PID_FILE,
help='file to store/retreive daemon pid')
parser.add_option(
'-l', '--logfile', dest='logfile', help='the log file location'
)
parser.add_option(
'-p',
'--pidfile',
dest='pidfile',
default=PID_FILE,
help='file to store/retreive daemon pid',
)
options, args = parser.parse_args()

if options.pidfile:
Expand Down
5 changes: 3 additions & 2 deletions demo/unix_ftpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@


def main():
authorizer = UnixAuthorizer(rejected_users=["root"],
require_valid_shell=True)
authorizer = UnixAuthorizer(
rejected_users=["root"], require_valid_shell=True
)
handler = FTPHandler
handler.authorizer = authorizer
handler.abstracted_fs = UnixFilesystem
Expand Down
40 changes: 21 additions & 19 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@


def get_version():
INIT = os.path.abspath(os.path.join(HERE, '..', 'pyftpdlib',
'__init__.py'))
INIT = os.path.abspath(
os.path.join(HERE, '..', 'pyftpdlib', '__init__.py')
)
with open(INIT) as f:
for line in f:
if line.startswith('__ver__'):
Expand All @@ -59,11 +60,13 @@ def get_version():
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.coverage',
'sphinx.ext.imgmath',
'sphinx.ext.viewcode',
'sphinx.ext.intersphinx']
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.coverage',
'sphinx.ext.imgmath',
'sphinx.ext.viewcode',
'sphinx.ext.intersphinx',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down Expand Up @@ -274,15 +277,12 @@ def get_version():
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
Expand All @@ -292,8 +292,7 @@ def get_version():
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'pyftpdlib.tex', 'pyftpdlib Documentation',
AUTHOR, 'manual'),
(master_doc, 'pyftpdlib.tex', 'pyftpdlib Documentation', AUTHOR, 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -333,10 +332,7 @@ def get_version():

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'pyftpdlib', 'pyftpdlib Documentation',
[author], 1)
]
man_pages = [(master_doc, 'pyftpdlib', 'pyftpdlib Documentation', [author], 1)]

# If true, show URL addresses after external links.
#
Expand All @@ -349,9 +345,15 @@ def get_version():
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'pyftpdlib', 'pyftpdlib Documentation',
author, 'pyftpdlib', 'One line description of project.',
'Miscellaneous'),
(
master_doc,
'pyftpdlib',
'pyftpdlib Documentation',
author,
'pyftpdlib',
'One line description of project.',
'Miscellaneous',
),
]

# Documents to append as an appendix to all manuals.
Expand Down
1 change: 1 addition & 0 deletions pyftpdlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class used to interact with the file system, providing a high level,
[I 13-02-19 10:56:27] 127.0.0.1:34179-[user] RETR /home/giampaolo/.vimrc
completed=1 bytes=1700 seconds=0.001
[I 13-02-19 10:56:39] 127.0.0.1:34179-[user] FTP session closed (disconnect).
"""


Expand Down
127 changes: 91 additions & 36 deletions pyftpdlib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,92 @@ def format_option(self, option):
def main():
"""Start a stand alone anonymous FTP server."""
usage = "python3 -m pyftpdlib [options]"
parser = optparse.OptionParser(usage=usage, description=main.__doc__,
formatter=CustomizedOptionFormatter())
parser.add_option('-i', '--interface', default=None, metavar="ADDRESS",
help="specify the interface to run on (default all "
"interfaces)")
parser.add_option('-p', '--port', type="int", default=2121, metavar="PORT",
help="specify port number to run on (default 2121)")
parser.add_option('-w', '--write', action="store_true", default=False,
help="grants write access for logged in user "
"(default read-only)")
parser.add_option('-d', '--directory', default=getcwdu(), metavar="FOLDER",
help="specify the directory to share (default current "
"directory)")
parser.add_option('-n', '--nat-address', default=None, metavar="ADDRESS",
help="the NAT address to use for passive connections")
parser.add_option('-r', '--range', default=None, metavar="FROM-TO",
help="the range of TCP ports to use for passive "
"connections (e.g. -r 8000-9000)")
parser.add_option('-D', '--debug', action='store_true',
help="enable DEBUG logging level")
parser.add_option('-v', '--version', action='store_true',
help="print pyftpdlib version and exit")
parser.add_option('-V', '--verbose', action='store_true',
help="activate a more verbose logging")
parser.add_option('-u', '--username', type=str, default=None,
help="specify username to login with (anonymous login "
"will be disabled and password required "
"if supplied)")
parser.add_option('-P', '--password', type=str, default=None,
help="specify a password to login with (username "
"required to be useful)")
parser = optparse.OptionParser(
usage=usage,
description=main.__doc__,
formatter=CustomizedOptionFormatter(),
)
parser.add_option(
'-i',
'--interface',
default=None,
metavar="ADDRESS",
help="specify the interface to run on (default all interfaces)",
)
parser.add_option(
'-p',
'--port',
type="int",
default=2121,
metavar="PORT",
help="specify port number to run on (default 2121)",
)
parser.add_option(
'-w',
'--write',
action="store_true",
default=False,
help="grants write access for logged in user (default read-only)",
)
parser.add_option(
'-d',
'--directory',
default=getcwdu(),
metavar="FOLDER",
help="specify the directory to share (default current directory)",
)
parser.add_option(
'-n',
'--nat-address',
default=None,
metavar="ADDRESS",
help="the NAT address to use for passive connections",
)
parser.add_option(
'-r',
'--range',
default=None,
metavar="FROM-TO",
help=(
"the range of TCP ports to use for passive "
"connections (e.g. -r 8000-9000)"
),
)
parser.add_option(
'-D', '--debug', action='store_true', help="enable DEBUG logging level"
)
parser.add_option(
'-v',
'--version',
action='store_true',
help="print pyftpdlib version and exit",
)
parser.add_option(
'-V',
'--verbose',
action='store_true',
help="activate a more verbose logging",
)
parser.add_option(
'-u',
'--username',
type=str,
default=None,
help=(
"specify username to login with (anonymous login "
"will be disabled and password required "
"if supplied)"
),
)
parser.add_option(
'-P',
'--password',
type=str,
default=None,
help=(
"specify a password to login with (username required to be useful)"
),
)

options, args = parser.parse_args()
if options.version:
Expand Down Expand Up @@ -96,11 +151,11 @@ def main():
if options.username:
if not options.password:
parser.error(
"if username (-u) is supplied, password ('-P') is required")
authorizer.add_user(options.username,
options.password,
options.directory,
perm=perm)
"if username (-u) is supplied, password ('-P') is required"
)
authorizer.add_user(
options.username, options.password, options.directory, perm=perm
)
else:
authorizer.add_anonymous(options.directory, perm=perm)

Expand Down

0 comments on commit a73a7eb

Please sign in to comment.