Skip to content

Commit

Permalink
Removed lupa library
Browse files Browse the repository at this point in the history
  • Loading branch information
AcidWeb committed Mar 12, 2024
1 parent 6801b4a commit 24fc38b
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 20 deletions.
267 changes: 267 additions & 0 deletions CB/SLPP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
# This module is adapted by layday <https://github.com/layday> from SLPP <https://github.com/SirAnthony/slpp>.
#
# Copyright (c) 2010, 2011, 2012 SirAnthony <anthony at adsorbtion.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import annotations

import re
import string
from collections.abc import Container
from itertools import count, islice
from operator import eq
from typing import Any

DIGITS = frozenset(string.digits)
HEXDIGITS = frozenset(string.hexdigits)
HEXDELIMS = frozenset('Xx')
EXPONENTS = frozenset('Ee')
WHITESPACE = frozenset(string.whitespace)
WHITESPACE_OR_CLOSING_SQ_BR = WHITESPACE | frozenset(']')
NEWLINE = frozenset('\r\n')

match_bare_word = re.compile(r'^[a-z_]\w*$', flags=re.IGNORECASE)


class ParseError(Exception):
pass


class _Sentinel(str):
pass


_sentinel = _Sentinel()


class _SLPP:
def __init__(self, text: str):
self._iter_text = iter(text)
self._next()

def _next(self):
self.c = next(self._iter_text, _sentinel)

def _next_eq(self, includes: Container[str]):
if self.c not in includes:
for c in self._iter_text:
if c in includes:
self.c = c
break
else:
self.c = _sentinel

def _next_not_eq(self, excludes: Container[str]):
if self.c in excludes:
for c in self._iter_text:
if c not in excludes:
self.c = c
break
else:
self.c = _sentinel

def _decode_table(self):
table: dict[Any, Any] | list[Any] = {}
idx = 0

self._next()
while True:
self._next_not_eq(WHITESPACE)

if self.c == '}':
# Convert table to list if k(0) = 1 and k = k(n-1) + 1, ...
if (
table
and all(map(eq, table, count(1)))
# bool is a subclass of int in Python but not in Lua
and not any(isinstance(k, bool) for k in islice(table, 0, 2))
):
table = list(table.values())

self._next()
return table

elif self.c == ',':
self._next()

else:
is_val_long_string_literal = False

if self.c == '[':
self._next()
if self.c == '[':
is_val_long_string_literal = True

item = self.decode()
self._next_not_eq(WHITESPACE_OR_CLOSING_SQ_BR)

c = self.c
if c and c in '=,':
self._next()

if c == '=':
if is_val_long_string_literal:
raise ParseError('malformed key', item)

# nil key produces a runtime error in Lua
if item is None:
raise ParseError('table keys cannot be nil')

# Item is a key
value = self.decode()
if (
# nil values are not persisted in Lua tables
value is not None
# Where the key is a valid index key-less values take precedence
and (not isinstance(item, int) or isinstance(item, bool) or item > idx)
):
table[item] = value
continue

if item is not None:
idx += 1
table[idx] = item

def _decode_string(self):
s = ''
start = self.c
end = None
prev_was_slash = False

if start == '[':
self._next_not_eq('[')
s += self.c
end = ']'
else:
end = start

for c in self._iter_text:
if prev_was_slash:
prev_was_slash = False

if c != end:
s += '\\'
elif c == end:
break
elif c == '\\' and start == end:
prev_was_slash = True
continue

s += c

self._next()
if start != end:
# Strip multiple closing brackets
self._next_not_eq(end)

return s

def _decode_bare_word(self):
s = self.c
for c in self._iter_text:
new_s = s + c
if match_bare_word.match(new_s):
s = new_s
else:
break

self._next()

if s == 'true':
return True
elif s == 'false':
return False
elif s == 'nil':
return None
return s

def _decode_number(self):
def get_digits():
n = ''

for c in self._iter_text:
if c in DIGITS:
n += c
else:
self.c = c
break
else:
self.c = _sentinel

return n

n = ''

if self.c == '-':
c = self.c
self._next()
if self.c == '-':
# This is a comment - skip to the end of the line
self._next_eq(NEWLINE)
return None

elif not self.c or self.c not in DIGITS:
raise ParseError('malformed number (no digits after minus sign)', c + self.c)

n += c

n += self.c + get_digits()
if n == '0' and self.c in HEXDELIMS:
n += self.c

for c in self._iter_text:
if c in HEXDIGITS:
n += c
else:
self.c = c
break
else:
self.c = _sentinel

else:
if self.c == '.':
n += self.c + get_digits()

