Skip to content

Commit

Permalink
os to pathlib and pyqt5 to pyside6
Browse files Browse the repository at this point in the history
  • Loading branch information
Trilarion committed Mar 4, 2024
1 parent acf3352 commit 1ec0f23
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def single_release(zip):

# get date from the files (latest of last modified)
latest_last_modified = 0
for dirpath, dirnames, filenames in os.walk(git_path):
for dirpath, dirnames, filenames in git_path.walk():
if dirpath.startswith(git_path / '.git'):
# not in '.git'
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def single_revision():
global original_date
if original_date is None:
latest_last_modified = 0
for dirpath, dirnames, filenames in os.walk(nonempty_temp_path):
for dirpath, dirnames, filenames in nonempty_temp_path.walk():
for filename in filenames:
filepath = dirpath / filename
lastmodified = os.path.getmtime(filepath)
Expand Down
6 changes: 3 additions & 3 deletions code/custom-conversions/lechemindeladam_svn_to_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def delete_global_excludes(folder):
"""
"""
for dirpath, dirnames, filenames in os.walk(folder):
for dirpath, dirnames, filenames in folder.walk():
rel_path = os.path.relpath(dirpath, folder)
for file in filenames:
if file in global_exclude:
Expand All @@ -191,7 +191,7 @@ def delete_empty_directories(folder):
"""
"""
for dirpath, dirnames, filenames in os.walk(folder, topdown=False):
for dirpath, dirnames, filenames in folder.walk(topdown=False):
rel_path = os.path.relpath(dirpath, folder)
if not filenames and not dirnames:
os.removedirs(dirpath)
Expand All @@ -202,7 +202,7 @@ def list_large_unwanted_files(folder):
"""
output = []
for dirpath, dirnames, filenames in os.walk(folder):
for dirpath, dirnames, filenames in folder.walk():
rel_path = os.path.relpath(dirpath, folder)
for file in filenames:
file_path = dirpath / file
Expand Down
2 changes: 1 addition & 1 deletion code/helpers/is_already_included.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def similarity(a, b):
if __name__ == "__main__":
similarity_threshold = 0.7

root_path = os.path.realpath(os.path.dirname(__file__) / os.path.pardir)
root_path = os.path.realpath(__file__.parent / os.path.pardir)

# read docs/data.json
data_file = root_path / 'docs', 'data.json'
Expand Down
15 changes: 9 additions & 6 deletions code/helpers/list_python_external_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ def local_module(module_base, file_path, module):
"""
module = module.split('.')
module[-1] += '.py'
pathA = module_base / *module
pathB = file_path / *module
pathA = module_base
pathB = file_path
for part in module:
pathA /= part
pathB /= part
return pathA.exists() or pathB.exists()


Expand All @@ -32,13 +35,13 @@ def local_module(module_base, file_path, module):
regex_as = re.compile(r"(as.*)$", re.MULTILINE)

# modify these locations
root_folder = r''
module_base = r''
root_folder = pathlib.Path(r'')
module_base = pathlib.Path(r'')

# get all *.py files below the root_folder
python_files = []
setup_files = []
for dirpath, dirnames, filenames in os.walk(root_folder):
for dirpath, dirnames, filenames in root_folder.walk():
for file in ('setup.py', 'requirements.txt'):
if file in filenames:
setup_files.append(dirpath / file)
Expand Down Expand Up @@ -80,7 +83,7 @@ def local_module(module_base, file_path, module):
matches = regex_from.findall(content)

for match in matches:
module = match[0] # only the from part
module = match[0] # only from part
module = module.strip()
if not local_module(module_base, file_path, module):
imports.append(module)
Expand Down
36 changes: 18 additions & 18 deletions code/html/generate_static_website.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,18 @@ def raise_helper(msg):
raise Exception(msg)


def write(text, file):
def write(text, path):
"""
Writes a generated HTML page to a file, but checks with a HTML parser before.
:param text:
:param file:
"""
# output file
if isinstance(file, str):
file = [file]
file = c.web_path / file
if isinstance(path, str):
path = [path]
file = c.web_path
for part in path:
file /= part

