Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use pytoolconfig for configuration #473

Merged
merged 35 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a66ba90
use pytoolconfig for configuration
bagel897 May 13, 2022
d26765e
migrate from setup.py to pyproject.toml
bagel897 May 3, 2022
79bbec6
remove pydantic dependency
bagel897 May 21, 2022
1046994
use read the docs and sphinx
bagel897 May 22, 2022
48c255b
add universal keys
bagel897 May 22, 2022
0f2964b
drop python2 support
bagel897 May 22, 2022
f8bab12
use pyupgrade to bump to python 3.7
bagel897 May 22, 2022
63f041d
drop python 3.6 support
bagel897 May 22, 2022
9c13ee6
fix dependency specifier
bagel897 May 22, 2022
ca4fb28
blacken
bagel897 May 22, 2022
ec2d323
merge prefs with dataclass
bagel897 May 24, 2022
1eaa5c0
Merge branch 'master' into toml
lieryan May 24, 2022
b84c46a
use rtd theme
bagel897 May 28, 2022
2bc1996
fix: typos, whitespace, defaults
bagel897 May 28, 2022
a57a27c
put all documentation and keys in prefs
bagel897 May 28, 2022
e1132c6
remove default config
bagel897 May 28, 2022
ff8f984
remove old config file
bagel897 May 28, 2022
7cf96e1
bump version, work on spacing issues
bagel897 May 28, 2022
4246efd
Merge branch 'master' of https://github.com/python-rope/rope into toml
bagel897 May 28, 2022
0753cc0
Update changelog
bagel897 May 28, 2022
02d47ec
Merge branch 'master' of https://github.com/python-rope/rope into toml
bagel897 Jun 4, 2022
10b5456
Merge branch 'master' into toml
lieryan Jun 7, 2022
f7dd7ec
Fix incorrect merge conflict resolution
lieryan Jun 7, 2022
be47cb5
Merge branch 'master' into toml
lieryan Jun 14, 2022
75fd6cd
Fix whitespace
lieryan Jun 8, 2022
e10caf6
Update CHANGELOG.md
lieryan Jun 14, 2022
d64369e
Merge branch 'master' into toml
lieryan Jun 14, 2022
8b97718
Merge branch 'master' into toml
lieryan Jun 14, 2022
58faa9f
Merge branch 'master' into toml
lieryan Jun 14, 2022
2f0d893
Fix typo preform_doa->perform_doa
lieryan Jun 14, 2022
9370185
Restore default_config.py
lieryan Jun 14, 2022
8e4f9d3
Change the default config.py to only document
lieryan Jun 14, 2022
f53c360
fix: use lambda and remove extra pass
bagel897 Jun 15, 2022
a7cc890
docs: link default_config.py
bagel897 Jun 15, 2022
992841f
Merge branch 'master' into toml
lieryan Jun 17, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 72 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
[tool.black]
[project]
name = 'rope'
description = 'a python refactoring library...'
readme = 'README.rst'
requires-python = '>=3.6'
classifiers = [
'Development Status :: 4 - Beta',
'Operating System :: OS Independent',
'Environment :: X11 Applications',
'Environment :: Win32 (MS Windows)',
'Environment :: MacOS X',
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)',
'Natural Language :: English',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Topic :: Software Development',
]
dynamic = ['version']
dependencies = ['pytoolconfig > 1.0.0']

[[project.authors]]
name = 'Ali Gholami Rudi'
email = 'aligrudi@users.sourceforge.net'

[project.license]
text = 'LGPL-3.0-or-later'

[project.urls]
Source = 'https://github.com/python-rope/rope'

target-version = ['py35', 'py36', 'py37', 'py38', 'py39']
[project.optional-dependencies]
dev = [
'pytest>=7.0.1',
'pytest-timeout>=2.1.0',
'build>=0.7.0',
'pytoolconfig[doc]'
]
[tool.setuptools]
packages = [
'rope',
'rope.base',
'rope.base.oi',
'rope.base.oi.type_hinting',
'rope.base.oi.type_hinting.providers',
'rope.base.oi.type_hinting.resolvers',
'rope.base.utils',
'rope.contrib',
'rope.refactor',
'rope.refactor.importutils',
]
[tool.setuptools.dynamic.version]
attr = 'rope.VERSION'

