Skip to content
This repository was archived by the owner on Feb 2, 2026. It is now read-only.

fix: prevent TypeError when gdriveutils is imported before arg_parser#141

Open
UsamaFoad wants to merge 1 commit into
gelbphoenix:mainfrom
UsamaFoad:UsamaFoad-patch-1
Open

fix: prevent TypeError when gdriveutils is imported before arg_parser#141
UsamaFoad wants to merge 1 commit into
gelbphoenix:mainfrom
UsamaFoad:UsamaFoad-patch-1

Conversation

@UsamaFoad
Copy link
Copy Markdown
Contributor

[PR] Fix TypeError in gdriveutils during module initialization
name: fix: prevent TypeError when gdriveutils is imported before arg_parser
labels: ["bug", "review"]
projects: gelbphoenix/2

Description

Fixed a TypeError occurring in cps/gdriveutils.py where os.path.exists() was called on a NoneType variable. This happened because the module was being imported (and thus executing module-level code) before the command-line arguments were parsed and cli_param.gd_path was initialized.

Added short-circuit logic to verify gd_path is not None before attempting to check for the path's existence.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Performance improvement
  • Documentation update
  • Dependency update
  • Configuration change

Areas Affected

  • Frontend (HTML/CSS/JavaScript)
  • Backend (Python)
  • Database/Storage
  • eBook management
  • OPDS functionality
  • User interface
  • Authentication/Authorization
  • API endpoints
  • Docker configuration
  • Documentation

How Has This Been Tested?

  • Manual Import Test: Verified that from cps.editbooks import editbook now completes successfully without a traceback.
  • CLI Initialization: Confirmed the app starts up without crashing when GDrive parameters are not yet set.
  • Regression Check: Ensured that providing a valid GDrive path still works as expected after initialization.

Test Configuration

  • Operating System: Mageia / Fedora / Linux (Localhost-live)
  • Python Version: 3.11+ (based on traceback syntax)
  • Context: Virtual Environment (.venv)

Screenshots/Video (if applicable)

N/A - This is a backend fix for a silent crash during import.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published
  • I have tested the changes with actual eBook or PDF files
  • I have verified OPDS compatibility (if applicable)
  • I have tested on multiple devices/screen sizes (if UI changes)

Related Issues

Fixes: Discovered during development; no existing issue tracked

(.venv) root@localhost-live:/opt/autocaliweb# python -c "from cps.editbooks import editbook; print('OK')" 

[2026-01-18 19:40:44,744] INFO {__init__.py:97} ProxyFix configured for 1 proxy(ies) to trust 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from cps.editbooks import editbook; print('OK')
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/autocaliweb/cps/editbooks.py", line 43, in <module>
    from . import constants, logger, isoLanguages, gdriveutils, uploader, helper, kobo_sync_status
  File "/opt/autocaliweb/cps/gdriveutils.py", line 179, in <module>
    if not os.path.exists(cli_param.gd_path):
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "<frozen genericpath>", line 19, in exists
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

Additional Notes

The root cause was the order of operations: editbooks.py imports gdriveutils.py, which immediately checks the filesystem for a path that cli.py hasn't had the chance to populate yet. This fix ensures the app remains stable during the early boot phase.


The gdriveutils module was triggering a TypeError during initialization because it attempted to validate cli_param.gd_path before the command-line arguments had been parsed.

Root Cause:
In cps/gdriveutils.py, os.path.exists() is called on cli_param.gd_path at the module level. Since CliParameter initializes gd_path as None, and the import occurs before arg_parser() is executed, the stat system call fails.

Changes:

  • Added a null check for cli_param.gd_path in cps/gdriveutils.py.
  • The logic now safely handles cases where Google Drive paths are not yet initialized or are explicitly disabled.

Traceback reference:
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

The gdriveutils module was triggering a TypeError during initialization because 
it attempted to validate `cli_param.gd_path` before the command-line arguments 
had been parsed.

Root Cause:
In `cps/gdriveutils.py`, `os.path.exists()` is called on `cli_param.gd_path` 
at the module level. Since `CliParameter` initializes `gd_path` as `None`, 
and the import occurs before `arg_parser()` is executed, the `stat` 
system call fails.

Changes:
- Added a null check for `cli_param.gd_path` in `cps/gdriveutils.py`.
- The logic now safely handles cases where Google Drive paths are not yet 
  initialized or are explicitly disabled.

Traceback reference:
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant