2121#
2222"""python-gammu - Phone communication library."""
2323
24- import glob
2524import os
2625import platform
2726import subprocess # noqa: S404
2827import sys
28+ from pathlib import Path
29+ from typing import cast
2930
3031from packaging .version import parse
3132from setuptools import Extension , setup
@@ -40,7 +41,7 @@ def __init__(self) -> None:
4041 self .on_windows = platform .system () == "Windows"
4142 self .has_pkgconfig = self .check_pkconfig ()
4243 self .has_env = "GAMMU_PATH" in os .environ
43- self .path = self .lookup_path ()
44+ self .path : Path = cast ( "Path" , self .lookup_path () )
4445 self .use_pkgconfig = self .has_pkgconfig and not self .has_env
4546
4647 def check_pkconfig (self ) -> bool :
@@ -50,28 +51,29 @@ def check_pkconfig(self) -> bool:
5051 except (subprocess .CalledProcessError , OSError ):
5152 return False
5253
53- def config_path (self , base ) :
54- return os . path . join ( base , "include" , "gammu" , "gammu-config.h" )
54+ def config_path (self , base : Path ) -> Path :
55+ return base / "include" / "gammu" / "gammu-config.h"
5556
56- def lookup_path (self ):
57+ def lookup_path (self ) -> Path | None :
58+ paths : list [Path ]
5759 if self .has_env :
58- paths = [os .environ ["GAMMU_PATH" ]]
60+ paths = [Path ( os .environ ["GAMMU_PATH" ]) ]
5961 elif self .on_windows :
6062 paths = [
61- "C:\\ Gammu" ,
62- "C:\\ Program Files\\ Gammu" ,
63- "C:\\ Program Files (x86)\\ Gammu" ,
63+ Path ( "C:\\ Gammu" ) ,
64+ Path ( "C:\\ Program Files\\ Gammu" ) ,
65+ Path ( "C:\\ Program Files (x86)\\ Gammu" ) ,
6466 ]
65- paths += glob . glob ("C:\\ Program Files\\ Gammu*" )
66- paths += glob . glob ("C:\\ Program Files (x86)\\ Gammu*" )
67+ paths += Path ("C:\\ Program Files" ). glob ( " Gammu*" )
68+ paths += Path ("C:\\ Program Files (x86)" ). glob ( " Gammu*" )
6769 else :
68- paths = ["/usr/local/" , "/usr/" ]
69- paths += glob . glob ("/opt/ gammu*" )
70+ paths = [Path ( "/usr/local/" ), Path ( "/usr/" ) ]
71+ paths += Path ("/opt" ). glob ( " gammu*" )
7072
7173 for path in paths :
7274 include = self .config_path (path )
73- if os . path . exists (include ):
74- return path
75+ if include . exists ():
76+ return Path ( path )
7577 return None
7678
7779 def check_version (self ) -> None :
@@ -91,7 +93,7 @@ def check_version(self) -> None:
9193 print ("Can not find supported Gammu version using pkg-config!" )
9294 sys .exit (100 )
9395
94- if self .path is None :
96+ if not self .path :
9597 print ("Failed to find Gammu!" )
9698 print ("Either it is not installed or not found." )
9799 print ("After install Gammu ensure that setup finds it by any of:" )
@@ -100,7 +102,7 @@ def check_version(self) -> None:
100102 sys .exit (101 )
101103
102104 version = None
103- with open ( self .config_path (self .path ), encoding = "utf-8" ) as handle :
105+ with self .config_path (self .path ). open ( encoding = "utf-8" ) as handle :
104106 for line in handle :
105107 if line .startswith ("#define GAMMU_VERSION " ):
106108 version = parse (line .split ('"' )[1 ])
@@ -109,7 +111,7 @@ def check_version(self) -> None:
109111 print ("Too old Gammu version, please upgrade!" )
110112 sys .exit (100 )
111113
112- def get_libs (self ):
114+ def get_libs (self ) -> list [ str ] :
113115 if self .use_pkgconfig :
114116 output = subprocess .check_output (
115117 ["pkg-config" , "--libs-only-l" , "gammu" , "gammu-smsd" ]
@@ -122,7 +124,7 @@ def get_libs(self):
122124 libs .append ("m" )
123125 return libs
124126
125- def get_cflags (self ):
127+ def get_cflags (self ) -> str :
126128 if self .use_pkgconfig :
127129 return (
128130 subprocess .check_output (
@@ -131,9 +133,9 @@ def get_cflags(self):
131133 .decode ("utf-8" )
132134 .strip ()
133135 )
134- return "-I{}" .format (os . path . join (self .path , "include" , "gammu" ))
136+ return "-I{}" .format ((self .path / "include" / "gammu" ). as_posix ( ))
135137
136- def get_ldflags (self ):
138+ def get_ldflags (self ) -> str :
137139 if self .use_pkgconfig :
138140 return (
139141 subprocess .check_output (
@@ -143,8 +145,8 @@ def get_ldflags(self):
143145 .strip ()
144146 )
145147 if self .on_windows :
146- return "/LIBPATH:{}" .format (os . path . join ( self .path , "lib" ) )
147- return "-L{}" .format (os . path . join (self .path , "lib" ))
148+ return "/LIBPATH:{}" .format (self .path / "lib" )
149+ return "-L{}" .format ((self .path / "lib" ). as_posix ( ))
148150
149151
150152def get_module ():
0 commit comments