# check file hash and use previous version
if file in previous_files and previous_files[file]['hash'] == file_hash(text):
Expand All @@ -248,9 +250,7 @@ def write(text, file):
raise RuntimeError(e)

# create output directory if necessary
containing_dir = os.path.dirname(file)
if not containing_dir.is_dir():
containing_dir.mkdir()
file.parent.mkdir(parents=True, exist_ok=True)

# write text
utils.write_text(file, text)
Expand Down Expand Up @@ -1032,14 +1032,14 @@ def generate(entries, inspirations, developers):
utils.copy_tree(c.web_template_path / 'js', c.web_js_path)

# copy screenshots path
files = [file for file in c.screenshots_path.iterdir() if file.endswith('.jpg')]
filenames = [file.name for file in c.screenshots_path.iterdir() if file.suffix == '.jpg']
c.web_screenshots_path.mkdir(parents=True, exist_ok=True)
for file in files:
shutil.copyfile(c.screenshots_path / file, c.web_screenshots_path / file)
for filename in filenames:
shutil.copyfile(c.screenshots_path / filename, c.web_screenshots_path / filename)

# collage_image and google search console token and favicon.svg
for file in ('collage_games.jpg', 'google1f8a3863114cbcb3.html', 'favicon.svg'):
shutil.copyfile(c.web_template_path / file, c.web_path / file)
for filename in ('collage_games.jpg', 'google1f8a3863114cbcb3.html', 'favicon.svg'):
shutil.copyfile(c.web_template_path / filename, c.web_path / filename)

# create Jinja Environment
environment = Environment(loader=FileSystemLoader(c.web_template_path), autoescape=True)
Expand Down Expand Up @@ -1287,12 +1287,12 @@ def generate(entries, inspirations, developers):

# create dictionary of file hashes
print('estimate file hashes')
for dirpath, dirnames, filenames in os.walk(c.web_path): # TODO in Python 3.12 Path.walk() exists
for file in filenames:
if any(file.endswith(ext) for ext in ('.html', '.svg')):
file = pathlib.Path(dirpath) / file
text = utils.read_text(file)
previous_files[file] = {'hash': file_hash(text), 'text': text}
for dirpath, dirnames, filenames in c.web_path.walk(): # TODO in Python 3.12 Path.walk() exists
for filename in filenames:
if any(filename.endswith(ext) for ext in ('.html', '.svg')):
filename = dirpath / filename
text = utils.read_text(filename)
previous_files[filename] = {'hash': file_hash(text), 'text': text}

# clean the output directory
print('clean current static website')
Expand Down
2 changes: 1 addition & 1 deletion code/html/osgameclones_download_images_create_collage.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def assemble_collage():
N = Nx * Ny

# paths
root_path = os.path.realpath(os.path.dirname(__file__) / os.path.pardir)
root_path = os.path.realpath(__file__.parent / os.path.pardir)
download_path = root_path / 'code' / 'images-download'
downsized_path = download_path / 'downsized'
output_file = root_path / 'code' / 'collage_games.jpg'
Expand Down
3 changes: 1 addition & 2 deletions code/maintenance_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ def write_entries(self):
osg.write_entries(self.entries)
print('entries written')


def check_template_leftovers(self):
"""
Checks for template leftovers.
Expand All @@ -130,7 +129,7 @@ def check_template_leftovers(self):

for check_string in check_strings:
if content.find(check_string) >= 0:
print(f'{os.path.basename(entry_path)}: found {check_string}')
print(f'{entry_path.parent}: found {check_string}')
print('checked for template leftovers')

def check_inconsistencies(self):
Expand Down
2 changes: 1 addition & 1 deletion code/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pygithub
lark-parser
BeautifulSoup4
PyQt5
PySide6
wikipedia
Jinja2
html5lib
Expand Down
11 changes: 5 additions & 6 deletions code/synchronization/osgameclones_synchronization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
"""
osgameclones has the following fields:
'updated', 'video', 'repo', 'license', 'originals', 'status', 'multiplayer', 'info', 'lang', 'feed', 'content', 'images', 'url', 'name', 'framework', 'type', 'development'
Expand Down Expand Up @@ -152,7 +152,7 @@ def create_many_to_one_mapping(map):
download_missing_screenshots = False

