printr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +31 -0
- data/README +45 -0
- data/Rakefile +7 -0
- data/app/helpers/application_helper.rb +2 -0
- data/app/views/layouts/application.html.erb +14 -0
- data/app/views/printr/item.prnt.erb +0 -0
- data/config/locales/en.yml +5 -0
- data/config.ru +4 -0
- data/db/seeds.rb +7 -0
- data/lib/generators/printr/install_generator.rb +13 -0
- data/lib/generators/printr/views_generator.rb +13 -0
- data/lib/generators/templates/printr.rb +3 -0
- data/lib/generators/templates/printrs.yml +3 -0
- data/lib/printr/engine.rb +7 -0
- data/lib/printr/version.rb +3 -0
- data/lib/printr.rb +157 -0
- data/lib/tasks/.gitkeep +0 -0
- data/printr.gemspec +21 -0
- data/test/performance/browsing_test.rb +9 -0
- data/test/test_helper.rb +13 -0
- metadata +78 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '3.0.6'
|
4
|
+
|
5
|
+
# Bundle edge Rails instead:
|
6
|
+
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
7
|
+
|
8
|
+
gem 'serialport'
|
9
|
+
|
10
|
+
# Use unicorn as the web server
|
11
|
+
# gem 'unicorn'
|
12
|
+
|
13
|
+
# Deploy with Capistrano
|
14
|
+
# gem 'capistrano'
|
15
|
+
|
16
|
+
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
|
17
|
+
# gem 'ruby-debug'
|
18
|
+
# gem 'ruby-debug19', :require => 'ruby-debug'
|
19
|
+
|
20
|
+
# Bundle the extra gems:
|
21
|
+
# gem 'bj'
|
22
|
+
# gem 'nokogiri'
|
23
|
+
# gem 'sqlite3-ruby', :require => 'sqlite3'
|
24
|
+
# gem 'aws-s3', :require => 'aws/s3'
|
25
|
+
|
26
|
+
# Bundle gems for the local environment. Make sure to
|
27
|
+
# put test-only gems in this group so their generators
|
28
|
+
# and rake tasks are available in development mode:
|
29
|
+
# group :development, :test do
|
30
|
+
# gem 'webrat'
|
31
|
+
# end
|
data/README
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
== Welcome to Printr
|
2
|
+
|
3
|
+
Printr is an interface to serial/usb/plain_file printers on *nix systems. You can use it with your udev
|
4
|
+
rules etc. It is built as a rails engine, and should be used as a gem.
|
5
|
+
|
6
|
+
You can pull the latest edition from this repo, or:
|
7
|
+
|
8
|
+
#Gemfile
|
9
|
+
gem 'printr'
|
10
|
+
|
11
|
+
Then run bundle install
|
12
|
+
|
13
|
+
The default source for printer configurations is :yaml, and expects a printrs.yml file to be present in
|
14
|
+
RAILS_ROOT/config/, you can also feed it off of an ActiveRecord, in initializers/printr.rb
|
15
|
+
|
16
|
+
config.printr_source = {:active_record => {
|
17
|
+
:class_name => YourActiveRecordClass,
|
18
|
+
:name => :attribute_name,
|
19
|
+
:path => :attribute_name }
|
20
|
+
}
|
21
|
+
|
22
|
+
Where:
|
23
|
+
|
24
|
+
:name must be an attribute of the active record object, and the value it resolves to must be
|
25
|
+
a snake_case_name
|
26
|
+
:path must be the full path to the printer, like /dev/som0 or /home/user/printer.txt
|
27
|
+
|
28
|
+
==Basic Usage:
|
29
|
+
|
30
|
+
@printer = Printr.new
|
31
|
+
@printer.the_name_of_some_printer "text"
|
32
|
+
|
33
|
+
==Advanced Usage
|
34
|
+
|
35
|
+
Create a printr folder in your views directory for your rails app. name the view #{some_name}.prnt.erb
|
36
|
+
|
37
|
+
So for instance, say you want an item view:
|
38
|
+
|
39
|
+
/views/printr/item.prnt.erb
|
40
|
+
|
41
|
+
Then somewhere in your app, call:
|
42
|
+
|
43
|
+
@printer = Printr.new
|
44
|
+
@printer.the_name_of_some_printer "item",binding
|
45
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require File.expand_path('../config/application', __FILE__)
|
5
|
+
require 'rake'
|
6
|
+
|
7
|
+
Printr::Application.load_tasks
|
File without changes
|
data/config.ru
ADDED
data/db/seeds.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# This file should contain all the record creation needed to seed the database with its default values.
|
2
|
+
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
|
3
|
+
#
|
4
|
+
# Examples:
|
5
|
+
#
|
6
|
+
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
|
7
|
+
# Mayor.create(:name => 'Daley', :city => cities.first)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Printr
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../../templates",__FILE__)
|
5
|
+
desc "Creates the printr initializer"
|
6
|
+
|
7
|
+
def copy_initializer
|
8
|
+
template "printr.rb", "config/initializers/printr.rb"
|
9
|
+
copy_file "printrs.yml","config/printrs.yml"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Printr
|
2
|
+
module Generators
|
3
|
+
class ViewsGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../../../../app/views",__FILE__)
|
5
|
+
desc "Copies default printer templates to your application"
|
6
|
+
argument :scope, :required => false, :default => nil,
|
7
|
+
:desc => "the scope to copy views to."
|
8
|
+
def copy_views
|
9
|
+
directory "printr", "app/views/#{scope || :printr}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/printr.rb
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'yaml'
|
3
|
+
require 'erb'
|
4
|
+
require 'serialport'
|
5
|
+
module Printr
|
6
|
+
require 'printr/engine' if defined?(Rails)
|
7
|
+
mattr_accessor :printr_source #:yaml or :active_record
|
8
|
+
@@printr_source = :yaml
|
9
|
+
# :active_record => {:class_name => ClassName, :name => :model_field_name, :path => :model_path_name
|
10
|
+
# E.G. printr_model = {:class_name => Printer, :name => :short_name, :path => :location }
|
11
|
+
# to create the list of printers it will call:
|
12
|
+
# Printer.all.each { |p| @printrs[p.send(Printr.printr_model[:name].snake_case.to_sym)] = p.send(Printr.printr_model[:path]) }
|
13
|
+
# so if you have a printer named bar_printer, then you can print to it with @printr.bar_printer 'textl'
|
14
|
+
mattr_accessor :sanitize_tokens #pair list of needle regex, replace must be by 2s, i.e. [/[abc]/,"x",/[123]/,'0']
|
15
|
+
@@sanitize_tokens = []
|
16
|
+
mattr_accessor :codes
|
17
|
+
@@codes = {
|
18
|
+
:hr => "=====================\n",
|
19
|
+
:header => "\e@\e!\x38",
|
20
|
+
:footer => "\n\n\n\n\x1DV\x00\x16\x20105"
|
21
|
+
}
|
22
|
+
mattr_accessor :printrs
|
23
|
+
@@printrs = {}
|
24
|
+
mattr_accessor :conf
|
25
|
+
@@conf = {}
|
26
|
+
def self.new
|
27
|
+
return Printr::Machine.new
|
28
|
+
end
|
29
|
+
def self.setup
|
30
|
+
yield self
|
31
|
+
end
|
32
|
+
def self.get_printers
|
33
|
+
if @@printr_source == :yaml then
|
34
|
+
@@conf = YAML::load(File.open("#{RAILS_ROOT}/config/printrs.yml"))
|
35
|
+
elsif @@printr_source.class == Hash then
|
36
|
+
if @@printr_source[:active_record] then
|
37
|
+
@@printr_source[:active_record][:class_name].all.each do |p|
|
38
|
+
key = p.send(@@printr_source[:active_record][:name]).to_sym
|
39
|
+
@@conf[key] = p.send(@@printr_source[:active_record][:path])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
return self.open_printers
|
44
|
+
end
|
45
|
+
def self.open_printers
|
46
|
+
@@conf.each do |key,value|
|
47
|
+
key = key.to_sym
|
48
|
+
puts "[PRINTING] Trying to open #{key}..."
|
49
|
+
begin
|
50
|
+
@@printrs[key] = SerialPort.new(value,9600)
|
51
|
+
puts "[PRINTING] Success for SerialPort: #{ @printrs[key].inspect }"
|
52
|
+
rescue Exception => e
|
53
|
+
puts "[PRINTING] Failed to open as SerialPort: #{ e.inspect }"
|
54
|
+
@@printrs[key] = nil
|
55
|
+
end
|
56
|
+
next if @@printrs[key]
|
57
|
+
# Try to open it as USB
|
58
|
+
begin
|
59
|
+
@@printrs[key] = File.open(value,'w:ISO8859-15')
|
60
|
+
rescue Errno::EBUSY
|
61
|
+
@@conf.each do |k,v|
|
62
|
+
if @@printrs[k] and @@printrs[k].class == File then
|
63
|
+
@@printrs[key] = @@printrs[k]
|
64
|
+
puts "[PRINTING] Reused."
|
65
|
+
end
|
66
|
+
end
|
67
|
+
rescue Exception => e
|
68
|
+
@@printrs[key] = File.open("#{RAILS_ROOT}/tmp/dummy-#{key}.txt","a")
|
69
|
+
puts "[PRINTING] Failed to open as either SerialPort or USB File. Created Dummy #{ @printrs[key].inspect } instead."
|
70
|
+
end
|
71
|
+
end
|
72
|
+
@@printrs
|
73
|
+
end
|
74
|
+
def self.close
|
75
|
+
puts "[PRINTING]============"
|
76
|
+
puts "[PRINTING]CLOSING Printers..."
|
77
|
+
@@printrs.map do |p|
|
78
|
+
begin
|
79
|
+
p.close
|
80
|
+
rescue
|
81
|
+
|
82
|
+
end
|
83
|
+
p = nil
|
84
|
+
end
|
85
|
+
end
|
86
|
+
# Instance Methods
|
87
|
+
class Machine
|
88
|
+
def initialize()
|
89
|
+
Printr.get_printers
|
90
|
+
# You can access these within the views by calling @printr.codes, or whatever
|
91
|
+
# you named the instance variable, as it will be snagged by the Binding
|
92
|
+
@codes = Printr.codes
|
93
|
+
|
94
|
+
# You can override the above codes in the printers.yml, to add
|
95
|
+
# say an ASCII header or some nonsense, or if they are using a
|
96
|
+
# standard printer etc etc.
|
97
|
+
if Printr.conf[:codes] then
|
98
|
+
Printr.conf[:codes].each do |key,value|
|
99
|
+
Printr.conf[key.to_sym] = value
|
100
|
+
end
|
101
|
+
end
|
102
|
+
Printr.open_printers
|
103
|
+
end
|
104
|
+
|
105
|
+
def test(key)
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
def logger
|
110
|
+
ActiveRecord::Base.logger
|
111
|
+
end
|
112
|
+
def print_to(key,text)
|
113
|
+
key = key.to_sym
|
114
|
+
begin
|
115
|
+
text = sanitize(text)
|
116
|
+
t = Printr.printrs[key].class
|
117
|
+
if Printr.printrs[key].class == File then
|
118
|
+
Printr.printrs[key].write text
|
119
|
+
Printr.printrs[key].flush
|
120
|
+
elsif Printr.printrs[key].class == SerialPort
|
121
|
+
Printr.printrs[key].write text
|
122
|
+
else
|
123
|
+
puts "Could not find #{key} #{Printr.printrs[key].class}"
|
124
|
+
end
|
125
|
+
rescue Exception => e
|
126
|
+
puts "#{e.inspect}"
|
127
|
+
Printr.close
|
128
|
+
end
|
129
|
+
end
|
130
|
+
def method_missing(sym, *args, &block)
|
131
|
+
if Printr.printrs[sym] then
|
132
|
+
if args[1].class == Binding then
|
133
|
+
print_to(sym,template(args[0],args[1])) #i.e. you call @printr.kitchen('item',binding)
|
134
|
+
else
|
135
|
+
print_to(sym,args[0])
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
def sanitize(text)
|
140
|
+
i = 0
|
141
|
+
begin
|
142
|
+
text.gsub!(Printr.sanitize_tokens[i],Printr.sanitize_tokens[i+1])
|
143
|
+
i += 2
|
144
|
+
end while i < Printr.sanitize_tokens.length
|
145
|
+
text
|
146
|
+
end
|
147
|
+
def template(name,bndng)
|
148
|
+
begin
|
149
|
+
erb = ERB.new(File.new("#{RAILS_ROOT}/app/views/printr/#{name}.prnt.erb",'r').read)
|
150
|
+
rescue Exception => e
|
151
|
+
puts e.inspect
|
152
|
+
end
|
153
|
+
text = erb.result(bndng)
|
154
|
+
return text
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
data/lib/tasks/.gitkeep
ADDED
File without changes
|
data/printr.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "printr/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "printr"
|
7
|
+
s.version = Printr::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Michael Franzl","Jason Martin"]
|
10
|
+
s.email = ["jason@jason-knight-martin.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{An engine for interfacing with printers.}
|
13
|
+
s.description = %q{This engine allows you to define printers and templates.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "printr"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
|
+
require File.expand_path('../../config/environment', __FILE__)
|
3
|
+
require 'rails/test_help'
|
4
|
+
|
5
|
+
class ActiveSupport::TestCase
|
6
|
+
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
|
7
|
+
#
|
8
|
+
# Note: You'll currently still have to declare fixtures explicitly in integration tests
|
9
|
+
# -- they do not yet inherit this setting
|
10
|
+
fixtures :all
|
11
|
+
|
12
|
+
# Add more helper methods to be used by all tests here...
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: printr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Franzl
|
9
|
+
- Jason Martin
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2011-05-18 00:00:00 +02:00
|
15
|
+
default_executable:
|
16
|
+
dependencies: []
|
17
|
+
|
18
|
+
description: This engine allows you to define printers and templates.
|
19
|
+
email:
|
20
|
+
- jason@jason-knight-martin.com
|
21
|
+
executables: []
|
22
|
+
|
23
|
+
extensions: []
|
24
|
+
|
25
|
+
extra_rdoc_files: []
|
26
|
+
|
27
|
+
files:
|
28
|
+
- .gitignore
|
29
|
+
- Gemfile
|
30
|
+
- README
|
31
|
+
- Rakefile
|
32
|
+
- app/helpers/application_helper.rb
|
33
|
+
- app/views/layouts/application.html.erb
|
34
|
+
- app/views/printr/item.prnt.erb
|
35
|
+
- config.ru
|
36
|
+
- config/locales/en.yml
|
37
|
+
- db/seeds.rb
|
38
|
+
- lib/generators/printr/install_generator.rb
|
39
|
+
- lib/generators/printr/views_generator.rb
|
40
|
+
- lib/generators/templates/printr.rb
|
41
|
+
- lib/generators/templates/printrs.yml
|
42
|
+
- lib/printr.rb
|
43
|
+
- lib/printr/engine.rb
|
44
|
+
- lib/printr/version.rb
|
45
|
+
- lib/tasks/.gitkeep
|
46
|
+
- printr.gemspec
|
47
|
+
- test/performance/browsing_test.rb
|
48
|
+
- test/test_helper.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: ""
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: printr
|
73
|
+
rubygems_version: 1.6.2
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: An engine for interfacing with printers.
|
77
|
+
test_files: []
|
78
|
+
|