forked from smerckel/dbdreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbdrename.py
127 lines (116 loc) · 4.08 KB
/
dbdrename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/env python3
# Copyright 2006,2007,2014 Lucas Merckelbach
#
# This file is part of dbdreader.
#
# dbdreader is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# dbdreader is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dbdreader. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import os
import re
import getopt
def usage():
print('')
print('Script to flip back and forth between the long and short names of the')
print('dbd or sbd files.')
print('')
print('Optionally supply -s to make the filenames sortable using sort()')
print('or supply -n to keep the original filename')
print('')
print('supply -c to convert between old style to new style')
print('supply -C to convert between new style to old style')
print('Default is sorting activated.')
print(' Lmm 27 Mar 2007')
sys.exit(0)
def makeSortable(filename):
[basename,ext]=filename.split('.')
[g,y,d,m,s]=basename.split('-')
s="%03d"%(int(s))
m="%02d"%(int(m))
d="%03d"%(int(d))
basename="-".join([g,y,d,m,s])
filename=".".join([basename,ext])
return(filename)
SORT=True # default
CONVERTold=False # default
CONVERTnew=False # default
try:
R=getopt.getopt(sys.argv[1:],'cCsnh')
except getopt.GetoptError:
print("Wrong option(s)!")
usage()
for (o,a) in R[0]:
if o=='-s':
SORT=True
if o=='-n':
SORT=False
if o=='-c':
CONVERTnew=True
if o=='-C':
CONVERTold=True
if o=='-h':
usage()
files=R[1]
for i in files:
fd=open(i,'br')
ID=fd.readline().decode('ascii').strip()
ignoreIt=False
if ID=='dbd_label: DBD(dinkum_binary_data)file':
for j in range(4):
shortfilename=fd.readline().decode('ascii').strip()
longfilename=fd.readline().decode('ascii').strip()
shortfilename=re.sub("^.* ","",shortfilename)
longfilename=re.sub("^.* ","",longfilename)
extension=re.sub("^.*\.","",i)
shortfilename+="."+extension
longfilename+="."+extension
elif "the8x3_filename" in ID: # mlg file apparently...
longfilename=fd.readline().decode('ascii').strip()
tmp=fd.readline().decode('ascii').strip()
if 'mlg' in tmp:
extension='.mlg'
else:
extension='.nlg'
shortfilename=re.sub("^the8x3_filename: +","",ID+extension)
longfilename=re.sub("^full_filename: +","",longfilename)+extension
else:
sys.stderr.write("Ignoring %s\n"%(i))
ignoreIt=True
if not ignoreIt:
command=None
if CONVERTnew or CONVERTold: # input filename must be the longfilename
# check if we have a longfilename
if i not in [longfilename,makeSortable(longfilename)]:
print("Chose to ignore processing ",i)
else:
if i==makeSortable(longfilename) and CONVERTold:
# switch to old format
command="mv "+i+" "+longfilename
elif i==longfilename and CONVERTnew:
command="mv "+longfilename+" "+makeSortable(longfilename)
else:
print("ignoring "+i)
else: # changing from long to short names or vice versa
if SORT:
longfilename=makeSortable(longfilename)
if i==shortfilename:
command="mv "+shortfilename+" "+longfilename
else:
command="mv "+longfilename+" "+shortfilename
if command!=None:
print("command: ",command)
R=os.system(command)
if R!=0:
raise ValueError("Could not execute %s"%(command))
fd.close()