# paths
root_path = os.path.realpath(os.path.dirname(__file__) / os.path.pardir, os.path.pardir)
root_path = c.root_path

# read our database
our_entries = osg.read_entries()
Expand All @@ -171,8 +171,7 @@ def create_many_to_one_mapping(map):
screenshots = osg.read_screenshots_overview()

# import osgameclones data
osgc_path = os.path.realpath(os.path.join(root_path, os.path.pardir, '11_osgameclones.git',
'games')) # this is specific for my local constellation
osgc_path = root_path.parent / '11_osgameclones.git' / 'games' # this is specific for my local constellation
osgc_files = osgc_path.iterdir()

# iterate over all yaml files in osgameclones/data folder and load contents
Expand Down Expand Up @@ -499,10 +498,10 @@ def create_many_to_one_mapping(map):
print(f'create new entry for {osgc_name}')
file_name = osg.canonical_name(osgc_name) + '.md'
target_file = c.entries_path / file_name
if os.path.isfile(target_file):
if target_file.is_file():
print(f'warning: file {file_name} already existing, save under slightly different name')
target_file = c.entries_path / file_name[:-3] + '-duplicate.md'
if os.path.isfile(target_file):
if target_file.is_file():
continue # just for safety reasons

# add Title and File
Expand Down
2 changes: 1 addition & 1 deletion code/synchronization/screenshot_import_bzt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

if __name__ == "__main__":
# paths
root_path = os.path.realpath(os.path.dirname(__file__) / os.path.pardir, os.path.pardir)
root_path = os.path.realpath(__file__.parent / os.path.pardir, os.path.pardir)

# read content of screenshots_bzt.txt
info = u.read_text(root_path / 'code' / 'synchronization' / 'screenshots_bzt.txt')
Expand Down
4 changes: 2 additions & 2 deletions code/utils/osg_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PySide6 import QtCore, QtGui, QtWidgets


def exception_hook(type, value, traceback):
Expand All @@ -20,7 +20,7 @@ def run_simple_button_app(title, actions):
:param actions:
:return:
"""
# fix PyQt5 eating exceptions (see http://stackoverflow.com/q/14493081/1536976)
# fix PySide6 eating exceptions (see http://stackoverflow.com/q/14493081/1536976)
sys.excepthook = exception_hook

# create app
Expand Down
20 changes: 12 additions & 8 deletions code/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def detect_archive_type(name):

def folder_size(path):
size = 0
for dirpath, dirnames, filenames in os.walk(path):
for dirpath, dirnames, filenames in path.walk():
for file in filenames:
size += os.path.getsize(dirpath / file)
size += (dirpath / file).stat().st_size
return size


Expand Down Expand Up @@ -138,7 +138,7 @@ def determine_latest_last_modified_date(folder):
"last modified" date of all these files.
"""
latest_last_modified = 0
for dirpath, dirnames, filenames in os.walk(folder):
for dirpath, dirnames, filenames in folder.walk():
for filename in filenames:
filepath = dirpath / filename
lastmodified = os.path.getmtime(filepath)
Expand Down Expand Up @@ -173,13 +173,17 @@ def copy_tree(source, destination):
# this gave an FileNotFoundError: [Errno 2] No such file or directory: '' on Windows
# distutils.dir_util.copy_tree(archive_path, git_path)
destination.mkdir(parents=True, exist_ok=True)
for dirpath, dirnames, filenames in os.walk(source): # TODO replace os.walk with path.walk (Python 3.12)
# first create all the directory on destination
for directory in (destination / os.path.relpath(os.path.join(dirpath, x, source)) for x in dirnames):

for dirpath, dirnames, filenames in source.walk():
# first create all the directories on destination
destination_path = destination / dirpath.relative_to(source)
for directory in (destination_path / dirname for dirname in dirnames):
directory.mkdir(parents=True, exist_ok=True)

# second copy all the files
for source_file in (pathlib.Path(dirpath) / x for x in filenames):
destination_file = destination / os.path.relpath(source_file, source)
for filename in filenames:
source_file = dirpath / filename
destination_file = destination_path / filename
shutil.copyfile(source_file, destination_file)


Expand Down

0 comments on commit 1ec0f23

Please sign in to comment.