Queer European MD passionate about IT
Browse Source

Prevent `message is not modified` exceptions

Davte 4 years ago
parent
commit
2131c6acf9
2 changed files with 24 additions and 11 deletions
  1. 1 1
      davtelepot/__init__.py
  2. 23 10
      davtelepot/useful_tools.py

+ 1 - 1
davtelepot/__init__.py

@@ -11,7 +11,7 @@ __author__ = "Davide Testa"
 __email__ = "davide@davte.it"
 __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
 __license__ = "GNU General Public License v3.0"
-__version__ = "2.5.19"
+__version__ = "2.5.20"
 __maintainer__ = "Davide Testa"
 __contact__ = "t.me/davte"
 

+ 23 - 10
davtelepot/useful_tools.py

@@ -186,12 +186,21 @@ operators = get_operators()
 operators_spacer = re.compile(r"(\d)\s*([+\-*%]|/{1,2})\s*(\d)")
 spaced_operators = r"\1 \2 \3"
 operators_space_remover = re.compile(r"(\d)\s*(\*\*)\s*(\d)")
-un_spaced_operators = r"\1\2\3"
+non_spaced_operators = r"\1\2\3"
+multiple_newlines_regex = re.compile(r"[\n|\r][\n|\s]{2,}")
+multiple_spaces_regex = re.compile(r"\s{2,}")
 
 
 def prettify_expression(expression):
+    """Make expression cleaner to read.
+
+    Place a single space around binary operators `+,-,*,%,/,//`, no space
+        around `**`, single newlines and single spaces.
+    """
     expression = operators_spacer.sub(spaced_operators, expression)
-    expression = operators_space_remover.sub(un_spaced_operators, expression)
+    expression = operators_space_remover.sub(non_spaced_operators, expression)
+    expression = multiple_newlines_regex.sub('\n', expression)
+    expression = multiple_spaces_regex.sub(' ', expression)
     return expression
 
 
@@ -353,6 +362,7 @@ async def calculate_session(bot: Bot,
     record = bot.db['calculations'].find_one(
         id=record_id
     )
+    old_expression = record['expression']
     if record is None:
         logging.error("Invalid record identifier!")
         return
@@ -389,19 +399,22 @@ async def calculate_session(bot: Bot,
             ['id']
         )
     if expression:
-        text = bot.get_message(
-            'useful_tools', 'calculate_command', 'result',
-            language=language,
-            expressions=evaluate_expressions(bot=bot,
-                                             expressions=expression,
-                                             language=language)
-        )
+        if expression.strip(' \n') != old_expression.strip(' \n'):
+            text = bot.get_message(
+                'useful_tools', 'calculate_command', 'result',
+                language=language,
+                expressions=evaluate_expressions(bot=bot,
+                                                 expressions=expression,
+                                                 language=language)
+            )
+        else:
+            text = ''
     else:
         text = bot.get_message(
             'useful_tools', 'calculate_command', 'instructions',
             language=language
         )
-    if last_entry is None:
+    if last_entry is None or not text:
         return
     await bot.edit_message_text(
         text=text,