ssb-pubmd 0.0.11__tar.gz → 0.0.13__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: ssb-pubmd
3
- Version: 0.0.11
3
+ Version: 0.0.13
4
4
  Summary: SSB Pubmd
5
5
  License: MIT
6
6
  Author: Olav Landsverk
@@ -55,43 +55,29 @@ Description-Content-Type: text/markdown
55
55
 
56
56
  - Supports logging in through a popup browser window.
57
57
 
58
- ## Requirements
59
-
60
- - This library uses [playwright](https://github.com/microsoft/playwright-python) to create a logged in browser context. This requires installing a [browser binary](https://playwright.dev/python/docs/browsers#install-browsers) and necessary [system dependencies](https://playwright.dev/python/docs/browsers#install-system-dependencies).
61
58
 
62
59
  ## Installation
63
60
 
64
- You can install _SSB Pubmd_ via [pip] from [PyPI]:
61
+ Installation with pip:
65
62
 
66
63
  ```console
67
64
  pip install ssb-pubmd
68
65
  ```
69
66
 
70
- ## Usage
71
-
72
- First set environment variables:
67
+ If you need to create a logged-in browser context, you will also need to install a [Playwright browser](https://playwright.dev/python/docs/browsers#install-browsers):
73
68
 
74
69
  ```console
75
- export PUBMD_LOGIN_URL=<https://www.example.com/login>
76
- export PUBMD_POST_URL=<https://www.example.com/post>
70
+ playwright install --with-deps chromium`
77
71
  ```
78
72
 
79
- To log in, run:
80
-
81
- ```console
82
- pubmd login
83
- ```
84
-
85
- Close the popup browser window when you are logged in.
73
+ ## Usage
86
74
 
87
- To synchronize markdown content to the CMS server, run:
75
+ Run the main command in a terminal to see available subcommands with documentation:
88
76
 
89
77
  ```console
90
- pubmd sync <file>
78
+ pubmd
91
79
  ```
92
80
 
93
- `<file>` should be an absolute or relative path, and the allowed extensions are `.ipynb` and `md`.
94
-
95
81
  ## Contributing
96
82
 
97
83
  Contributions are very welcome.
@@ -29,43 +29,29 @@
29
29
 
30
30
  - Supports logging in through a popup browser window.
31
31
 
32
- ## Requirements
33
-
34
- - This library uses [playwright](https://github.com/microsoft/playwright-python) to create a logged in browser context. This requires installing a [browser binary](https://playwright.dev/python/docs/browsers#install-browsers) and necessary [system dependencies](https://playwright.dev/python/docs/browsers#install-system-dependencies).
35
32
 
36
33
  ## Installation
37
34
 
38
- You can install _SSB Pubmd_ via [pip] from [PyPI]:
35
+ Installation with pip:
39
36
 
40
37
  ```console
41
38
  pip install ssb-pubmd
42
39
  ```
43
40
 
44
- ## Usage
45
-
46
- First set environment variables:
41
+ If you need to create a logged-in browser context, you will also need to install a [Playwright browser](https://playwright.dev/python/docs/browsers#install-browsers):
47
42
 
48
43
  ```console
49
- export PUBMD_LOGIN_URL=<https://www.example.com/login>
50
- export PUBMD_POST_URL=<https://www.example.com/post>
44
+ playwright install --with-deps chromium`
51
45
  ```
52
46
 
53
- To log in, run:
54
-
55
- ```console
56
- pubmd login
57
- ```
58
-
59
- Close the popup browser window when you are logged in.
47
+ ## Usage
60
48
 
61
- To synchronize markdown content to the CMS server, run:
49
+ Run the main command in a terminal to see available subcommands with documentation:
62
50
 
63
51
  ```console
64
- pubmd sync <file>
52
+ pubmd
65
53
  ```
66
54
 
67
- `<file>` should be an absolute or relative path, and the allowed extensions are `.ipynb` and `md`.
68
-
69
55
  ## Contributing
70
56
 
71
57
  Contributions are very welcome.
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "ssb-pubmd"
3
- version = "0.0.11"
3
+ version = "0.0.13"
4
4
  description = "SSB Pubmd"
5
5
  authors = ["Olav Landsverk <stud-oll@ssb.no>"]
6
6
  license = "MIT"
@@ -0,0 +1,92 @@
1
+ """Command-line interface."""
2
+
3
+ import json
4
+
5
+ import click
6
+
7
+ from ssb_pubmd.browser_context import BrowserRequestContext as RequestContext
8
+ from ssb_pubmd.markdown_syncer import MarkdownSyncer
9
+
10
+ CONFIG_FILE = "pubmd_config.json"
11
+
12
+
13
+ class ConfigKeys:
14
+ """The keys used in the configuration file."""
15
+
16
+ LOGIN = "login_url"
17
+ POST = "post_url"
18
+
19
+
20
+ def get_config_value(key: str) -> str:
21
+ """Get a configuration value."""
22
+ try:
23
+ with open(CONFIG_FILE) as json_file:
24
+ config = json.load(json_file)
25
+ value = config[key]
26
+ except FileNotFoundError:
27
+ click.echo(
28
+ f"Configuration file '{CONFIG_FILE}' not found. Please run the 'config' command first."
29
+ )
30
+ except Exception:
31
+ click.echo(
32
+ f"Error reading configuration file '{CONFIG_FILE}'. Please run the 'pubmd config' command again."
33
+ )
34
+ return str(value)
35
+
36
+
37
+ @click.group()
38
+ def cli() -> None:
39
+ """'pubmd' is a tool to sync markdown and notebook files to a CMS application.
40
+
41
+ Setup with subcommands 'config' and 'login', then use subcommand 'sync'.
42
+ """
43
+ pass
44
+
45
+
46
+ @click.command()
47
+ def config() -> None:
48
+ """Configure the CMS to connect to."""
49
+ login_url = click.prompt("Enter the login URL", type=str)
50
+ post_url = click.prompt("Enter the post URL", type=str)
51
+
52
+ config = {ConfigKeys.LOGIN: login_url, ConfigKeys.POST: post_url}
53
+
54
+ with open(CONFIG_FILE, "w") as json_file:
55
+ json.dump(config, json_file, indent=4)
56
+
57
+ click.echo(f"\nThe configuration has been stored in:\n{CONFIG_FILE}")
58
+
59
+
60
+ @click.command()
61
+ def login() -> None:
62
+ """Login to the CMS application."""
63
+ login_url = get_config_value(ConfigKeys.LOGIN)
64
+ request_context = RequestContext()
65
+ storage_state_file, storage_state = request_context.create_new(login_url)
66
+ click.echo(f"\nThe browser context has been stored in:\n{storage_state_file}")
67
+
68
+
69
+ @click.command()
70
+ @click.argument("content_file_path", type=click.Path())
71
+ def sync(content_file_path: str) -> None:
72
+ """Sync a markdown or notebook file to the CMS."""
73
+ post_url = get_config_value(ConfigKeys.POST)
74
+ request_context = RequestContext()
75
+ request_context.recreate_from_file()
76
+
77
+ syncer = MarkdownSyncer(post_url=post_url, request_context=request_context)
78
+ syncer.content_file_path = content_file_path
79
+
80
+ content_id = syncer.sync_content()
81
+
82
+ click.echo(
83
+ f"File '{click.format_filename(content_file_path)}' synced to CMS with content ID: {content_id}"
84
+ )
85
+
86
+
87
+ cli.add_command(config)
88
+ cli.add_command(login)
89
+ cli.add_command(sync)
90
+
91
+ if __name__ == "__main__":
92
+ cli() # pragma: no cover
@@ -4,7 +4,7 @@ from playwright.sync_api import sync_playwright
4
4
 
5
5
  from .markdown_syncer import Response
6
6
 
7
- BROWSER_CONTEXT_FILE = "browser_context.json"
7
+ BROWSER_CONTEXT_FILE = "pubmd_browser_context.json"
8
8
 
9
9
 
10
10
  class BrowserRequestContext:
@@ -52,11 +52,9 @@ class BrowserRequestContext:
52
52
  if self._context is None:
53
53
  raise ValueError("Browser context has not been created.")
54
54
 
55
- # print(self._context.cookies(url))
56
-
57
55
  api_response = self._context.request.post(
58
56
  url,
59
- data=data,
57
+ params=data,
60
58
  )
61
59
 
62
60
  try:
@@ -1,52 +0,0 @@
1
- """Command-line interface."""
2
-
3
- import os
4
-
5
- import click
6
-
7
- from ssb_pubmd.browser_context import BrowserRequestContext as RequestContext
8
- from ssb_pubmd.markdown_syncer import MarkdownSyncer
9
-
10
-
11
- @click.group()
12
- def cli() -> None:
13
- """Command-line interface for the ssb_pubmd package."""
14
- pass
15
-
16
-
17
- @click.command()
18
- def login() -> None:
19
- """Login to the server."""
20
- login_url = os.getenv("PUBMD_LOGIN_URL", "")
21
- request_context = RequestContext()
22
- print(login_url)
23
- storage_state_file, storage_state = request_context.create_new(login_url)
24
- click.echo(
25
- f"The following browser context object is now stored in {storage_state_file}:"
26
- )
27
- click.echo(storage_state)
28
-
29
-
30
- @click.command()
31
- @click.argument("content_file_path", type=click.Path())
32
- def sync(content_file_path: str) -> None:
33
- """Sync the content."""
34
- post_url = os.getenv("PUBMD_POST_URL", "")
35
- request_context = RequestContext()
36
- request_context.recreate_from_file()
37
-
38
- syncer = MarkdownSyncer(post_url=post_url, request_context=request_context)
39
- syncer.content_file_path = content_file_path
40
-
41
- content_id = syncer.sync_content()
42
-
43
- click.echo(
44
- f"File '{click.format_filename(content_file_path)}' synced to CMS with content ID: {content_id}"
45
- )
46
-
47
-
48
- cli.add_command(login)
49
- cli.add_command(sync)
50
-
51
- if __name__ == "__main__":
52
- cli() # pragma: no cover
File without changes