@@ -43,44 +43,25 @@ def set_or_update_collection_path(self) -> None:
43
43
The configparser doesn't preserve comments, so we need to read the file
44
44
and write it back with the new collection path.
45
45
"""
46
- config = ConfigParser ()
47
- config .read (self .path )
48
- if not config .has_section ("defaults" ):
49
- with self .path .open (mode = "r+" ) as file :
50
- content_str = file .read ()
51
- file .seek (0 , 0 )
52
- file .truncate ()
53
- file .write ("[defaults]\n collections_path = .\n " + content_str )
54
- file .write ("\n " )
55
- return
56
-
57
- if not config .has_option ("defaults" , "collections_path" ):
58
- with self .path .open (mode = "r+" ) as file :
59
- content_list = file .read ().splitlines ()
60
- idx = content_list .index ("[defaults]" ) + 1
61
- content_list .insert (idx , "collections_path = ." )
62
- file .seek (0 , 0 )
63
- file .truncate ()
64
- file .write ("\n " .join (content_list ))
65
- file .write ("\n " )
66
- return
67
-
68
- with self .path .open (mode = "r+" ) as file :
69
- content_list = file .read ().splitlines ()
70
- idx = next (
71
- i for i , line in enumerate (content_list ) if line .startswith ("collections_path" )
72
- )
73
- content_list [idx ] = "collections_path = ."
74
- file .seek (0 , 0 )
75
- file .truncate ()
76
- file .write ("\n " .join (content_list ))
77
- file .write ("\n " )
78
- return
46
+ contents = self .path .read_text ().splitlines ()
47
+ collections_path = "collections_path = ."
48
+
49
+ if "[defaults]" not in contents :
50
+ contents .insert (0 , "[defaults]" )
51
+
52
+ idx = [i for i , line in enumerate (contents ) if line .startswith ("collections_path" )]
53
+
54
+ if idx :
55
+ contents [idx [0 ]] = collections_path
56
+ else :
57
+ insert_at = contents .index ("[defaults]" ) + 1
58
+ contents .insert (insert_at , collections_path )
59
+
60
+ with self .path .open (mode = "w" ) as file :
61
+ file .write ("\n " .join (contents ) + "\n " )
79
62
80
63
def author_new (self ) -> None :
81
64
"""Author the file and update it."""
82
- config = ConfigParser ()
83
- config .add_section ("defaults" )
84
- config .set ("defaults" , "collections_path" , "." )
85
- with self .path .open (mode = "w" ) as f :
86
- config .write (f )
65
+ contents = ["[defaults]" , "collections_path = ." ]
66
+ with self .path .open (mode = "w" ) as file :
67
+ file .write ("\n " .join (contents ) + "\n " )
0 commit comments