Update versioninfo.py
[Neo-Hockey-Test.git] / example.py
blob2c1d91ae7dc0a545ea833abc7b28b3673c90f734
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 '''
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the Revised BSD License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 Revised BSD License for more details.
13 Copyright 2015-2024 Game Maker 2k - https://github.com/GameMaker2k
14 Copyright 2015-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
16 $FileInfo: example.py - Last Update: 10/17/2024 Ver. 0.9.6 RC 1 - Author: cooldude2k $
17 '''
19 from __future__ import absolute_import, division, print_function, unicode_literals, generators, with_statement, nested_scopes
20 import os
21 import random
22 import sys
23 import pyhockeystats
25 # Ensure compatibility with both Python 2 and 3
26 try:
27 reload(sys) # Only required in Python 2
28 sys.setdefaultencoding('utf-8')
29 except (NameError, AttributeError):
30 pass
32 # Function to gather valid directories
33 def get_data_directories():
34 potential_dirs = [
35 "./data/xml", "./data/xmlalt",
36 "./data/json", "./data/jsonalt",
37 "./data/sql", "./php/data"
39 return [d for d in potential_dirs if os.path.isdir(d)]
41 # Function to process a file based on its extension and load data
42 def process_file(filepath, extensions, extensionsc):
43 fileinfo = os.path.splitext(filepath)
44 ext = fileinfo[1].lower()
45 subext = os.path.splitext(fileinfo[0])[1].lower() if ext in extensionsc else None
47 if ext in extensions or subext in extensions:
48 funcarray = {}
49 sqlitedatatype = False
51 if ext == ".xml" or subext == ".xml":
52 if pyhockeystats.CheckXMLFile(filepath):
53 if pyhockeystats.CheckHockeyXML(filepath):
54 funcarray = {'informat': "xml", 'inxmlfile': filepath}
55 elif pyhockeystats.CheckHockeySQLiteXML(filepath):
56 funcarray = {'informat': "xml", 'inxmlfile': filepath}
57 sqlitedatatype = True
58 elif ext == ".db3" and pyhockeystats.CheckSQLiteDatabase(filepath):
59 funcarray = {'informat': "database", 'insdbfile': filepath}
60 elif ext == ".sql" or subext == ".sql":
61 funcarray = {'informat': "sql", 'insqlfile': filepath}
62 elif ext == ".json" or subext == ".json":
63 funcarray = {'informat': "json", 'injsonfile': filepath}
64 else:
65 return None
67 if sqlitedatatype:
68 hockeyarray = pyhockeystats.MakeHockeySQLiteArrayFromHockeySQLiteData(funcarray)
69 else:
70 hockeyarray = pyhockeystats.MakeHockeyArrayFromHockeyData(funcarray)
72 if pyhockeystats.CheckHockeySQLiteArray(hockeyarray):
73 hockeyarray = pyhockeystats.MakeHockeyArrayFromHockeySQLiteArray(hockeyarray)
75 return hockeyarray if pyhockeystats.CheckHockeyArray(hockeyarray) else None
76 return None
78 # Function to display hockey data
79 def display_hockey_data(hockeyarray):
80 for hlkey in hockeyarray['leaguelist']:
81 for hckey in hockeyarray[hlkey]['conferencelist']:
82 for hdkey in hockeyarray[hlkey][hckey]['divisionlist']:
83 for htkey in hockeyarray[hlkey][hckey][hdkey]['teamlist']:
84 conference = hockeyarray[hlkey].get('conferenceinfo', {}).get('fullname', '')
85 division = hockeyarray[hlkey][hckey].get('divisioninfo', {}).get('fullname', '')
86 team = hockeyarray[hlkey][hckey][hdkey][htkey]['teaminfo']['fullname']
87 league = hockeyarray[hlkey]['leagueinfo']['fullname']
89 if conference and division:
90 print("{league} / {conference} / {division} / {team}".format(
91 league=league, conference=conference, division=division, team=team))
92 elif conference:
93 print("{league} / {conference} / {team}".format(
94 league=league, conference=conference, team=team))
95 elif division:
96 print("{league} / {division} / {team}".format(
97 league=league, division=division, team=team))
98 else:
99 print("{league} / {team}".format(
100 league=league, team=team))
102 # Main function
103 def main():
104 defroot = get_data_directories()
105 if not defroot:
106 print("No valid data directories found.")
107 sys.exit(1)
109 rootdir = sys.argv[1] if len(sys.argv) > 1 else random.choice(defroot)
110 extensions = pyhockeystats.extensionswd
111 extensionsc = pyhockeystats.outextlistwd
113 if os.path.isdir(rootdir):
114 for subdir, _, files in os.walk(rootdir):
115 for file in files:
116 filepath = os.path.join(subdir, file)
117 hockeyarray = process_file(filepath, extensions, extensionsc)
119 if hockeyarray:
120 print("\n--------------------------------------------------------------------------")
121 print("File: {}".format(filepath))
122 display_hockey_data(hockeyarray)
123 print("--------------------------------------------------------------------------\n")
124 elif os.path.isfile(rootdir):
125 hockeyarray = process_file(rootdir, extensions, extensionsc)
126 if hockeyarray:
127 print("\n--------------------------------------------------------------------------")
128 print("File: {}".format(rootdir))
129 display_hockey_data(hockeyarray)
130 print("--------------------------------------------------------------------------\n")
131 else:
132 print("Invalid path provided.")
133 sys.exit(1)
135 if __name__ == "__main__":
136 main()