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

nix flake #81

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ item key will be saved to a txt file.
```python
zt_rw.readwise.save_failed_items_to_json("failed_readwise_highlights.json")
```

## Approach 3 (through the nix flake)
If you have `nix`, you can skip any installation and do:
```sh
nix run github:e-alizadeh/Zotero2Readwise -- <readwise_token> <zotero_key> <zotero_id>
```
The text file with failed highlights, which usually would be written to the Zotero2Readwise python package directory, will now be written to you working directory, since nix does not allow writing to package directories.
If you don't want this file created, supply `--suppress_failures` as an additional argument.
Comment on lines +79 to +85
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Approve new "Approach 3" section with minor correction.

The new section effectively introduces the Nix flake approach, aligning with the PR objectives. The command format is clear, and the note about the output file location is valuable for users.

However, there's a minor grammatical error that should be corrected:

In line 84, "you working directory" should be "your working directory".

Please apply this change:

-The text file with failed highlights, which usually would be written to the Zotero2Readwise python package directory, will now be written to you working directory, since nix does not allow writing to package directories.
+The text file with failed highlights, which usually would be written to the Zotero2Readwise python package directory, will now be written to your working directory, since nix does not allow writing to package directories.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
## Approach 3 (through the nix flake)
If you have `nix`, you can skip any installation and do:
```sh
nix run github:e-alizadeh/Zotero2Readwise -- <readwise_token> <zotero_key> <zotero_id>
```
The text file with failed highlights, which usually would be written to the Zotero2Readwise python package directory, will now be written to you working directory, since nix does not allow writing to package directories.
If you don't want this file created, supply `--suppress_failures` as an additional argument.
## Approach 3 (through the nix flake)
If you have `nix`, you can skip any installation and do:
```sh
nix run github:e-alizadeh/Zotero2Readwise -- <readwise_token> <zotero_key> <zotero_id>
```
The text file with failed highlights, which usually would be written to the Zotero2Readwise python package directory, will now be written to your working directory, since nix does not allow writing to package directories.
If you don't want this file created, supply `--suppress_failures` as an additional argument.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~84-~84: “you” seems less likely than “your” (belonging to you).
Context: ...ckage directory, will now be written to you working directory, since nix does not a...

(AI_HYDRA_LEO_CP_YOU_YOUR)


---
# [Zotero2Readwise-Sync](https://github.com/e-alizadeh/Zotero2Readwise-Sync)

Expand Down
155 changes: 155 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
description = "Nix flake for running zotero2readwise script";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
inputs.poetry2nix.url = "github:nix-community/poetry2nix";

outputs = { self, nixpkgs, poetry2nix }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
inherit (poetry2nix.lib.mkPoetry2Nix { inherit pkgs; }) mkPoetryApplication defaultPoetryOverrides;
Comment on lines +6 to +10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance cross-platform compatibility.

The current implementation hardcodes the system to "x86_64-linux". To improve cross-platform compatibility, consider using a more flexible approach.

Replace the hardcoded system with a dynamic approach:

outputs = { self, nixpkgs, poetry2nix }:
  let
    supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
    forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
    pkgs = forAllSystems (system: nixpkgs.legacyPackages.${system});
  in
  {
    apps = forAllSystems (system: {
      default = {
        type = "app";
        program = "${self.packages.${system}.default}/bin/run";
      };
    });
    
    packages = forAllSystems (system: {
      default = let
        inherit (poetry2nix.lib.mkPoetry2Nix { inherit (pkgs.${system}) pkgs; }) mkPoetryApplication defaultPoetryOverrides;
      in
        mkPoetryApplication {
          # ... rest of your mkPoetryApplication configuration
        };
    });
  };

This change will allow the flake to work on multiple architectures and operating systems.

zotero2readwise = mkPoetryApplication {
projectDir = ./.;
# Pyzotero requires setuptools.
# https://github.com/nix-community/poetry2nix/blob/master/docs/edgecases.md
overrides = defaultPoetryOverrides.extend (final: prev: {
pyzotero = prev.pyzotero.overridePythonAttrs ( old: {
buildInputs = (old.buildInputs or [ ]) ++ [ prev.setuptools ];
});
});
};
in
{
apps.${system}.default = {
type = "app";
# replace <script> with the name in the [tool.poetry.scripts] section of your pyproject.toml
program = "${zotero2readwise}/bin/run";
};
};
}

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ env = "GH_TOKEN" # default env name containing the GitHub Token
python = "^3.8"
Pyzotero = "^1.4.26"
requests = "^2.26.0"
setuptools = ">= 45" # necessary for python 3.12

[tool.poetry.group.dev.dependencies]
ipython = "^7.32.0"
pre-commit = "^2.16.0"
ipdb = "^0.13.9"

[tool.poetry.scripts]
run = "zotero2readwise.run:main"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Expand Down
5 changes: 4 additions & 1 deletion zotero2readwise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
from pathlib import Path

__author__ = "Essi Alizadeh"
__version__ = "0.4.5"

TOP_DIR = Path(__file__).parent
FAILED_ITEMS_DIR = TOP_DIR
# If TOP_DIR is readonly os with nix,
# then failed_zotero_items.json is written to the working directory instead.
FAILED_ITEMS_DIR = TOP_DIR if os.access(TOP_DIR, os.W_OK) else Path.cwd()
15 changes: 12 additions & 3 deletions zotero2readwise/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from zotero2readwise.zt2rw import Zotero2Readwise


if __name__ == "__main__":
def main():
parser = ArgumentParser(description="Generate Markdown files")
parser.add_argument(
"readwise_token",
Expand Down Expand Up @@ -47,6 +47,11 @@
action='store_true',
help="Include Zotero items since last run"
)
parser.add_argument(
"--suppress_failures",
action='store_true',
help="Do not write annotations that failed to port to a report file."
)

args = vars(parser.parse_args())

Expand All @@ -68,8 +73,12 @@
include_annotations=args["include_annotations"],
include_notes=args["include_notes"],
filter_colors=args["filter_color"],
since=since
since=since,
write_failures=not args["suppress_failures"]
)
zt2rw.run()
if args["use_since"]:
write_library_version(zt2rw.zotero_client)
write_library_version(zt2rw.zotero_client)

if __name__ == "__main__":
main()
8 changes: 5 additions & 3 deletions zotero2readwise/zt2rw.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def __init__(
include_annotations: bool = True,
include_notes: bool = False,
filter_colors: List[str] = [],
since: int = 0
since: int = 0,
write_failures: bool = True
):
self.readwise = Readwise(readwise_token)
self.zotero_client = get_zotero_client(
Expand All @@ -29,6 +30,7 @@ def __init__(
self.include_annots = include_annotations
self.include_notes = include_notes
self.since = since
self.write_failures = write_failures

def get_all_zotero_items(self) -> List[Dict]:
"""
Expand All @@ -54,7 +56,7 @@ def run(self, zot_annots_notes: List[Dict] = None) -> None:

formatted_items = self.zotero.format_items(zot_annots_notes)

if self.zotero.failed_items:
if self.write_failures and self.zotero.failed_items:
self.zotero.save_failed_items_to_json("failed_zotero_items.json")

self.readwise.post_zotero_annotations_to_readwise(formatted_items)
Expand All @@ -80,4 +82,4 @@ def retrieve_all(self, item_type: str, since: int = 0):

print("It may take some time...")
query = self.zotero_client.items(itemType={item_type}, since=since)
return self.zotero_client.everything(query)
return self.zotero_client.everything(query)