Skip to content

Commit

Permalink
Merge pull request #42 from mrf345/testing
Browse files Browse the repository at this point in the history
Add encoding to `execute`. Fix `wmic` encoding `utf-16`.
  • Loading branch information
mrf345 authored Jan 2, 2020
2 parents 3086840 + be19902 commit 860405f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
11 changes: 7 additions & 4 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
from app.middleware import db


def execute(command, parser=None):
''' Utility to execute a system command and get its output without
breaking any ongoing execution loops.
def execute(command, parser=None, encoding='utf-8'):
'''
Utility to execute a system command and get its output without
breaking any ongoing execution loops.
Parameter
---------
command: str
system command to execute.
parser: str
factor to parse the output and clean it with.
encoding: str
encoding to read the command output with.
Returns
-------
Expand All @@ -39,7 +42,7 @@ def execute(command, parser=None):
parsed = []

os.system(f'{command} > "{temp_file}"')
with open(temp_file, 'r') as file:
with open(temp_file, 'r', encoding=encoding) as file:
output += file.read()
os.remove(temp_file)

Expand Down
2 changes: 1 addition & 1 deletion app/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def serial(t_id):
# to solve Linux printer permissions
if os.name == 'nt':
# NOTE: To list all windows printers
if execute('wmic printer get sharename', parser='\n\n')[1:]:
if execute('wmic printer get sharename', parser='\n', encoding='utf-16')[1:]:
if langu == 'ar':
print_ticket_windows_ar(
q.product,
Expand Down
3 changes: 2 additions & 1 deletion app/views/customize.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def customize():
def ticket():
""" view of ticket customization """
printers = execute('wmic printer get sharename',
parser='\n\n')[1:] if os.name == 'nt' else listp()
parser='\n',
encoding='utf-16')[1:] if os.name == 'nt' else listp()
form = forms.Printer_f(printers, session.get('lang'))
tc = data.Touch_store.query.first()
pr = data.Printer.query.first()
Expand Down

0 comments on commit 860405f

Please sign in to comment.