Skip to content

Commit 28cdaca

Browse files
authored
Ask version 1.1.0 (#72)
* Repl. mode. * F-strings. * Raw strings. * Abnex & Regex.
1 parent 55ea91c commit 28cdaca

30 files changed

+764
-738
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ __pypackages__/
9898
celerybeat-schedule
9999
celerybeat.pid
100100

101-
# SageMath parsed files
101+
# SageMath translated files
102102
*.sage.py
103103

104104
# Environments

CONTRIBUTING.md

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,16 @@
1515
- (You can also add **tags** if you want)
1616

1717
- If your PR is **approved** it will get merged either into `dev` or `master`.
18-
- If it gets marked as **Changes requested**, do the chanages, **commit** & **push**, and request **re-review* by @buscedv
18+
- If it gets marked as **Changes requested**, do the chanages, **commit** & **push**, and request **re-review** by @buscedv.
1919

2020
## Code style
2121
- Use tabs.
2222
- Use single quotes `'` and not double quotes `"`.
2323
- If you have to use some sort of string formating use f-strings.
24-
- Don't put unneccesary comments.
25-
- Follow the PEP8 guidelines, ex. two empty lines between methods.
26-
- Ask is supposed to be in a single python script i.e. `ask.py`.
27-
- A transpiled Ask program `app.py` should be able to run completely standalone. So all neccesary function, classes, utilities, etc. needs to be added to the transpiled code. This means no module importing.
28-
29-
## Dev environment
30-
1. Create a vritual environment:
31-
- `python3 -m venv venv`
32-
2. Activate it:
33-
- `source venv/bin/activate`
34-
3. Install dependencies:
35-
- `pip install -r requirements.txt`
36-
37-
**When running ask**
38-
39-
4. Get helpful info and debug messages:
40-
41-
- Run ask with the `-d` flag.
42-
- `python3 ask.py [my file].ask -d`
24+
- Don't put unnecessary comments.
25+
- But when you do add comments make sure that:
26+
- There is a space after the pund/hashtag.
27+
- The comment starts with a capital letter.
28+
- It ends with a full-stop/period.
29+
- Follow the PEP8 guidelines, e.g. two empty lines between function definitions.
30+
- A transpiled Ask program `app.py` should be able to run completely standalone. So all necessary functions, classes, utilities, etc. needs to be added to the transpiled code. This means no module importing in the output file!

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ The transpiled app is completely standalone and doesn't require Ask in any way.
3434
- `$ pipx install ask-lang`.
3535
- Then run your apps with: `$ ask [your file].ask`.
3636

37+
## Run locally
38+
1. Clone this repo: `https://github.com/Buscedv/Ask.git`.
39+
2. Install [Poetry](https://python-poetry.org/).
40+
3. Create a new virtual environment: `python3 venv venv`.
41+
4. Activate it: `source venv/bin/activate`.
42+
5. Install dependencies: `poetry install`.
43+
6. (Optional but helpful in some cases) Run Ask in development mode: [Docs](https://docs.ask-lang.org/development-tools/running-in-development-mode1).
44+
45+
If you want to contribute please read the CONTRIBUTING.md file for code style, standards, etc.
46+
3747
## Example (Ask vs Flask)
3848
Here is the same basic app with one GET route written in Ask and in Python with Flask.
3949

@@ -88,9 +98,9 @@ if __name__ == '__main__':
8898
As you can see Ask hides away all the clutter and boilerplate.
8999

90100
## Documentation
91-
You can find the full documentation on [docs.ask-lang.org](https://docs.ask-lang.org)
101+
You can find the full documentation on [docs.ask-lang.org](https://docs.ask-lang.org).
92102

93103
## Contact
94-
- Website: [ask-lang.org](https://ask-lang.org)
95-
- Email: [me(a)edvard.dev](mailto:me@edvard.dev)
96-
- GitHub: [Buscedv](https://github.com/Buscedv)
104+
- Website: [ask-lang.org](https://ask-lang.org).
105+
- Email: [me(a)edvard.dev](mailto:me@edvard.dev).
106+
- GitHub: [Buscedv](https://github.com/Buscedv).

ask_lang/__main__.py

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,109 @@
22

33
import os
44
import sys
5+
from typing import List, Tuple
6+
7+
from tabulate import tabulate
58

69
from ask_lang import cfg
710
from ask_lang.transpiler import transpiler
8-
from ask_lang.utilities import utils
11+
from ask_lang.utilities import files, serve_run, printing, askfile
12+
13+
14+
def parse_sys_args(sys_args: List[str]) -> Tuple[str, bool]:
15+
flags = ['-d', '--dev', '-xd', '--extra-dev', '-v', '--version', '-h', '--help']
16+
17+
file_name = ''
18+
no_valid_flags = True
19+
20+
for param in sys_args[1:]:
21+
if param in flags:
22+
no_valid_flags = False
23+
24+
if param in ['-d', '--dev']:
25+
cfg.is_dev = True
26+
if param in ['-xd', '--extra-d']:
27+
cfg.is_extra_dev = True
28+
printing.style_print('Extra Dev Mode Activated!', 'red', ['bold'])
29+
elif param in ['-v', '--version']:
30+
printing.style_print('- Version:', color='blue', end=' ')
31+
print(cfg.project_information["version"])
32+
elif param in ['-h', '--help']:
33+
print('Usage: ask_lang [OPTIONS] [FILE]...', end='\n\n')
34+
print(tabulate(
35+
[
36+
['-h', '--help', 'Show this message.'],
37+
['-v', '--version', 'Show version information.'],
38+
['-d', '--d', 'Turn on developer/debug mode.'],
39+
],
40+
headers=['Option', 'Long Format', 'Description']
41+
))
42+
print()
43+
print('Other configurations can be added to a file called `Askfile.toml`.')
44+
print('Go to: https://docs.ask-lang.org for more information', end='\n\n')
45+
else:
46+
file_name = param
47+
48+
if cfg.is_dev and file_name == '':
49+
no_valid_flags = True
50+
51+
return file_name, no_valid_flags
52+
53+
54+
def repl(first_time: bool = False):
55+
cfg.is_repl = True
56+
57+
if first_time:
58+
printing.style_print('Type "q" to quit.', color='gray')
59+
60+
line = input('Ask >>> ')
61+
62+
# Quit/Exit
63+
if line == 'q':
64+
files.maybe_delete_app(True)
65+
return
66+
67+
transpiler.transpile([line])
68+
serve_run.run_server()
69+
70+
repl()
971

1072

1173
def main():
12-
utils.initial_print()
74+
printing.initial_print()
1375

1476
if len(sys.argv) > 1:
15-
param_file_name, no_valid_flags = utils.parse_sys_args(sys.argv)
77+
param_file_name, no_valid_flags = parse_sys_args(sys.argv)
78+
79+
if not param_file_name and True in [x in sys.argv for x in ['-d', '--dev', '-xd', '--extra-dev']]:
80+
repl(True)
1681

1782
cfg.source_file_name = param_file_name
1883
if os.path.isfile(f'{os.getcwd()}/{cfg.source_file_name}'):
1984
# Transpiles.
2085
with open(cfg.source_file_name) as f:
2186
source_lines = f.readlines()
2287

88+
if not source_lines:
89+
printing.style_print('\t- The file is empty!', color='red')
90+
exit(1)
91+
2392
transpiler.transpile(source_lines)
2493

25-
# Starts server
26-
utils.run_server()
94+
if askfile.get(['system', 'server'], True):
95+
# Starts server
96+
serve_run.run_server()
97+
else:
98+
printing.style_print('\nAuto start server is turned OFF.', styles=['bold'])
99+
print('\t - The transpiled code can be found in:', end=' ')
100+
printing.style_print(askfile.get(['system', 'output_path'], 'app.py'), color='blue', end='')
101+
print('.')
27102
else:
28103
if no_valid_flags:
29-
utils.style_print('- The file could not be found!', color='red')
104+
printing.style_print('- The file could not be found!', color='red')
30105
else:
31-
# TODO: Launch Repl.
32-
utils.style_print('- Please provide a script file!', color='red')
106+
repl(True)
33107

34108

35109
if __name__ == '__main__':
36110
main()
37-

ask_lang/cfg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
ask_config = {}
4545

4646
project_information = {
47-
'version': '1.0.0',
47+
'version': '1.1.0',
4848
}
4949

5050
source_file_name = ''
5151
is_dev = False
5252
is_extra_dev = False
53+
is_repl = False
54+
repl_previous_transpiled = ''
5355
transpilation_result = defaultdict(lambda: '', {})

ask_lang/tests/transpiler/test_parser.py

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# coding=utf-8
2+
import unittest
3+
4+
from ask_lang.transpiler import translator
5+
6+
7+
class TestTranspilerTranslatorInsertBasicDecoratorCodeToInsert(unittest.TestCase):
8+
# TODO: Add advanced tests here.
9+
pass
10+
11+
12+
class TestTranspilerTranslatorTranslate(unittest.TestCase):
13+
# TODO: Add advanced tests here.
14+
pass
15+
16+
17+
if __name__ == '__main__':
18+
unittest.main()

ask_lang/tests/transpiler/utilities/test_lexer_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def test_add_new_line_at_the_end(self):
7676
self.assertEqual('word\n', lexer_utils.reformat_line('word\n'))
7777

7878
def test_quotes(self):
79-
self.assertEqual('"Hello, World!"\n', lexer_utils.reformat_line('\'Hello, World!\''))
79+
self.assertEqual('\'Hello, World!\'\n', lexer_utils.reformat_line('\'Hello, World!\''))
80+
self.assertEqual('"Hello, World!"\n', lexer_utils.reformat_line('"Hello, World!"'))
81+
self.assertEqual('"Hello, World!" test\n', lexer_utils.reformat_line('"Hello, World!" test'))
8082

8183
def test_space_before_parenthesis(self):
8284
self.assertEqual('print()\n', lexer_utils.reformat_line('print ()\n'))

0 commit comments

Comments
 (0)