├── README.md ├── .gitignore ├── labelPrinter.py └── labelPrinterXL.py /README.md: -------------------------------------------------------------------------------- 1 | # event-label-printer 2 | 3 | Automatic print guest name labels, when they checking - using Descope Flows 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | 162 | #Virtual Environment 163 | venv 164 | 165 | #TestingProgram 166 | testprint2.py 167 | 168 | #ManagementKey 169 | .env -------------------------------------------------------------------------------- /labelPrinter.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | from dotenv import load_dotenv 5 | from datetime import datetime 6 | from descope import ( AuthException, DescopeClient ) 7 | 8 | LINE_CLEAR = '\x1b[2K' 9 | MAX_NAME_LINE = 20 10 | MAX_COMPANY_LINE = 39 11 | MAX_TITLE_LINE = 40 12 | MAX_BALE_HEADER_LINE = 100 13 | FONT_CHAR_SET = "ANSI_CHARSET" 14 | FONT_FACE_NAME = "Consolas" 15 | 16 | print("Setting up...") 17 | 18 | try: 19 | import win32printing 20 | PRINTING_ENV = True 21 | except: 22 | print("No win32printing. Running without.") 23 | PRINTING_ENV = False 24 | 25 | load_dotenv() 26 | management_key = os.getenv("MANAGEMENT_KEY") 27 | project_id = os.getenv("PROJECT_ID") 28 | 29 | print("Project ID: " + project_id + ". Management Key: " + management_key[:5] + "********" + management_key[-5:]) 30 | 31 | try: 32 | descope_client = DescopeClient( project_id = project_id, management_key = management_key ) 33 | except Exception as error: 34 | print("failed to initialize. Error:") 35 | print(error) 36 | exit(1) 37 | 38 | def get_print_string(arr, key, max): 39 | if not key in arr: 40 | return "" 41 | if (len(arr[key]) > max): 42 | # trim spaces 43 | return arr[key][:max].rstrip() 44 | else: 45 | return arr[key].rstrip() 46 | 47 | def get_name_lines(full_name): 48 | name_lines = ["", ""] 49 | words = full_name.split(" ") 50 | 51 | name_lines[0] = words[0] 52 | 53 | if len(words) > 1: 54 | name_lines[1] = " ".join(words[1:])[:MAX_NAME_LINE] 55 | else: 56 | name_lines[1] = " " 57 | return name_lines 58 | 59 | 60 | def search_users(): 61 | custom_attributes = {"checkedIn": True, "approved": True, "printed": False} 62 | try: 63 | resp = descope_client.mgmt.user.search_all(custom_attributes=custom_attributes) 64 | users = resp["users"] 65 | 66 | print(end=LINE_CLEAR) 67 | now = datetime.now() 68 | current_time = now.strftime("%H:%M:%S") 69 | print(" " + current_time + " Running... ", end='\r') 70 | 71 | if (len(users) > 0): 72 | print() 73 | print() 74 | print(" Found " + str(len(users)) + " users to print.") 75 | 76 | return users 77 | 78 | except AuthException as error: 79 | print("Unable to search users.") 80 | print("Status Code: " + str(error.status_code)) 81 | print("Error: " + str(error.error_message)) 82 | return [] 83 | 84 | def update_user(user): 85 | login_id = user["loginIds"][0] 86 | attribute_key = "printed" 87 | attribute_val = True 88 | 89 | try: 90 | descope_client.mgmt.user.update_custom_attribute(login_id=login_id, attribute_key=attribute_key, attribute_val=attribute_val) 91 | print (" Successfully updated user. Email: " + login_id) 92 | print() 93 | except AuthException as error: 94 | print ("Unable to update user's custom attribute.") 95 | print ("Status Code: " + str(error.status_code)) 96 | print ("Error: " + str(error.error_message)) 97 | exit(1) 98 | 99 | def print_user(user): 100 | print(" Printing " + user["name"]) 101 | 102 | font_gap = { "height": 6, "weight": 400, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 103 | font_header = { "height": 12, "weight": 400, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 104 | font_name = { "height": 24, "weight": 600, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 105 | font_company = { "height": 16, "weight": 600, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 106 | font_title = { "height": 14, "weight": 400, "italic": True, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 107 | 108 | label_header = get_print_string(user["customAttributes"],"labelHeader",MAX_BALE_HEADER_LINE) 109 | name_lines = get_name_lines(user["name"]) 110 | company_name = get_print_string(user["customAttributes"],"companyName",MAX_COMPANY_LINE) 111 | title = get_print_string(user["customAttributes"],"title",MAX_TITLE_LINE) 112 | 113 | if (not PRINTING_ENV): 114 | print(" " + "-" * 35) 115 | print(" | " + label_header) 116 | print(" | " + name_lines[0]) 117 | print(" | " + name_lines[1]) 118 | print(" | " + company_name) 119 | print(" | " + title) 120 | print(" " + "-" * 35) 121 | return 122 | 123 | with win32printing.Printer( printer_name="iDPRT SP410", margin=(0, 0, 5, 0) ) as _printer: 124 | try: 125 | _printer.start_doc # start job 126 | _printer.start_page # using one label 127 | 128 | _printer.text(" ", align="center", font_config=font_gap) 129 | 130 | _printer.text(label_header, align="center", font_config=font_header) 131 | _printer.text(" ", align="center", font_config=font_gap) 132 | 133 | _printer.text(name_lines[0], align="center", font_config=font_name) 134 | _printer.text(" ", align="center", font_config=font_gap) 135 | _printer.text(name_lines[1], align="center", font_config=font_name) 136 | _printer.text(" ", align="center", font_config=font_gap) 137 | 138 | _printer.text("\u2500" * 20, align="center") 139 | _printer.text(" ", align="center", font_config=font_gap) 140 | 141 | _printer.text(company_name, align="center", font_config=font_company) 142 | 143 | _printer.text(" ", align="center", font_config=font_gap) 144 | 145 | _printer.text(title, align="center", font_config=font_title) 146 | 147 | _printer.end_page 148 | finally: 149 | _printer.end_doc 150 | return 151 | 152 | 153 | def print_loop(): 154 | while True: 155 | users_list = search_users() # All users checked in but not printed 156 | if users_list != None: 157 | for user in users_list: 158 | if user != None: 159 | print_user(user) 160 | update_user(user) 161 | 162 | time.sleep(1) 163 | 164 | def main(): 165 | print_loop() 166 | return 167 | 168 | main() 169 | -------------------------------------------------------------------------------- /labelPrinterXL.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | from dotenv import load_dotenv 5 | from datetime import datetime 6 | from descope import ( AuthException, DescopeClient ) 7 | 8 | LINE_CLEAR = '\x1b[2K' 9 | MAX_NAME_LINE = 20 10 | MAX_COMPANY_LINE = 39 11 | MAX_TITLE_LINE = 40 12 | MAX_BALE_HEADER_LINE = 100 13 | FONT_CHAR_SET = "ANSI_CHARSET" 14 | FONT_FACE_NAME = "Consolas" 15 | 16 | print("Setting up...") 17 | 18 | try: 19 | import win32printing 20 | PRINTING_ENV = True 21 | except: 22 | print("No win32printing. Running without.") 23 | PRINTING_ENV = False 24 | 25 | load_dotenv() 26 | management_key = os.getenv("MANAGEMENT_KEY") 27 | project_id = os.getenv("PROJECT_ID") 28 | 29 | print("Project ID: " + project_id + ". Management Key: " + management_key[:5] + "********" + management_key[-5:]) 30 | 31 | try: 32 | descope_client = DescopeClient( project_id = project_id, management_key = management_key ) 33 | except Exception as error: 34 | print("failed to initialize. Error:") 35 | print(error) 36 | exit(1) 37 | 38 | def get_print_string(arr, key, max): 39 | if not key in arr: 40 | return "" 41 | if (len(arr[key]) > max): 42 | # trim spaces 43 | return arr[key][:max].rstrip() 44 | else: 45 | return arr[key].rstrip() 46 | 47 | def get_name_lines(full_name): 48 | name_lines = ["", ""] 49 | words = full_name.split(" ") 50 | 51 | name_lines[0] = words[0] 52 | 53 | if len(words) > 1: 54 | name_lines[1] = " ".join(words[1:])[:MAX_NAME_LINE] 55 | else: 56 | name_lines[1] = " " 57 | return name_lines 58 | 59 | 60 | def search_users(): 61 | custom_attributes = {"checkedIn": True, "approved": True, "printed": False} 62 | try: 63 | resp = descope_client.mgmt.user.search_all(custom_attributes=custom_attributes) 64 | users = resp["users"] 65 | 66 | print(end=LINE_CLEAR) 67 | now = datetime.now() 68 | current_time = now.strftime("%H:%M:%S") 69 | print(" " + current_time + " Running... ", end='\r') 70 | 71 | if (len(users) > 0): 72 | print() 73 | print() 74 | print(" Found " + str(len(users)) + " users to print.") 75 | 76 | return users 77 | 78 | except AuthException as error: 79 | print("Unable to search users.") 80 | print("Status Code: " + str(error.status_code)) 81 | print("Error: " + str(error.error_message)) 82 | return [] 83 | 84 | def update_user(user): 85 | login_id = user["loginIds"][0] 86 | attribute_key = "printed" 87 | attribute_val = True 88 | 89 | try: 90 | descope_client.mgmt.user.update_custom_attribute(login_id=login_id, attribute_key=attribute_key, attribute_val=attribute_val) 91 | print (" Successfully updated user. Email: " + login_id) 92 | print() 93 | except AuthException as error: 94 | print ("Unable to update user's custom attribute.") 95 | print ("Status Code: " + str(error.status_code)) 96 | print ("Error: " + str(error.error_message)) 97 | exit(1) 98 | 99 | def print_user(user): 100 | print(" Printing " + user["name"]) 101 | 102 | font_gap = { "height": 12, "weight": 400, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 103 | font_header = { "height": 12, "weight": 400, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 104 | font_name = { "height": 32, "weight": 600, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 105 | font_company = { "height": 20, "weight": 600, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 106 | font_title = { "height": 16, "weight": 400, "italic": True, "charSet": FONT_CHAR_SET, "faceName": FONT_FACE_NAME } 107 | 108 | label_header = get_print_string(user["customAttributes"],"labelHeader",MAX_BALE_HEADER_LINE) 109 | name_lines = get_name_lines(user["name"]) 110 | company_name = get_print_string(user["customAttributes"],"companyName",MAX_COMPANY_LINE) 111 | title = get_print_string(user["customAttributes"],"title",MAX_TITLE_LINE) 112 | 113 | if (not PRINTING_ENV): 114 | print(" " + "-" * 35) 115 | print(" | " + label_header) 116 | print(" | " + name_lines[0]) 117 | print(" | " + name_lines[1]) 118 | print(" | " + company_name) 119 | print(" | " + title) 120 | print(" " + "-" * 35) 121 | return 122 | 123 | with win32printing.Printer( printer_name="iDPRT SP410", margin=(0, 0, 5, 0) ) as _printer: 124 | try: 125 | _printer.start_doc # start job 126 | _printer.start_page # using one label 127 | 128 | _printer.text(" ", align="center", font_config=font_gap) 129 | 130 | _printer.text(label_header, align="center", font_config=font_header) 131 | _printer.text(" ", align="center", font_config=font_gap) 132 | 133 | _printer.text(name_lines[0], align="center", font_config=font_name) 134 | _printer.text(" ", align="center", font_config=font_gap) 135 | _printer.text(name_lines[1], align="center", font_config=font_name) 136 | _printer.text(" ", align="center", font_config=font_gap) 137 | 138 | _printer.text("\u2500" * 20, align="center") 139 | _printer.text(" ", align="center", font_config=font_gap) 140 | 141 | _printer.text(company_name, align="center", font_config=font_company) 142 | 143 | _printer.text(" ", align="center", font_config=font_gap) 144 | 145 | _printer.text(title, align="center", font_config=font_title) 146 | 147 | _printer.end_page 148 | finally: 149 | _printer.end_doc 150 | return 151 | 152 | 153 | def print_loop(): 154 | while True: 155 | users_list = search_users() # All users checked in but not printed 156 | if users_list != None: 157 | for user in users_list: 158 | if user != None: 159 | print_user(user) 160 | update_user(user) 161 | 162 | time.sleep(1) 163 | 164 | def main(): 165 | print_loop() 166 | return 167 | 168 | main() 169 | --------------------------------------------------------------------------------