|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2006 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing some common utility functions for the pysvn package. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import contextlib |
|
12 |
|
13 from PyQt6.QtCore import QDateTime, Qt |
|
14 |
|
15 import Utilities |
|
16 |
|
17 from .Config import DefaultConfig, DefaultIgnores |
|
18 |
|
19 |
|
20 def formatTime(seconds): |
|
21 """ |
|
22 Module function to return a formatted time string. |
|
23 |
|
24 @param seconds time in seconds since epoch to be formatted (float or long) |
|
25 @return formatted time string (string) |
|
26 """ |
|
27 return ( |
|
28 QDateTime.fromSecsSinceEpoch(int(seconds)) |
|
29 .toTimeSpec(Qt.TimeSpec.LocalTime) |
|
30 .toString("yyyy-MM-dd hh:mm:ss") |
|
31 ) |
|
32 |
|
33 |
|
34 def dateFromTime_t(seconds): |
|
35 """ |
|
36 Module function to return the date. |
|
37 |
|
38 @param seconds time in seconds since epoch to be formatted (float or long) |
|
39 @return date (QDate) |
|
40 """ |
|
41 return QDateTime.fromSecsSinceEpoch(int(seconds)).toTimeSpec( |
|
42 Qt.TimeSpec.LocalTime).date() |
|
43 |
|
44 |
|
45 def getServersPath(): |
|
46 """ |
|
47 Module function to get the filename of the servers file. |
|
48 |
|
49 @return filename of the servers file (string) |
|
50 """ |
|
51 if Utilities.isWindowsPlatform(): |
|
52 appdata = os.environ["APPDATA"] |
|
53 return os.path.join(appdata, "Subversion", "servers") |
|
54 else: |
|
55 homedir = Utilities.getHomeDir() |
|
56 return os.path.join(homedir, ".subversion", "servers") |
|
57 |
|
58 |
|
59 def getConfigPath(): |
|
60 """ |
|
61 Module function to get the filename of the config file. |
|
62 |
|
63 @return filename of the config file (string) |
|
64 """ |
|
65 if Utilities.isWindowsPlatform(): |
|
66 appdata = os.environ["APPDATA"] |
|
67 return os.path.join(appdata, "Subversion", "config") |
|
68 else: |
|
69 homedir = Utilities.getHomeDir() |
|
70 return os.path.join(homedir, ".subversion", "config") |
|
71 |
|
72 |
|
73 def createDefaultConfig(): |
|
74 """ |
|
75 Module function to create a default config file suitable for eric. |
|
76 """ |
|
77 config = getConfigPath() |
|
78 with contextlib.suppress(OSError): |
|
79 os.makedirs(os.path.dirname(config)) |
|
80 with contextlib.suppress(OSError), open(config, "w") as f: |
|
81 f.write(DefaultConfig) |
|
82 |
|
83 |
|
84 def amendConfig(): |
|
85 """ |
|
86 Module function to amend the config file. |
|
87 """ |
|
88 config = getConfigPath() |
|
89 try: |
|
90 with open(config, "r") as f: |
|
91 configList = f.read().splitlines() |
|
92 except OSError: |
|
93 return |
|
94 |
|
95 newConfig = [] |
|
96 ignoresFound = False |
|
97 amendList = [] |
|
98 for line in configList: |
|
99 if line.find("global-ignores") in [0, 2]: |
|
100 ignoresFound = True |
|
101 if line.startswith("# "): |
|
102 line = line[2:] |
|
103 newConfig.append(line) |
|
104 for amend in DefaultIgnores: |
|
105 if amend not in line: |
|
106 amendList.append(amend) |
|
107 elif ignoresFound: |
|
108 if line.startswith("##"): |
|
109 ignoresFound = False |
|
110 if amendList: |
|
111 newConfig.append(" " + " ".join(amendList)) |
|
112 newConfig.append(line) |
|
113 continue |
|
114 elif line.startswith("# "): |
|
115 line = line[2:] |
|
116 newConfig.append(line) |
|
117 oldAmends = amendList[:] |
|
118 amendList = [] |
|
119 for amend in oldAmends: |
|
120 if amend not in line: |
|
121 amendList.append(amend) |
|
122 else: |
|
123 newConfig.append(line) |
|
124 |
|
125 if newConfig != configList: |
|
126 with contextlib.suppress(OSError), open(config, "w") as f: |
|
127 f.write("\n".join(newConfig)) |