44import os
55import subprocess
66import textwrap
7+ from pathlib import Path
78from typing import Literal , Optional
89
910import requests
3738global REPORT_CONTEXT
3839
3940# Read in the InvenTree settings file
40- here = os . path . dirname (__file__ )
41- settings_file = os . path . join ( here , 'inventree_settings.json ' )
41+ here = Path (__file__ ). parent
42+ gen_base = here . joinpath ( 'generated ' )
4243
43- with open (settings_file , encoding = 'utf-8' ) as sf :
44+ with open (gen_base . joinpath ( 'inventree_settings.json' ) , encoding = 'utf-8' ) as sf :
4445 settings = json .load (sf )
4546
4647 GLOBAL_SETTINGS = settings ['global' ]
4748 USER_SETTINGS = settings ['user' ]
4849
4950# Tags
50- with open (os . path . join ( here , 'inventree_tags.yml' ), encoding = 'utf-8' ) as f :
51+ with open (gen_base . joinpath ( 'inventree_tags.yml' ), encoding = 'utf-8' ) as f :
5152 TAGS = yaml .load (f , yaml .BaseLoader )
5253# Filters
53- with open (os . path . join ( here , 'inventree_filters.yml' ), encoding = 'utf-8' ) as f :
54+ with open (gen_base . joinpath ( 'inventree_filters.yml' ), encoding = 'utf-8' ) as f :
5455 FILTERS = yaml .load (f , yaml .BaseLoader )
5556# Report context
56- with open (os . path . join ( here , 'inventree_report_context.json' ), encoding = 'utf-8' ) as f :
57+ with open (gen_base . joinpath ( 'inventree_report_context.json' ), encoding = 'utf-8' ) as f :
5758 REPORT_CONTEXT = json .load (f )
5859
5960
6061def get_repo_url (raw = False ):
6162 """Return the repository URL for the current project."""
62- mkdocs_yml = os . path . join ( os . path . dirname ( __file__ ), 'mkdocs.yml' )
63+ mkdocs_yml = here . joinpath ( 'mkdocs.yml' )
6364
6465 with open (mkdocs_yml , encoding = 'utf-8' ) as f :
6566 mkdocs_config = yaml .safe_load (f )
@@ -77,10 +78,10 @@ def check_link(url) -> bool:
7778 We allow a number attempts and a lengthy timeout,
7879 as we do not want false negatives.
7980 """
80- CACHE_FILE = os . path . join ( os . path . dirname ( __file__ ), 'url_cache.txt' )
81+ CACHE_FILE = gen_base . joinpath ( 'url_cache.txt' )
8182
8283 # Keep a local cache file of URLs we have already checked
83- if os . path . exists (CACHE_FILE ):
84+ if CACHE_FILE . exists ():
8485 with open (CACHE_FILE , encoding = 'utf-8' ) as f :
8586 cache = f .read ().splitlines ()
8687
@@ -93,6 +94,7 @@ def check_link(url) -> bool:
9394
9495 while attempts > 0 :
9596 response = requests .head (url , timeout = 5000 )
97+
9698 # Ensure GH is not causing issues
9799 if response .status_code in (200 , 429 ):
98100 # Update the cache file
@@ -147,13 +149,8 @@ def sourcedir(dirname: str, branch=None):
147149 dirname = dirname [1 :]
148150
149151 # This file exists at ./docs/main.py, so any directory we link to must be relative to the top-level directory
150- here = os .path .dirname (__file__ )
151- root = os .path .abspath (os .path .join (here , '..' ))
152-
153- directory = os .path .join (root , dirname )
154- directory = os .path .abspath (directory )
155-
156- if not os .path .exists (directory ) or not os .path .isdir (directory ):
152+ directory = here .parent .joinpath (dirname )
153+ if not directory .exists () or not directory .is_dir ():
157154 raise FileNotFoundError (f'Source directory { dirname } does not exist.' )
158155
159156 repo_url = get_repo_url ()
@@ -188,12 +185,8 @@ def sourcefile(filename, branch=None, raw=False):
188185 filename = filename [1 :]
189186
190187 # This file exists at ./docs/main.py, so any file we link to must be relative to the top-level directory
191- here = os .path .dirname (__file__ )
192- root = os .path .abspath (os .path .join (here , '..' ))
193-
194- file_path = os .path .join (root , filename )
195-
196- if not os .path .exists (file_path ):
188+ file_path = here .parent .joinpath (filename )
189+ if not file_path .exists ():
197190 raise FileNotFoundError (f'Source file { filename } does not exist.' )
198191
199192 # Construct repo URL
@@ -214,11 +207,8 @@ def sourcefile(filename, branch=None, raw=False):
214207 @env .macro
215208 def invoke_commands ():
216209 """Provides an output of the available commands."""
217- here = os .path .dirname (__file__ )
218- base = os .path .join (here , '..' )
219- base = os .path .abspath (base )
220- tasks = os .path .join (base , 'tasks.py' )
221- output = os .path .join (here , 'invoke-commands.txt' )
210+ tasks = here .parent .joinpath ('tasks.py' )
211+ output = gen_base .joinpath ('invoke-commands.txt' )
222212
223213 command = f'invoke -f { tasks } --list > { output } '
224214
@@ -232,17 +222,15 @@ def invoke_commands():
232222 @env .macro
233223 def listimages (subdir ):
234224 """Return a listing of all asset files in the provided subdir."""
235- here = os .path .dirname (__file__ )
236-
237- directory = os .path .join (here , 'docs' , 'assets' , 'images' , subdir )
225+ directory = here .joinpath ('docs' , 'assets' , 'images' , subdir )
238226
239227 assets = []
240228
241229 allowed = ['.png' , '.jpg' ]
242230
243- for asset in os . listdir ( directory ):
244- if any (asset .endswith (x ) for x in allowed ):
245- assets .append (os . path . join (subdir , asset ))
231+ for asset in directory . iterdir ( ):
232+ if any (str ( asset ) .endswith (x ) for x in allowed ):
233+ assets .append (str (subdir / asset . relative_to ( directory ) ))
246234
247235 return assets
248236
@@ -255,11 +243,9 @@ def includefile(filename: str, title: str, fmt: str = ''):
255243 title: The title of the collapse block in the documentation
256244 fmt: The format of the included file (e.g., 'python', 'html', etc.)
257245 """
258- here = os .path .dirname (__file__ )
259- path = os .path .join (here , '..' , filename )
260- path = os .path .abspath (path )
246+ path = here .parent .joinpath (filename )
261247
262- if not os . path .exists (path ):
248+ if not path .exists ():
263249 raise FileNotFoundError (f'Required file { path } does not exist.' )
264250
265251 with open (path , encoding = 'utf-8' ) as f :
@@ -276,10 +262,8 @@ def includefile(filename: str, title: str, fmt: str = ''):
276262 @env .macro
277263 def templatefile (filename ):
278264 """Include code for a provided template file."""
279- base = os .path .basename (filename )
280- fn = os .path .join (
281- 'src' , 'backend' , 'InvenTree' , 'report' , 'templates' , filename
282- )
265+ base = Path (filename ).name
266+ fn = Path ('src' , 'backend' , 'InvenTree' , 'report' , 'templates' , filename )
283267
284268 return includefile (fn , f'Template: { base } ' , fmt = 'html' )
285269
0 commit comments