[tool.black]
target-version = [
'py36',
'py37',
'py38',
'py39',
]
include = 'rope/.*\.pyi?$'
force-exclude = 'ropetest'

[build-system]
requires = [
'setuptools',
'setuptools-scm',
]
build-backend = 'setuptools.build_meta'
173 changes: 173 additions & 0 deletions rope/base/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
from dataclasses import dataclass
from typing import Callable, Dict, List, Optional

from pytoolconfig import PyToolConfig, field
from pytoolconfig.sources import Source

from rope.base.resources import Folder
from rope.base.utils import pycompat


@dataclass
class RopePrefs:
ignored_resources: List[str] = field(
default_factory=[
"*.pyc",
"*~",
".ropeproject",
".hg",
".svn",
"_svn",
".git",
".tox",
".venv",
"venv",
].copy,
description="""
Specify which files and folders to ignore in the project.
Changes to ignored resources are not added to the history and
VCSs. Also they are not returned in `Project.get_files()`.
Note that ``?`` and ``*`` match all characters but slashes.
'*.pyc': matches 'test.pyc' and 'pkg/test.pyc'
'mod*.pyc': matches 'test/mod1.pyc' but not 'mod/1.pyc'
'.svn': matches 'pkg/.svn' and all of its children
'build/*.o': matches 'build/lib.o' but not 'build/sub/lib.o'
'build//*.o': matches 'build/lib.o' and 'build/sub/lib.o'
""",
)
save_objectdb: bool = field(
default=False, description="Should rope save object information or not."
)
compress_objectdb: bool = False
automatic_soa: bool = True
soa_followed_calls: int = field(
default=0, description="The depth of calls to follow in static object analysis"
)
preform_doa: bool = field(
default=True,
description="""
If `False` when running modules or unit tests 'dynamic object analysis' is turned off. This makes them much faster.
""",
)
validate_objectdb: bool = field(
default=False,
description="Rope can check the validity of its object DB when running.",
)

max_history_items: int = field(default=32, description="How many undos to hold?")
save_history: bool = field(
default=True, description="Shows whether to save history across sessions."
)
compress_history: bool = False

indent_size: int = field(
default=4,
description="""
Set the number spaces used for indenting. According to
:PEP:`8`, it is best to use 4 spaces. Since most of rope's
unit-tests use 4 spaces it is more reliable, too.
""",
)

extension_modules: List[str] = field(
default_factory=list,
description="Builtin and c-extension modules that are allowed to be imported and inspected by rope.",
)

import_dynload_stdmods: bool = field(
default=True,
description="Add all standard c-extensions to extension_modules list.",
)
ignore_syntax_errors: bool = field(default=False)

ignore_bad_imports: bool = field(
default=False,
description="If `True`, rope ignores unresolvable imports. Otherwise, they appear in the importing namespace.",
)

prefer_module_from_imports: bool = field(
default=False,
description="""
If `True`, rope will insert new module imports as `from <package> import <module>`by default.""",
)

# If `True`, rope will transform a comma list of imports into
# multiple separate import statements when organizing
# imports.
split_imports: bool = field(default=False)

# If `True`, rope will remove all top-level import statements and
# reinsert them at the top of the module when making changes.
pull_imports_to_top: bool = field(default=True)

sort_imports_alphabetically: bool = field(
default=False,
description="""
If `True`, rope will sort imports alphabetically by module name instead
of alphabetically by import statement, with from imports after normal
imports.
""",
)
type_hinting_factory: str = field(
"rope.base.oi.type_hinting.factory.default_type_hinting_factory",
description="""
Location of implementation of
rope.base.oi.type_hinting.interfaces.ITypeHintingFactory In general
case, you don't have to change this value, unless you're an rope expert.
Change this value to inject you own implementations of interfaces
listed in module rope.base.oi.type_hinting.providers.interfaces
For example, you can add you own providers for Django Models, or disable
the search type-hinting in a class hierarchy, etc.
""",
)
project_opened: Optional[Callable] = field(
None, description="""This function is called after opening the project"""
)


class _RopeConfigSource(Source):
"""Custom source for rope config.py files."""

name: str = "config.py"
run_globals: Dict

