FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-barcode/barcode/base.py at python37 · srolin/python-barcode · GitHub
srolin
/
python-barcode
Public
forked from
WhyNotHugo/python-barcode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-barcode
/
barcode
/
base.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
115 lines (91 loc) · 3.16 KB
Breadcrumbs
python-barcode
/
barcode
/
base.py
Copy path
File metadata and controls
executable file
·
115 lines (91 loc) · 3.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
"""barcode.base
"""
from
barcode
.
writer
import
SVGWriter
class
Barcode
(
object
):
name
=
''
digits
=
0
default_writer
=
SVGWriter
default_writer_options
=
{
'module_width'
:
0.2
,
'module_height'
:
15.0
,
'quiet_zone'
:
6.5
,
'font_size'
:
10
,
'text_distance'
:
5.0
,
'background'
:
'white'
,
'foreground'
:
'black'
,
'write_text'
:
True
,
'text'
:
''
,
}
def
to_ascii
(
self
):
code
=
self
.
build
()
for
i
,
line
in
enumerate
(
code
):
code
[
i
]
=
line
.
replace
(
'1'
,
'X'
).
replace
(
'0'
,
' '
)
return
'
\n
'
.
join
(
code
)
def
__repr__
(
self
):
return
'<{0}({1!r})>'
.
format
(
self
.
__class__
.
__name__
,
self
.
get_fullcode
())
def
build
(
self
):
raise
NotImplementedError
def
get_fullcode
(
self
):
"""Returns the full code, encoded in the barcode.
:returns: Full human readable code.
:rtype: String
"""
raise
NotImplementedError
def
save
(
self
,
filename
,
options
=
None
,
text
=
None
):
"""Renders the barcode and saves it in `filename`.
:parameters:
filename : String
Filename to save the barcode in (without filename
extension).
options : Dict
The same as in `self.render`.
text : str (unicode on Python 2)
Text to render under the barcode.
:returns: The full filename with extension.
:rtype: String
"""
if
text
:
output
=
self
.
render
(
options
,
text
)
else
:
output
=
self
.
render
(
options
)
_filename
=
self
.
writer
.
save
(
filename
,
output
)
return
_filename
def
write
(
self
,
fp
,
options
=
None
,
text
=
None
):
"""Renders the barcode and writes it to the file like object
`fp`.
:parameters:
fp : File like object
Object to write the raw data in.
options : Dict
The same as in `self.render`.
text : str (unicode on Python 2)
Text to render under the barcode.
"""
output
=
self
.
render
(
options
,
text
)
if
hasattr
(
output
,
'tostring'
):
output
.
save
(
fp
,
format
=
self
.
writer
.
format
)
else
:
fp
.
write
(
output
)
def
render
(
self
,
writer_options
=
None
,
text
=
None
):
"""Renders the barcode using `self.writer`.
:parameters:
writer_options : Dict
Options for `self.writer`, see writer docs for details.
text : str (unicode on Python 2)
Text to render under the barcode.
:returns: Output of the writers render method.
"""
options
=
Barcode
.
default_writer_options
.
copy
()
options
.
update
(
writer_options
or
{})
if
options
[
'write_text'
]
or
text
is
not
None
:
if
text
is
not
None
:
options
[
'text'
]
=
text
else
:
options
[
'text'
]
=
self
.
get_fullcode
()
self
.
writer
.
set_options
(
options
)
code
=
self
.
build
()
raw
=
self
.
writer
.
render
(
code
)
return
raw
Back
|
FazBrowse Home
|
New Git URL