eric6/Plugins/VcsPlugins/vcsPySvn/SvnUtilities.py

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

eric ide

mercurial