forked from lowrie/pyRouterJig
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig_file.py
157 lines (133 loc) · 5.21 KB
/
config_file.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
###########################################################################
#
# Copyright 2015-2016 Robert B. Lowrie (http://github.com/lowrie)
#
# This file is part of pyRouterJig.
#
# pyRouterJig 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.
#
# pyRouterJig 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
# pyRouterJig; see the file LICENSE. If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################
'''
This module contains utilities for creating and reading the
user configuration file
'''
import os, imp, shutil
import utils
_CONFIG_INIT = r'''
######################################################################
# Options for pyRouterJig. Be careful editing this file. Any errors
# that occur will not be friendly.
#
# This file is a python script, so for example, '#' as the first
# character on a line is a comment.
######################################################################
# Do not change the version, because this tells pyRouterJig which
# version initially created this file.
version = '%s'
######################################################################
# Options below may be changed
######################################################################
# If True, use metric units. If False, use English units.
metric = False
# If using English units, this parameter sets the size of an increment.
# Ignored for metric units, where 1 increment = 1 mm.
increments_per_inch = 32
# Initial board width, in increments
board_width = 240
# Initial bit width, in increments
bit_width = 16
# Initial bit depth, in increments
bit_depth = 24
# Initial bit angle, in degrees
bit_angle = 0
# Avoid fingers that are smaller than this dimension.
# Specified in increments.
min_finger_width = 2
# Trim this amount from each side of the Top- and Bottom-board fingers to form the optional clamping cauls.
# Specified in increments.
caul_trim = 1
# On save image, minimum width of image in pixels. Does not apply to screenshots.
min_image_width = 2048
# The folder which contains wood grain image files. Prefix the string with the character-r to prevent
# python from interpreting the character-\ (used in Windows file paths) as an escape.
wood_images = r'%s'
# This is either a wood name (the file prefix of an image file in wood_images),
# or the following Qt fill patterns:
# DiagCrossPattern, BDiagPattern, FDiagPattern, Dense1Pattern, Dense5Pattern
default_wood = 'DiagCrossPattern'
# Set debug to True to turn on debugging. This will print a lot of output to
# stdout during a pyRouterJig session. This option is typically only useful
# for developers.
debug = False
#debug = True
# The margins object controls top, bottom, and side margins, along with the
# separation between objects in the figure.
# Specified in increments.
top_margin = 8
left_margin = top_margin
right_margin = top_margin
bottom_margin = 16
separation = top_margin
# Colors are specified as a mix of three values between 0 and 255, as
# (red, green, blue)
# Examples:
# (255, 0, 0) red
# (0, 255, 0) green
# (0, 0, 255) blue
# (255, 255, 255) white
# (0, 0, 0) black
#
# Useful site: http://www.colorpicker.com/
# Background color
background_color = (240, 231, 201)
'''
def version_number(version):
'''Splits the string version into its version number'''
vs = version.split('.')
return int(vs[0]) * 100 + int(vs[1]) * 10 + int(vs[2])
def create_config(filename):
'''
Creates the configuration file.
'''
wood_images = os.path.join(os.path.expanduser('~'), 'wood_images')
content = _CONFIG_INIT % (utils.VERSION, wood_images)
fd = open(filename, 'w')
fd.write(content)
fd.close()
def read_config(min_version_number):
'''
Reads the configuration file. If it does not exist, it's created.
'''
global _CONFIG_INIT
filename = os.path.join(os.path.expanduser('~'), '.pyrouterjig')
if not os.path.exists(filename):
# create the config file
create_config(filename)
msg = 'Configuration file %s created' % filename
else:
msg = 'Read configuration file %s' % filename
config = imp.load_source('', filename)
vnum = version_number(config.version)
msg_level = 0
if vnum < min_version_number:
msg_level = 1
backup = filename + config.version
shutil.move(filename, backup)
create_config(filename)
config = imp.load_source('', filename)
msg = 'Your configuration file %s was outdated and has been moved to %s.'\
' A new configuration file has been created. Any changes you made'\
' to the old file will need to be migrated to the new file.'\
' Unfortunately, we are unable to automatically migrate your old'\
' settings.' % (filename, backup)
return (config, msg, msg_level)