99__copyright__ = 'Copyright (C) 2025-present, DV Klopfenstein, PhD. All rights reserved.'
1010__author__ = "DV Klopfenstein, PhD"
1111
12- from os import makedirs
12+ import os
13+ import os .path as op
1314from os .path import exists
14- from os .path import basename
15- from os .path import join
16- from os .path import abspath
17- from os .path import dirname
18- from os .path import normpath
1915from logging import debug
2016from collections import namedtuple
2117
22- from tomlkit import comment
23- from tomlkit import document
24- from tomlkit import nl
25- from tomlkit import table
18+ import tomlkit
2619from tomlkit .toml_file import TOMLFile
2720
2821from timetracker .consts import DIRTRK
@@ -46,19 +39,19 @@ class CfgProj:
4639 def __init__ (self , filename ):
4740 assert filename is not None
4841 self .filename = filename
49- ####self.exists = exists(self.filename)
5042 debug ('CfgProj args %d filename %s' , exists (filename ), filename )
51- self .trksubdir = DIRTRK if filename is None else basename (dirname (filename ))
52- self .dircfg = abspath (DIRTRK ) if filename is None else normpath (dirname (filename ))
53- self .dirproj = dirname (self .dircfg )
43+ dnam = op .dirname
44+ self .trksubdir = DIRTRK if filename is None else op .basename (dnam (filename ))
45+ self .dircfg = op .abspath (DIRTRK ) if filename is None else op .normpath (dnam (filename ))
46+ self .dirproj = dnam (self .dircfg )
5447
5548 def file_exists (self ):
5649 """Return True if config file exists and False otherwise"""
5750 return exists (self .filename )
5851
5952 def get_filename_cfg (self ):
6053 """Get the full filename of the local config file"""
61- return join (self .dircfg , 'config' )
54+ return op . join (self .dircfg , 'config' )
6255
6356 def get_filename_csv (self , username = None , dirhome = None ):
6457 """Get the csv filename by reading the cfg csv pattern and filling in"""
@@ -98,7 +91,7 @@ def wr_ini_file(self, project=None, dircsv=None, fcfg_global=None):
9891 fname = self .get_filename_cfg ()
9992 assert not exists (fname )
10093 if not exists (self .dircfg ):
101- makedirs (self .dircfg , exist_ok = True )
94+ os . makedirs (self .dircfg , exist_ok = True )
10295 if dircsv is None :
10396 dircsv = '.'
10497 doc = self ._get_new_doc (project , dircsv )
@@ -109,7 +102,7 @@ def wr_ini_file(self, project=None, dircsv=None, fcfg_global=None):
109102 def wr_gitignore (self ):
110103 """Add .gitignore file in .timetracker/ directory"""
111104 error = None
112- fname = join (self .dircfg , '.gitignore' )
105+ fname = op . join (self .dircfg , '.gitignore' )
113106 try :
114107 fptr = open (fname , 'w' , encoding = 'utf-8' )
115108 except (PermissionError , OSError ) as err :
@@ -136,10 +129,11 @@ def reinit(self, project, dircsv, fcfg_global=None, ntdoc=None):
136129 doc ['project' ] = project
137130 chgd = True
138131 if docproj .csv_filename != (csv_new := self ._assemble_csv_filepat (dircsv , doc ['project' ])):
132+ dnam = op .dirname
139133 print ('In local project configuration file, changed csv directory:\n '
140134 f' local cfg: { fname } \n '
141- f' csvdir WAS: { dirname (docproj .csv_filename )} \n '
142- f' csvdir NOW: { dirname (csv_new )} ' )
135+ f' csvdir WAS: { dnam (docproj .csv_filename )} \n '
136+ f' csvdir NOW: { dnam (csv_new )} ' )
143137 doc ['csv' ]['filename' ] = csv_new
144138 chgd = True
145139 if fcfg_global is not None and docproj .global_config_filename != fcfg_global :
@@ -156,13 +150,13 @@ def reinit(self, project, dircsv, fcfg_global=None, ntdoc=None):
156150
157151 def get_project_from_filename (self ):
158152 """Get the default project name from the project directory filename"""
159- return basename (self .dirproj )
153+ return op . basename (self .dirproj )
160154
161155 #-------------------------------------------------------------
162156 def _assemble_csv_filepat (self , dircsv , project ):
163157 if dircsv is None or dircsv == '' :
164158 dircsv = '.'
165- return join (dircsv , self .CSVPAT .replace ('PROJECT' , project ))
159+ return op . join (dircsv , self .CSVPAT .replace ('PROJECT' , project ))
166160
167161 @staticmethod
168162 def _wr_cfg (fname , doc ):
@@ -177,7 +171,7 @@ def _wr_cfg(fname, doc):
177171 return ret
178172
179173 def _rd_doc (self ):
180- """Read a config file and load it into a TOML document """
174+ """Read a config file and load it into a TOML doc """
181175 fin_cfglocal = self .get_filename_cfg ()
182176 return TOMLFile (fin_cfglocal ).read () if exists (fin_cfglocal ) else None
183177
@@ -189,7 +183,7 @@ def _get_dircsv(self, dirhome):
189183 """Read the project cfg to get the csv dir name for storing time data"""
190184 ##fcsv = self._read_csvdir_from_cfgfile(dirhome)
191185 ##if fcsv is not None:
192- ## return dirname(fcsv)
186+ ## return op. dirname(fcsv)
193187 dircsv = get_abspath (DIRCSV , self .dirproj , dirhome )
194188 return dircsv
195189
@@ -206,17 +200,17 @@ def _get_new_doc(self, project, dircsv):
206200 assert project is not None and isinstance (project , str )
207201 #assert dircsv
208202 debug ('TODO: dircsv=%s' , dircsv )
209- doc = document ()
210- doc .add (comment ("TimeTracker project configuration file" ))
211- doc .add (nl ())
203+ doc = tomlkit . document ()
204+ doc .add (tomlkit . comment ("TimeTracker project configuration file" ))
205+ doc .add (tomlkit . nl ())
212206 doc ["project" ] = project
213207
214208 # [csv]
215209 # format = "timetracker_architecting_bez.csv"
216- csv_section = table ()
210+ csv_section = tomlkit . table ()
217211 #csvdir.comment("Directory where the csv file is stored")
218212 ##csvpat = self.CSVPAT.replace('PROJECT', project)
219- ##csv_section.add("filename", join(self._get_dircsv_relname(dirhome), csvpat))
213+ ##csv_section.add("filename", op. join(self._get_dircsv_relname(dirhome), csvpat))
220214 csv_section .add ("filename" , self ._assemble_csv_filepat (dircsv , project ))
221215 doc .add ("csv" , csv_section )
222216 return doc
@@ -237,7 +231,7 @@ def _update_doc_globalcfgname(self, doc, fcfg_global):
237231 def _add_doc_globalcfgfname (doc , fcfg_global ):
238232 # [global_config]
239233 # filename = "/home/uname/myglobal.cfg"
240- section = table ()
234+ section = tomlkit . table ()
241235 #csvdir.comment("Directory where the csv file is stored")
242236 section .add ("filename" , fcfg_global )
243237 doc .add ("global_config" , section )
0 commit comments