Skip to content
Merged
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
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[build-system]
requires = ["setuptools>=64.0.0", "wheel"]
requires = ["setuptools>=64.0.0", "wheel", "PyYAML>=6.0"]
build-backend = "setuptools.build_meta"

[project]
name = "universalpython"
version = "0.5.1-b.2"
dynamic = ["scripts"]
authors = [
{name = "Saad Bazaz", email = "saadbazaz@hotmail.com"},
]
Expand Down Expand Up @@ -41,9 +42,6 @@ packages = ["universalpython"]
[tool.setuptools.package-data]
universalpython = ["languages/**/*.yaml", "modes/*.py"]

[project.scripts]
universalpython = "universalpython.universalpython:main"

[tool.semantic_release]
version_toml = ["pyproject.toml:project.version"]
upload_to_pypi = false
Expand Down
30 changes: 30 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from setuptools import setup
import os
import yaml

LANGUAGES_DIR = os.path.join(os.path.dirname(__file__), 'universalpython', 'languages')

def get_aliases():
aliases = []
if not os.path.exists(LANGUAGES_DIR):
return aliases
for lang_dir in os.listdir(LANGUAGES_DIR):
yaml_path = os.path.join(LANGUAGES_DIR, lang_dir, 'default.yaml')
if not os.path.isfile(yaml_path):
continue
with open(yaml_path, encoding='utf-8') as f:
data = yaml.safe_load(f)
for alias in data.get('aliases', []):
aliases.append(f'{alias}=universalpython.universalpython:main')
return aliases

console_scripts = [
'universalpython=universalpython.universalpython:main',
'unipy=universalpython.universalpython:main',
] + get_aliases()

setup(
entry_points={
'console_scripts': console_scripts,
}
)
4 changes: 4 additions & 0 deletions universalpython/languages/cs/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "univerzálnípython"
- "českýpython"

letters:
start : "A"
end : "Ž"
Expand Down
4 changes: 4 additions & 0 deletions universalpython/languages/de/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "universellpython"
- "deutschpython"

letters:
start : "a"
end : "z"
Expand Down
3 changes: 3 additions & 0 deletions universalpython/languages/emoji/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
aliases:
- "emojipython"

letters:
start : "a"
end : "z"
Expand Down
4 changes: 4 additions & 0 deletions universalpython/languages/fr/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "pythonuniversel"
- "françaispython"

letters:
start : "a"
end : "z"
Expand Down
4 changes: 4 additions & 0 deletions universalpython/languages/ga/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "pythonuile"
- "gaeilgepython"

letters:
start : "a"
end : "z"
Expand Down
5 changes: 5 additions & 0 deletions universalpython/languages/hi/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
aliases:
- "hindipython"
- "यूनिवर्सलपाइथन"
- "हिंदीपाइथन"

letters:
start : "अ"
end : "य़"
Expand Down
4 changes: 4 additions & 0 deletions universalpython/languages/hu/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "univerzálispython"
- "magyarpython"

letters:
start : "a"
end : "z"
Expand Down
4 changes: 4 additions & 0 deletions universalpython/languages/ko/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "유니버설파이썬"
- "한국어파이썬"

letters:
start : "가"
end : "힣"
Expand Down
4 changes: 4 additions & 0 deletions universalpython/languages/nl/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "universelepython"
- "nederlandsepython"

letters:
start : "a"
end : "z"
Expand Down
4 changes: 4 additions & 0 deletions universalpython/languages/tr/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "evrenselpython"
- "türkçepython"

letters:
start : "a"
end : "z"
Expand Down
4 changes: 4 additions & 0 deletions universalpython/languages/ua/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "універсальнийпітон"
- "українськапітон"

letters:
start : "Ж"
end : "і"
Expand Down
4 changes: 4 additions & 0 deletions universalpython/languages/ur/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
aliases:
- "یونیورسلپایتھان"
- "اردوپایتھان"

letters:
start : "ا"
end : "ی"
Expand Down
4 changes: 2 additions & 2 deletions universalpython/modes/lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run(args, code):
dictionary = args.get("dictionary", "")
if dictionary:
try:
with open(dictionary) as f:
with open(dictionary, encoding='utf-8') as f:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why was this necessary, can you explain

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To handle international characters.
The alias system was breaking on my system (Windows) otherwise

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Alright, are you able to test this locally on Linux and macOS?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

image

Sorry i don't have access to macOS, tested it on Linux, works perfectly

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great. I don't think this will cause issues on macOS then. Will merge soon.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Cool :)

language_dict = yaml.safe_load(f)
except (yaml.YAMLError, OSError) as e:
if not args.get("suppress_warnings", False):
Expand Down Expand Up @@ -170,7 +170,7 @@ def t_error(t):

# Output handling
if args.get("keep") or args.get("keep_only"):
with open("compiled.en.py", "w") as f:
with open("compiled.en.py", "w", encoding='utf-8') as f:
f.write(compiled_code)

if args.get("return"):
Expand Down
29 changes: 28 additions & 1 deletion universalpython/universalpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,22 @@ def build_language_map():
language_map[lang_dir] = lang_files

return language_map

def build_alias_map():
"""Build a map of {alias_name: lang_code} from language YAML files."""
alias_map = {}
for lang_code, lang_files in DEFAULT_LANGUAGE_MAP.items():
if 'default' not in lang_files:
continue
with open(lang_files['default'], encoding='utf-8') as f:
data = yaml.safe_load(f)
for alias in data.get('aliases', []):
alias_map[alias] = lang_code
return alias_map

# Build language map at module load
DEFAULT_LANGUAGE_MAP = build_language_map()
DEFAULT_ALIAS_MAP = build_alias_map()

def detect_language_from_filename(filename):
"""Detect language from file extension (e.g., my-program.de.py -> german)
Expand Down Expand Up @@ -71,6 +85,18 @@ def detect_language_from_comment(code):
return (first_file, lang_code)
return None

def detect_language_from_alias(invoked_name):
"""Detect language from command alias.
Returns: (filepath, lang_code) or None"""
command = os.path.basename(invoked_name).lower()
lang_code = DEFAULT_ALIAS_MAP.get(command)

if lang_code and lang_code in DEFAULT_LANGUAGE_MAP:
lang_files = DEFAULT_LANGUAGE_MAP[lang_code]
first_file = next(iter(lang_files.values())) if lang_files else None
return (first_file, lang_code)
return None

def determine_language(args, filename, code):
"""Determine target language based on priority rules"""
detected_dictionary = None
Expand All @@ -85,6 +111,7 @@ def determine_language(args, filename, code):
else:
detected_dictionary, detected_lang = (detect_language_from_comment(code) or
detect_language_from_filename(filename) or
detect_language_from_alias(sys.argv[0]) or
(None, None))

# Update source_language with the detected language if not explicitly set
Expand Down Expand Up @@ -171,7 +198,7 @@ def main():
args = vars(ap.parse_args())

filename = args["file"][0]
with open(filename) as code_pyfile:
with open(filename, encoding='utf-8') as code_pyfile:
code = code_pyfile.read()

# Default mode is 'lex' if not specified
Expand Down