def __init__(self, ropefolder: Folder):
self.ropefolder = ropefolder
self.prefs = {}
self.run_globals = {}

def _read(self) -> bool:
if self.ropefolder is None or not self.ropefolder.has_child("config.py"):
return False
config = self.ropefolder.get_child("config.py")
self.run_globals.update(
{
"__name__": "__main__",
"__builtins__": __builtins__,
"__file__": config.real_path,
}
)
pycompat.execfile(config.real_path, self.run_globals)
return True

def parse(self) -> Optional[Dict]:
if not self._read():
return None
if "set_prefs" in self.run_globals:
self.run_globals["set_prefs"](self.prefs)
if "project_opened" in self.run_globals:
self.prefs["project_opened"] = self.run_globals["project_opened"]
return self.prefs


def get_config(root: Folder, ropefolder: Folder) -> PyToolConfig:
custom_sources = [_RopeConfigSource(ropefolder)]
config = PyToolConfig(
"rope",
root.pathlib,
RopePrefs,
custom_sources=custom_sources,
bases=[".ropefolder"],
recursive=False,
)
return config
32 changes: 10 additions & 22 deletions rope/base/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import shutil
import sys
import warnings
from dataclasses import asdict

import rope.base.fscommands
import rope.base.resourceobserver as resourceobserver
import rope.base.utils.pycompat as pycompat
from rope.base import exceptions, taskhandle, prefs, history, pycore, utils
from rope.base import exceptions, history, prefs, pycore, taskhandle, utils
from rope.base.config import get_config
from rope.base.exceptions import ModuleNotFoundError
from rope.base.resources import File, Folder, _ResourceMatcher

Expand Down Expand Up @@ -255,34 +256,21 @@ def _create_recursively(self, folder):
folder.create()

def _init_prefs(self, prefs):
run_globals = {}
if self.ropefolder is not None:
config = self.get_file(self.ropefolder.path + "/config.py")
run_globals.update(
{
"__name__": "__main__",
"__builtins__": __builtins__,
"__file__": config.real_path,
}
)
if config.exists():
config = self.ropefolder.get_child("config.py")
pycompat.execfile(config.real_path, run_globals)
else:
exec(self._default_config(), run_globals)
if "set_prefs" in run_globals:
run_globals["set_prefs"](self.prefs)
config = get_config(self.root, self.ropefolder).parse()
for key, value in asdict(config).items():
self.prefs[key] = value
for key, value in prefs.items():
self.prefs[key] = value
self._init_other_parts()
self._init_ropefolder()
if "project_opened" in run_globals:
run_globals["project_opened"](self)
if config.project_opened:
config.project_opened(self)

def _default_config(self):
import rope.base.default_config
import inspect

import rope.base.default_config

return inspect.getsource(rope.base.default_config)

def _init_other_parts(self):
Expand Down
6 changes: 6 additions & 0 deletions rope/base/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from rope.base import change
from rope.base import exceptions
from rope.base import fscommands
from pathlib import Path


class Resource(object):
Expand Down Expand Up @@ -86,6 +87,11 @@ def real_path(self):
"""Return the file system path of this resource"""
return self.project._get_resource_path(self.path)

@property
def pathlib(self):
"""Return the file as a pathlib path."""
return Path(self.real_path)

def __eq__(self, obj):
return self.__class__ == obj.__class__ and self.path == obj.path

Expand Down
15 changes: 15 additions & 0 deletions ropetest/projecttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,21 @@ def project_opened(project):
self.assertTrue(self.project.get_file("loaded").exists())
myfile = self.project.get_file("myfile.txt")
self.assertTrue(self.project.is_ignored(myfile))
def test_loading_pyproject(self):
self.project = testutils.sample_project(ropefolder=".ropeproject")
config = self.project.get_file("pyproject.toml")
if not config.exists():
config.create()
config.write(
dedent("""\
[tool.rope]
ignored_resources=["pyproject.py"]
""")
)
self.project.close()
self.project = Project(self.project.address, ropefolder=".ropeproject")
myfile = self.project.get_file("pyproject.py")
self.assertTrue(self.project.is_ignored(myfile))

def test_ignoring_syntax_errors(self):
self.project = testutils.sample_project(
Expand Down