if self.c in EXPONENTS:
n += self.c
self._next() # +-
n += self.c + get_digits()

try:
return int(n, 0)
except ValueError:
return float(n)

def decode(self):
self._next_not_eq(WHITESPACE)
if not self.c:
raise ParseError('input is empty')
elif self.c == '{':
return self._decode_table()
elif self.c in '\'"[':
return self._decode_string()
elif self.c == '-' or self.c in DIGITS:
return self._decode_number()
else:
return self._decode_bare_word()


def loads(s: str) -> Any:
return _SLPP(s).decode()
21 changes: 10 additions & 11 deletions CB/Wago.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import bbcode
import requests
from io import StringIO, BytesIO
from lupa import LuaRuntime
from pathlib import Path
from zipfile import ZipFile
from markdown import Markdown
from urllib.parse import quote_plus
from . import retry, HEADERS
from .SLPP import loads


def markdown_unmark_element(element, stream=None):
Expand All @@ -26,7 +26,6 @@ def markdown_unmark_element(element, stream=None):

class BaseParser:
def __init__(self):
self.lua = LuaRuntime()
self.urlParser = re.compile('/([a-zA-Z0-9_-]+)/(\d+)')
self.list = {}
self.ignored = {}
Expand All @@ -44,13 +43,13 @@ def parse_storage(self):
with open(Path(f'WTF/Account/{self.accountName}/SavedVariables/WeakAuras.lua'), 'r', encoding='utf-8',
errors='ignore') as file:
data = file.read().replace('WeakAurasSaved = {', '{')
wadata = self.lua.eval(data)
wadata = loads(data)
for wa in wadata['displays']:
if wadata['displays'][wa]['url']:
if 'url' in wadata['displays'][wa]:
search = self.urlParser.search(wadata['displays'][wa]['url'])
if search is not None and search.group(1) and search.group(2):
if not wadata['displays'][wa]['parent'] and not wadata['displays'][wa]['ignoreWagoUpdate']:
if wadata['displays'][wa]['skipWagoUpdate']:
if 'parent' not in wadata['displays'][wa] and 'ignoreWagoUpdate' not in wadata['displays'][wa]:
if 'skipWagoUpdate' in wadata['displays'][wa]:
self.ignored[search.group(1)] = int(wadata['displays'][wa]['skipWagoUpdate'])
self.list[search.group(1)] = int(search.group(2))

Expand All @@ -67,16 +66,16 @@ def parse_storage_internal(self, data):
if data[script]['url']:
search = self.urlParser.search(data[script]['url'])
if search is not None and search.group(1) and search.group(2):
if not data[script]['ignoreWagoUpdate']:
if data[script]['skipWagoUpdate']:
if 'ignoreWagoUpdate' not in data[script]:
if 'skipWagoUpdate' in data[script]:
self.ignored[search.group(1)] = int(data[script]['skipWagoUpdate'])
self.list[search.group(1)] = int(search.group(2))

def parse_storage(self):
with open(Path(f'WTF/Account/{self.accountName}/SavedVariables/Plater.lua'), 'r', encoding='utf-8',
errors='ignore') as file:
data = file.read().replace('PlaterDB = {', '{', 1).rsplit('PlaterLanguage = {', 1)[0]
platerdata = self.lua.eval(data)
platerdata = loads(data)
for profile in platerdata['profiles']:
data = platerdata['profiles'][profile]['script_data']
if data:
Expand All @@ -88,8 +87,8 @@ def parse_storage(self):
if data:
search = self.urlParser.search(data)
if search is not None and search.group(1) and search.group(2):
if not platerdata['profiles'][profile]['ignoreWagoUpdate']:
if platerdata['profiles'][profile]['skipWagoUpdate']:
if 'ignoreWagoUpdate' not in platerdata['profiles'][profile]:
if 'skipWagoUpdate' in platerdata['profiles'][profile]:
self.ignored[search.group(1)] = int(platerdata['profiles'][profile]['skipWagoUpdate'])
self.list[search.group(1)] = int(search.group(2))

Expand Down
3 changes: 0 additions & 3 deletions CurseBreaker.spec
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import sys
sys.path.append(SPECPATH)

import platform
from CB import __version__ as version

Expand Down
1 change: 0 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ requests = "*"
markdown = "*"
bbcode = "*"
rich = "*"
lupa = "==1.14.1"

[dev-packages]

Expand Down
3 changes: 0 additions & 3 deletions hook-lupa.py

This file was deleted.

3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ pyperclip
requests
markdown
bbcode
rich
lupa==1.14.1
rich

0 comments on commit 24fc38b

Please sign in to comment.