From ee6958af021add511d97cc85c34d4c741d5b826c Mon Sep 17 00:00:00 2001 From: Cyril ZORMAN Date: Fri, 10 Jan 2025 12:40:11 +0100 Subject: [PATCH] add ruff linter in CI --- .github/workflows/ruff.yml | 8 +++ .github/workflows/unittest.yml | 2 +- API/utils.py | 8 +-- app.py | 14 ++-- lotemplate/CalcTemplate.py | 17 +---- lotemplate/Statement/CalcSearchStatement.py | 4 +- lotemplate/Statement/CalcTableStatement.py | 15 ++-- lotemplate/Statement/CounterStatement.py | 22 +++--- lotemplate/Statement/ForStatement.py | 2 +- lotemplate/Statement/HtmlStatement.py | 9 ++- lotemplate/Statement/IfStatement.py | 2 +- lotemplate/Statement/ImageStatement.py | 2 +- lotemplate/Statement/TableStatement.py | 2 +- lotemplate/Statement/TextStatement.py | 2 +- lotemplate/Template.py | 20 ++---- lotemplate/WriterTemplate.py | 13 +--- lotemplate/__init__.py | 17 +++-- lotemplate/connexion.py | 19 ++--- lotemplate/lofunction.py | 12 ++-- lotemplate/unittest/test_cachedjson.py | 8 +-- lotemplate/unittest/test_calc.py | 11 +-- lotemplate/unittest/test_comparaison.py | 14 ++-- lotemplate/unittest/test_content.py | 6 +- lotemplate/unittest/test_function.py | 19 ++--- lotemplate/unittest/test_json_convertion.py | 7 +- lotemplate/unittest/test_template_scan.py | 77 ++++++++++----------- lotemplate/utils.py | 9 ++- lotemplate_cli.py | 7 +- 28 files changed, 133 insertions(+), 215 deletions(-) create mode 100644 .github/workflows/ruff.yml diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml new file mode 100644 index 0000000..4adeffd --- /dev/null +++ b/.github/workflows/ruff.yml @@ -0,0 +1,8 @@ +name: Ruff +on: [ push, pull_request ] +jobs: + ruff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: astral-sh/ruff-action@v3 diff --git a/.github/workflows/unittest.yml b/.github/workflows/unittest.yml index 2755c4e..a9f6db4 100644 --- a/.github/workflows/unittest.yml +++ b/.github/workflows/unittest.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 diff --git a/API/utils.py b/API/utils.py index a61fced..4b6cad7 100644 --- a/API/utils.py +++ b/API/utils.py @@ -2,20 +2,14 @@ Copyright (C) 2023 Probesys """ -from flask import * +from flask import Response, send_file import lotemplate as ot -import configargparse as cparse import glob import os import sys -import shlex, subprocess -from time import sleep from typing import Union -from zipfile import ZipFile -import random -import pdb host='localhost' port='200' diff --git a/app.py b/app.py index 10a56ad..9801195 100644 --- a/app.py +++ b/app.py @@ -2,14 +2,12 @@ Copyright (C) 2023 Probesys """ -from flask import * +from flask import Flask,request, jsonify,after_this_request,send_file from werkzeug.utils import secure_filename import os -import hashlib from shutil import copyfile, rmtree -import pdb from os.path import isfile, join from os import listdir from API import utils @@ -22,7 +20,7 @@ def main_route(): if request.headers.get('secretkey', '') != os.environ.get('SECRET_KEY', ''): return utils.error_sim( - 'ApiError', 'invalid_secretkey', f"The secret key is invalid or not given", {'key': 'secret_key'}), 401 + 'ApiError', 'invalid_secretkey', "The secret key is invalid or not given", {'key': 'secret_key'}), 401 if request.method == 'PUT': if 'directory' not in request.headers: return utils.error_sim( @@ -43,7 +41,7 @@ def main_route(): def directory_route(directory): if request.headers.get('secretkey', '') != os.environ.get('SECRET_KEY', ''): return utils.error_sim( - 'ApiError', 'invalid_secretkey', f"The secret key is invalid or not given", {'key': 'secret_key'}), 401 + 'ApiError', 'invalid_secretkey', "The secret key is invalid or not given", {'key': 'secret_key'}), 401 if not os.path.isdir(f"uploads/{directory}") and request.method != 'PUT': return utils.error_sim( 'ApiError', 'dir_not_found', f"the specified directory {repr(directory)} doesn't exist", @@ -96,12 +94,12 @@ def delete_image(response): if request.method == 'POST': try: os.remove(file) - except Exception as ex: + except Exception: print("Error delete file " + str(file)) return response if request.headers.get('secretkey', '') != os.environ.get('SECRET_KEY', ''): return utils.error_sim( - 'ApiError', 'invalid_secretkey', f"The secret key is invalid or not given", {'key': 'secret_key'}), 401 + 'ApiError', 'invalid_secretkey', "The secret key is invalid or not given", {'key': 'secret_key'}), 401 if not os.path.isdir(f"uploads/{directory}"): return utils.error_sim( 'ApiError', 'dir_not_found', f"the specified directory {repr(directory)} doesn't exist", @@ -145,7 +143,7 @@ def delete_image(response): def download_route(directory, file): if request.headers.get('secretkey', '') != os.environ.get('SECRET_KEY', ''): return utils.error_sim( - 'ApiError', 'invalid_secretkey', f"The secret key is invalid or not given", {'key': 'secret_key'}), 401 + 'ApiError', 'invalid_secretkey', "The secret key is invalid or not given", {'key': 'secret_key'}), 401 if not os.path.isdir(f"uploads/{directory}"): return utils.error_sim( 'ApiError', 'dir_not_found', f"the specified directory {repr(directory)} doesn't exist", diff --git a/lotemplate/CalcTemplate.py b/lotemplate/CalcTemplate.py index a34f1e9..47c37d4 100644 --- a/lotemplate/CalcTemplate.py +++ b/lotemplate/CalcTemplate.py @@ -8,28 +8,15 @@ __all__ = ( 'CalcTemplate', ) -import os from typing import Union -from sorcery import dict_of -import uno -import unohelper -from com.sun.star.beans import PropertyValue -from com.sun.star.io import IOException -from com.sun.star.lang import IllegalArgumentException, DisposedException -from com.sun.star.connection import NoConnectException -from com.sun.star.uno import RuntimeException -from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK -from com.sun.star.style.BreakType import PAGE_AFTER from . import errors -from .utils import * from lotemplate.Statement.CalcTableStatement import CalcTableStatement -from . import Template +from .Template import Template from lotemplate.Statement.CalcSearchStatement import CalcTextStatement -import pdb from jsondiff import diff class CalcTemplate(Template): @@ -80,7 +67,7 @@ def scan(self, **kwargs) -> dict[str: dict[str, Union[str, list[str]]]]: :return: list containing all the variables founded in the template """ - should_close = kwargs.get("should_close", False) + #should_close = kwargs.get("should_close", False) texts = {} #(Pdb) self.doc.getSheets().getElementNames() for sheet in self.doc.getSheets(): diff --git a/lotemplate/Statement/CalcSearchStatement.py b/lotemplate/Statement/CalcSearchStatement.py index a60932f..b726047 100644 --- a/lotemplate/Statement/CalcSearchStatement.py +++ b/lotemplate/Statement/CalcSearchStatement.py @@ -1,8 +1,6 @@ import re from com.sun.star.lang import XComponent -from com.sun.star.beans import PropertyValue, UnknownPropertyException import regex -import pdb class CalcTextStatement: text_regex_str = r'\$(\w+(\(((?:\\.|.)*?)\))?)' @@ -65,7 +63,7 @@ def fill(doc: XComponent, variable: str, value: str) -> None: search = doc.createReplaceDescriptor() search.SearchString = variable search.ReplaceString = value - founded = doc.replaceAll(search) + doc.replaceAll(search) diff --git a/lotemplate/Statement/CalcTableStatement.py b/lotemplate/Statement/CalcTableStatement.py index cc8c90e..d295622 100644 --- a/lotemplate/Statement/CalcTableStatement.py +++ b/lotemplate/Statement/CalcTableStatement.py @@ -1,12 +1,9 @@ from com.sun.star.lang import XComponent -from com.sun.star.uno import RuntimeException from typing import Union -import lotemplate.errors as errors from lotemplate.Statement.CalcSearchStatement import CalcTextStatement -from com.sun.star.sheet.CellInsertMode import NONE, DOWN, RIGHT, ROWS,COLUMNS +from com.sun.star.sheet.CellInsertMode import DOWN, RIGHT from com.sun.star.sheet.CellDeleteMode import UP, LEFT import re -import pdb def incr_chr(c): @@ -26,7 +23,7 @@ class CalcTableStatement: table_pattern = re.compile("^table_[udlr]_(.+)",re.IGNORECASE) def isTableVar(var): - if mymatch := re.match(CalcTableStatement.table_pattern,var): + if re.match(CalcTableStatement.table_pattern,var): return True else: return False @@ -51,9 +48,8 @@ def scan_range(myrange) -> None: nonlocal doc return CalcTextStatement.scan(myrange,True) tab_vars = {} - table_vars = {} for NamedRange in doc.NamedRanges: - if mymatch := re.match(CalcTableStatement.table_pattern,NamedRange.getName()): + if re.match(CalcTableStatement.table_pattern,NamedRange.getName()): named = scan_range(NamedRange) tab_vars[NamedRange.getName()] = {'type': 'object', 'value': named} @@ -69,9 +65,10 @@ def fill(doc: XComponent, variable: str, value) -> None: :return: None """ myrange=doc.NamedRanges.getByName(variable) - mycellrange=myrange.getReferredCells() + #mycellrange=myrange.getReferredCells() mycellrangeaddr=myrange.getReferredCells().getRangeAddress() + mycontent,finalcol,finalrow =myrange.getContent().rsplit('$', 2) StartColumn=myrange.getReferredCells().getRangeAddress().StartColumn @@ -103,7 +100,7 @@ def fill(doc: XComponent, variable: str, value) -> None: except IndexError: CalcTextStatement.fill(rangetocopy,'&'+key,"") - if left==True: + if left: myrange.setContent(mycontent+'$'+incr_str(finalcol,maxlen*size)+'$'+finalrow) mycellrangeaddr.EndColumn=mycellrangeaddr.EndColumn+maxlen*size mycellrangeaddr.StartColumn=mycellrangeaddr.StartColumn+maxlen*size diff --git a/lotemplate/Statement/CounterStatement.py b/lotemplate/Statement/CounterStatement.py index e920be2..d310995 100644 --- a/lotemplate/Statement/CounterStatement.py +++ b/lotemplate/Statement/CounterStatement.py @@ -55,13 +55,13 @@ def scan_counter(doc: XComponent) -> None: scan for counter statement. No return. We just verify that there is and endif for each if statement """ - def compute_counter(x_found): - """ - Compute the counter statement. - """ - counter_text = x_found.getText() - counter_cursor = counter_text.createTextCursorByRange(x_found) - cursor_statement = CounterStatement(counter_cursor.String) + #def compute_counter(x_found): + # """ + # Compute the counter statement. + # """ + # counter_text = x_found.getText() + #counter_cursor = counter_text.createTextCursorByRange(x_found) + #cursor_statement = CounterStatement(counter_cursor.String) def find_counter_to_compute(doc, search, x_found): """ @@ -70,7 +70,7 @@ def find_counter_to_compute(doc, search, x_found): if x_found is None: return None - compute_counter(x_found) + # compute_counter(x_found) # searching for the next counter statement. x_found_after = doc.findNext(x_found.End, search) @@ -139,9 +139,9 @@ def find_counter_to_compute(doc, search, x_found): if x_found is None: return None - text = x_found.getText() - cursor = text.createTextCursorByRange(x_found) - str = cursor.String + #text = x_found.getText() + #cursor = text.createTextCursorByRange(x_found) + #str = cursor.String compute_counter(x_found) diff --git a/lotemplate/Statement/ForStatement.py b/lotemplate/Statement/ForStatement.py index af22047..951aecc 100644 --- a/lotemplate/Statement/ForStatement.py +++ b/lotemplate/Statement/ForStatement.py @@ -77,7 +77,7 @@ def scan_single_for(doc: XComponent, local_x_found) -> str: scan for a single for statement """ for_statement = ForStatement(local_x_found.getString()) - position_in_text = len(for_statement.for_string) + #position_in_text = len(for_statement.for_string) endfor_search = doc.createSearchDescriptor() endfor_search.SearchString = ForStatement.end_regex diff --git a/lotemplate/Statement/HtmlStatement.py b/lotemplate/Statement/HtmlStatement.py index f278226..73312b2 100644 --- a/lotemplate/Statement/HtmlStatement.py +++ b/lotemplate/Statement/HtmlStatement.py @@ -1,4 +1,3 @@ -import re from sorcery import dict_of import lotemplate.errors as errors from com.sun.star.lang import XComponent @@ -24,7 +23,7 @@ def scan_single_html(local_x_found) -> None: """ scan for a single for statement """ - html_statement = HtmlStatement(local_x_found.getString()) + #html_statement = HtmlStatement(local_x_found.getString()) endhtml_search = doc.createSearchDescriptor() endhtml_search.SearchString = HtmlStatement.end_regex endhtml_search.SearchRegularExpression = True @@ -34,7 +33,7 @@ def scan_single_html(local_x_found) -> None: cursor = local_x_found.getText().createTextCursorByRange(local_x_found) raise errors.TemplateError( 'no_endhtml_found', - f"The statement [html] has no endhtml", + "The statement [html] has no endhtml", dict_of(cursor.String) ) @@ -55,7 +54,7 @@ def html_replace(template, doc: XComponent) -> None: """ def compute_html(doc, local_x_found): - html_statement = HtmlStatement(local_x_found.getString()) + #html_statement = HtmlStatement(local_x_found.getString()) endhtml_search = doc.createSearchDescriptor() endhtml_search.SearchString = HtmlStatement.end_regex endhtml_search.SearchRegularExpression = True @@ -65,7 +64,7 @@ def compute_html(doc, local_x_found): cursor = local_x_found.getText().createTextCursorByRange(local_x_found) raise errors.TemplateError( 'no_endhtml_found', - f"The statement [html] has no endhtml", + "The statement [html] has no endhtml", dict_of(cursor.String) ) diff --git a/lotemplate/Statement/IfStatement.py b/lotemplate/Statement/IfStatement.py index d02b846..ca33f9b 100644 --- a/lotemplate/Statement/IfStatement.py +++ b/lotemplate/Statement/IfStatement.py @@ -188,7 +188,7 @@ def find_if_to_compute(doc, search, x_found): if match is not None: raise errors.TemplateError( 'too_many_endif_found', - f"The document has too many endif", + "The document has too many endif", {} ) diff --git a/lotemplate/Statement/ImageStatement.py b/lotemplate/Statement/ImageStatement.py index 398de43..0502c6d 100644 --- a/lotemplate/Statement/ImageStatement.py +++ b/lotemplate/Statement/ImageStatement.py @@ -1,4 +1,4 @@ -from com.sun.star.beans import PropertyValue, UnknownPropertyException +from com.sun.star.beans import PropertyValue from com.sun.star.lang import XComponent import regex from urllib import request diff --git a/lotemplate/Statement/TableStatement.py b/lotemplate/Statement/TableStatement.py index c4ddc6d..1da8ca3 100644 --- a/lotemplate/Statement/TableStatement.py +++ b/lotemplate/Statement/TableStatement.py @@ -57,7 +57,7 @@ def scan_cell(cell) -> None: except errors.TemplateError as e: raise e continue - except RuntimeException as e: + except RuntimeException: continue return list_tab_vars if get_list else tab_vars diff --git a/lotemplate/Statement/TextStatement.py b/lotemplate/Statement/TextStatement.py index 1dfd9f0..b72ba99 100644 --- a/lotemplate/Statement/TextStatement.py +++ b/lotemplate/Statement/TextStatement.py @@ -1,6 +1,6 @@ import re from com.sun.star.lang import XComponent -from com.sun.star.beans import PropertyValue, UnknownPropertyException +from com.sun.star.beans import UnknownPropertyException import regex from lotemplate.Statement.ForStatement import ForStatement from lotemplate.Statement.TableStatement import TableStatement diff --git a/lotemplate/Template.py b/lotemplate/Template.py index 0d597e2..f22de89 100644 --- a/lotemplate/Template.py +++ b/lotemplate/Template.py @@ -12,32 +12,20 @@ import os from typing import Union from sorcery import dict_of -import uno import unohelper from com.sun.star.beans import PropertyValue from com.sun.star.io import IOException from com.sun.star.lang import IllegalArgumentException, DisposedException -from com.sun.star.connection import NoConnectException from com.sun.star.uno import RuntimeException -from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK -from com.sun.star.style.BreakType import PAGE_AFTER from . import errors -from . import Connexion -from .utils import * +#from . import Connexion +from .utils import get_file_url,get_cached_json -from lotemplate.Statement.ForStatement import ForStatement -from lotemplate.Statement.HtmlStatement import HtmlStatement -from lotemplate.Statement.IfStatement import IfStatement -from lotemplate.Statement.TextStatement import TextStatement -from lotemplate.Statement.TableStatement import TableStatement -from lotemplate.Statement.ImageStatement import ImageStatement -from lotemplate.Statement.CounterStatement import CounterManager import uuid import shutil import json -import pdb class Template: @@ -95,7 +83,7 @@ def open_doc_from_url(self): def validDocType(self,doc): pass - def __init__(self, file_path: str, cnx: Connexion, should_scan: bool, json_cache_dir=None): + def __init__(self, file_path: str, cnx, should_scan: bool, json_cache_dir=None): """ An object representing a LibreOffice/OpenOffice template that you can fill, scan, export and more @@ -124,7 +112,7 @@ def __init__(self, file_path: str, cnx: Connexion, should_scan: bool, json_cache with open(cachedjson) as f: self.variables = json.load(f) return - except: + except Exception: pass self.variables = self.scan(should_close=True) if json_cache_dir: diff --git a/lotemplate/WriterTemplate.py b/lotemplate/WriterTemplate.py index 653253e..5441e7f 100644 --- a/lotemplate/WriterTemplate.py +++ b/lotemplate/WriterTemplate.py @@ -9,30 +9,19 @@ 'Template', ) -import os from typing import Union from sorcery import dict_of import uno -import unohelper -import time -import pdb -import random -import uuid from com.sun.star.beans import PropertyValue -from com.sun.star.io import IOException -from com.sun.star.lang import IllegalArgumentException, DisposedException -from com.sun.star.connection import NoConnectException -from com.sun.star.uno import RuntimeException from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK from com.sun.star.style.BreakType import PAGE_AFTER from . import errors -from .utils import * -from . import Template +from .Template import Template from lotemplate.Statement.ForStatement import ForStatement from lotemplate.Statement.HtmlStatement import HtmlStatement diff --git a/lotemplate/__init__.py b/lotemplate/__init__.py index 73f6a55..208545a 100644 --- a/lotemplate/__init__.py +++ b/lotemplate/__init__.py @@ -17,18 +17,17 @@ 'Template', 'CalcTemplate', 'WriterTemplate', - 'errors', 'convert_to_datas_template', 'is_network_based', 'get_file_url', 'TemplateFromExt', + 'start_multi_office', + 'randomConnexion', ) -from .errors import * -from .utils import * -from .connexion import * -from .Template import * -from .WriterTemplate import * -from .CalcTemplate import * -from .lofunction import * - +from .connexion import Connexion +from .utils import convert_to_datas_template,is_network_based,get_file_url +from .Template import Template +from .WriterTemplate import WriterTemplate +from .CalcTemplate import CalcTemplate +from .lofunction import TemplateFromExt,start_multi_office,randomConnexion diff --git a/lotemplate/connexion.py b/lotemplate/connexion.py index 9de9ab6..c62f0fe 100644 --- a/lotemplate/connexion.py +++ b/lotemplate/connexion.py @@ -11,22 +11,17 @@ ) import os -from typing import Union from sorcery import dict_of -import shlex,subprocess +import shlex +import subprocess import uno -import unohelper -from com.sun.star.beans import PropertyValue -from com.sun.star.io import IOException -from com.sun.star.lang import IllegalArgumentException, DisposedException from com.sun.star.connection import NoConnectException from com.sun.star.uno import RuntimeException -from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK -from com.sun.star.style.BreakType import PAGE_AFTER from time import sleep from . import errors -from .utils import * -import pdb +#from .utils import * +from .WriterTemplate import WriterTemplate +from .CalcTemplate import CalcTemplate def start_office(host:str="localhost",port:str="2000"): """ @@ -108,8 +103,8 @@ def TemplateFromExt(file_path: str, cnx: Connexion, should_scan: bool): filename, file_extension = os.path.splitext(file_path) ods_ext=('.xls','.xlsx','.ods') if file_extension in ods_ext: - document = ot.CalcTemplate(file_path, cnx , should_scan) + document = CalcTemplate(file_path, cnx , should_scan) else: - document = ot.WriterTemplate(file_path, cnx , should_scan) + document = WriterTemplate(file_path, cnx , should_scan) return document diff --git a/lotemplate/lofunction.py b/lotemplate/lofunction.py index 166c906..b115cdc 100644 --- a/lotemplate/lofunction.py +++ b/lotemplate/lofunction.py @@ -12,13 +12,11 @@ ) import os -from .WriterTemplate import * -from .CalcTemplate import * -from .connexion import * -import shlex,subprocess +from .WriterTemplate import WriterTemplate +from .CalcTemplate import CalcTemplate +from .connexion import Connexion,start_office import random -import pdb -def TemplateFromExt(file_path: str, cnx: Connexion, should_scan: bool,json_cache_dir=None): +def TemplateFromExt(file_path: str, cnx, should_scan: bool,json_cache_dir=None): filename, file_extension = os.path.splitext(file_path) ods_ext=('.xls','.xlsx','.ods') @@ -44,7 +42,7 @@ def start_multi_office(host:str="localhost",start_port:int=2000,nb_env:int=1): :return: list of (host,port,lo dir) """ if nb_env <= 0: - raise TypeError("%s is an invalid positive int value" % value) + raise TypeError("%s is an invalid positive int value" % nb_env) soffices=[] port=start_port for i in range(nb_env): diff --git a/lotemplate/unittest/test_cachedjson.py b/lotemplate/unittest/test_cachedjson.py index fd26061..385435e 100644 --- a/lotemplate/unittest/test_cachedjson.py +++ b/lotemplate/unittest/test_cachedjson.py @@ -4,14 +4,10 @@ import unittest import lotemplate as ot -from time import sleep -import subprocess import filecmp import os -import json -from lotemplate.unittest.test_function import * -cnx = start_office() +cnx = ot.start_multi_office() class Test_calc(unittest.TestCase): @@ -20,7 +16,7 @@ def test_cachedjson(self): cachejson="lotemplate/unittest/files/content/e89fbedb61af3994184da3e5340bd9e9-calc_variables.ods.json" if os.path.isfile(cachejson): os.remove(cachejson) - ot.TemplateFromExt("lotemplate/unittest/files/templates/calc_variables.ods",cnx,True,json_cache_dir='lotemplate/unittest/files/content/') + ot.TemplateFromExt("lotemplate/unittest/files/templates/calc_variables.ods",ot.randomConnexion(cnx),True,json_cache_dir='lotemplate/unittest/files/content/') self.assertTrue(filecmp.cmp(cachejson,"lotemplate/unittest/files/content/e89fbedb61af3994184da3e5340bd9e9-calc_variables.ods.expected.json", shallow=False)) diff --git a/lotemplate/unittest/test_calc.py b/lotemplate/unittest/test_calc.py index dd32adf..6240f5a 100644 --- a/lotemplate/unittest/test_calc.py +++ b/lotemplate/unittest/test_calc.py @@ -4,14 +4,9 @@ import unittest import lotemplate as ot -from time import sleep -import subprocess -import filecmp -import os -import json -from lotemplate.unittest.test_function import * +from lotemplate.unittest.test_function import compare_files_html -cnx = start_office() +cnx = ot.start_multi_office() class Test_calc(unittest.TestCase): @@ -23,7 +18,7 @@ def test_scan(self): ""}, "toto": {"type": "text", "value": ""}, "myvar": {"type": "text", "value": ""}, "foobar": {"type": "text", "value": ""}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/calc_variables.ods", cnx, False)).scan()) + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/calc_variables.ods", ot.randomConnexion(cnx), False)).scan()) doc.close() def test_var(self): diff --git a/lotemplate/unittest/test_comparaison.py b/lotemplate/unittest/test_comparaison.py index 891046c..d256cb1 100644 --- a/lotemplate/unittest/test_comparaison.py +++ b/lotemplate/unittest/test_comparaison.py @@ -4,16 +4,14 @@ import unittest import lotemplate as ot -from time import sleep -import subprocess -from lotemplate.unittest.test_function import * +from lotemplate.unittest.test_function import to_data -cnx=start_office() +cnx=ot.start_multi_office() class Text(unittest.TestCase): - temp = ot.TemplateFromExt("lotemplate/unittest/files/comparaison/text_vars.odt", cnx, True) + temp = ot.TemplateFromExt("lotemplate/unittest/files/comparaison/text_vars.odt", ot.randomConnexion(cnx), True) def test_valid(self): self.temp.search_error(to_data("lotemplate/unittest/files/comparaison/text_vars_valid.json")) @@ -35,7 +33,7 @@ def test_invalid_incorrect_value(self): temp.close() - temp_tab = ot.TemplateFromExt("lotemplate/unittest/files/comparaison/static_tab.odt", cnx, True) + temp_tab = ot.TemplateFromExt("lotemplate/unittest/files/comparaison/static_tab.odt", ot.randomConnexion(cnx), True) def test_tab_valid(self): self.temp_tab.search_error(to_data("lotemplate/unittest/files/comparaison/static_tab_valid.json")) @@ -45,7 +43,7 @@ def test_tab_valid(self): class Tables(unittest.TestCase): - temp = ot.TemplateFromExt("lotemplate/unittest/files/comparaison/two_row_tab_varied.odt", cnx, True) + temp = ot.TemplateFromExt("lotemplate/unittest/files/comparaison/two_row_tab_varied.odt", ot.randomConnexion(cnx), True) def test_valid(self): self.temp.search_error(to_data("lotemplate/unittest/files/comparaison/two_row_tab_varied_valid.json")) @@ -67,7 +65,7 @@ def test_invalid_unknown_variable(self): class Images(unittest.TestCase): - temp = ot.TemplateFromExt("lotemplate/unittest/files/comparaison/img_vars.odt", cnx, True) + temp = ot.TemplateFromExt("lotemplate/unittest/files/comparaison/img_vars.odt", ot.randomConnexion(cnx), True) def test_valid(self): self.temp.search_error(to_data("lotemplate/unittest/files/comparaison/img_vars_valid.json")) diff --git a/lotemplate/unittest/test_content.py b/lotemplate/unittest/test_content.py index 25d5900..6104eaa 100644 --- a/lotemplate/unittest/test_content.py +++ b/lotemplate/unittest/test_content.py @@ -3,14 +3,12 @@ """ import unittest -import urllib.request import lotemplate as ot -from time import sleep -from test_function import * +from test_function import compare_files -cnx=start_office() +cnx=ot.start_multi_office() class Text(unittest.TestCase): diff --git a/lotemplate/unittest/test_function.py b/lotemplate/unittest/test_function.py index f635049..4d4516c 100644 --- a/lotemplate/unittest/test_function.py +++ b/lotemplate/unittest/test_function.py @@ -4,28 +4,17 @@ import lotemplate as ot -from time import sleep -import shlex,subprocess import filecmp import os import json from pypdf import PdfReader -def start_office(): - mylo=(ot.start_multi_office()) - sleep(2) - cnx= ot.randomConnexion(mylo) - return cnx def file_to_dict(file_path: str) -> dict: - if ot.is_network_based(file_path): - return json.loads(urllib.request.urlopen(file_path).read()) - else: with open(file_path) as f: return json.loads(f.read()) -def to_data(file: str): - return ot.convert_to_datas_template(test_json_convertion.file_to_dict(file)) + def to_data(file: str): @@ -41,7 +30,7 @@ def get_filename(ext: str): temp = None if os.path.isfile(get_filename('ods')): - temp = ot.TemplateFromExt(get_filename('ods'), cnx, True) + temp = ot.TemplateFromExt(get_filename('ods'), ot.randomConnexion(cnx), True) if temp is None: if name == 'debug': @@ -91,9 +80,9 @@ def get_filename(ext: str): temp = None if os.path.isfile(get_filename('odt')): - temp = ot.TemplateFromExt(get_filename('odt'), cnx, True) + temp = ot.TemplateFromExt(get_filename('odt'), ot.randomConnexion(cnx), True) if os.path.isfile(get_filename('docx')): - temp = ot.TemplateFromExt(get_filename('docx'), cnx, True) + temp = ot.TemplateFromExt(get_filename('docx'), ot.randomConnexion(cnx), True) if temp is None: if name == 'debug': diff --git a/lotemplate/unittest/test_json_convertion.py b/lotemplate/unittest/test_json_convertion.py index d84ee6f..31378b2 100644 --- a/lotemplate/unittest/test_json_convertion.py +++ b/lotemplate/unittest/test_json_convertion.py @@ -3,13 +3,12 @@ """ import unittest -import urllib.request import lotemplate as ot +import json +from .test_function import file_to_dict -from test_function import * - -cnx = start_office() +cnx = ot.start_multi_office() class Generic(unittest.TestCase): diff --git a/lotemplate/unittest/test_template_scan.py b/lotemplate/unittest/test_template_scan.py index d67374d..924a57a 100644 --- a/lotemplate/unittest/test_template_scan.py +++ b/lotemplate/unittest/test_template_scan.py @@ -4,13 +4,10 @@ import unittest import lotemplate as ot -from time import sleep -import subprocess -from lotemplate.unittest.test_function import * -cnx=start_office() +cnx=ot.start_multi_office() class Text(unittest.TestCase): @@ -24,7 +21,7 @@ def test_noformat(self): "aet": {"type": "text", "value": ""}, "h": {"type": "text", "value": ""} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/text_vars_noformat.odt", cnx, False)).scan()) + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/text_vars_noformat.odt", ot.randomConnexion(cnx), False)).scan()) doc.close() def test_format(self): @@ -37,34 +34,34 @@ def test_format(self): "aet": {"type": "text", "value": ""}, "h": {"type": "text", "value": ""} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/text_vars.odt", cnx, False)).scan()) + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/text_vars.odt", ot.randomConnexion(cnx), False)).scan()) doc.close() def test_text_var_in_header(self): self.assertEqual( {"my_var": {"type": "text", "value": ""}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/text_var_in_header.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/text_var_in_header.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_static_table(self): self.assertEqual( {"var1": {"type": "text", "value": ""}, "var2": {"type": "text", "value": ""}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/static_tab.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/static_tab.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_function_variable(self): self.assertEqual( {"test(\"jean\")": {"type": "text", "value": ""}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/function_variable.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/function_variable.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_for_variable(self): self.assertEqual( {"tutu": {"type": "array", "value": []}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/for.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/for.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() @@ -73,7 +70,7 @@ class Images(unittest.TestCase): def test_one_image(self): self.assertEqual( {"image": {"type": "image", "value": ""}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/img_vars.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/img_vars.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() @@ -84,14 +81,14 @@ def test_multiple_images(self): "image2": {"type": "image", "value": ""}, "image3": {"type": "image", "value": ""} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/multiple_img_vars.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/multiple_img_vars.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() class Ifs(unittest.TestCase): def test_no_endif(self): with self.assertRaises(ot.errors.TemplateError): - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/invalid_if_statement.odt", cnx, False)).scan() + (ot.TemplateFromExt("lotemplate/unittest/files/templates/invalid_if_statement.odt", ot.randomConnexion(cnx), False)).scan() class Tables(unittest.TestCase): @@ -99,42 +96,42 @@ class Tables(unittest.TestCase): def test_multiple_row(self): self.assertEqual( {"var1": {"type": "table", "value": [""]}, "var2": {"type": "table", "value": [""]}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/multiple_row_tab.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/multiple_row_tab.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_one_row_varied(self): self.assertEqual( {"var": {"type": "table", "value": [""]}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/one_row_tab_varied.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/one_row_tab_varied.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_two_row_varied(self): self.assertEqual( {"var1": {"type": "table", "value": [""]}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/two_row_tab_varied.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/two_row_tab_varied.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_invalid_var(self): with self.assertRaises(ot.errors.TemplateError): - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/invalid_var_tab.odt", cnx, False)).scan() + (ot.TemplateFromExt("lotemplate/unittest/files/templates/invalid_var_tab.odt", ot.randomConnexion(cnx), False)).scan() def test_invalid_vars(self): with self.assertRaises(ot.errors.TemplateError): - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/invalid_vars_tab.odt", cnx, False)).scan() + (ot.TemplateFromExt("lotemplate/unittest/files/templates/invalid_vars_tab.odt", ot.randomConnexion(cnx), False)).scan() def test_for(self): self.assertEqual( {'tutu': {'type': 'array', 'value': []}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/for.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/for.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_for_missing_endfor(self): with self.assertRaises(ot.errors.TemplateError): - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/for_missing_endfor.odt", cnx, False)).scan() + (ot.TemplateFromExt("lotemplate/unittest/files/templates/for_missing_endfor.odt", ot.randomConnexion(cnx), False)).scan() def test_two_tabs_varied(self): self.assertEqual( @@ -146,7 +143,7 @@ def test_two_tabs_varied(self): "4": {"type": "table", "value": [""]}, "5": {"type": "table", "value": [""]} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/two_tabs_varied.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/two_tabs_varied.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() @@ -157,7 +154,7 @@ def test_function_variable(self): "test": {"type": "table", "value": [""]}, "test2": {"type": "text", "value": ""}, }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/function_variable_tab.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/function_variable_tab.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() @@ -179,7 +176,7 @@ def test_multiple_variables(self): "static2": {"type": "text", "value": ""}, "static3": {"type": "text", "value": ""} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/multiple_variables.odt", cnx, False)).scan()) + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/multiple_variables.odt", ot.randomConnexion(cnx), False)).scan()) doc.close() def test_multiple_pages(self): @@ -199,22 +196,22 @@ def test_multiple_pages(self): "date": {"type": "text", "value": ""}, "lieu": {"type": "text", "value": ""} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/multiple_pages.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/multiple_pages.odt", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_online_empty_doc(self): with self.assertRaises(ot.errors.FileNotFoundError): - (doc := ot.TemplateFromExt( - "https://www.mtsac.edu/webdesign/accessible-docs/word/example03.docx", cnx, False)) + ( ot.TemplateFromExt( + "https://www.mtsac.edu/webdesign/accessible-docs/word/example03.docx", ot.randomConnexion(cnx), False)) def test_invalid_path(self): with self.assertRaises(ot.errors.FileNotFoundError): - ot.TemplateFromExt("bfevg", cnx, True) + ot.TemplateFromExt("bfevg", ot.randomConnexion(cnx), True) def test_duplicated_variable(self): with self.assertRaises(ot.errors.TemplateError): - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/duplicated_variables.odt", cnx, False)).scan() + (ot.TemplateFromExt("lotemplate/unittest/files/templates/duplicated_variables.odt", ot.randomConnexion(cnx), False)).scan() class OtherFormats(unittest.TestCase): @@ -229,7 +226,7 @@ def test_ott(self): "signature": {"type": "text", "value": ""}, "photo": {"type": "image", "value": ""} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.ott", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.ott", ot.randomConnexion(cnx), False)).scan() ) doc.close() @@ -243,14 +240,14 @@ def test_docx(self): "signature": {"type": "text", "value": ""}, "photo": {"type": "image", "value": ""} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.docx", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.docx", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_text(self): self.assertEqual( {"signature": {"type": "text", "value": ""}}, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.txt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.txt", ot.randomConnexion(cnx), False)).scan() ) doc.close() @@ -264,13 +261,13 @@ def test_html(self): "signature": {"type": "text", "value": ""}, "photo": {"type": "image", "value": ""} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.html", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.html", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_html_without_endhtml(self): with self.assertRaises(ot.errors.TemplateError): - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/html_without_endhtml.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/html_without_endhtml.odt", ot.randomConnexion(cnx), False)).scan() doc.close() @@ -283,12 +280,12 @@ def test_rtf(self): "prenon": {"type": "text", "value": ""}, "signature": {"type": "text", "value": ""} }, - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.rtf", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/format.rtf", ot.randomConnexion(cnx), False)).scan() ) doc.close() def test_for_inside_if(self): - doc = ot.TemplateFromExt("lotemplate/unittest/files/content/for_inside_if.odt", cnx, False) + doc = ot.TemplateFromExt("lotemplate/unittest/files/content/for_inside_if.odt", ot.randomConnexion(cnx), False) self.assertEqual( { 'tata': {'type': 'text', 'value': ''}, @@ -308,30 +305,30 @@ def test_for_inside_if(self): def test_if_too_many_endif(self): with self.assertRaises(ot.errors.TemplateError) as cm: - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/if_too_many_endif.odt", cnx, False)).scan() + (ot.TemplateFromExt("lotemplate/unittest/files/templates/if_too_many_endif.odt", ot.randomConnexion(cnx), False)).scan() self.assertEqual(cm.exception.code, "too_many_endif_found") def test_if_syntax_error(self): with self.assertRaises(ot.errors.TemplateError) as cm: - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/if_syntax_error.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/if_syntax_error.odt", ot.randomConnexion(cnx), False)).scan() doc.close() self.assertEqual(cm.exception.code, "syntax_error_in_if_statement") def test_for_syntax_error(self): with self.assertRaises(ot.errors.TemplateError) as cm: - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/for_syntax_error.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/for_syntax_error.odt", ot.randomConnexion(cnx), False)).scan() doc.close() self.assertEqual(cm.exception.code, "syntax_error_in_for_statement") def test_if_syntax_error_no_endif(self): with self.assertRaises(ot.errors.TemplateError) as cm: - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/if_syntax_error_no_endif.odt", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/if_syntax_error_no_endif.odt", ot.randomConnexion(cnx), False)).scan() doc.close() self.assertEqual(cm.exception.code, "no_endif_found") def test_invalid(self): with self.assertRaises(ot.errors.TemplateError): - (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/invalid_format.jpg", cnx, False)).scan() + (doc := ot.TemplateFromExt("lotemplate/unittest/files/templates/invalid_format.jpg", ot.randomConnexion(cnx), False)).scan() doc.close() diff --git a/lotemplate/utils.py b/lotemplate/utils.py index ce53bcc..757e3bd 100644 --- a/lotemplate/utils.py +++ b/lotemplate/utils.py @@ -23,7 +23,6 @@ import hashlib from . import errors -import pdb @@ -130,10 +129,10 @@ def recursive_check_type(rec_type, rec_value): f"{repr(get_type(rec_type, is_type=True))}", dict(varible=var_name, expected_variable_value_type=get_type(rec_type, is_type=True)) ) - if rec_type.__origin__ == list: + if rec_type.__origin__ is list: for element in rec_value: recursive_check_type(rec_type.__args__[0], element) - if rec_type.__origin__ == dict: + if rec_type.__origin__ is dict: for key, value in rec_value: recursive_check_type(rec_type.__args__[0], key) recursive_check_type(rec_type.__args__[1], value) @@ -237,7 +236,7 @@ def get_cleaned_array(var_name: str, var_value: list) -> list: template = {} for variable_name, variable_infos in json.items(): - if type(variable_infos) != dict: + if type(variable_infos) is not dict: raise errors.JsonSyntaxError( 'invalid_variable_base_value_type', f"The value type {repr(get_type(variable_infos))} isn't accepted in variable, only objects " @@ -261,7 +260,7 @@ def get_cleaned_array(var_name: str, var_value: list) -> list: dict_of(variable_name, information=invalid_infos[0]) ) - if type(variable_infos['type']) != str: + if type(variable_infos['type']) is not str: raise errors.JsonSyntaxError( 'invalid_variable_type_value_type', f"The 'type' information is supposed to be string, not a {repr(get_type(variable_infos['type']))} " diff --git a/lotemplate_cli.py b/lotemplate_cli.py index 5313b1f..174fa32 100755 --- a/lotemplate_cli.py +++ b/lotemplate_cli.py @@ -12,10 +12,7 @@ import urllib.error import sys import traceback -import shlex, subprocess -from time import sleep import os -import random def set_arguments() -> cparse.Namespace: """ @@ -93,10 +90,10 @@ def set_arguments() -> cparse.Namespace: path='exports' print( - f"Document saved as " + + "Document saved as " + repr(document.export( filename, path, True)) ) except Exception as exception: - print(f'Ignoring exception on json :', file=sys.stderr) + print('Ignoring exception on json :', file=sys.stderr) traceback.print_exception(type(exception), exception, exception.__traceback__, file=sys.stderr) document.close()