Skip to content

Commit

Permalink
add is_url and url_contents functions
Browse files Browse the repository at this point in the history
  • Loading branch information
javierluraschi committed Jun 14, 2024
1 parent ef34cff commit 13140e9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.3.2

- Added `is_url` and `url_contents` to assist URL processing

## 2.2.6

- Add `hidden` parameter to `save` to save file as hidden
Expand Down
1 change: 1 addition & 0 deletions python/hal9/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from hal9.deploy import deploy
from hal9.iobind import load, save, input
from hal9.code import extract
from hal9.urls import is_url, url_contents
4 changes: 2 additions & 2 deletions python/hal9/iobind.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import os
from pathlib import Path
from hal9.filetotext import extract_from_url
from hal9.urls import url_contents

def add_extension(path):
_, extension = os.path.splitext(path)
Expand Down Expand Up @@ -54,5 +54,5 @@ def save(name, contents, hidden = False):
def input(prompt = "", extract = True):
text = original_input(prompt)
if extract:
text = extract_from_url(text)
text = url_contents(text)
return text
19 changes: 11 additions & 8 deletions python/hal9/filetotext.py → python/hal9/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import urllib.request
import importlib.util

def extract_from_url(message):
def is_url(prompt):
result = urllib.parse.urlparse(prompt)
return all([result.scheme, result.netloc])

def url_contents(prompt):
try:
result = urllib.parse.urlparse(message)
if not all([result.scheme, result.netloc]):
return message
if not is_url(prompt):
return prompt

textract_spec = importlib.util.find_spec("textract")
if textract_spec is None:
return message
return prompt

file_extension = os.path.splitext(result.path)[1]
file_extension = os.path.splitext(prompt)[1]

with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) as temp_file:
with urllib.request.urlopen(message) as response:
with urllib.request.urlopen(prompt) as response:
temp_file.write(response.read())
temp_file_path = temp_file.name

Expand All @@ -26,4 +29,4 @@ def extract_from_url(message):
os.remove(temp_file_path)
return text
except Exception as e:
return message
return prompt
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hal9"
version = "2.3.1"
version = "2.3.2"
description = ""
authors = ["Javier Luraschi <[email protected]>"]
readme = "README.md"
Expand Down
19 changes: 19 additions & 0 deletions website/reference/url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# URL

Convenience functions to process URL prompts.

## IS
`is_url(prompt)` <br/><br/>
Tests if `prompt` is a URL.

| Param | Type | Description |
| --- | --- | --- |
| prompt | <code>String</code> | The prompt to test. |

## CONTENTS
`url_contents(prompt)` <br/><br/>
Retrieves the contents from a `prompt` that is a URL.

| Param | Type | Description |
| --- | --- | --- |
| prompt | <code>String</code> | The prompt to retrieve. |

0 comments on commit 13140e9

Please sign in to comment.